Article directory
1. yolov5 v6.0 training model
-
Official website download yolov5 v6.0 code
-
Download the official pre-trained model
-
The library files required to install yolov5, requirements.txt are included in the downloaded yolov5 source code.
pip install -r C:\Users\10001540\Downloads\yolov5-6.0\requirements.txt
-
Open the detect.py file in the yolov5 source code and modify the location of the model.
Various errors may occur after running. You can refer to online tutorials.
2. Convert the trained yolov5 model to tensorrt engine
-
Go to tensorrtx official website to download the code
-
Copy gen_wts.py in yolov5 under tensorrtx to the yolov5 source code folder
-
Refer to the official instructions of yolov5 to convert the yolov5 model file yolov5s.pt to the yolov5s.wts file.
python gen_wts.py -w weights/yolov5s.pt -o yolov5s.wts
-
Enter the yolov5 folder under tensorrtx and modify the CMakeList.txt inside, as follows:
cmake_minimum_required(VERSION 2.6) project(yolov5) #1 set(OpenCV_DIR "D:\\Program Files\\opencv\\build") #2 set(OpenCV_INCLUDE_DIRS ${OpenCV_DIR}\\include) #3 set(OpenCV_LIB_DIRS ${OpenCV_DIR}\\x64\\vc15\\lib) #4 set(OpenCV_Debug_LIBS "opencv_world454d.lib") #5 set(OpenCV_Release_LIBS "opencv_world454.lib") #6 set(TRT_DIR "C:\\Program Files\\NVIDIA GPU Computing Toolkit\\TensorRT-8.2.3.0") #7 set(TRT_INCLUDE_DIRS ${TRT_DIR}\\include) #8 set(TRT_LIB_DIRS ${TRT_DIR}\\lib) #9 set(Dirent_INCLUDE_DIRS "Z:\\code\\dirent-master\\include") #10 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) #11 include_directories(${TRT_INCLUDE_DIRS}) #12 link_directories(${TRT_LIB_DIRS}) #13 include_directories(${OpenCV_INCLUDE_DIRS}) #14 link_directories(${OpenCV_LIB_DIRS}) #15 include_directories(${Dirent_INCLUDE_DIRS}) #16 # -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 " lib path: ${OpenCV_LIB_DIRS}") message(STATUS " Debug libraries: ${OpenCV_Debug_LIBS}") message(STATUS " Release libraries: ${OpenCV_Release_LIBS}") message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}") 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") #18 target_link_libraries(yolov5 debug ${OpenCV_Debug_LIBS}) #19 target_link_libraries(yolov5 optimized ${OpenCV_Release_LIBS}) #20 target_link_libraries(yolov5 ${CUDA_LIBRARIES}) #21 target_link_libraries(yolov5 Threads::Threads) if(NOT DEFINED CMAKE_CUDA_ARCHITECTURES) set(CMAKE_CUDA_ARCHITECTURES 70 75 80 86) endif(NOT DEFINED CMAKE_CUDA_ARCHITECTURES)
Things to note here:
-
In the yolov5 folder under tensorrtx, open cmd and enter the following code
mkdir build cd build cmake ..
-
Enter the build folder, open the yolov5.sln file with vs, and then click Generate to generate the solution. If
the following appears, the generation is successful:
-
Set yolov5 as startup item
-
Enter the yolov5 property page and set the debugging options as follows:
-
After setting up and running the program,
it may take some time to run. If the following appears, it means that the model conversion is successful.
-
Test the generated model and set it as follows in the properties page:
The following content indicates that the operation was successful: