# 一.前言
商汤和港中文联合开源了 mmdetection—基于 PyTorch 的开源目标检测工具包。
工具包支持 Mask RCNN 等多种流行的检测框架,读者可在 PyTorch 环境下测试不同的预训练模型及训练新的检测分割模型。
项目地址:https://github.com/open-mmlab/mmdetection
mmdetection 目标检测工具包
mmdetection 的主要特征可以总结为以下几个方面:
#二.搭建环境
## 1.cuda环境搭建
本文电脑显卡是1080ti,系统ubuntu16Lte,因为之前用的cuda版本都是8.0,所以这次首先要升级cuda版本
之前博文有教程:https://blog.csdn.net/qq_35153620/article/details/102724191
## 2.安装python环境
我安装的是python3.6
(1)下载anaconda,https://www.anaconda.com/
这里选择anaconda2或者anaconda3都可以,
安装,bash Anaconda2-2019.10-Linux-x86_64.sh(下载的anaconda安装文件名)
安装过程,点回车和yes就行
(2)创建python3.6环境
输入指令:
conda create -n pytorch python=3.6 numpy
(3)安装pytorch
https://pytorch.org/
选择你需要的pytorch,
在输入安装命令
conda install pytorch torchvision cudatoolkit=10.1 -c pytorch
测试pytorch的gpu版本是否可以正常使用:
输入命令进入python编译模式:
python
输入命令加载pytorch:
import pytorch
输入命令查看gpu是否可以使用:
print(torch.cuda.is_available())
如果返回True,则表示安装成功
(4)安装opencv
conda install opencv
Package选择Conda,
最后会出现安装命令,先输入命令进入python3.6环境下:
source activate pytorch
(5)安装opencv
直接安装可能会比较慢,建议用镜像源,使用清华镜像源:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes
输入命令安装:
conda install opencv
(5)安装mmcv
pip install mmcv
(6)安装matplotlib
conda install matplotlib
(7)安装terminaltables
conda install terminaltables
(8)安装pycocotools
conda install pycocotools
(9)安装scipy
conda install scipy
(10)安装imgaug
conda install imgaug
(11)安装mmdetection
进入mmdetection文件下,输入命令:
python setup.py develop
## 安装nccl用于gpu并行计算
从网址下载,需要登录账号,https://developer.nvidia.com/nccl
根据https://docs.nvidia.com/deeplearning/sdk/nccl-install-guide/index.html网页中的教程,选择安装本地版本还是网络版本
# 进行demo测试
config_file = '/configs/faster_rcnn_r50_fpn_1x.py'
checkpoint_file = '/checkpoints/faster_rcnn_r50_fpn_1x_20181010-3d1b3351.pth'
上面地址最好改成绝对地址,且checkpoint_file存放的权重文件,
权重最好提前下好,https://s3.ap-northeast-2.amazonaws.com/open-mmlab/mmdetection/models/faster_rcnn_r50_fpn_1x_20181010-3d1b3351.pth
这个比较慢,可以再百度云下载
https://pan.baidu.com/s/1yj1vxUMQRl5Olga4D79-Aw#list/path=%2F
提取码:dpyl
from mmdet.apis import init_detector, inference_detector, show_result
import mmcv
config_file = '/home/omnisky/project/lyx/mmdetection/configs/faster_rcnn_r50_fpn_1x.py'
checkpoint_file = '/home/omnisky/project/lyx/mmdetection/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')
# 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
show_result(img, result, model.CLASSES)
# or save the visualization results to image files
show_result(img, result, model.CLASSES, 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)
show_result(frame, result, model.CLASSES, wait_time=1)