目录
一、环境版本
1.1 概述
1.2 Ubuntu版本
1.3 python、pytorch与tensorrt版本
1.4 cudnn版本
1.5 驱动版本
1.6 opencv版本
二、TensorRT与opencv环境安装
2.1 opencv安装
2.2 TensorRT安装
三、yolov5转tensorrt
3.1 下载yolov5项目
3.2 下载tensorrtx项目
3.3 开始准备编译
3.4 获取wts模型
3.5 编译命令
3.6 测试
四、附录
系统环境:Ubuntu18.04 python=3.8.5 torch=1.10.0 TensorRT=7.2.3.4 opencv=4.5.1
cuda=11.2 cudnn=8.2.2 Driver Version: 460.32.03 显卡:RTX 3090
在终端运行: lsb_release -a
在终端运行: lsb_release -a
在终端运行: dpkg -L libcudnn8
在终端运行: watch -n 1 nvidia-smi
在终端运行: pkg-config --modversion opencv
2.1 opencv安装
- 可参考网:Home - OpenCV
- ippicv包:https://github.com/opencv/opencv_3rdparty/tree/ippicv/master_20191018/ippicv
- 编译命令:cmake -D CMAKE_INSTALL_PREFIX=/usr/local -D CMAKE_BUILD_TYPE=Release -D BUILD_TIFF=ON
注:在conda环境中,由于安装pytorch,会自动安装“libtiff”,卸载后导致pytorch不能用,然后导致opencv在编译过程中会报错,具体如下:
/usr/local/lib/libopencv_imgcodecs.so.4.5.1: undefined reference to `TIFFReadRGBAStrip@LIBTIFF_4.0' /usr/local/lib/libopencv_imgcodecs.so.4.5.1: undefined reference to `TIFFReadDirectory@LIBTIFF_4.0' /usr/local/lib/libopencv_imgcodecs.so.4.5.1: undefined reference to `TIFFWriteEncodedStrip@LIBTIFF_4.0' /usr/local/lib/libopencv_imgcodecs.so.4.5.1: undefined reference to `TIFFIsTiled@LIBTIFF_4.0' /usr/local/lib/libopencv_imgcodecs.so.4.5.1: undefined reference to `TIFFWriteScanline@LIBTIFF_4.0' /usr/local/lib/libopencv_imgcodecs.so.4.5.1: undefined reference to `TIFFGetField@LIBTIFF_4.0' /usr/local/lib/libopencv_imgcodecs.so.4.5.1: undefined reference to `TIFFScanlineSize@LIBTIFF_4.0' /usr/local/lib/libopencv_imgcodecs.so.4.5.1: undefined reference to `TIFFWriteDirectory@LIBTIFF_4.0' /usr/local/lib/libopencv_imgcodecs.so.4.5.1: undefined reference to `TIFFReadEncodedTile@LIBTIFF_4.0' /usr/local/lib/libopencv_imgcodecs.so.4.5.1: undefined reference to `TIFFReadRGBATile@LIBTIFF_4.0' /usr/local/lib/libopencv_imgcodecs.so.4.5.1: undefined reference to `TIFFClose@LIBTIFF_4.0' /usr/local/lib/libopencv_imgcodecs.so.4.5.1: undefined reference to `TIFFClientOpen@LIBTIFF_4.0' /usr/local/lib/libopencv_imgcodecs.so.4.5.1: undefined reference to `TIFFRGBAImageOK@LIBTIFF_4.0' /usr/local/lib/libopencv_imgcodecs.so.4.5.1: undefined reference to `TIFFOpen@LIBTIFF_4.0' /usr/local/lib/libopencv_imgcodecs.so.4.5.1: undefined reference to `TIFFReadEncodedStrip@LIBTIFF_4.0' /usr/local/lib/libopencv_imgcodecs.so.4.5.1: undefined reference to `TIFFSetField@LIBTIFF_4.0' /usr/local/lib/libopencv_imgcodecs.so.4.5.1: undefined reference to `TIFFSetWarningHandler@LIBTIFF_4.0' /usr/local/lib/libopencv_imgcodecs.so.4.5.1: undefined reference to `TIFFSetErrorHandler@LIBTIFF_4.0
解决方法:
- opencv源码安装编译时候,保证conda环境没有libtiff包名
- 在cmake 编译过程中开启 BUILD_TIFF
参考链接:
- LIBTIFF_4.0 link errors - OpenCV Q&A Forum
- https://stackoverflow.com/questions/23934386/using-opencv-with-libtiff
- OpenCV LIBTIFF_4.0 link errors - 普兒 - 博客园
- Linux(Ubuntu 18.04)中安装OpenCV + OpenCV_Contrib - 花落未殇 - 博客园
- https://blog.51cto.com/u_14587161/3000682
- https://blog.csdn.net/jiaken2660/article/details/117036606
2.2 TensorRT安装
- 可参考网:https://developer.nvidia.com/zh-cn/tensorrt,有tar包和deb两种安装方,本文选择tar包进行解压安装。
- 根据tar包解压安装:tar -xvf TensorRT-7.2.3.4.Ubuntu-18.04.x86_64-gnu.cuda-11.1.cudnn8.1.tar.gz
- 安装命令:分别进入python、uff和graphsurgeon根据python环境,进行pip安装相应的包
- vi ~/.bashrc 添加 export LD_LIBRARY_PATH=/usr/local/TensorRT-7.2.3.4/lib:/usr/local/cuda/bin:$LD_LIBRARY_PATH
export INCLUDE_PATH=/usr/local/TensorRT-7.2.3.4/include:/usr/local/cuda/lib64:$INCLUDE_PATH- source ~/.bashrc
GitHub - ultralytics/yolov5: YOLOv5 in PyTorch > ONNX > CoreML > TFLite
https://github.com/wang-xinyu/tensorrtx
在tensorrx项目中,进入yolov5目录,修改Cmakelists.txt:
修改自己tcuda和tensorrt路径:
# cuda
include_directories(/usr/local/cuda/include)
link_directories(/usr/local/cuda/lib64)
# tensorrt
include_directories(/usr/local/TensorRT-7.2.3.4/include)
link_directories(/usr/local/TensorRT-7.2.3.4/lib)
在终端运行: python gen_wts.py
在终端运行:mkdir build && cd build:
在终端运行:cmake ..
在终端运行:make -j128
在终端运行:./yolov5 -s // serialize model to plan file i.e. 'yolov5s.engine' 序列化模型
在终端运行:./yolov5 -d ../data/images // deserialize plan file and run inference, the images in samples will be processed. 测试用例
或者在终端运行:CUDA_VISIBLE_DEVICES=6 python yolov5_trt.py