# MMDetection-OM-Inference **Repository Path**: wwfu/mmdetection-om-inference ## Basic Information - **Project Name**: MMDetection-OM-Inference - **Description**: MMDetection模型在昇腾NPU芯片上离线推理公用流程。感谢大家的使用与反馈,因工作原因,不定时更新。 - **Primary Language**: Python - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 6 - **Forks**: 4 - **Created**: 2023-04-18 - **Last Updated**: 2025-07-05 ## Categories & Tags **Categories**: Uncategorized **Tags**: Ascend, CANN, mmdetection, 目标检测, 实例分割 ## README
 
Ascend Hot     CANN Explore     MMDetection Try
 
[![Static Badge](https://img.shields.io/badge/release-v0.0.1-yellow)](https://gitee.com/wwfu/mmdetection-om-inference/releases/tag/v0.0.1) [![Static Badge](https://img.shields.io/badge/author-wwfu-blue)](https://gitee.com/wwfu) [![Static Badge](https://img.shields.io/badge/author-likedemo-orange)](https://gitee.com/likedemo) [![Static Badge](https://img.shields.io/badge/license-Apache2.0-green)](LICENSE) [![Static Badge](https://img.shields.io/badge/language-Python-cyan)](https://www.python.org/) [![Static Badge](https://img.shields.io/badge/CANN-6.3.RC1-red)](https://www.hiascend.com/document/detail/zh/canncommercial/63RC1/overview/index.html)
## 简介 MMDetection是OpenMMLab基于PyTroch开发的目标检测工具箱。凭借其丰富的模型组件与对主流检测算法的广泛支持,使用MMDetection可以轻松地进行模型开发与训练,但在应用场景中进行离线推理时,却仍然容易遇到各种各样的难题。基于此背景,我们在昇腾NPU平台上对多个MMDetection模型进行了离线推理试验并统一了整个离线推理流程,使得MMDetection模型在NPU平台上离线推理的难度大大降低。 MMDetection 支持了多种检测任务,包括目标检测,实例分割,全景分割,以及半监督目标检测。目前我们只对实际应用较多的目标检测、实例分割进行了适配。 ## 特性 1. **开箱即用**,使用端到端的模型转换与推理工具,无需关注MMDetection与CANN的内部细节,即可快速实现MMDetection模型在NPU芯片上的转换与推理。 2. **可扩展**,可根据提供的接口快捷地注册自定义数据预处理操作、修改算子映射,添加ONNX修改策略,以快速实现新模型的转换与推理。 ## 推理环境 该项目在CANN6.3.RC1环境下开发测试,请参考[PyTorch框架推理环境准备](https://www.hiascend.com/document/detail/zh/ModelZoo/pytorchframework/pies/pies_00001.html)完成NPU驱动固件与CANN的安装。然后设置CANN的环境变量: ```bash # 以CANN包实际安装路径为准 source /usr/local/Ascend/ascend-toolkit/set_env.sh ``` 最后执行以下命令创建python虚拟环境并安装第三方依赖。 ```bash env_name=npu-mmdet bash tools/create_env.sh ${env_name} conda activate ${env_name} ``` 上述命令除了在线模型必不可少的`PyTroch`外,还安装了ONNX模型改图工具[auto-optimizer](https://gitee.com/ascend/msadvisor/tree/master/auto-optimizer)与OM模型的推理工具[ais-bench](https://gitee.com/ascend/tools/tree/master/ais-bench_workload/tool/ais_bench),这些工具的具体安装细节与使用文档请移步其Gitee主页。 ## 模型列表 我们在MMDetection仓下筛选出了一些经典的目标检测、实例分割进行适配。目前仅支持生成batchsize为1的离线模型。 ### 目标检测 | 模型 | 配置文件 | 权重文件 | input_shape | 官方精度
(bboxAP) | 离线精度
(bboxAP) | 纯推理fps | | -- | -- | -- | -- | -- | -- | -- | | Faster R-CNN | [r50_fpn_1x](https://github.com/open-mmlab/mmdetection/blob/v3.0.0/configs/faster_rcnn/faster-rcnn_r50_fpn_1x_coco.py) | [Checkpoint](https://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth) | 800x1088 | 37.4 | 37.1 | 42.7 | | Faster R-CNN | [r101_fpn_2x](https://github.com/open-mmlab/mmdetection/blob/v3.0.0/configs/faster_rcnn/faster-rcnn_r101_fpn_2x_coco.py) | [Checkpoint](https://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r101_fpn_2x_coco/faster_rcnn_r101_fpn_2x_coco_bbox_mAP-0.398_20200504_210455-1d2dac9c.pth) | 800x1088 | 39.8 | 39.4 | 37.1 | | SSD | [mobilenetv2-scratch_8xb24-600e](https://github.com/open-mmlab/mmdetection/blob/v3.0.0/configs/ssd/ssdlite_mobilenetv2-scratch_8xb24-600e_coco.py) | [Checkpoint]( https://download.openmmlab.com/mmdetection/v2.0/ssd/ssdlite_mobilenetv2_scratch_600e_coco/ssdlite_mobilenetv2_scratch_600e_coco_20210629_110627-974d9307.pth) | 320x320 | 21.3 | 21.3 | 172.5 | | YOLOX | [s_8xb8-300e](https://github.com/open-mmlab/mmdetection/blob/v3.0.0/configs/yolox/yolox_s_8xb8-300e_coco.py) | [Checkpoint]( https://download.openmmlab.com/mmdetection/v2.0/yolox/yolox_s_8x8_300e_coco/yolox_s_8x8_300e_coco_20211121_095711-4592a793.pth) | 640x640 | 40.5 | 40.3 | 48.8 | | YOLOX | [x_8xb8-300e](https://github.com/open-mmlab/mmdetection/blob/v3.0.0/configs/yolox/yolox_x_8xb8-300e_coco.py) | [Checkpoint]( https://download.openmmlab.com/mmdetection/v2.0/yolox/yolox_x_8x8_300e_coco/yolox_x_8x8_300e_coco_20211126_140254-1ef88d67.pth) | 640x640 | 50.9 | 50.6 | 41.5 | | Cascade R-CNN | [r50_fpn_1x](https://github.com/open-mmlab/mmdetection/blob/v3.0.0/configs/cascade_rcnn/cascade-rcnn_r50_fpn_1x_coco.py) | [Checkpoint]( https://download.openmmlab.com/mmdetection/v2.0/cascade_rcnn/cascade_rcnn_r50_fpn_1x_coco/cascade_rcnn_r50_fpn_1x_coco_20200316-3dc56deb.pth) | 800x1088 | 40.3 | 39.9 | 26.6 | | Cascade R-CNN | [r101_fpn_1x](https://github.com/open-mmlab/mmdetection/blob/v3.0.0/configs/cascade_rcnn/cascade-rcnn_r101_fpn_1x_coco.py) | [Checkpoint]( https://download.openmmlab.com/mmdetection/v2.0/cascade_rcnn/cascade_rcnn_r101_fpn_1x_coco/cascade_rcnn_r101_fpn_1x_coco_20200317-0b6a2fbf.pth) | 800x1088 | 42.0 | 41.3 | 24.4 | | RetinaNet | [r50_fpn_1x](https://github.com/open-mmlab/mmdetection/blob/v3.0.0/configs/retinanet/retinanet_r50_fpn_1x_coco.py) | [Checkpoint](https://download.openmmlab.com/mmdetection/v2.0/retinanet/retinanet_r50_fpn_1x_coco/retinanet_r50_fpn_1x_coco_20200130-c2398f9e.pth) | 800x1088 | 36.5 | 36.0 | 46.5 | | YOLOV3 | [d53_8xb8-ms-416-273e](https://github.com/open-mmlab/mmdetection/blob/v3.0.0/configs/yolo/yolov3_d53_8xb8-ms-416-273e_coco.py) | [Checkpoint]( https://download.openmmlab.com/mmdetection/v2.0/yolo/yolov3_d53_mstrain-416_273e_coco/yolov3_d53_mstrain-416_273e_coco-2b60fcd9.pth) | 320x416 | 30.9 | 30.5 | 107.7 | | ATSS | [r50_fpn_1x](https://github.com/open-mmlab/mmdetection/blob/v3.0.0/configs/atss/atss_r50_fpn_1x_coco.py) | [Checkpoint]( https://download.openmmlab.com/mmdetection/v2.0/atss/atss_r50_fpn_1x_coco/atss_r50_fpn_1x_coco_20200209-985f7bd0.pth) | 800x1088 | 39.4 | 39.1 | 47.1 | | FCOS | [x101-64x4d_fpn_gn-head_ms-640-800-2x](https://github.com/open-mmlab/mmdetection/blob/v3.0.0/configs/fcos/fcos_x101-64x4d_fpn_gn-head_ms-640-800-2x_coco.py) | [Checkpoint]( https://download.openmmlab.com/mmdetection/v2.0/fcos/fcos_x101_64x4d_fpn_gn-head_mstrain_640-800_2x_coco/fcos_x101_64x4d_fpn_gn-head_mstrain_640-800_2x_coco-ede514a8.pth) | 800x1088 | 42.6 | 42.0 | 22.7 | | FoveaBox | [r50_fpn_gn-head-align_ms-640-800-4xb4-2x](https://github.com/open-mmlab/mmdetection/blob/v3.0.0/configs/foveabox/fovea_r50_fpn_gn-head-align_ms-640-800-4xb4-2x_coco.py) | [Checkpoint]( https://download.openmmlab.com/mmdetection/v2.0/foveabox/fovea_align_r50_fpn_gn-head_mstrain_640-800_4x4_2x_coco/fovea_align_r50_fpn_gn-head_mstrain_640-800_4x4_2x_coco_20200205-85ce26cb.pth) | 800x1088 | 40.4 | 40.3 | 18.4 | | FSAF | [r50_fpn_1x](https://github.com/open-mmlab/mmdetection/blob/v3.0.0/configs/fsaf/fsaf_r50_fpn_1x_coco.py) | [Checkpoint]( https://download.openmmlab.com/mmdetection/v2.0/fsaf/fsaf_r50_fpn_1x_coco/fsaf_r50_fpn_1x_coco-94ccc51f.pth) | 800x1088 | 37.4 | 36.8 | 59.9 | | CenterNet | [r18-dcnv2_8xb16-crop512-140e](https://github.com/open-mmlab/mmdetection/blob/v3.0.0/configs/centernet/centernet_r18-dcnv2_8xb16-crop512-140e_coco.py) | [Checkpoint]( https://download.openmmlab.com/mmdetection/v2.0/centernet/centernet_resnet18_dcnv2_140e_coco/centernet_resnet18_dcnv2_140e_coco_20210702_155131-c8cd631f.pth) | 448x672 | 29.5 | 29.0 | 137.3 | | GFL | [r50_fpn_ms-2x](https://github.com/open-mmlab/mmdetection/blob/v3.0.0/configs/gfl/gfl_r50_fpn_ms-2x_coco.py) | [Checkpoint]( https://download.openmmlab.com/mmdetection/v2.0/gfl/gfl_r50_fpn_mstrain_2x_coco/gfl_r50_fpn_mstrain_2x_coco_20200629_213802-37bb1edc.pth) | 800x1088 | 42.9 | 42.8 | 45.9 | ### 实例分割 | 模型 | 配置文件 | 权重文件 | input_shape | 官方精度
(maskAP) | 离线精度
(maskAP) | 纯推理fps | | -- | -- | -- | -- | -- | -- | -- | | Mask R-CNN | [r50_fpn_amp-1x](https://github.com/open-mmlab/mmdetection/blob/v3.0.0/configs/mask_rcnn/mask-rcnn_r50_fpn_amp-1x_coco.py) | [Checkpoint]( https://download.openmmlab.com/mmdetection/v2.0/fp16/mask_rcnn_r50_fpn_fp16_1x_coco/mask_rcnn_r50_fpn_fp16_1x_coco_20200205-59faf7e4.pth) | 800x1088 | 34.7 | 34.4 | 34.8 | | Mask R-CNN | [r101_fpn_2x](https://github.com/open-mmlab/mmdetection/blob/v3.0.0/configs/mask_rcnn/mask-rcnn_r101_fpn_2x_coco.py) | [Checkpoint]( https://download.openmmlab.com/mmdetection/v2.0/mask_rcnn/mask_rcnn_r101_fpn_2x_coco/mask_rcnn_r101_fpn_2x_coco_bbox_mAP-0.408__segm_mAP-0.366_20200505_071027-14b391c7.pth) | 800x1088 | 36.6 | 36.0 | 30.9 | | Swin Transformer | [t-p4-w7_fpn_amp-ms-crop-3x](https://github.com/open-mmlab/mmdetection/blob/v3.0.0/configs/swin/mask-rcnn_swin-t-p4-w7_fpn_amp-ms-crop-3x_coco.py) | [Checkpoint]( https://download.openmmlab.com/mmdetection/v2.0/swin/mask_rcnn_swin-t-p4-w7_fpn_fp16_ms-crop-3x_coco/mask_rcnn_swin-t-p4-w7_fpn_fp16_ms-crop-3x_coco_20210908_165006-90a4008c.pth) | 800x1088 | 41.7 | 40.8 | 10.1 | | Cascade Mask R-CNN | [r50_fpn_1x](https://github.com/open-mmlab/mmdetection/blob/v3.0.0/configs/cascade_rcnn/cascade-mask-rcnn_r50_fpn_1x_coco.py) | [Checkpoint]( https://download.openmmlab.com/mmdetection/v2.0/cascade_rcnn/cascade_mask_rcnn_r50_fpn_1x_coco/cascade_mask_rcnn_r50_fpn_1x_coco_20200203-9d4dcb24.pth) | 800x1088 | 35.9 | 35.3 | 18.9 | | Cascade Mask R-CNN | [r101_fpn_1x](https://github.com/open-mmlab/mmdetection/blob/v3.0.0/configs/cascade_rcnn/cascade-mask-rcnn_r101_fpn_1x_coco.py) | [Checkpoint]( https://download.openmmlab.com/mmdetection/v2.0/cascade_rcnn/cascade_mask_rcnn_r101_fpn_1x_coco/cascade_mask_rcnn_r101_fpn_1x_coco_20200203-befdf6ee.pth) | 800x1088 | 37.3 | 36.7 | 17.7 | ## 快速使用 ### PyTorch到OM端到端转换 **用法:** ```bash python tools/convert.py [-h | --help] [--task {det,seg}] [--image IMAGE] [--batch_sizes BATCH_SIZES [BATCH_SIZES ...]] [--use_dynamic_batch_size] [--input_shape INPUT_SHAPE INPUT_SHAPE] [--soc_version SOC_VERSION] [--save_dir SAVE_DIR] [--file_prefix FILE_PREFIX] ``` **参数说明:** + `config`: 模型配置文件路径。 + `checkpoint`: PyTorch模型权重文件路径。 + `--task`: 说明模型支持的任务,可选值为['det', 'seg'],分别表示目标检测、实例分割,默认值为 'det'。 + `--image`: 指定一张图片路径,导出ONNX模型时,使用此图片生成的数据进行 Tracing。 + `--batch_sizes`: 指定一个或多个batchsize值。 + `--use_dynamic_batch_size`: 若设置此参数并且指定了多个batchsize,则最终生成一个动态batchsize的OM,否则每个batchsize各生成一个静态OM。 + `--input_shape`: 指定模型输入的高与宽,eg: `--input_shape H W`,默认值为 [800, 1088]。 + `--soc_version`: 目标芯片的版本,例如`--soc_version Ascend310P3`。 + `--save_dir`: 存放模型的目录,如果不指定,则以config的文件名创建存放目录。 + `--file_prefix`: 模型存放的文件名前缀,默认为'model'。 需要注意的是,若`npu_mmdet/convert/deploy_configs`目录下的配置文件包含同名项时,优先使用命令行参数值。 **示例:** 1. 目标检测模型转换 ```bash # 下载模型权重文件 wget -P ./models https://download.openmmlab.com/mmdetection/v2.0/yolox/yolox_s_8x8_300e_coco/yolox_s_8x8_300e_coco_20211121_095711-4592a793.pth # PyTorch到OM端到端转换 python tools/convert.py \ mmdetection/configs/yolox/yolox_s_8xb8-300e_coco.py \ models/yolox_s_8x8_300e_coco_20211121_095711-4592a793.pth \ --batch_sizes 1 \ --input_shape 640 640 \ --save_dir models \ --file_prefix yolox_s ``` 执行结束后将会生成`models/yolox_s_bs1.om`,该模型为batch_size为 1 的静态模型,只能处理shape为 1x3x640x640 的数据。 2. 实例分割模型转换 ```bash # 下载模型权重文件 wget -P ./models https://download.openmmlab.com/mmdetection/v2.0/fp16/mask_rcnn_r50_fpn_fp16_1x_coco/mask_rcnn_r50_fpn_fp16_1x_coco_20200205-59faf7e4.pth # PyTorch到OM端到端转换 python tools/convert.py \ mmdetection/configs/mask_rcnn/mask-rcnn_r50_fpn_amp-1x_coco.py \ models/mask_rcnn_r50_fpn_fp16_1x_coco_20200205-59faf7e4.pth \ --task seg \ --batch_sizes 1 4 8 \ --input_shape 800 1088 ``` 执行结束后将会在`mask-rcnn_r50_fpn_amp-1x_coco`目录下生成`model_bs1.om`、`model_bs4.om`、`model_bs8.om`。 ### 端到端推理 **用法:** ```bash python tools/infer.py [-h | --help] [--inputs INPUTS] [--out_dir OUT_DIR] [--device DEVICE] [--pred_score_thr PRED_SCORE_THR] [--show] [--no_save_vis] [--no_save_pred] [--print_result] ``` **参数说明:** + `config`: 模型配置文件路径。 + `om_model`: OM模型路径。 + `--inputs`: 单张图片路径或者文件夹路径。 + `--out_dir`: 指定输出目录,用于保存预测结果或可视化结果,默认为'./outputs'。 + `--device`: NPU芯片ID,默认为0。 + `--pred_score_thr`: 检测框的输出阈值,丢弃置信度小于该阈值的检测框,默认为0.3。 + `--show`: 是否在弹出窗口中显示图像。 + `--no_save_vis`: 不保存将预测结果可视化后的图片。 + `--no_save_pred`: 不保存json格式的预测结果。 + `--print_result`: 是否打印预测结果。 **示例:** 1. 使用yolox_s模型进行目标检测 ```bash python tools/infer.py \ mmdetection/configs/yolox/yolox_s_8xb8-300e_coco.py \ models/yolox_s_bs1.om \ --inputs demo/test_images \ --out_dir demo/det_outputs ``` 2. 使用maskrcnn_r50模型进行目标检测+实例分割 ```bash python tools/infer.py \ mmdetection/configs/mask_rcnn/mask-rcnn_r50_fpn_amp-1x_coco.py \ mask-rcnn_r50_fpn_amp-1x_coco/model_bs1.om \ --inputs demo/test_images \ --out_dir demo/seg_outputs ``` ### 模型精度评估 **数据集:** 该项目使用COCO2017的验证集来测试模型精度,进入[下载页面](https://cocodataset.org/#download),自行下载并解压,然后按照以下的目录结构存放: ``` ├── data/ ├── coco ├── annotations/ │ └── instances_val2017.json └── val2017/ ├── 000000000139.jpg ├── ... └── 000000581781.jpg ``` **用法:** ```bash python tools/evaluate.py [-h | --help] [--mode {online,offline}] [--om_model OM_MODEL] [--checkpoint CHECKPOINT] [--batch_size BATCH_SIZE] [--input_shape INPUT_SHAPE INPUT_SHAPE] [--use_offline_data] ``` **参数说明:** + `config`: 模型配置文件路径。 + `--mode`: 指定在线模型评估还是离线模型评估,取值分别对应'online'与'offline',默认值为'offline'. + `--om_model`: OM模型路径, 当`--mode=offline`时,必须指定此参数。 + `--checkpoint`: PyTorch模型权重文件路径, 当`--mode=online`时,必须指定此参数。 + `--batch_size`: 模型输入的batchsize, 当`--mode=online`时生效,默认为1。 + `--use_offline_data`: 若为True,则使用离线 ImageProprocessor 生成的数据来评估模型精度,否则,使用在线 DataLoader 加载的数据。仅在`--mode=online`时生效。 + `--input_shape`: 在`--use_offline_data`的情况下, 需指定模型输入数据的高宽,例如`--input_shape H W`. **示例:** 1. 评估OM模型的精度 ```bash python tools/evaluate.py \ mmdetection/configs/yolox/yolox_s_8xb8-300e_coco.py \ --mode offline \ --om_model models/yolox_s_bs1.om ``` 2. 评估PyTorch模型的精度 ```bash python tools/evaluate.py \ mmdetection/configs/yolox/yolox_s_8xb8-300e_coco.py \ --mode online \ --checkpoint models/yolox_s_8x8_300e_coco_20211121_095711-4592a793.pth \ --batch_size 1 ``` 3. 评估PyTorch模型的精度,使用离线数据 ```bash python tools/evaluate.py \ mmdetection/configs/yolox/yolox_s_8xb8-300e_coco.py \ --mode online \ --checkpoint models/yolox_s_8x8_300e_coco_20211121_095711-4592a793.pth \ --batch_size 1 \ --use_offline_data \ --input_shape 640 640 ``` ## 开源许可证 该项目采用 [Apache 2.0](LICENSE) 开源许可证。