文章目录
前言
对于深度学习模型边端部署,使用TensorRT能大大提升推理性能,减少资源开销,VideoPipe也提供了针对TensorRT-8.5(要求cuda-11.1)的环境测试。
本文环境已在搭载4070Ti-Super的设备上经过验证,系统版本为 Ubuntu-18.04, gcc/g++ --version为7.5.0,亲测安装成功。
此外,在另一台搭载1660-Ubuntu18.04的设备上也经过验证,只要打上支持相应型号显卡的驱动,并安装cuda11.1(由于使用的是TensorRT8.5,强烈建议安装cuda11.1,cuda11.7、11.8安装测试时,发现存在NVIDIA-Driver不匹配、OpenCV编译不通过等报错),同样可以完美运行。
安装Ubuntu18.04
建议使用Ubuntu18.04(双系统或多系统,不能使用VMWare或WSL虚拟机)进行安装,如果有20.04也可以尝试使用20.04,但22.04不行,因为cuda11.1不支持22.04.
搜索添加Ubuntu相应的apt install
镜像源
安装Nvidia-smi显卡驱动
1、卸载已存在的显卡驱动、cuda等环境,防止出现版本冲突编译混乱
sudo apt-get --purge remove '*cublas*' 'cuda*' 'nsight*' 'nvidia*'
sudo apt-get autoremove
sudo apt remove --purge nvidia-* // 防止卸不干净
sudo apt remove --purge libnvidia-*
并禁止nouveau驱动
2、安装Nvidia-smi显卡驱动
对于40系显卡,推荐一个稳定便捷的驱动版本:NVIDIA-Linux-x86_64-550.142.run
百度/Google上搜索NVIDIA-Linux-x86_64-550.142.run下载完毕后,直接终端输入sudo bash NVIDIA-Linux-x86_64-550.142.run即可进入安装(除了32位的不选yes,其他的一路continue, yes就可以了),此版本还不需要关闭Lightdm,所以用向日葵远程也可以安装。
安装完毕后 sudo reboot
重启,分辨率是高清的,且nvidia-smi有返回说明就是驱动装好了。
此外,还有`NVIDIA-Linux-x86_64-560.28.23测试过,也可以支持cuda11.1,个别版本的NVIDIA-Driver可能由于具体环境原因安装时会出现冲突,这里建议对于30系以下的显卡安装NVIDIA-Driver-455.23,对30系及以上的显卡安装NVIDIA-Driver-550/560,避免出现cuda11.1与Driver不匹配的报错。
安装cuda-11.1
wget https://developer.download.nvidia.com/compute/cuda/11.1.0/local_installers/cuda_11.1.0_455.23.05_linux.run
sudo bash cuda_11.1.0_455.23.05_linux.run
输入accept,已经装上显卡驱动的不需要再勾选driver,
cuda-11.1环境变量
sudo gedit ~/.bashrc
export PATH=/usr/local/cuda-11.1/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-11.1/lib64:$LD_LIBRARY_PATH
source ~/.bashrc
cuda11.1安装完毕后,输入nvcc -V
有以下提示就是安装成功了:
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2020 NVIDIA Corporation
Built on Tue_Sep_15_19:10:02_PDT_2020
Cuda compilation tools, release 11.1, V11.1.74
Build cuda_11.1.TC455_06.29069683_0
安装cudnn
Nvidia官网下载 cudnn-8.0.5的.tar文件,注意不要下成12.X的版本
切换到存放目录,终端输入tar -xvf cudnn-11.1-linux-x64-v8.0.5.39.tgz
进行解压
cd cuda
sudo cp include/cudnn*.h /usr/local/cuda/include
sudo cp lib64/libcudnn* /usr/local/cuda/lib64
// 这个命令会等待时间长一点
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*
cat /usr/local/cuda/include/cudnn_version.h | grep CUDNN_MAJOR -A 2
// 有返回就是安装成功
cudnn环境变量
sudo gedit ~/.bashrc
export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
export CUDA_HOME=/usr/local/cuda
source ~/.bashrc
安装Freetype
Freetype 和 harfbuzz是互相依赖的,这里推荐先安装包含harfbuzz的freetype
sudo apt-get install libfreetype6-dev
ubuntu18.04中可能要安装依赖:sudo apt-get install libfreetype6=2.8.1-2ubuntu2
安装freetype2需要的依赖u
sudo apt install zlib2
sudo apt install libpng-dev
sudo apt install libharfbuzz-dev
安装gstreamer
Ubuntu-18.04安装方式:
apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libgstreamer-plugins-bad1.0-dev gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-libav gstreamer1.0-tools gstreamer1.0-x gstreamer1.0-alsa gstreamer1.0-gl gstreamer1.0-gtk3 gstreamer1.0-qt5 gstreamer1.0-pulseaudio libgstrtspserver-1.0-dev gstreamer1.0-rtsp
Ubuntu-20.04安装方式:
安装基础依赖:
sudo apt install gstreamer1.0-tools gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-plugins-bad
安装gstreamer:
sudo apt install gstreamer1.0-tools gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-libav
测试命令:
gst-launch-1.0 -v videotestsrc ! autovideosink
这个命令会创建一个视频测试源并通过自动视频接收器显示它。如果没有错误信息,那么 GStreamer 已经正确安装并可以运行:
如果还是不行,尝试18.04的安装方法,可能会提示缺少一些依赖,按提示安装,如果安装失败,尽量不要去使用apt --fix broken命令,可能会误删除已安装好的包,可以先尝试安装几个依赖,然后执行sudo dpkg --configure -a
和sudo apt install -f
,apt会自动构建依赖关系进行安装。
安装ffmpeg
sudo apt install ffmpeg
sudo apt install libx264-dev libxvidcore-dev
sudo apt-get install libgtk2.0-dev pkg-config
安装openssl
sudo apt install build-essential
sudo apt install openssl
sudo apt-get install libssl-dev
安装Boost(如果需要用到websocket通信)
参考:在Ubuntu上安装Boost的五种方法(全网最全,建议收藏)
安装OpenCV-4.6.0
github官网上下载opencv-4.6.0、opencv_contrib、opencv_extra,并sudo cp -r 到/home路径下,创建一个build_bash.sh,使用下列命令进行编译安装:sudo bash build_bash.sh
VERSION=4.6.0
cd opencv
rm -rf ./build
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda \
-D CUDNN_INCLUDE_DIR=/usr/local/cuda/include \
-D WITH_TBB=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_CUBLAS=1 \
-D WITH_CUDA=ON \
-D BUILD_opencv_cudacodec=OFF \
-D WITH_CUDNN=ON \
-D OPENCV_DNN_CUDA=ON \
-D CUDA_ARCH_BIN=7.5 \
-D WITH_V4L=ON \
-D WITH_QT=OFF \
-D WITH_OPENGL=ON \
-D WITH_FFMPEG=ON \
-D WITH_GSTREAMER=ON \
-D WITH_FREETYPE=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_PC_FILE_NAME=opencv.pc \
-D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D INSTALL_C_EXAMPLES=OFF \
-D CUDNN_INCLUDE_DIR=/usr/local/cuda/include \
-D CUDNN_LIBRARY=/usr/lib/x86_64-linux-gnu/libcudnn.so \
-D FREETYPE_INCLUDE_DIRS=/usr/include/freetype2 \
-D FREETYPE_LIBRARY=/usr/lib/x86_64-linux-gnu/libfreetype.so \
-D OPENCV_DOWNLOAD_MIRROR_ID=gitcode \
..
make -j 12
sudo make -j 12 install
注意CUDA_ARCH_BIN=7.5 无需更改8.6或8.9等新的算力编号,因为cuda11.1也不支持那么高。
安装TensorRT-8.5
VideoPipe的ReadME中其实也介绍了相关的教程,和NVIDIA官网的安装教程一致:
主要是下载TensorRT-8.5,7.2.1.6不建议尝试,没有编译成功,
其余的按照上述步骤tar -xzvf TensorRT-8.5.1.7.Linux.x86_64-gnu.cuda-11.8.cudnn8.6.tar.gz
在 /usr/local
路径下, create softlink by ln -s /usr/local/TensorRT-8.5.1.7 /usr/local/tensorRT
添加环境变量:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/tensorRT/lib:/usr/local/cuda/lib64:/usr/local/lib
export CPATH=$CPATH:/usr/local/cuda/include:/usr/local/tensorRT/include
至 ~/.bashrc
安装VideoPipe
在/home/usrname/VideoPipe下新建目录:mkdir build
cd /nodes
打开vp_infer_nodes.cpp,第一行加入宏定义:#define VP_WITH_CUDA
编译cuda+tensorrt版本
cd ..
cd /build
cmake -DVP_WITH_CUDA=ON -DVP_WITH_TRT=ON ..
也可以创建一个build_bash.sh,方便多次编译:
#!/bin/bash
# 设置项目根目录路径
PROJECT_DIR="/home/fine3/yolov8n-trt"
BUILD_DIR="${PROJECT_DIR}/build"
# 清空之前的 build 目录(如果存在)
echo "Cleaning previous build..."
rm -rf "${BUILD_DIR}"
# 创建新的 build 目录
echo "Creating new build directory..."
mkdir -p "${BUILD_DIR}"
# 进入到 build 目录
cd "${BUILD_DIR}"
# 使用 CMake 配置项目
echo "Running CMake..."
cmake -DVP_WITH_CUDA=ON -DVP_WITH_TRT=ON ..
# 编译项目
echo "Building project..."
make -j12
到此编译全部结束,安装完成。
运行测试样例,可以看到显存利用正常,已开始利用NVIDIA-GPU进行TensorRT推理。
报错排查
tensorRT engine plan error
如果编译没有问题,推理时遇到算力不匹配的error,只需要在当前环境下重新转换.wts生成一下.engine就可以。
视频编解码报错
如果编译时构建target报错:Trying to load shared library libnvinfer_builder_resource.so.8.5.3 6: [libLoader.h::DynamicLibrary::54] Error Code 6: Internal Error (Unable to load library: libnvinfer_builder_resource.so.8.5.3)
find /usr -iname libnvinfer_builder_resource.so.8.5.1
// 复制下tensorRT的target构建文件
sudo cp /usr/local/TensorRT-8.5.1.7/targets/x86_64-linux-gnu/lib/libnvinfer_builder_resource.so.8.5.1 /usr/lib
视频输入源读取失败
OpenCV: FFMPEG: tag 0x47504a4d/'MJPG' is not supported with codec id 8 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x7634706d/'mp4v'
[ WARN:1@1.007] global /home/ubuntu/opencv-4.6.0/opencv/modules/videoio/src/cap_gstreamer.cpp (1374) open OpenCV | GStreamer warning: unable to query duration of stream
[ WARN:1@1.008] global /home/ubuntu/opencv-4.6.0/opencv/modules/videoio/src/cap_gstreamer.cpp (1405) open OpenCV | GStreamer warning: Cannot query video position: status=1, value=-1, duration=-1
FPS 值为: 24 帧/秒
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_M_construct null not valid
Aborted (core dumped)
sudo apt install ffmpeg
sudo apt install libx264-dev libxvidcore-dev
sudo apt-get install libgtk2.0-dev pkg-config
重新编译opencv和videopipe
相关参考教程
ubuntu禁止nouveau驱动 https://blog.csdn.net/changxiaoshen/article/details/104166008
ubuntu18.04下载国内镜像及安装事项 https://blog.csdn.net/m0_46577050/article/details/143278902
Linux学习笔记(二十) – 在Ubuntu上进行FreeType库的编译 https://blog.csdn.net/u014337397/article/details/81115439
Ubuntu系统下交叉编译harfbuzz https://blog.csdn.net/m0_37605642/article/details/145641973
在Ubuntu上安装Boost的五种方法(全网最全,建议收藏) https://blog.csdn.net/ccf19881030/article/details/143655055
Websocket++安装使用 https://zhuanlan.zhihu.com/p/59925926