mmdetection(简称mmdet)是OpenMMLab商汤科技的开源目标检测软件。配置还是会遇到不少坑,下面给出在Windows下面的配置和运行。
(1)建立conda环境
我配置了python3.9没有成功,然后配置了3.8成功搞定,pytorch1.9,cuda10.2,安装conda后利用下面命令创建虚拟环境:
conda create --name py38 python=3.8
(2)从GitHub下载mmdet源码并进行前期预备工作,地址:https://github.com/open-mmlab/mmdetection
以管理员权限运行conda prompt,然后进入安装目录我的是G:\mmdetection-master。
执行下面三个方面的其他组件安装
1)pytorch安装,用下面的命令
conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch
如果比较慢,就换成
pip3 install torch==1.9.1+cu102 torchvision==0.10.1+cu102 torchaudio===0.9.1 -f https://download.pytorch.org/whl/torch_stable.html
2)安装源码中的必须组件(pip3 和 pip命令没区别)
pip install -r requirements.txt
3)安装mmvc
pip install mmcv-full
(3)编译mmdet
运行命令
#激活py38环境
activate py38
#执行编译
python setup.py build develop
(4)运行和测试目标检测
cd/demo,然后建立一个py文件
在文件里面运行如下的代码
from mmdet.apis import init_detector, inference_detector, show_result_pyplot
import mmcv
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'
model = init_detector(config_file, checkpoint_file, device='cuda:0')
img = 'demo.jpg'
# img='input1.jpg'
result = inference_detector(model, img)
show_result_pyplot(model, img, result)
输入结果如下: