CMakeLists文件配置
这里默认你的Opencv已经安装好了
打开之前下载的YOLOv5TensorRT这个文件,修改CMakeLists.txt文件。修改Opencv、Tensorrt、dirent.h的目录。注意这三个文件必须填写绝对路径!
注:其中dirent.h在YOLOv5TensorRT/include/下,修改arch=compute_75;code=sm_75【因为我用的是英伟达1650,这个填写的是显卡算力,根据自己的显卡去修改,可参考:CUDA GPU | NVIDIA Developer】
我的CMakeLists.txt如下
cmake_minimum_required(VERSION 2.6) project(yolov5) #change to your own path ################################################## set(OpenCV_DIR "F:\\opencv\\opencv\\build") set(TRT_DIR "F:\\TensorRT-8.2.4.2") set(Dirent_INCLUDE_DIRS "E:\\YOLOv5TensorRT\\include") ################################################## 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 10.2 REQUIRED) message(STATUS " libraries: ${CUDA_LIBRARIES}") message(STATUS " include path: ${CUDA_INCLUDE_DIRS}") include_directories(${CUDA_INCLUDE_DIRS}) include_directories(${Dirent_INCLUDE_DIRS}) #change to your GPU own compute_XX ########################################################################################### set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS};-std=c++11;-g;-G;-gencode;arch=compute_75;code=sm_75) ########################################################################################### #### 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) # -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) add_executable(yolov5 ${PROJECT_SOURCE_DIR}/yolov5.cpp ${PROJECT_SOURCE_DIR}/yololayer.cu ${PROJECT_SOURCE_DIR}/yololayer.h ${PROJECT_SOURCE_DIR}/preprocess.cu) target_link_libraries(yolov5 "nvinfer" "nvinfer_plugin") target_link_libraries(yolov5 ${OpenCV_LIBS}) target_link_libraries(yolov5 ${CUDA_LIBRARIES}) target_link_libraries(yolov5 Threads::Threads)
运行Cmake
在YOLOv5TensorRT/下建一个build文件
打开CMake,代码为YOLOv5TensorRT,build目录为刚才新建的build路径
然后点击Configure(下图中的路径还是写的Yolov5_Tensorrt_Win10是老项目,因为添加了东西,其实已经换成了YOLOv5TensorRT和YOLOv5TensorRT/build )
运行完以后会出现以下界面,显示配置完成,点击Generate 在点击open Project会自动打开VS
有时候会提升找不到cuda,检查一下路径对不对。【有时候Cmake的时候有各种问题,欢迎大家把遇到的问题和解决办法留言,方便大家一起解决学习】
编译
进入:E:\YOLOv5TensorRT\build ,打开yolov5.sln 项目文件
然后依次打开项目中的yolov5/Header Files/yololayer.h,可以修改红色框中的输入大小和类的数量。
上VS界面上面的Debug改为Release
右键项目重新生成
编译成功以后,会在YOLOv5TensorRT\build\Release下生成一个yolov5.exe程序
程序运行
生成engine文件
将最前面生成的yolov5s.wts序列化模型复制到这个exe文件下。
输入:使用的是s模型,最后则输入s,若为m模型,最后一个参数则需要改成m
./yolov5.exe -s yolov5s.wts yolov5s.engine s
该过程是将yolov5s.wts转化成yolov5s.engine文件的过程,这个过程比较长【差不多10~20分钟】,耐心等待。成功以后如下显示
./yolov5.exe -s yolov5s.wts yolov5s.engine s Loading weights: yolov5s.wts Building engine, please wait for a while... [05/27/2022-16:39:23] [W] [TRT] TensorRT was linked against cuBLAS/cuBLASLt 10.2.2 but loaded cuBLAS/cuBLASLt 10.2.1 [05/27/2022-16:39:24] [W] [TRT] TensorRT was linked against cuDNN 8.2.1 but loaded cuDNN 8.2.0 [05/27/2022-16:48:34] [W] [TRT] TensorRT was linked against cuBLAS/cuBLASLt 10.2.2 but loaded cuBLAS/cuBLASLt 10.2.1 [05/27/2022-16:48:34] [W] [TRT] TensorRT was linked against cuDNN 8.2.1 but loaded cuDNN 8.2.0 Build engine successfully!
同时在exe文件夹下生成了engine文件
预测
图像预测:
将E:\YOLOv5TensorRT\ 下的整个pictures文件复制到exe程序文件下,同时将coco_classes.txt文件也放进来
然后打开cmd运行如下命令进行预测:
./yolov5.exe -d yolov5s.engine -img ./pictures
出现如下:
E:\Yolov5_Tensorrt_Win10\build\Release>yolov5 -d yolov5s.engine ./pictures [05/27/2022-16:54:45] [W] [TRT] TensorRT was linked against cuDNN 8.2.1 but loaded cuDNN 8.2.0 [05/27/2022-16:54:45] [W] [TRT] TensorRT was linked against cuDNN 8.2.1 but loaded cuDNN 8.2.0 7ms 7ms
此时会在当前目录下生成预测结果图:
视频预测
./yolov5.exe -d yolov5s.engine -video 0
如果需要替换自己的类,记得在代码中把类yololayer.h的数量改一下,如果放自己的txt,在yolov5.cpp中的主函数里将classes_path换成自己的路径即可。
完成上述即完成了整个过程。