YOLOV5 tensorRT部署以及问题解决办法

TensorRT部署yolov5s参考博文:https://blog.csdn.net/weixin_39588099/article/details/119994675

所有步骤几乎一样,需要记录几点:
1、CMakeList.txt修改

cmake_minimum_required(VERSION 2.6)
project(yolov5)  
set(OpenCV_DIR "F:\\yolov5_tensorRT\\opencv\\build")  #1
set(OpenCV_INCLUDE_DIRS "F:\\yolov5_tensorRT\\opencv\\build\\include")  #2
set(OpenCV_LIBS "F:\\yolov5_tensorRT\\opencv\\build\\x64\\vc15\\lib\\opencv_world454d.lib") #3
set(TRT_DIR "F:\\yolov5_tensorRT\\TensorRT-8.0.3.4")  #4

add_definitions(-std=c++11)
add_definitions(-DAPI_EXPORTS)
option(CUDA_USE_STATIC_CUDA_RUNTIME OFF)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_BUILD_TYPE Debug)

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads)

# setup CUDA
find_package(CUDA REQUIRED)
message(STATUS "    libraries: ${CUDA_LIBRARIES}")
message(STATUS "    include path: ${CUDA_INCLUDE_DIRS}")

include_directories(${CUDA_INCLUDE_DIRS})

####
enable_language(CUDA)  # add this line, then no need to setup cuda path in vs
####
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${TRT_DIR}\\include)
include_directories(F:\\yolov5_tensorRT\\tensorrtx\\include) #5


#find_package(OpenCV)
include_directories(${OpenCV_INCLUDE_DIRS})
include_directories(${OpenCV_INCLUDE_DIRS}\\opencv2) #6


# -D_MWAITXINTRIN_H_INCLUDED for solving error: identifier "__builtin_ia32_mwaitx" is undefined
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Ofast -D_MWAITXINTRIN_H_INCLUDED")

# setup opencv
find_package(OpenCV QUIET
    NO_MODULE
    NO_DEFAULT_PATH
    NO_CMAKE_PATH
    NO_CMAKE_ENVIRONMENT_PATH
    NO_SYSTEM_ENVIRONMENT_PATH
    NO_CMAKE_PACKAGE_REGISTRY
    NO_CMAKE_BUILDS_PATH
    NO_CMAKE_SYSTEM_PATH
    NO_CMAKE_SYSTEM_PACKAGE_REGISTRY
)

message(STATUS "OpenCV library status:")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")

include_directories(${OpenCV_INCLUDE_DIRS})
link_directories(${TRT_DIR}\\lib) #7
link_directories(${OpenCV_DIR}\\x64\\vc15\\lib) #8

add_executable(yolov5 preprocess.cu preprocess.h ${PROJECT_SOURCE_DIR}/calibrator.cpp yolov5 ${PROJECT_SOURCE_DIR}/yolov5.cpp ${PROJECT_SOURCE_DIR}/yololayer.cu ${PROJECT_SOURCE_DIR}/yololayer.h)   #4
target_link_libraries(yolov5 "nvinfer" "nvinfer_plugin")   #9
target_link_libraries(yolov5 ${OpenCV_LIBS})          #10
target_link_libraries(yolov5 ${CUDA_LIBRARIES})   #11
target_link_libraries(yolov5 Threads::Threads)       #12

2、在路径F:\yolov5_tensorRT\tensorrtx\yolov5添加preprocess.cu和preprocess.h文件
下载链接:https://download.csdn.net/download/zhouzongzong/86340827

3、Cmake生成文件后编译出现错误解决办法:YOLOV5 tensorRT部署以及问题解决办法_第1张图片解决方案:
设置项目属性,在预定义处理器中添加定义NOMINMAX来禁止使用Visual C++的min/max宏定义。
vs工程属性中进行修改,“属性——c++——预处理器——预处理器定义”中加入NOMINMAX即可。

4、重要的一点:生成引擎文件yolov5s.engine时候出现以下错误
YOLOV5 tensorRT部署以及问题解决办法_第2张图片
需要更改cmake结果文件中的CLASS_NUM,和生成yolov5s.wts的python训练时候一致
YOLOV5 tensorRT部署以及问题解决办法_第3张图片结果展示:
YOLOV5 tensorRT部署以及问题解决办法_第4张图片
[TRT] TensorRT was linked against cuBLAS/cuBLAS LT 11.5.1 but loaded cuBLAS/cuBLAS LT 11.4.2

其中版本问题解决方案可以参考博文:
https://blog.csdn.net/u013171226/article/details/119353781
更改后:
YOLOV5 tensorRT部署以及问题解决办法_第5张图片

你可能感兴趣的:(深度学习,opencv,计算机视觉,c++)