本文提供深度学习CV领域模型加速部署案例,仓库实现的cuda c支持多batch图像预处理、推理、decode、NMS。大部分模型转换流程为:torch->onnx->tensorrt。下图左边是yolov7-python源码推理结果,右边是本文基于tensorrt实现yolov7模型部署的推理结果。
有源码!有源码!有源码! 不要慌,哈哈哈。
如果您对tensorrt不是很熟悉,请务必保持下面库版本一致。
请注意: Linux系统安装以下库,务必去进入系统bios下,关闭安全启动(设置 secure boot 为 disable)
sudo apt-get update
sudo apt-get install build-essential
sudo apt-get install git
sudo apt-get install gdb
sudo apt-get install cmake
sudo apt-get install libopencv-dev
# pkg-config --modversion opencv
注:Nvidia相关网站需要注册账号。
ubuntu-drivers devices
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt update
sudo apt install nvidia-driver-470-server # for ubuntu18.04
nvidia-smi
wget https://developer.download.nvidia.com/compute/cuda/11.3.0/local_installers/cuda_11.3.0_465.19.01_linux.run
sudo sh cuda_11.3.0_465.19.01_linux.run
cuda的安装过程中,需要你在bash窗口手动作一些选择,这里选择如下:
#===========
#= Summary =
#===========
#Driver: Not Selected
#Toolkit: Installed in /usr/local/cuda-11.3/
#......
把cuda添加到环境变量:
vim ~/.bashrc
把下面拷贝到 .bashrc里面
# cuda v11.3
export PATH=/usr/local/cuda-11.3/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-11.3/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
export CUDA_HOME=/usr/local/cuda-11.3
刷新环境变量和验证
source ~/.bashrc
nvcc -V
bash窗口打印如下信息表示cuda11.3安装正常
nvcc: NVIDIA (R) Cuda compiler driver<br>
Copyright (c) 2005-2021 NVIDIA Corporation<br>
Built on Sun_Mar_21_19:15:46_PDT_2021<br>
Cuda compilation tools, release 11.3, V11.3.58<br>
Build cuda_11.3.r11.3/compiler.29745058_0<br>
# 解压
tar -zxvf cudnn-11.3-linux-x64-v8.2.0.53.tgz
将cudnn的头文件和lib拷贝到cuda11.3的安装目录下:
sudo cp cuda/include/cudnn.h /usr/local/cuda/include/
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64/
sudo chmod a+r /usr/local/cuda/include/cudnn.h
sudo chmod a+r /usr/local/cuda/lib64/libcudnn*
本教程中,tensorrt只需要下载\、解压即可,不需要安装。
# 解压
tar -zxvf TensorRT-8.4.2.4.Linux.x86_64-gnu.cuda-11.6.cudnn8.4.tar.gz
# 快速验证一下tensorrt+cuda+cudnn是否安装正常
cd TensorRT-8.4.2.4/samples/sampleMNIST
make
cd ../../bin/
导出tensorrt环境变量(it is important!),注:将LD_LIBRARY_PATH:后面的路径换成你自己的!后续编译onnx模型的时候也需要执行下面第一行命令
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/xxx/temp/TensorRT-8.4.2.4/lib
./sample_mnist
bash窗口打印类似如下图的手写数字识别表明cuda+cudnn+tensorrt安装正常
git clone https://github.com/FeiYull/tensorrt-alpha
设置您自己TensorRT根目录:
git clone https://github.com/FeiYull/tensorrt-alpha
cd tensorrt-alpha/cmake
vim common.cmake
# 在文件common.cmake中的第20行中,设置成你自己的目录,别和我设置一样的路径eg:
# set(TensorRT_ROOT /root/TensorRT-8.4.2.4)
可以直接从网盘下载onnx文件[weiyun]:weiyun or google driver ,你也可以自己下载仓库,然后按照下面指令手动导出onnx文件:
下载yolov7源码
git clone https://github.com/WongKinYiu/yolov7
git checkout 072f76c72c641c7a1ee482e39f604f6f8ef7ee92
安装 yolov7环境
pip install -r requirements.txt
用以下指令导出onnx模型文件
# 640
python export.py --weights yolov7-tiny.pt --dynamic --grid
python export.py --weights yolov7.pt --dynamic --grid
python export.py --weights yolov7x.pt --dynamic --grid
# 1280
python export.py --weights yolov7-w6.pt --dynamic --grid --img-size 1280
将你的onnx模型放到这个路径:tensorrt-alpha/data/yolov7
cd tensorrt-alpha/data/yolov7
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/TensorRT-8.4.2.4/lib
编译onnx模型,会得到例如xxxx.trt格式的文件,下文推理要用到。
# 640
../../../../TensorRT-8.4.2.4/bin/trtexec --onnx=yolov7-tiny.onnx --saveEngine=yolov7-tiny.trt --buildOnly --minShapes=images:1x3x640x640 --optShapes=images:4x3x640x640 --maxShapes=images:8x3x640x640
../../../../TensorRT-8.4.2.4/bin/trtexec --onnx=yolov7.onnx --saveEngine=yolov7.trt --buildOnly --minShapes=images:1x3x640x640 --optShapes=images:4x3x640x640 --maxShapes=images:8x3x640x640
../../../../TensorRT-8.4.2.4/bin/trtexec --onnx=yolov7x.onnx --saveEngine=yolov7x.trt --buildOnly --minShapes=images:1x3x640x640 --optShapes=images:4x3x640x640 --maxShapes=images:8x3x640x640
# 1280
../../../../TensorRT-8.4.2.4/bin/trtexec --onnx=yolov7-w6.onnx --saveEngine=yolov7-w6.trt --buildOnly --minShapes=images:1x3x1280x1280 --optShapes=images:4x3x1280x1280 --maxShapes=images:8x3x1280x1280
注:如果报错(Error Code 1: Cuda Runtime (an illegal memory access was encountered “bool context = m_context->executeV2((void**)bindings)” returns false)
可能是爆显存了,降低batch-size就行了
下载源码并编译
git clone https://github.com/FeiYull/tensorrt-alpha
cd tensorrt-alpha/yolov7
mkdir build
cd build
cmake ..
make -j10
# note: the dstImage will be saved in tensorrt-alpha/yolov7/build by default
执行推理任务,支持图片、视频、摄像头
## 640
# infer image
./app_yolov7 --model=../../data/yolov7/yolov7-tiny.trt --size=640 --batch_size=1 --img=../../data/6406401.jpg --show --savePath
./app_yolov7 --model=../../data/yolov7/yolov7-w6.trt --size=1280 --batch_size=1 --img=../../data/6406401.jpg --show --savePath=../
# infer video
./app_yolov7 --model=../../data/yolov7/yolov7-tiny.trt --size=640 --batch_size=8 --video=../../data/people.mp4 --show
# infer camera
./app_yolov7 --model=../../data/yolov7/yolov7-tiny.trt --size=640 --batch_size=4 --cam_id=0 --show
都看到这里了,觉得可以请点赞收藏,有条件的去仓库点个star,仓库:https://github.com/FeiYull/tensorrt-alpha