安装
1.使用 conda 新建虚拟环境,并进入该虚拟环境;
conda create -n open-mmlab python=3.7 -y
conda activate open-mmlab
2.安装pytorch和torchvision
conda install pytorch torchvision -c pytorch
如果下载太慢/服务器无法翻墙,可以使用Anaconda清华镜像源,https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/。替换仓库源之后使用以下命令安装,去掉-c pytorch
conda install pytorch torchvision
3.安装mmcv-full
pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/cu101/torch1.7.0/index.html
4.安装mmdetection
pip install mmdet
5.下载mmdet代码
git clone https://github.com/open-mmlab/mmdetection.git
验证MMdetection安装是否成功
1.下载预训练模型,并将模型放入代码的checkpoint文件夹中(如果没有,请新建该文件夹)
下载地址: http://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth
2. 在mmdetection主目录中新建test_demo.py文件,文件内容为
from mmdet.apis import init_detector, inference_detector, show_result_pyplot
config_file = 'configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py'
checkpoint_file = 'checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'
device = 'cuda:0'
# 初始化检测器
model = init_detector(config_file, checkpoint_file, device=device)
# 推理演示图像
result = inference_detector(model, 'demo/demo.jpg')
img = 'demo/demo.jpg'
show_result_pyplot(model, img, result)
3.此时运行改代码会报错,还需要修改一个文件mmdet/core/visualization/image.py。
在该文件的函数 imshow_det_bboxes 开头添加
show=False
out_file='result.jpg' #会输出检测后的图片到主目录的result.jpg中
4.运行该文件,输出结果如下。安装成功。