MMDeploy User Guide

MMDeploy User Guide

Reference :
MMlab Official Document
MMDeploy Deployment Actual Combat

1. Environment configuration

1. Create the conda environment of mmdeploy

conda create --name mmdeploy python=3.8 -y
conda activate mmdeploy
# pytorch安装
conda install pytorch=={pytorch_version} torchvision=={torchvision_version} cudatoolkit={cudatoolkit_version} -c pytorch -c conda-forge    

2. Install MMCV

pip install -U openmim
mim install mmcv-full

3. Install the MMDeploy operator library and the inference SDK.
The environment variables imported by export in the following installation steps can be written to bashrc, or write a local script, and execute it once every time you enter the conda virtual environment, as mentioned later.

# 安装 MMDeploy ONNX Runtime 自定义算子库和推理 SDK
wget https://github.com/open-mmlab/mmdeploy/releases/download/v0.8.0/mmdeploy-0.8.0-linux-x86_64-onnxruntime1.8.1.tar.gz
tar -zxvf mmdeploy-0.8.0-linux-x86_64-onnxruntime1.8.1.tar.gz
cd mmdeploy-0.8.0-linux-x86_64-onnxruntime1.8.1
pip install dist/mmdeploy-0.8.0-py3-none-linux_x86_64.whl
pip install sdk/python/mmdeploy_python-0.8.0-cp38-none-linux_x86_64.whl
cd ..
# 安装推理引擎 ONNX Runtime
pip install onnxruntime==1.8.1
wget https://github.com/microsoft/onnxruntime/releases/download/v1.8.1/onnxruntime-linux-x64-1.8.1.tgz
tar -zxvf onnxruntime-linux-x64-1.8.1.tgz
export ONNXRUNTIME_DIR=$(pwd)/onnxruntime-linux-x64-1.8.1
export LD_LIBRARY_PATH=$ONNXRUNTIME_DIR/lib:$LD_LIBRARY_PATH
# 安装 MMDeploy TensorRT 自定义算子库和推理 SDK
wget https://github.com/open-mmlab/mmdeploy/releases/download/v0.8.0/mmdeploy-0.8.0-linux-x86_64-cuda11.1-tensorrt8.2.3.0.tar.gz
tar -zxvf mmdeploy-v0.8.0-linux-x86_64-cuda11.1-tensorrt8.2.3.0.tar.gz
cd mmdeploy-0.8.0-linux-x86_64-cuda11.1-tensorrt8.2.3.0
pip install dist/mmdeploy-0.8.0-py3-none-linux_x86_64.whl
pip install sdk/python/mmdeploy_python-0.8.0-cp38-none-linux_x86_64.whl
cd ..
# 安装推理引擎 TensorRT
# !!! 从 NVIDIA 官网下载 TensorRT-8.2.3.0 CUDA 11.x 安装包并解压到当前目录
pip install TensorRT-8.2.3.0/python/tensorrt-8.2.3.0-cp38-none-linux_x86_64.whl
pip install pycuda
export TENSORRT_DIR=$(pwd)/TensorRT-8.2.3.0
export LD_LIBRARY_PATH=${TENSORRT_DIR}/lib:$LD_LIBRARY_PATH
# !!! 从 NVIDIA 官网下载 cuDNN 8.2.1 CUDA 11.x 安装包并解压到当前目录
export CUDNN_DIR=$(pwd)/cuda
export LD_LIBRARY_PATH=$CUDNN_DIR/lib64:$LD_LIBRARY_PATH

4. Install and compile the MMDeploy project
注意下面的第二步

## 克隆 mmdeploy 仓库。转换时,需要使用 mmdeploy 仓库中的配置文件,建立转换流水线
git clone --recursive https://github.com/open-mmlab/mmdeploy.git
python -m pip install -r mmdeploy/requirements/runtime.txt  ## 注意,官方文档没有提到

## 编译Model Converter自定义算子库
cd mmdeploy
mkdir -p build && cd build
cmake -DCMAKE_CXX_COMPILER=g++ -DMMDEPLOY_TARGET_BACKENDS=trt -DONNXRUNTIME_DIR=${ONNXRUNTIME_DIR} ..  #服务器g++版本,9.4满足>7
make -j8 && make install

## 安装Model Converter
cd mmdeploy
pip install -e .

5. Install mmdetection

git clone https://github.com/open-mmlab/mmdetection.git
cd mmdetection
pip install -v -e .
cd ..

2. Model conversion example

Just refer to the official documentation.

# 执行转换命令,实现端到端的转换,以下是我的实例
python mmdeploy/tools/deploy.py mmdeploy/configs/mmdet/detection/detection_tensorrt_dynamic-320x320-1344x1344.py mmdetection/configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth mmdetection/demo/demo.jpg --work-dir checkpoints/mmdeploy_model/faster-rcnn --device cuda:1 --dump-info

3. Troubleshooting

Q1. ModuleNotFoundError: No module named ‘onnx’
A1.

pip install onnx  -i https://pypi.douban.com/simple/

Q2 . ImportError: cannot import name 'create_calib_input_data' from 'mmdeploy.apis'
A2 . The official document does not mention it, please install it yourself

cd mmdeploy
python -m pip install -r mmdeploy/requirements/runtime.txt

Q3 . TensorRT is not available, please install TensorRT and build TensorRT custom ops first or TRTBatchedNMS Plugin not found
A3 . The custom operator library is not installed. I choose the trt backend here. Reference: Compile Model Converter

# 先安装cmake>=3.14以上
wget https://github.com/Kitware/CMake/releases/download/v3.20.0/cmake-3.20.0-linux-x86_64.tar.gz
tar -xzvf cmake-3.20.0-linux-x86_64.tar.gz
sudo ln -sf $(pwd)/cmake-3.20.0-linux-x86_64/bin/* /usr/bin/
# 编译自定义算子库
cd mmdeploy
mkdir -p build && cd build
cmake -DCMAKE_CXX_COMPILER=g++ -DMMDEPLOY_TARGET_BACKENDS=trt -DONNXRUNTIME_DIR=${ONNXRUNTIME_DIR} ..  #服务器g++版本,9.4满足>7
make -j8 && make install

Q4 . When running the model conversion script, it prompts that No module named 'mmdeploy' cannot be found.
A4 . Add mmdeploy to the python search path, you can put the following content into ~/.bashrc, or write a script file, and execute it once every time you enter the openmmlab virtual environment.

#!/bin/bash
## onnxruntime
export ONNXRUNTIME_DIR=/workspace/Cuisc/onnxruntime-linux-x64-1.8.1
export LD_LIBRARY_PATH=$ONNXRUNTIME_DIR/lib:$LD_LIBRARY_PATH

## TensorRT
export TENSORRT_DIR=/workspace/Cuisc/TensorRT-8.2.4.2
export LD_LIBRARY_PATH=${TENSORRT_DIR}/lib:$LD_LIBRARY_PATH

## CuDNN
export CUDNN_DIR=/workspace/Cuisc/cuda
export LD_LIBRARY_PATH=$CUDNN_DIR/lib64:$LD_LIBRARY_PATH

## MMDeploy
export PYTHONPATH=/workspace/Cuisc/mmdeploy:$PYTHONPATH

Guess you like

Origin blog.csdn.net/weixin_36354875/article/details/127500332