1. 简介
商汤科技(2018 COCO 目标检测挑战赛冠军)和香港中文大学最近开源了一个基于Pytorch实现的深度学习目标检测工具箱mmdetection,支持Faster-RCNN,Mask-RCNN,Fast-RCNN,Cascade-RCNN等主流目标检测框架。可以快速部署自己的模型。
2.环境要求
- Linux (不支持windows)
- Python 3.5+
- >=PyTorch 1.1
- >=CUDA 9.0
- NCCL 2
- >=GCC 4.9
- mmcv
3.安装
按照官方文档建议先安装Anaconda,创建python虚拟环境,使用conda进行安装
- conda创建一个虚拟环境
conda create -n open-mmlab python=3.7 -y #创建名为open-mmlab,python版本为3.7的虚拟环境
conda activate open-mmlab #进入虚拟环境
- 安装pytorch及torchvision
conda install pytorch torchvision -c pytorch
将会安装以下包
如果不指定任何版本会安装最新的cuda10.1版的pytorch=1.4。可以指定pytorch和对应的cuda版本,如安装cuda9.0版的pytorch1.1
conda install pytorch=1.1 torchvision cudatoolkit=9.0 -c pytorch
网速问题
conda 使用默认源可能下载的很慢,可以改成国内的清华源,在用户目录下创建.condarc文件
gedit ~/.condarc
将下面这部分清华源代码复制到用户目录~/.condarc文件里
channels:
- defaults
show_channel_urls: true
channel_alias: https://mirrors.tuna.tsinghua.edu.cn/anaconda
default_channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
查看一下当前使用源
conda config --show-sources
显示清华源就好了,如果不行conda clean -i
更新一下。
还因为网速问题下载不了的话,下载不了的时候会在错误中提示包的下载地址,手动下载下来放到~/anaconda3/pkgs/目录下,使用以下命令安装本地包
conda install --use-local mypackage
- 下载mmdetection
git clone https://github.com/open-mmlab/mmdetection.git
cd mmdetection
- 安装mmdetection
pip install mmcv cython #安装mmcv和cython
pip install albumentations>=0.3.2 imagecorruptions pycocotools six terminaltables #安装依赖包
python setup.py develop #编译mmtection库,需要等一会
pip安装时如果速度很慢,临时使用在后面加 -i https://pypi.tuna.tsinghua.edu.cn/simple,使用国内源,例如使用清华源安装mmcv:
pip install mmcv -i https://pypi.tuna.tsinghua.edu.cn/simple
永久更换为国内源则建文件~/.pip/pip.conf
mkdir ~/.pip
cd ~/.pip
gedit pip.conf
复制以下内容即可
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
必须先安装mmcv,再运行setup.py编译,不然会报错。
- 验证是否安装成功
下载一个faster_rcnn_r50_fpn_1x的预训练模型,保存到mmdetection/checkpoints目录下,运行下面的代码,如果能显示图片,说明安装成功了。
from mmdet.apis import init_detector, inference_detector, show_result
import mmcv
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')
# test a single image and show the results
img = 'test.jpg' # or