安装mmdetection 前请准备好pytorch环境,PyTorch 0.4 ,PyTorch 1.1 或更高版本,自己根据需要进行选择安装!
强烈建议在anaconda上安装自己需要的环境!
conda create -n open-mmlab python=3.7 -y
source activate open-mmlab
conda install -c pytorch pytorch torchvision -y
conda install cython -y
其他相关依赖包(如opencv,matplotlib、pillow等)这里省略,如运行程序发现没有,你可以用pip或conda进行安装!
1. 我的安装方法!
(1)PyTorch-0.4.1版本mmdetectionhttps://github.com/open-mmlab/mmdetection/tree/pytorch-0.4.1
目前,CSDN上网友们给的教程基本都是针对pytorch-0.4.1版本mmdetection的安装方法!
(pytorch) liuq@liuq:~/liuq$ git clone https://github.com/ming71/mmdetection-annotated.git
(pytorch) liuq@liuq:~/liuq$ cd mmdetection/
(pytorch) liuq@liuq:~/liuq/mmdetection$ ./compile.sh
(pytorch) liuq@liuq:~/liuq/mmdetection$ python setup.py install
(2)PyTorch 1.1版本mmdetection --推荐使用最新版本https://github.com/open-mmlab/mmdetection/tree/master
官方安装方法:https://github.com/open-mmlab/mmdetection/blob/master/INSTALL.md
(pytorch) liuq@liuq:~/liuq$ git clone https://github.com/open-mmlab/mmdetection.git
(pytorch) liuq@liuq:~/liuq$ cd mmdetection/
(pytorch) liuq@liuq:~/liuq/mmdetection$ python setup.py develop 或 pip install -v -e .
特别注意
当你运行程序出现下面错误提示,说明你的mmdetection的安装方法是有问题的!请确认你当前使用的版本,切记不同版本的安装方法不一样!!
本人,在这里入坑停留了一天,最后才仔细看了官网资料才发现!!!!
PyTorch 1.1版本mmdetection要采用“python setup.py develop 或 pip install -v -e .”安装!!!
PyTorch-0.4.1版本mmdetection采用 “./compile.sh” 编译再用python setup.py install安装!
(pytorch) dl@dl:~/VSST/Tianchi/mmdetection$ python test.py
Traceback (most recent call last):
File "test.py", line 2, in <module>
from mmdet.apis import init_detector, inference_detector, show_result
File "/home/dl/VSST/Tianchi/mmdetection/mmdet/apis/__init__.py", line 2, in <module>
from .train import train_detector
File "/home/dl/VSST/Tianchi/mmdetection/mmdet/apis/train.py", line 10, in <module>
from mmdet import datasets
File "/home/dl/VSST/Tianchi/mmdetection/mmdet/datasets/__init__.py", line 1, in <module>
from .custom import CustomDataset
File "/home/dl/VSST/Tianchi/mmdetection/mmdet/datasets/custom.py", line 12, in <module>
from .extra_aug import ExtraAugmentation
File "/home/dl/VSST/Tianchi/mmdetection/mmdet/datasets/extra_aug.py", line 5, in <module>
from mmdet.core.evaluation.bbox_overlaps import bbox_overlaps
File "/home/dl/VSST/Tianchi/mmdetection/mmdet/core/__init__.py", line 6, in <module>
from .post_processing import * # noqa: F401, F403
File "/home/dl/VSST/Tianchi/mmdetection/mmdet/core/post_processing/__init__.py", line 1, in <module>
from .bbox_nms import multiclass_nms
File "/home/dl/VSST/Tianchi/mmdetection/mmdet/core/post_processing/bbox_nms.py", line 3, in <module>
from mmdet.ops.nms import nms_wrapper
File "/home/dl/VSST/Tianchi/mmdetection/mmdet/ops/__init__.py", line 1, in <module>
from .dcn import (DeformConv, DeformConvPack, ModulatedDeformConv,
File "/home/dl/VSST/Tianchi/mmdetection/mmdet/ops/dcn/__init__.py", line 1, in <module>
from .functions.deform_conv import deform_conv, modulated_deform_conv
File "/home/dl/VSST/Tianchi/mmdetection/mmdet/ops/dcn/functions/deform_conv.py", line 5, in <module>
from .. import deform_conv_cuda
ImportError: cannot import name 'deform_conv_cuda'
2. 测试(官方提供的预训练模型跑测试)
测试前请下载好需要的模型文件:https://github.com/open-mmlab/mmdetection/blob/master/MODEL_ZOO.md https://github.com/open-mmlab/mmdetection/blob/master/GETTING_STARTED.md
import mmcv
from mmdet.apis import init_detector, inference_detector, show_result
config_file = 'configs/faster_rcnn_r50_fpn_1x.py'
checkpoint_file = 'checkpoints/faster_rcnn_r50_fpn_1x_20181010-3d1b3351.pth'
# build the model from a config file and a checkpoint file
model = init_detector(config_file, checkpoint_file, device='cuda:0')
# (1)测试单张图片,并显示测试结果
img = 'demo/test1.jpg' # or img = mmcv.imread(img), which will only load it once
result = inference_detector(model, img)
show_result(img, result, model.CLASSES)
"""
# (2)同时测试几张图片,并保存测试结果
imgs = ['demo/test1.jpg', 'demo/test2.jpg']
for i, result in enumerate(inference_detector(model, imgs)):
show_result(imgs[i], result, model.CLASSES, out_file='result_{}.jpg'.format(i))
# (3)测试视频和显示测试结果
video = mmcv.VideoReader('demo/Venice-2.mp4')
for frame in video:
result = inference_detector(model, frame)
show_result(frame, result, model.CLASSES, wait_time=1)
"""
2. 训练
在mmdetection下的data路径下准备好训练数据(建议通过软连接方式指定数据路径!),本人测试用的是coco2017数据集。
(1)coco2017数据下载
http://images.cocodataset.org/zips/train2017.zip
http://images.cocodataset.org/annotations/annotations_trainval2017.zip
http://images.cocodataset.org/zips/val2017.zip
http://images.cocodataset.org/annotations/stuff_annotations_trainval2017.zip
http://images.cocodataset.org/zips/test2017.zip
http://images.cocodataset.org/annotations/image_info_test2017.zip
(2)训练命令
python tools/train.py configs/faster_rcnn_r50_fpn_1x.py
“faster_rcnn_r101_fpn_1x”测试
import os
import mmcv
from mmdet.apis import init_detector, inference_detector, show_result
# 指定路径后,返回该路径下的文件绝对地址列表
def Get_pathlist(imgPath):
filelist = os.listdir(imgPath)
imglist=[]
for i in filelist:
path = os.path.join(os.path.abspath(imgPath), i)
imglist.append(path)
return imglist
config_file = 'configs/faster_rcnn_r101_fpn_1x.py'
checkpoint_file = 'checkpoints/faster_rcnn_r101_fpn_1x_20181129-d1468807.pth'
model = init_detector(config_file, checkpoint_file, device='cuda:0')
# test a list of images and write the results to image files
imgPath ="demo" # 测试图片路径
dst_Path="result/" # 推断结果保存路径
if not os.path.exists(dst_Path):
os.makedirs(dst_Path)
imgs=Get_pathlist(imgPath) # 获取指定路径下的所有图片绝对路径
for i, result in enumerate(inference_detector(model, imgs)):
show_result(imgs[i], result, model.CLASSES, out_file=os.path.join(dst_Path, 'result_{}.jpg'.format(i)))
常用模型评估测试图片
import os
import mmcv
from mmdet.apis import init_detector, inference_detector, show_result
config_file = 'configs/faster_rcnn_r50_fpn_1x.py'
checkpoint_file = 'checkpoints/faster_rcnn_r50_fpn_1x_20181010-3d1b3351.pth'
model = init_detector(config_file, checkpoint_file, device='cuda:0')
# 指定路径后,返回该路径下的文件绝对地址列表
def Get_pathlist(imgPath):
filelist = os.listdir(imgPath)
imglist=[]
for i in filelist:
path = os.path.join(os.path.abspath(imgPath), i)
imglist.append(path)
return imglist
imgPath ="demo" # 测试图片路径
dst_Path=os.path.join("result/", config_file.split("/")[-1].split(".")[0]) # 推断结果保存路径
if not os.path.exists(dst_Path):
os.makedirs(dst_Path)
imgs=Get_pathlist(imgPath) # 获取指定路径下的所有图片绝对路径
for i, result in enumerate(inference_detector(model, imgs)):
show_result(imgs[i], result, model.CLASSES, out_file=os.path.join(dst_Path,'result_{}.jpg'.format(i)))
常用模型评估测试视频
import os
import mmcv
from mmdet.apis import init_detector, inference_detector, show_result
config_file = 'configs/cascade_rcnn_r101_fpn_1x.py'
checkpoint_file = 'checkpoints/cascade_rcnn_r101_fpn_1x_20181129-d64ebac7.pth'
model = init_detector(config_file, checkpoint_file, device='cuda:0')
# 测试视频和显示测试结果
video = mmcv.VideoReader('test1.mov') # mp4,avi,mov等常用的视频格式都可以!
# height = video.height
# width = video.width
dst_Path=os.path.join("result/", config_file.split("/")[-1].split(".")[0]) # 推断结果保存路径
num=0
for frame in video:
result = inference_detector(model, frame)
num=num+1
print(num)
# show_result(frame, result, model.CLASSES, wait_time=1) # 不保存测试的图片(帧)
show_result(frame, result, model.CLASSES, wait_time=1,out_file=os.path.join(dst_Path,'result_{}.jpg'.format(num))) #保存测试后的图片(帧)!