目标检测mmdetection-demo

使用MMDetection检测器进行推断

两级检测器

在本教程中,我们使用Mask R-CNN,一个简单的两级检测器作为示例。

Mask R-CNN的高级架构如下图所示。

Mask R-CNN基于最初的Faster R-CNN添加了一个mask branch。它还使用RoIAlign,这是RoIPooling的更精确版本,用于RoI特征提取,以提高性能。更多详情可在https://arxiv.org/abs/1703.06870中找到。

首先在mmdetection目录下创建一个文件夹并且下载权重

!mkdir checkpoints
!wget -c https://download.openmmlab.com/mmdetection/v2.0/mask_rcnn/mask_rcnn_r50_caffe_fpn_mstrain-poly_3x_coco/mask_rcnn_r50_caffe_fpn_mstrain-poly_3x_coco_bbox_mAP-0.408__segm_mAP-0.37_20200504_163245-42aa3d00.pth \
      -O checkpoints/mask_rcnn_r50_caffe_fpn_mstrain-poly_3x_coco_bbox_mAP-0.408__segm_mAP-0.37_20200504_163245-42aa3d00.pth
import mmcv
from mmcv.runner import load_checkpoint
from mmdet.apis import inference_detector, show_result_pyplot
from mmdet.models import build_detector

# 选择使用配置并初始化检测器
config = '../configs/mask_rcnn/mask_rcnn_r50_caffe_fpn_mstrain-poly_3x_coco.py'
# 设置要加载的权重文件
checkpoint = 'checkpoints/mask_rcnn_r50_caffe_fpn_mstrain-poly_3x_coco_bbox_mAP-0.408__segm_mAP-0.37_20200504_163245-42aa3d00.pth'
# 设置用于计算的设备cpu或gpu 
device='cuda:0'
# 加载配置
config = mmcv.Config.fromfile(config)
# 将预训练设置为None,因为这里不需要预训练模型
config.model.pretrained = None
# 初始化检测器
model = build_detector(config.model)
# 加载权重
checkpoint = load_checkpoint(model, checkpoint, map_location=device)
# 设置用于推理的模型类
model.CLASSES = checkpoint['meta']['CLASSES']
# 我们需要设置模型的cfg以进行推理
model.cfg = config
# 将模型放在GPU上
model.to(device)
# 将模型转换为评估模式
model.eval()

开始推理

模型已成功创建并加载,让我们看看它有多好。我们使用MMDetection中实现的高级API推理检测器。创建此API是为了简化推理过程。代码的详细信息可在此处找到。

 #Use the detector to do inference
img = 'demo.jpg'
result = inference_detector(model, img)
# 打印图片
show_result_pyplot(model, img, result, score_thr=0.3)
结果如下图所示:

### MMDetection 目标检测库使用教程 MMDetection 是一个基于 PyTorch 的目标检测开源工具箱,广泛应用于自动驾驶、安防监控等领域[^1]。它为研究者和开发者提供了强大的功能支持,促进了深度学习目标检测技术的发展。 #### 安装指南 如果通过 MIM (Model Index Manager) 安装了 MMDetection,则可以通过以下方式初始化模型并执行推理操作: ```python from mmdet.apis import init_detector, inference_detector config_file = 'rtmdet_tiny_8xb32-300e_coco.py' checkpoint_file = 'rtmdet_tiny_8xb32-300e_coco_20220902_112414-78e30dcc.pth' model = init_detector(config_file, checkpoint_file, device='cpu') # 或者设置设备为 cuda:0 result = inference_detector(model, 'demo/demo.jpg') ``` 上述代码展示了如何加载预训练权重文件以及配置文件来创建模型实例,并对指定图片进行预测[^2]。 #### 可视化结果 为了更好地理解检测效果,可以利用可视化脚本来展示目标框及其置信度分数。以下是实现这一过程的一个简单命令行调用方法: ```bash python your_script.py path/to/image.jpg path/to/config.py path/to/checkpoint.pth --score-thr 0.5 ``` 此命令会读取输入图像路径、模型配置文件位置、检查点参数文件地址,并设定得分阈值以过滤低概率的结果[^4]。 对于初学者而言,建议按照官方文档中的指导完成环境搭建与依赖项安装之后再尝试运行以上例子[^3]。如果有任何疑问或者遇到困难都可以访问 GitHub 上对应的 Issues 页面寻求帮助。 ### 示例代码总结 综上所述,下面给出完整的端到端示例流程供参考: ```python import cv2 from mmdet.apis import init_detector, inference_detector, show_result_pyplot # 加载模型 config_file = 'configs/rtmdet/rtmdet_tiny_8xb32-300e_coco.py' checkpoint_file = 'checkpoints/rtmdet_tiny_8xb32-300e_coco_20220902_112414-78e30dcc.pth' device = 'cpu' # 如果有 GPU 支持可改为 'cuda:0' model = init_detector(config_file, checkpoint_file, device=device) # 推理一张图片 img_path = 'demo/demo.jpg' result = inference_detector(model, img_path) # 显示结果 show_result_pyplot(model, img_path, result) ``` 该段程序不仅完成了基本的功能需求还加入了图形界面显示部分使得整个演示更加直观易懂。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值