教程同样适用与ubuntu22.04、ubuntu20.04。如果您对tensorrt不是很熟悉,请务必保持下面库版本一致。请注意,Linux系统安装以下库,务必去进入系统bios下,关闭安全启动(设置 secure boot 为 disable)。tensorrt依赖cuda、cudnn,本文也会给出安装办法,顺便opencv的安装方法也给了。最后,也会分享如何在书写cmakelists文件,以便在项目中使用tensorrt。
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安装正常
由于tensorrt依赖cuda cudnn,所以我们先新建common.cmake文件,如下,并在文件中声明相关库的头文件、lib路径等。
# set
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated-declarations")
# find thirdparty
find_package(CUDA REQUIRED)
list(APPEND ALL_LIBS
${CUDA_LIBRARIES}
${CUDA_cublas_LIBRARY}
${CUDA_nppc_LIBRARY} ${CUDA_nppig_LIBRARY} ${CUDA_nppidei_LIBRARY} ${CUDA_nppial_LIBRARY})
# include cuda's header
list(APPEND INCLUDE_DRIS ${CUDA_INCLUDE_DIRS})
set(TensorRT_ROOT /home/xxxxxx/TensorRT-8.4.2.4)
find_library(TRT_NVINFER NAMES nvinfer HINTS ${TensorRT_ROOT} PATH_SUFFIXES lib lib64 lib/x64)
find_library(TRT_NVINFER_PLUGIN NAMES nvinfer_plugin HINTS ${TensorRT_ROOT} PATH_SUFFIXES lib lib64 lib/x64)
find_library(TRT_NVONNX_PARSER NAMES nvonnxparser HINTS ${TensorRT_ROOT} PATH_SUFFIXES lib lib64 lib/x64)
find_library(TRT_NVCAFFE_PARSER NAMES nvcaffe_parser HINTS ${TensorRT_ROOT} PATH_SUFFIXES lib lib64 lib/x64)
find_path(TENSORRT_INCLUDE_DIR NAMES NvInfer.h HINTS ${TensorRT_ROOT} PATH_SUFFIXES include)
list(APPEND ALL_LIBS ${TRT_NVINFER} ${TRT_NVINFER_PLUGIN} ${TRT_NVONNX_PARSER} ${TRT_NVCAFFE_PARSER})
# include tensorrt's headers
list(APPEND INCLUDE_DRIS ${TENSORRT_INCLUDE_DIR})
set(SAMPLES_COMMON_DIR ${TensorRT_ROOT}/samples/common)
list(APPEND INCLUDE_DRIS ${SAMPLES_COMMON_DIR})
message(STATUS ***INCLUDE_DRIS*** = ${INCLUDE_DRIS})
message(STATUS "ALL_LIBS: ${ALL_LIBS}")
有一点需要特别注意,上述文件中set(TensorRT_ROOT /home/xxxxxx/TensorRT-8.4.2.4)表示设置tensorrt的路径,你修改为自己的实际路径就行,下面再新建CMakeLists.txt文件,在第5行文件中会包含上述common.cmake文件,你根据自己实际情况修改路径。
这样就能将app_yolov8.cpp和一堆其他的.cpp和.cu文件包含进工程,其中main函数在app_yolov8.cpp中。
cmake_minimum_required(VERSION 3.10)
set(CMAKE_BUILD_TYPE "Debug")
#set(CMAKE_BUILD_TYPE "Release")
PROJECT(yolov8 VERSION 1.0.0 LANGUAGES C CXX CUDA)
include(${CMAKE_CURRENT_SOURCE_DIR}/../cmake/common.cmake)
message(STATUS ${ALL_LIBS})
file(GLOB CPPS
${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/*.cu
)
list(REMOVE_ITEM CPPS app_yolov8.cpp)
list (LENGTH CPPS length)
find_package(OpenCV REQUIRED)
include_directories(${INCLUDE_DRIS} ${OpenCV_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR})
add_library(${PROJECT_NAME} SHARED ${CPPS})
target_link_libraries(${PROJECT_NAME} ${ALL_LIBS} ${OpenCV_LIBRARIES})
set_property(TARGET ${PROJECT_NAME} PROPERTY CUDA_ARCHITECTURES 50 61 72 75)
target_compile_options(${PROJECT_NAME} PUBLIC
$<$:--default-stream per-thread -lineinfo; --use_fast_math --disable-warnings>)
add_executable(app_yolov8 app_yolov8.cpp)
# NVCC
# target_link_libraries(detect ${PROJECT_NAME} ${ALL_LIBS} ${OpenCV_LIBRARIES} libgflags_nothreads.a)
target_link_libraries(app_yolov8 ${PROJECT_NAME} ${ALL_LIBS} ${OpenCV_LIBRARIES} )
上述的两个文件分别参考:
common.cmake : https://github.com/FeiYull/TensorRT-Alpha/blob/main/cmake/common.cmake
CMakeLists.txt :https://github.com/FeiYull/TensorRT-Alpha/blob/main/yolov8/CMakeLists.txt