快捷键

模型部署

OpenMMLab 代码库的部署,包括 MMDetection、MMPretrain 等,由 MMDeploy 支持。MMDetection 的最新部署指南可以从 这里 找到。

本教程的组织结构如下

安装

请按照 指南 安装 mmdet。然后,按照 指南从源代码安装 mmdeploy。

注意

如果您安装了 mmdeploy 预构建包,请使用“git clone https://github.com/open-mmlab/mmdeploy.git –depth=1” 克隆其存储库以获取部署配置文件。

转换模型

假设 mmdetection 和 mmdeploy 存储库位于同一目录中,工作目录是 mmdetection 的根路径。

Faster R-CNN 模型为例。您可以从 这里 下载其检查点,然后将其转换为 ONNX 模型,如下所示

from mmdeploy.apis import torch2onnx
from mmdeploy.backend.sdk.export_info import export2SDK

img = 'demo/demo.jpg'
work_dir = 'mmdeploy_models/mmdet/onnx'
save_file = 'end2end.onnx'
deploy_cfg = '../mmdeploy/configs/mmdet/detection/detection_onnxruntime_dynamic.py'
model_cfg = 'configs/faster_rcnn/faster-rcnn_r50_fpn_1x_coco.py'
model_checkpoint = 'faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'
device = 'cpu'

# 1. convert model to onnx
torch2onnx(img, work_dir, save_file, deploy_cfg, model_cfg,
           model_checkpoint, device)

# 2. extract pipeline info for inference by MMDeploy SDK
export2SDK(deploy_cfg, model_cfg, work_dir, pth=model_checkpoint,
           device=device)

在模型转换期间指定正确的部署配置至关重要。MMDeploy 已经提供了所有支持 MMDetection 的后端模型的内置部署配置 文件,其配置文件路径遵循模式

{task}/{task}_{backend}-{precision}_{static | dynamic}_{shape}.py
  • {task}: MMDetection 中的任务。

    有两个任务。一个是 detection,另一个是 instance-seg,表示实例分割。

    MMDet 模型,如 RetinaNetFaster R-CNNDETR 等,属于 detection 任务。而 Mask R-CNNinstance-seg 模型之一。

    请记住使用 detection/detection_*.py 部署配置文件来转换检测模型,并使用 instance-seg/instance-seg_*.py 来部署实例分割模型。

  • {backend}: 推理后端,例如 onnxruntime、tensorrt、ppllnn、ncnn、openvino、coreml 等。

  • {precision}: fp16、int8。当为空时,表示 fp32。

  • {static | dynamic}: 静态形状或动态形状。

  • {shape}: 模型的输入形状或形状范围。

因此,在上面的例子中,您也可以使用 detection_tensorrt-fp16_dynamic-320x320-1344x1344.pyFaster R-CNN 转换为 tensorrt-fp16 模型。

提示

将 MMDet 模型转换为 TensorRT 模型时,–device 应设置为“cuda”。

模型规范

在继续模型推理章节之前,让我们进一步了解转换后的模型结构,这对于模型推理非常重要。

转换后的模型位于工作目录中,如之前的例子中的 mmdeploy_models/mmdet/onnx。它包含

mmdeploy_models/mmdet/onnx
├── deploy.json
├── detail.json
├── end2end.onnx
└── pipeline.json

其中,

  • end2end.onnx: 可以由 ONNX 运行时推断的后端模型。

  • xxx.json: MMDeploy SDK 所需的信息。

整个包 mmdeploy_models/mmdet/onnx 被定义为 MMDeploy SDK 模型,即 MMDeploy SDK 模型 包含后端模型和推理元数据信息。

模型推理

后端模型推理

以之前转换的 end2end.onnx 模型为例,您可以使用以下代码来推理模型并可视化结果。

from mmdeploy.apis.utils import build_task_processor
from mmdeploy.utils import get_input_shape, load_config
import torch

deploy_cfg = '../mmdeploy/configs/mmdet/detection/detection_onnxruntime_dynamic.py'
model_cfg = 'configs/faster_rcnn/faster-rcnn_r50_fpn_1x_coco.py'
device = 'cpu'
backend_model = ['mmdeploy_models/mmdet/onnx/end2end.onnx']
image = 'demo/demo.jpg'

# read deploy_cfg and model_cfg
deploy_cfg, model_cfg = load_config(deploy_cfg, model_cfg)

# build task and backend model
task_processor = build_task_processor(model_cfg, deploy_cfg, device)
model = task_processor.build_backend_model(backend_model)

# process input image
input_shape = get_input_shape(deploy_cfg)
model_inputs, _ = task_processor.create_input(image, input_shape)

# do model inference
with torch.no_grad():
    result = model.test_step(model_inputs)

# visualize results
task_processor.visualize(
    image=image,
    model=model,
    result=result[0],
    window_name='visualize',
    output_file='output_detection.png')

SDK 模型推理

您也可以执行以下 SDK 模型推理,

from mmdeploy_python import Detector
import cv2

img = cv2.imread('demo/demo.jpg')

# create a detector
detector = Detector(model_path='mmdeploy_models/mmdet/onnx',
                    device_name='cpu', device_id=0)
# perform inference
bboxes, labels, masks = detector(img)

# visualize inference result
indices = [i for i in range(len(bboxes))]
for index, bbox, label_id in zip(indices, bboxes, labels):
    [left, top, right, bottom], score = bbox[0:4].astype(int), bbox[4]
    if score < 0.3:
        continue

    cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 0))

cv2.imwrite('output_detection.png', img)

除了 Python API 外,MMDeploy SDK 还提供了其他 FFI(外部函数接口),例如 C、C++、C#、Java 等。您可以在 演示 中了解其用法。

支持的模型

请参考 这里 以获取支持的模型列表。