requirements
建议最好用Linux系统,Windows环境下坑特别多,不建议使用,填坑环节过于繁琐且费事。
官方安装教程:https://mmdetection.readthedocs.io/en/latest/get_started.html#prepare-environment
mmcv文档:https://mmcv.readthedocs.io/en/latest/build.html
conda create -n open-mmlab python=3.7 -y
conda activate open-mmlab # 或 source activate open-mmlab
conda install pytorch==1.7.1 torchvision==0.8.2 torchaudio==0.7.2 cudatoolkit=10.1 -c pytorch
pip install openmim
mim install mmdet
MIM命令可以自动下载OpenMMLab的projects和requirements
pip install opencv-python
推荐使用预编译好的库安装
pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/{cu_version}/{torch_version}/index.html
例如,我装的cuda版本是10.1,pytorch是1.7.1,对应的命令为
pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/cu101/torch1.7.1/index.html
不好用的话可以尝试下这个命令,我需要用的是最新版本1.3.7,根据自己需要选择对应版本
,从官网https://github.com/open-mmlab/mmcv查询需要的版本
pip install mmcv-full==1.3.7 -f https://download.openmmlab.com/mmcv/dist/cu101/torch1.7.0/index.html
pip install opencv-python
最后执行
git clone https://github.com/open-mmlab/mmdetection.git
cd mmdetection
pip install -v -e .
环境安装完成之后,跑个DEMO试试吧
from mmdet.apis import init_detector, inference_detector
config_file = 'configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py'
# download the checkpoint from model zoo and put it in `checkpoints/`
# url: https://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth
checkpoint_file = 'checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'
device = 'cuda:0'
# init a detector
model = init_detector(config_file, checkpoint_file, device=device)
# inference the demo image
inference_detector(model, 'demo/demo.jpg')
前向推理就是用已经训练好的model对图片中的目标进行检测。在MMDetection中,模型在config文件中定义,训练好的model参数保存在checkpoint文件中
from mmdet.apis import init_detector, inference_detector
import mmcv
# Specify the path to model config and checkpoint file
config_file = 'configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py'
checkpoint_file = 'checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'
# build the model from a config file and a checkpoint file
model = init_detector(config_file, checkpoint_file, device='cuda:0')
# test a single image and show the results
img = 'test.jpg' # or img = mmcv.imread(img), which will only load it once
result = inference_detector(model, img)
# visualize the results in a new window
model.show_result(img, result)
# or save the visualization results to image files
model.show_result(img, result, out_file='result.jpg')
# test a video and show the results
video = mmcv.VideoReader('video.mp4')
for frame in video:
result = inference_detector(model, frame)
model.show_result(frame, result, wait_time=1)
mmdetection
├── mmdet
├── tools
├── configs
├── data #手动创建data、VOCdevkit、VOC2007、Annotations、JPEGImages、ImageSets、Main这些文件夹
│ ├── VOCdevkit
│ │ ├── VOC2007
│ │ │ ├── Annotations #把test.txt、trainval.txt对应的xml文件放在这
│ │ │ ├── JPEGImages #把test.txt、trainval.txt对应的图片放在这
│ │ │ ├── ImageSets
│ │ │ │ ├── Main
│ │ │ │ │ ├── test.txt
│ │ │ │ │ ├── trainval.txt
cd /mmdetection/configs/base/datasets 进入目录后打开voc0712.py
在data的配置 要删除屏蔽VOC2012的路径,和VOC2012变量 保存文件
cd /mmdetection/mmdet/datasets
进入目录后打开voc.py文件
这个CLASSES 是VOC标签的类别 我们要换成自己数据集的类别标签
cd /mmdetection/mmdet/core/evaluation
进入目录后打开class_names.py 文件
修改 voc_classes() 函数返回的标签,换成自己数据集的标签 保存退出
cd mmdetection/configs/faster_rcnn
我们这次选用faster_rcnn 模型训练,进入目录后打开faster_rcnn_r50_fpn_1x_coco.py文件
把原来COCO_detection.py 修改成VOC0712.py 文件
cd /mmdetection/configs/base/models
进入目录后打开 faster_rcnn_r50_fpn.py 文件 ,修改num_classes 数量,num_classes 的值等于类别数量,注意不需要加背景了
指定GPU训练
CUDA_VISIBLE_DEVICES=0 python3 ./tools/train.py ./configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py
自动从test文件里随机抽取图片进行预测
python tools/test.py
work_dirs/{训练自动生成dir}/{配置文件.py}
work_dirs/{训练自动生成的dir}/{epoch12.pth}
--show
例如:
python tools/test.py work_dirs/faster_rcnn_r50_fpn_1x_voc/faster_rcnn_r50_fpn_1x_voc.py work_dirs/faster_rcnn_r50_fpn_1x_voc/epoch_12.pth --show