Skip to content

Latest commit

 

History

History
175 lines (125 loc) · 9.55 KB

File metadata and controls

175 lines (125 loc) · 9.55 KB

MMDetection Deployment

MMDetection aka mmdet is an open source object detection toolbox based on PyTorch. It is a part of the OpenMMLab project.

Installation

Install mmdet

If you have already done that, move on to the next section. Otherwise, please follow the guide to finish mmdet installation.

Install mmdeploy

There are several methods to install mmdeploy, among which you can choose an appropriate one according to your target platform and device.

Method I: Install precompiled package

TODO. MMDeploy hasn't released based on dev-1.x branch.

Method II: Build using scripts

If your target platform is Ubuntu 18.04 or later version, we encourage you to run scripts. For example, the following commands help install mmdeploy as well as inference engine - ONNX Runtime automatically.

git clone --recursive -b dev-1.x https://github.com/open-mmlab/mmdeploy.git
cd mmdeploy
python3 tools/scripts/build_ubuntu_x64_ort.py $(nproc)
export PYTHONPATH=$(pwd)/build/lib:$PYTHONPATH
export LD_LIBRARY_PATH=$(pwd)/../mmdeploy-dep/onnxruntime-linux-x64-1.8.1/lib/:$LD_LIBRARY_PATH

Method III: Build from source

If neither I nor II meets your requirements, building mmdeploy from source is the last option.

Convert model

You can use tools/deploy.py to convert mmdet models to the specified backend models. Its detailed usage can be learned from here.

The following shows an example about converting faster r-cnn model to onnx model that can be inferred by ONNX Runtime.

cd mmdeploy
# download faster r-cnn model from mmdet model zoo
mim download mmdet --config faster-rcnn_r50_fpn_1x_coco --dest .
# convert mmdet model to onnxruntime model with dynamic shape
python tools/deploy.py \
    configs/mmdet/detection/detection_onnxruntime_dynamic.py \
    faster-rcnn_r50_fpn_1x_coco.py \
    faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth \
    tests/data/tiger.jpeg \
    --work-dir mmdeploy_models/mmdet/ort \
    --device cpu \
    --show \
    --dump-info

It is crucial to specify the correct deployment config during model conversion. We've already provided builtin deployment config files of all supported backends for mmdetection, under which the config file path follows the pattern:

{task}/{task}_{backend}-{precision}_{static | dynamic}_{shape}.py
  • {task}: task in mmdetection.

    There are two of them. One is detection and the other is instance-seg, indicating instance segmentation.

    mmdet models like RetinaNet, Faster R-CNN and DETR and so on belongs to detection task. While Mask R-CNN is one of instance-seg models. You can find more of them in chapter Supported models.

    DO remember to use detection/detection_*.py deployment config file when trying to converting detection models and use instance-seg/instance-seg_*.py to deploy instance segmentation models.

  • {backend}: inference backend, such as onnxruntime, tensorrt, pplnn, ncnn, openvino, coreml and etc.

  • {precision}: fp16, int8. When it's empty, it means fp32

  • {static | dynamic}: static shape or dynamic shape

  • {shape}: input shape or shape range of a model

Therefore, in the above example, you can also convert faster r-cnn to other backend models by changing the deployment config file detection_onnxruntime_dynamic.py to others, e.g., converting to tensorrt-fp16 model by detection_tensorrt-fp16_dynamic-320x320-1344x1344.py.

When converting mmdet models to tensorrt models, --device should be set to "cuda"

Model Specification

Before moving on to model inference chapter, let us talk more about the converted model structure which is very important to do model inference.

The converted model locates in the working directory like mmdeploy_models/mmdet/ort in the previous example. It includes:

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

in which,

  • end2end.onnx: backend model which can be inferred by ONNX Runtime
  • deploy.json: meta information about backend model
  • pipeline.json: inference pipeline of mmdeploy SDK
  • detail.json: conversion parameters

And the whole package mmdeploy_models/mmdet/ort is defined as mmdeploy SDK model. In other words, mmdeploy SDK model includes not only backend model but also inference meta information.

Backend model inference

mmdeploy provides a unified API named as inference_model to do this job, making all inference backends API transparent to users.

Take the previous converted end2end.onnx model as an example,

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

deploy_cfg = 'configs/mmdet/detection/detection_onnxruntime_dynamic.py'
model_cfg = './faster-rcnn_r50_fpn_1x_coco.py'
device = 'cpu'
backend_model = ['./mmdeploy_models/mmdet/ort/end2end.onnx']
image = './tests/data/tiger.jpeg'

# 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 model inference

TODO

Supported models

Model Task OnnxRuntime TensorRT ncnn PPLNN OpenVINO
ATSS ObjectDetection Y Y N N Y
FCOS ObjectDetection Y Y Y N Y
FoveaBox ObjectDetection Y N N N Y
FSAF ObjectDetection Y Y Y Y Y
RetinaNet ObjectDetection Y Y Y Y Y
SSD ObjectDetection Y Y Y N Y
VFNet ObjectDetection N N N N Y
YOLOv3 ObjectDetection Y Y Y N Y
YOLOX ObjectDetection Y Y Y N Y
Cascade R-CNN ObjectDetection Y Y N Y Y
Faster R-CNN ObjectDetection Y Y Y Y Y
Faster R-CNN + DCN ObjectDetection Y Y Y Y Y
GFL ObjectDetection Y Y N ? Y
RepPoints ObjectDetection N Y N ? Y
DETR ObjectDetection Y Y N ? Y
Cascade Mask R-CNN InstanceSegmentation Y N N N Y
Mask R-CNN InstanceSegmentation Y Y N N Y
Swin Transformer InstanceSegmentation Y Y N N N