标题 C++环境下tensorrt加速推理部署测试
延续上一篇,基于yolov5利用C++进行检测分类测试。
C++环境下tensorrt加速推理部署测试
1.文件下载
1)yolov5 V6.0版本源码下载
https://github.com/ultralytics/yolov5/tags
2)yolov5s.pt 权重文件下载
https://github.com/ultralytics/yolov5/releases
3)Dirent文件下载
是一个c/c++编程接口
https://github.com/tronkko/dirent
2.部署实施
1.生成yolov5s.wts文件
1)将下载后的Dirent里面dirent.h复制到tensorrtx/yolov5的文件夹中
2)将tensorrtx/yolov5文件夹中的gen_wts.py文件复制到下载好的yolov5源码中,运行得到wts文件
源码工程目录下直接cmd 运行命令python gen_wts.py -w yolov5s.pt -o yolov5s.wts
2.生成yolov5.exe文件
1.CMakeList.txt修改
主要修改后面带#部分的对应路径,与自己工程实际路径相对应
/
cmake_minimum_required(VERSION 3.2)
if(NOT DEFINED CMAKE_CUDA_ARCHITECTURES)
set(CMAKE_CUDA_ARCHITECTURES 50)###########
endif()
set(CudaToolkitDir "C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v11.1")###########
project(yolov5)
set(OpenCV_DIR "D:\\AppMy\\opencv\\opencv\\build") #1
set(TRT_DIR "D:\\Myproject\\TensorRT-8.2.5.1.Windows10.x86_64.cuda-11.4.cudnn8.2\\TensorRT-8.2.5.1") #2
set(OpenCV_INCLUDE_DIRS "D:\\AppMy\\opencv\\opencv\\build\\include") #3
set(OpenCV_LIBS "D:\\AppMy\\opencv\\opencv\\build\\x64\\vc15\\lib\\opencv_world340.lib") #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(D:\\yanyi\\project_process\\Completed_projects\\yolo-deploy\\tensorrtx-master\\yolov5) # 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 ${PROJECT_SOURCE_DIR}/calibrator.cpp ${PROJECT_SOURCE_DIR}/yolov5.cpp ${PROJECT_SOURCE_DIR}/yololayer.cu ${PROJECT_SOURCE_DIR}/yololayer.h ${PROJECT_SOURCE_DIR}/preprocess.cu ${PROJECT_SOURCE_DIR}/preprocess.h)
add_executable(yolov5 ${PROJECT_SOURCE_DIR}/yolov5.cpp ${PROJECT_SOURCE_DIR}/common.hpp ${PROJECT_SOURCE_DIR}/yololayer.cu ${PROJECT_SOURCE_DIR}/yololayer.h ${PROJECT_SOURCE_DIR}/preprocess.cu ${PROJECT_SOURCE_DIR}/preprocess.h) #17
target_link_libraries(yolov5 "nvinfer" "nvinfer_plugin") #5
target_link_libraries(yolov5 ${OpenCV_LIBS}) #6
target_link_libraries(yolov5 ${CUDA_LIBRARIES}) #7
target_link_libraries(yolov5 Threads::Threads) #8
/
2.利用CMake软件进行文件配置
然后打开CMake软件,输入tensorrtx下的yolov5文件路径和build(文件夹名称自定义)路径,会自动创建文件夹,勾选以下选项
点击configure后,弹出如下对话框,选择对应vs版本和系统版本
点击finish后出现configure done
依次点击generate open project
注意:若此处报错,检查cmakelist文件中的路径设置
选择Realse和x64,点击生成解决方案
编译成功后会显示成功,只要没有失败即可,在yolov5-build-Realse文件中生成exe文件
3.生成yolov5s.engine文件
将上述生成的wts文件复制到yolov5-Build-Release文件下,cmd执行:
yolov5.exe -s yolov5s.wts yolov5s.engine s
等待几分钟后,会在当前目录生成一个yolov5s.engine文件
此处注意yolov5源码中的类别数量和训练模型中的类别数量一致,否则报错
4.测试检测结果
cmd运行:yolov5.exe -d yolov5s.engine ./samplse
若在项目编译过程中出现多个错误,提示找不到各类函数,尝试以下进行解决:
项目右击,生成依赖项,生成自定义,选择第一个 cuda
https://blog.csdn.net/qq_44224801/article/details/126110532spm=1001.2014.3001.5502
在该博主文档参考下,自己配置修改总结