项目作者: grimoire

项目描述 :
convert mmdetection model to tensorrt, support fp16, int8, batch input, dynamic shape etc.
高级语言: Python
项目地址: git://github.com/grimoire/mmdetection-to-tensorrt.git
创建时间: 2020-07-12T10:50:30Z
项目社区:https://github.com/grimoire/mmdetection-to-tensorrt

开源协议:Apache License 2.0

下载


MMDet to TensorRT

[!NOTE]

The main branch is used to support model conversion of MMDetection>=3.0.
If you want to convert model on older MMDetection, Please switch to branch:

News

  • 2024.02: Support MMDetection>=3.0

Introduction

This project aims to support End2End deployment of models in MMDetection with TensorRT.

Mask support is experiment.

Features:

  • fp16
  • int8(experiment)
  • batched input
  • dynamic input shape
  • combination of different modules
  • DeepStream

Requirement

  • install MMDetection:

    1. pip install openmim
    2. mim install mmdet==3.3.0
  • install torch2trt_dynamic:

    1. git clone https://github.com/grimoire/torch2trt_dynamic.git torch2trt_dynamic
    2. cd torch2trt_dynamic
    3. pip install -e .
  • install amirstan_plugin:

    • Install tensorrt: TensorRT
    • clone repo and build plugin

      1. git clone --depth=1 https://github.com/grimoire/amirstan_plugin.git
      2. cd amirstan_plugin
      3. git submodule update --init --progress --depth=1
      4. mkdir build
      5. cd build
      6. cmake -DTENSORRT_DIR=${TENSORRT_DIR} ..
      7. make -j10

      [!NOTE]

      DON’T FORGET setting the environment variable(in ~/.bashrc):

      1. export AMIRSTAN_LIBRARY_PATH=${amirstan_plugin_root}/build/lib

Installation

Host

  1. git clone https://github.com/grimoire/mmdetection-to-tensorrt.git
  2. cd mmdetection-to-tensorrt
  3. pip install -e .

Docker

Build docker image

  1. sudo docker build -t mmdet2trt_docker:v1.0 docker/

Run (will show the help for the CLI entrypoint)

  1. sudo docker run --gpus all -it --rm -v ${your_data_path}:${bind_path} mmdet2trt_docker:v1.0

Or if you want to open a terminal inside de container:

  1. sudo docker run --gpus all -it --rm -v ${your_data_path}:${bind_path} --entrypoint bash mmdet2trt_docker:v1.0

Example conversion:

  1. sudo docker run --gpus all -it --rm -v ${your_data_path}:${bind_path} mmdet2trt_docker:v1.0 ${bind_path}/config.py ${bind_path}/checkpoint.pth ${bind_path}/output.trt

Usage

Create a TensorRT model from mmdet model.
detail can be found in getting_started.md

CLI

  1. # conversion might take few minutes.
  2. mmdet2trt ${CONFIG_PATH} ${CHECKPOINT_PATH} ${OUTPUT_PATH}

Run mmdet2trt -h for help on optional arguments.

Python

  1. shape_ranges=dict(
  2. x=dict(
  3. min=[1,3,320,320],
  4. opt=[1,3,800,1344],
  5. max=[1,3,1344,1344],
  6. )
  7. )
  8. trt_model = mmdet2trt(cfg_path,
  9. weight_path,
  10. shape_ranges=shape_ranges,
  11. fp16_mode=True)
  12. # save converted model
  13. torch.save(trt_model.state_dict(), save_model_path)
  14. # save engine if you want to use it in c++ api
  15. with open(save_engine_path, mode='wb') as f:
  16. f.write(trt_model.state_dict()['engine'])

[!NOTE]

The input of the engine is the tensor after preprocess.
The output of the engine is num_dets, bboxes, scores, class_ids. if you enable the enable_mask flag, there will be another output mask.
The bboxes output of the engine did not divided by scale_factor.

how to perform inference with the converted model.

  1. from mmdet.apis import inference_detector
  2. from mmdet2trt.apis import create_wrap_detector
  3. # create wrap detector
  4. trt_detector = create_wrap_detector(trt_model, cfg_path, device_id)
  5. # result share same format as mmdetection
  6. result = inference_detector(trt_detector, image_path)

Try demo in demo/inference.py, or demo/cpp if you want to do inference with c++ api.

Read getting_started.md for more details.

How does it works?

Most other project use pytorch=>ONNX=>tensorRT route, This repo convert pytorch=>tensorRT directly, avoid unnecessary ONNX IR.
Read how-does-it-work for detail.

Support Model/Module

[!NOTE]

Some models have only been tested on MMDet<3.0. If you found any failed model,
Please report in the issue.

  • Faster R-CNN
  • Cascade R-CNN
  • Double-Head R-CNN
  • Group Normalization
  • Weight Standardization
  • DCN
  • SSD
  • RetinaNet
  • Libra R-CNN
  • FCOS
  • Fovea
  • CARAFE
  • FreeAnchor
  • RepPoints
  • NAS-FPN
  • ATSS
  • PAFPN
  • FSAF
  • GCNet
  • Guided Anchoring
  • Generalized Attention
  • Dynamic R-CNN
  • Hybrid Task Cascade
  • DetectoRS
  • Side-Aware Boundary Localization
  • YOLOv3
  • PAA
  • CornerNet(WIP)
  • Generalized Focal Loss
  • Grid RCNN
  • VFNet
  • GROIE
  • Mask R-CNN(experiment)
  • Cascade Mask R-CNN(experiment)
  • Cascade RPN
  • DETR
  • YOLOX

Tested on:

  • torch=2.2.0
  • tensorrt=8.6.1
  • mmdetection=3.3.0
  • cuda=11.7

FAQ

read this page if you meet any problem.

License

This project is released under the Apache 2.0 license.