本文最后更新于:2024年5月11日 下午
mmdetection 是商汤科技(2018 COCO 目标检测挑战赛冠军)和香港中文大学开源的基于Pytorch实现的深度学习目标检测工具箱,性能强大,运算效率高,配置化编程,比较容易训练、测试。但pytorch模型不易于部署,运算速度还有进一步提升的空间,当前比较有效的方法是将模型转换为行为相同的tensorrt模型,本文记录转换流程。
任务思路
转换mmdetection 的 pytorch模型到tensorrt模型有多种方法,本文使用 mmdetection-to-tensorrt 库作为核心,完成直接的模型转换。
该库跳过了通常的 pth -> onnx -> tensorrt 的转换步骤,直接从pth转成tensorrt模型,并且已经成功支持了很多mmdetection 的模型转换。
Support Model/Module
- 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
完成步骤
- 配置环境
- 安装tensorrt 7.2.3.4
- 安装 mmdetection-to-tensorrt 库并安装依赖
- 使用 mmdetection-to-tensorrt 转换模型
- 结果测试
配置环境
本机 gpu Nvidia GTX 1080 服务器
时间 2021.03
-
操作系统 Ubuntu 16.04
-
Nvidia 显卡驱动 460.39
-
Cuda 版本 11.1
-
Cudnn 版本 8.1.1
具体配置方法教程很多,在此不再赘述,需要根据个人具体情况配置
安装 tensorrt并配置环境
选择的版本是 tensorrt 7.2.3.4
建议Python环境安装 Anaconda
安装 PyCuda
1 |
|
安装tensorrt
-
下载tensorrt
-
选择 TensorRT-7.2.3.4.Ubuntu-16.04.x86_64-gnu.cuda-11.1.cudnn8.1.tar.gz
-
解压
1 |
|
解压后文件夹内文件:
1
2
# ls
TensorRT-Release-Notes.pdf bin data doc graphsurgeon include lib onnx_graphsurgeon python samples targets uff
- 安装tensorrt
根据自己的 Python 版本选择合适的包进行安装
1 |
|
- 安装graphsurgeon wheel
1 |
|
- 配置环境变量
1 |
|
- 测试
1 |
|
安装 mmdetection
- 链接 https://github.com/open-mmlab/mmdetection
- 安装文档 https://github.com/open-mmlab/mmdetection/blob/master/docs/get_started.md
安装 mmcv
1 |
|
下载并安装 mmdetection
- 下载
1 |
|
- 配置环境并安装
1 |
|
安装 mmdetection-to-tensorrt
安装 torch2trt_dynamic
1 |
|
安装 amirstan_plugin
1 |
|
- 更新子模块
1 |
|
讲道理一句话就可以了,不过我在执行这句命令时报错,如果没报错继续下面的步骤
子模块更新报错解决方案
http协议不好用,需要改成git
- 修改
amirstan_plugin/.gitmodules
文件
将第三行地址改为
git@github.com:NVIDIA/cub.git
1 |
|
- 修改
amirstan_plugin/.git/modules/third_party/cub/config
将
remote "origin"
地址改为git@github.com:NVIDIA/cub.git
1 |
|
- 再次执行
1 |
|
- 新建build文件夹
1 |
|
- 生成 makefile
1 |
|
若输出:
1 |
|
则说明 makefile 生成成功,保存在
build
文件夹下
- 编译
1 |
|
此时在build/lib
文件夹下生成了很多文件
1 |
|
- 配置环境变量
1 |
|
安装 mmdetection-to-tensorrt
进入
mmdetection-to-tensorrt
根目录
1 |
|
- 测试是否成功
1 |
|
测试
-
在
mmdetection-to-tensorrt
项目中,运行demo
文件夹下的inference.py
文件 -
修改
inference.py
文件中的 parser 参数 :- img:测试图像路径
- config:mmdetection 的模型配置文件
- checkpoint:模型 pth 文件路径
- save_path:tensorrt 模型存放路径
- score-thr:检测有效阈值
-
配置好后运行该文件即可以生成模型在测试图像上的检测结果
参考资料
“觉得不错的话,给点打赏吧 ୧(๑•̀⌄•́๑)૭”

微信支付

支付宝支付