MMDeploy 使用指南

MMDeploy 使用指南

参考
MMlab 官方文档
MMDeploy 部署实战

一. 环境配置

1. 创建mmdeploy的conda环境

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. 安装MMCV

pip install -U openmim
mim install mmcv-full

3. 安装MMDeploy算子库和推理SDK
下面安装步骤中的export导入的环境变量可以写到bashrc中,或者写一个本地脚本,每次进入conda虚拟环境后,执行一次,后面有提到。

# 安装 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. 安装和编译MMDeploy工程
注意下面的第二步

## 克隆 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. 安装mmdetection

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

二. 模型转换实例

参考官方文档即可。

# 执行转换命令,实现端到端的转换,以下是我的实例
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

三. 问题排疑

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. 官方文档没有提到,请自行安装

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 或者 TRTBatchedNMS Plugin not found
A3. 没有安装自定义算子库,我这里选择trt后端,参考:编译 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. 运行模型转换脚本提示找不到 No module named ‘mmdeploy’。
A4. 将mmdeploy加入python搜索路径,可以将以下内容放入~/.bashrc,或者令写一个脚本文件,每次进入openmmlab虚拟环境后,执行一次。

#!/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

你可能感兴趣的:(公开教程,1024程序员节,深度学习,python)