本人刚入门目标检测,发现了一个开源的工具箱mmdetection,便自己使用里面的模型试着测试一张图片,网上大多数博客写的还是不够详细,所以想自己写一篇,方便大家及自己今后可以回顾。
首先,mmdetection的安装就不用多说了,网上有很多。
1、克隆mmdetection到自己的文件夹中(安装mmdetection时已经克隆就不需要在克隆了),
在终端服务器中输入
git clone https://github.com/open-mmlab/mmdetection
cd mmdetection
2、下载已经训练好的训练模型。
https://github.com/open-mmlab/mmdetection/blob/master/docs/MODEL_ZOO.md
本人下载的是faster_rcnn_r101_fpn_1x_20181129-d1468807.pth
在mmdetection中创建models文件夹,将其放入其中
3、在mmdetection中在mmdetection中创建mycode文件夹,再在mycode下创建test文件夹,新建test.py
#coding=utf-8
from mmdet.apis import init_detector
from mmdet.apis import inference_detector
from mmdet.apis import show_result
# 模型配置文件
config_file = '../../configs/faster_rcnn_r101_fpn_1x.py'
# 预训练模型文件
checkpoint_file = '../../models/faster_rcnn_r101_fpn_1x_20181129-d1468807.pth'
# 通过模型配置文件与预训练文件构建模型
model = init_detector(config_file, checkpoint_file, device='cuda:0')
# 测试单张图片并进行展示
img = 'test1.jpg'
result = inference_detector(model, img)
show_result(img, result, model.CLASSES)
其中,faster_rcnn_r101_fpn_1x.py文件是mmdetection中存在的,需与下载的pth文件相对应。图片存放在test文件夹中。