下列内容参照官网
- 使用现有模型进行推理
推理是指使用训练好的模型来检测图像上的目标。
在 MMDetection 中,一个模型被定义为一个配置文件和对应的存储在 checkpoint 文件内的模型参数的集合。
这里以Faster R-CNN为例子,下载config_file和checkpoint_file两个文件。
下面是建立模型和在图像或视频上进行推理的例子:
from mmdet.apis import init_detector, inference_detector
import mmcv
if __name__ == '__main__':
config_file = '../configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py'
checkpoint_file = '../checkpoints/faster_rcnn/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'
model = init_detector(config_file, checkpoint_file, device='cuda:0')
img = 'demo.jpg'
result = inference_detector(model, img)
model.show_result(img, result)
model.show_result(img, result, out_file='demo_result.jpg')
![在这里插入图片描述](https://img-blog.csdnimg.cn/76b73b5e8a0548409274a9903302ae7b.jpg?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5Y-25ZC56JC95LqG6aOO,size_19,color_FFFFFF,t_70,g_se,x_16#pic_center)