MMDeploy学习笔记(三):MMDetection模型部署

MMDeploy学习笔记(三):MMDetection模型部署

本博客演示了如何利用MMDetection和MMDeploy进行模型的部署。首先需要下载并配置MMDetection和MMDeploy的环境,然后按照如下的步骤进行YOLOX模型的部署。

  1. 下载模型权重文件
mkdir mmdetection/checkpoint
cd mmdetection/checkpoint/
# 下载 yolox 模型权重文件
wget https://download.openmmlab.com/mmdetection/v2.0/yolox/yolox_tiny_8x8_300e_coco/yolox_tiny_8x8_300e_coco_20211124_171234-b4047906.pth 

# 下载 retinanet 模型权重文件
# wget https://download.openmmlab.com/mmdetection/v2.0/retinanet/retinanet_r18_fpn_1x_coco/retinanet_r18_fpn_1x_coco_20220407_171055-614fd399.pth

# 下载 Faster R-CNN 模型权重文件
# wget https://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth
  1. Pytorch模型转ONNX模型
python mmdeploy/tools/deploy.py \
        mmdeploy/configs/mmdet/detection/detection_onnxruntime_dynamic.py \
        mmdetection/configs/yolox/yolox_tiny_8x8_300e_coco.py \
        mmdetection/checkpoint/yolox_tiny_8x8_300e_coco_20211124_171234-b4047906.pth \
        mmdetection/demo/demo.jpg \
        --work-dir mmdeploy_models/mmdetection/yolox \
        --device cpu
  1. 使用ONNXRunTime推理
  • 预测方式一:在命令行shell中运行
python
import cv2
from mmdeploy_python import Detector
img = cv2.imread('mmdetection/demo/demo.jpg')
onnx_path = 'mmdeploy_models/mmdetection/yolox'
detector = Detector(onnx_path, 'cpu')
result = detector(img)

结果为bbox、类别以及对应的得分。

  • 预测方式二:直接运行命令行预测脚本
SPDLOG_LEVEL=error python mmdeploy/demo/python/object_detection.py cpu mmdeploy_models/mmdetection/yolox mmdetection/demo/demo.jpg

运行成功后,在本地会生成output_detection.png目标检测预测结果图像文件。

猜你喜欢

转载自blog.csdn.net/weixin_43603658/article/details/129719192