1.下载mmdetection
mmdetection是一个工具,没有安装程序,只需要在github上clone一个到服务器上就可以了。
链接:https://github.com/open-mmlab/mmdetection
2.创建虚拟环境
为我们的项目创建一个虚拟环境,此处省略具体的Linux指令
3.配置虚拟环境
进入虚拟环境后,要开始配置所需要的包
torch、torchvision
方法一:指令安装
安装指令
conda install pytorch==xxx torchvision==xxx torchaudio==xxx cudatoolkit=xxx -c pytorch
方法二:whl车轮安装
whl文件下载地址:download.pytorch.org/whl/torch_s…
下载好了以后,运行:pip install 文件.whl
requirements文件
pip install -r requirements/build.txt
ninja
pip install ninja
mmcv-full
由于我在github上clone的版本是mmdetection-master,所以必须要下载mmcv-full
关于本机cuda、torch、torchvision、mmcv的版本是相互对应的。其中,安装mmcv-full时,需要根据torch和cuda的版本对应表,获得下载对应版本mmcv-full的指令
版本对应表链接:github.com/open-mmlab/…
如:这里的cu101/torch1.6.0对应自己的cuda和torch的版本号
pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/cu101/torch1.6.0/index.html
其他
pip install -v -e .
4.准备数据集
- 在文件夹下创建一个data文件夹,data文件夹结构如下
data
|---coco
|---annotations
|---instances_train2017.json
|---instances_val2017.json
|---val2017
|---train2017
val2017和train2017是验证集和测试集的图片文件夹,
instances_train2017.json和instances_val2017.json是训练集和验证集的标注文件
5.模型配置
配置好环境、准备好训练所需的数据集后,我们需要一个神经网络模型model替我们去训练数据,在自定义一个模型之前,我们可以首先尝试根据自己的需要魔改官方模型
我们先尝试改一个fasterRCNN
-
进入
configs/_base_/models/目录 -
打开faster_rcnn_r50_fpn.py文件
根据模型的文件名,我们可以得到一些关于模型的信息:
- faster_rcnn 方法名
- r50 50层训练
- fpn 目标检测处理方式
- mstrain 多尺度训练策略
-
搜索num_classes,将类的数量改为自己数据集类的数量(不需要因为背景+1,因为mmdetection会自动处理)
# num_classes=80,
num_classes=1,
- 在
mmdet/datasets/coco.py中也要做对应的修改
def coco_classes():
return [
'''
'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train',
'truck', 'boat', 'traffic_light', 'fire_hydrant', 'stop_sign',
'parking_meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep',
'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella',
'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard',
'sports_ball', 'kite', 'baseball_bat', 'baseball_glove', 'skateboard',
'surfboard', 'tennis_racket', 'bottle', 'wine_glass', 'cup', 'fork',
'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange',
'broccoli', 'carrot', 'hot_dog', 'pizza', 'donut', 'cake', 'chair',
'couch', 'potted_plant', 'bed', 'dining_table', 'toilet', 'tv',
'laptop', 'mouse', 'remote', 'keyboard', 'cell_phone', 'microwave',
'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase',
'scissors', 'teddy_bear', 'hair_drier', 'toothbrush'
'''
'tree',
]
- 在
mmdet/core/evaluation/class_names.py中,找到coco数据集,修改coco数据集对应的类
'''
CLASSES = ('person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',
'train', 'truck', 'boat', 'traffic light', 'fire hydrant',
'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog',
'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe',
'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee',
'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat',
'baseball glove', 'skateboard', 'surfboard', 'tennis racket',
'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl',
'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot',
'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch',
'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop',
'mouse', 'remote', 'keyboard', 'cell phone', 'microwave',
'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock',
'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush')
'''
CLASSES = ('tree',)
6.最后,我们通过运行tool/train.py来生成配置文件
--work-dir 生成配置文件的位置
python tools/train.py configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py --work-dir record
-
生成配置文件之后,我们可以中断掉,然后在工作目录中找到这个配置文件,做修改之后 (各种微调,比如学习率,训练次数,是否继续上次的训练文件等)
-
运行配置文件用于训练数据
python tools/train.py record/faster_rcnn_r50_fpn_1x_coco.py