运行环境
- ubuntu18.04
- cuda10.1
- python3.6( Anaconda)
- pytorch1.4
- torchvision0.5.0
一. anaconda创建虚拟环境
- 创建虚拟环境
create -n python3.6 python==3.6
- 激活虚拟环境
conda activate python3.6
二. pytorch安装
- 进入官网https://pytorch.org/,选择自己需要的版本安装
不建议安装最新版本的pytorch,否则安装编译faster-rcnn的时候会编译失败,我选择的pytorch1.4
- 建议选择pip安装pytorch,不容易失败,conda容易安装失败
pip install torch==1.4.0 torchvision==0.5.0
- 验证pytorch是否安装成功
(python3.6) xxX@OMEN:~$ python
Python 3.6.13 |Anaconda, Inc.| (default, Feb 23 2021, 21:15:04)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> import torchvision
>>> torch.cuda.is_available()
True
>>>
三. 下载faster-rcnn.pytorch代码
git clone https://github.com/jwyang/faster-rcnn.pytorch.git
此时代码下载到主目录下faster-rcnn.pytorch
cd faster-rcnn.pytorch && mkdir data
#安装python依赖项
pip install -r requirements.txt
#编译cuda依赖项
cd lib
sh make.sh
报错ImportError: torch.utils.ffi is deprecated. Please use cpp extensions instead.
原因是当前使用的是pytorch0.4版本的,需要切换到pytorch1.0,参考博客中的“Step1、安装”重新安装编译安装。
四. 训练模型
参考博客
- 下载特征提取层的预训练(pre-trained)权重
VGG 16:
ResNet 101 :
下载完成后把他们放到data/pretrained_model目录下。 - 准备数据集
#Beyond the demo: installation for training and testing models
#Download the training, validation, test data and VOCdevkit
wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtrainval_06-Nov-2007.tar
wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtest_06-Nov-2007.tar
wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCdevkit_08-Jun-2007.tar
#Extract all of these tars into one directory named VOCdevkit
tar xvf VOCtrainval_06-Nov-2007.tar
tar xvf VOCtest_06-Nov-2007.tar
tar xvf VOCdevkit_08-Jun-2007.tar
#It should have this basic structure
# $VOCdevkit/ # development kit
# $VOCdevkit/VOCcode/ # VOC utility code
# $VOCdevkit/VOC2007 # image sets, annotations, etc.
# ... and several other directories ...
# Create symlinks for the PASCAL VOC dataset
cd $FRCN_ROOT/data
ln -s $VOCdevkit VOCdevkit2007
# Using symlinks is a good idea because you will likely want to share the same PASCAL dataset installation between multiple projects.
- 训练模型
CUDA_VISIBLE_DEVICES=0 python trainval_net.py --dataset pascal_voc --net res101 --cuda