DETR系列:RT-DETR实战部署

上篇文章介绍RT-detr的论文内容(RT-DETR 论文解析),本篇文章介绍算法复现、tensorRT加速、python代码部署tensorRT测试单张图片或文件夹。

1.复现模型详情

本次复现主要测试下表中RT-DETR-R50和RT-DETR-L(hgnetv2)
请添加图片描述

2.环境准备

我安装的是cuda 10.2 cudnn 8.6.0,TensorRT 8.5.1(Paddle官网要求确保TensorRT的版本>=8.5.1),PaddlePaddle >= 2.4.1(rtdetr要求)

1)CUDA、CUDNN安装
TensorRT>=8.5.1要求cudnn版本大于8.6.0,因此,cudnn需要安装8.6.0或以上版本,参考:https://blog.csdn.net/weixin_60864335/article/details/126671341

2)PaddleDetection配置

  • 创建paddle虚拟环境
    conda create -n paddle python=3.7.0
    

注意,这里建议直接指定版本3.7.0,因为后面安装tensorrt时候版本高可能安装识别,我一开始是3.7.15版本,安装tensorrt python时报错:tensorrt-8.5.1.7-cp37-none-linux_x86_64.whl is not a supported wheel on this platform,因此最好一开始就配置成低版本python。
完成之后输入下面命令进入环境

conda activate paddle 
  • 安装paddle
    python -m pip install paddlepaddle-gpu==2.4.2 -i https://pypi.tuna.tsinghua.edu.cn/simple
    //检查是否安装成功
    (paddle) ubuntu:~$ python
    >>> import paddle
    >>> paddle.utils.run_check()
    

输出下面语句表示paddle安装成功
PaddlePaddle is installed successfully! Let's start deep learning with PaddlePaddle now.

  • 安装PaddleDetection
# 克隆PaddleDetection仓库
cd <path/to/clone/PaddleDetection>
git clone https://github.com/PaddlePaddle/PaddleDetection.git

# 安装其他依赖
cd PaddleDetection
pip install -r requirements.txt

# 编译安装paddledet
python setup.py install

安装后确认测试通过:

python ppdet/modeling/tests/test_architectures.py

测试通过后会提示如下信息:

Ran 7 tests in 12.816s
OK

3)TensorRT安装
参考:https://blog.csdn.net/qq_41596730/article/details/128169273

安装之后tensorRT测试:

(paddle) ubuntu:~$ python
>>> import tensorrt
>>> 

正常即表示安装成功,若报如下错误
Error Code 2: Internal Error (Assertion cublasStatus == CUBLAS_STATUS_SUCCES
原因是CUDA的问题,到官网上,除了下载安装CUDA的包、还需要把Patch1、Patch2都下载安装上。
若报如下错误:

ImportError: libnvinfer.so.7: cannot open shared object file: No such file or directory

解决方法:将TensorRT中的链接文件.so文件进行复制到/usr/lib/文件夹中

4)环境测试

cd /home/PaddleDetection-develop
conda activate paddle 
python tools/infer.py -c configs/rtdetr/rtdetr_r50vd_6x_coco.yml \
              -o weights=https://bj.bcebos.com/v1/paddledet/models/rtdetr_r50vd_6x_coco.pdparams \
              --infer_img=./demo/000000570688.jpg

3.训练

训练命令:

python tools/train.py -c configs/rtdetr/rtdetr_hgnetv2_l_6x_coco.yml --eval -o pretrain_weights=models/rtdetr_hgnetv2_l_6x_coco.pdparams

中断后继续训练:

python tools/train.py -c configs/rtdetr/rtdetr_hgnetv2_l_6x_coco.yml --eval -r output/10.pdparams

评估:

python tools/infer.py -c configs/rtdetr/rtdetr_hgnetv2_l_6x_coco.yml -o weights=models/rtdetr_hgnetv2_l_6x_coco.pdparams --infer_img=./demo/000000570688.jpg

4.部署

1. 导出模型
cd /home/PaddleDetection-develop
python tools/export_model.py -c configs/rtdetr/rtdetr_r50vd_6x_coco.yml \
              -o weights=https://bj.bcebos.com/v1/paddledet/models/rtdetr_r50vd_6x_coco.pdparams trt=True \
              --output_dir=output_inference
2. 转换模型至ONNX
pip install onnx==1.13.0
pip install paddle2onnx==1.0.5
  • 转换模型:
paddle2onnx --model_dir=./output_inference/rtdetr_r50vd_6x_coco/ \
            --model_filename model.pdmodel  \
            --params_filename model.pdiparams \
            --opset_version 16 \
            --save_file rtdetr_r50vd_6x_coco.onnx
3. 转换成TensorRT:
# 保证TensorRT的版本>=8.5.1
trtexec --onnx=./rtdetr_r50vd_6x_coco.onnx \
        --workspace=4096 \
        --shapes=image:1x3x640x640 \
        --saveEngine=rtdetr_r50vd_6x_coco.trt \
        --avgRuns=100 \
        --fp16

5.python代码

点击这里下载代码

6.测试

测试下面2个模型转换前和转换后的耗时,可以看出,转换成trt后速度提升十分明显,能够满足实时性需求。
在这里插入图片描述

  • 1
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CV温故知新

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值