【实战】基于TensorRT 加速YOLO系列以及其他加速算法实战与对比

今天cv调包侠尝试了使用TensorRT 做YOLO的加速,先概述我这边实现的速度和精度对比:

精度上对比:

可以看到,精度上使用TensorRT 精度不掉,反而略微上升了一些些(具体情况未知,还在摸索)

TensorRT 速度上的对比:

另外值得注意的是,我使用的TensorRT的作者介绍说:YOLOV5 s小模型原本已经很快了,使用python版的tensorRT加速反而慢了一些,使用cpp版快了3倍,如果是使用YOLOV5 X的大模型,加速效果会更明显。

下面开始手把手教学,先大致说说思路:

1:配置cuda cudnn 和TensorRT ,先配置适合你的电脑的cuda cudnn,以及到Nvidia官网下载适合你cuda 和cudnn呃tensorRT版本,

2:再分别下载tensorRT 源码;YOlov5源码,以及YOLOv5模型

3:编译tensorRT-v5生成engine

4:测试模型

官方的介绍是 Ubuntu16.04 / cuda10.0 / cudnn7.6.5 / tensorrt7.0.0 / opencv3.3 的配置:

1. Install CUDA 安装cuda,cudnn

Go to cuda-10.0-download. Choose Linux -> x86_64 -> Ubuntu -> 16.04 -> deb(local) and download the .deb package.

Then follow the installation instructions.

sudo dpkg -i cuda-repo-ubuntu1604-10-0-local-10.0.130-410.48_1.0-1_amd64.deb
sudo apt-key add /var/cuda-repo-<version>/7fa2af80.pub
sudo apt-get update
sudo apt-get install cuda

2. Install TensorRT 安装tensorRT

Go to nvidia-tensorrt-7x-download. You might need login.

Choose TensorRT 7.0 and TensorRT 7.0.0.11 for Ubuntu 1604 and CUDA 10.0 DEB local repo packages

Install with following commands, after apt install tensorrt, it will automatically install cudnn, nvinfer, nvinfer-plugin, etc.

sudo dpkg -i nv-tensorrt-repo-ubuntu1604-cuda10.0-trt7.0.0.11-ga-20191216_1-1_amd64.deb
sudo apt update
sudo apt install tensorrt

3. Install OpenCV 安装opencv

sudo add-apt-repository ppa:timsc/opencv-3.3
sudo apt-get update
sudo apt install libopencv-dev

4. Check your installation

dpkg -l | grep cuda
dpkg -l | grep nvinfer
dpkg -l | grep opencv

5. Run tensorrtx

It is recommanded to go through the getting started guide, lenet5 as a demo. first.

But if you are proficient in tensorrt, please check the readme of the model you want directly.

我的配置是cuda 10.1 cudnn 7 ,用同样的方式配置好自己的版本,下载了tensorRT 6.0.1 的deb包,并且安装了。

然后将TensorRT/yolov5/CMakeLists.txt 修改一下刚刚安装的TensorRT的include和lib,如图:

1. generate yolov5s.wts from pytorch with yolov5s.pt, or download .wts from model zoo

git clone https://github.com/wang-xinyu/tensorrtx.git
git clone https://github.com/ultralytics/yolov5.git
// download its weights 'yolov5s.pt'
// copy tensorrtx/yolov5/gen_wts.py into ultralytics/yolov5
// ensure the file name is yolov5s.pt and yolov5s.wts in gen_wts.py
// go to ultralytics/yolov5
python gen_wts.py
// a file 'yolov5s.wts' will be generated.

【实战】基于TensorRT 加速YOLO系列以及其他加速算法实战与对比_第1张图片

接下来可以编译生成yolov5s.engine了:

2. build tensorrtx/yolov5 and run

// put yolov5s.wts into tensorrtx/yolov5
// go to tensorrtx/yolov5
// ensure the macro NET in yolov5.cpp is s
mkdir build
cd build
cmake ..
make
sudo ./yolov5 -s             // serialize model to plan file i.e. 'yolov5s.engine'
sudo ./yolov5 -d  ../samples // deserialize plan file and run inference, the images in samples will be processed.

3. check the images generated, as follows. _zidane.jpg and _bus.jpg

4. optional, load and run the tensorrt model in python

// install python-tensorrt, pycuda, etc.
// ensure the yolov5s.engine and libmyplugins.so have been built
python yolov5_trt.py

其中:sudo ./yolov5 -d …/samples命令中…/samples 就是你测试图片的文件夹路径,我们可以自己修改路径来推理。然后

就可以推理了。如果有什么报错的话,可以通过公众号咨询我~

另外tensroRT 默认使用的是固定尺寸(默认尺寸),而pytorch 版的使用的是等比例缩放到640X 640 所以如果要做消融对比的实验,建议同一个size做对比,我文章开头部分的对比图片,是将pytorch版的img-size改成了608 来推理,所以速度会快一些。对比下来实际上使用cpp版yolov5s 速度还是快了很多,大家可以尝试使用yolov5x 来加速尝试一下。如果你想改tensorRT 为640x640 ,那么可以修改yololayer.h 中的20行左右input_H,input_W = 608为640。但是我没有实际测试~

另外对于训练自己的数据集,使用tensorRT加速,需要修改一下一些地方:

将yolov5.cpp 中的第七行:改成 #define USE_FP16 改成自己的FP32 正常我们的模型训练出来是FP32的。

我建议还是使用yolov5_trt来做推理,main函数中修改一下类别,等等。

另外,使用Openvino加速

cpu(i5 )下原yolov5s 下640X 640是 380ms每帧,使用Openvino是300ms以下,gpu我没测试,应该也是稍微快一些,大家可以根据我另一篇文章测试一下~

另外,CV调包侠自己做了个目标检测中必备地一个工具~ 使用我的工具,可以方便快捷地做数据标注!哦不,你什么也不用干,工具来标注~ 可以让你一分钟就获取大量数据,并且获取xml标注文件,获取labels归一化标签,直接可以使用yolo训练,关注公众号,星标一下,过几天开源出来~
【实战】基于TensorRT 加速YOLO系列以及其他加速算法实战与对比_第2张图片

你可能感兴趣的:(视觉识别模块,深度学习,目标检测精读,深度学习,人工智能,机器学习,计算机视觉,AI)