MMDeploy 研究ノート (3): MMDetection モデルの展開

MMDeploy 研究ノート (3): 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 による推論
  • 予測方法1:コマンドラインシェルで実行
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、カテゴリ、および対応するスコアです。

  • 予測方法 2: コマンドライン予測スクリプトを直接実行する
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