官方安装编译步骤链接
tensorrtx/run_on_windows.md at master · wang-xinyu/tensorrtx · GitHubImplementation of popular deep learning networks with TensorRT network definition API - tensorrtx/run_on_windows.md at master · wang-xinyu/tensorrtxhttps://github.com/wang-xinyu/tensorrtx/blob/master/tutorials/run_on_windows.md其中的TensorRT 只需要下载解压就行,opencv最好是下载Windows版本的exe,安装到随便一个位置,其实Windows opencv的安装就是解压过程
CMakeLists的配置上面官网已经说的很清楚了,这里不再重复,注意选择自己电脑的VS版本,2015到2019都可以的。
CMakeLists.txt是最关键的,下面是我的版本
cmake_minimum_required(VERSION 2.6)
project(yolov5) # 1
set(OpenCV_DIR "D:\\software\\opencv\\build") #2
set(TRT_DIR "D:\\3DReconstruction\\tensorrtx\\TensorRT-8.2.1.8.Windows10.x86_64.cuda-11.4.cudnn8.2\\TensorRT-8.2.1.8") #3
set(Dirent_INCLUDE_DIRS "D:\\3DReconstruction\\tensorrtx\\dirent-master\\include")
add_definitions(-DAPI_EXPORTS) #这行解决错误1
add_definitions(-std=c++11)
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)
# -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 ${PROJECT_SOURCE_DIR}/preprocess.h) #4 ${PROJECT_SOURCE_DIR}/preprocess.cu ${PROJECT_SOURCE_DIR}/preprocess.h 这是后来加的用于解决错误2,下面也有说明
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
生成结束后,done了就行,其它红色是警告,不用管
生成阶段的主要错误:
错误1(在CMakeLists.txt中解决了)
LNK2001 无法解析的外部符号 "public: __cdecl nvinfer1::YoloPluginCreator::YoloPluginCreator(void)" (??0YoloPluginCreator@nvinfer1@@QEAA@XZ)
错误2(在CMakeLists.txt中解决了)
无法解析的外部符号 "void __cdecl preprocess_kernel_img(unsigned char *,int,int,float *,int,int,struct CUstream_st *)" (?preprocess_kernel_img@@YAXPEAEHHPEAMHHPEAUCUstream_st@@@z),函数 main 中引用了该符号
这里是因为缺少了函数preprocess_kernel_img,在CMakeLists.txt里加${PROJECT_SOURCE_DIR}/preprocess.cu ${PROJECT_SOURCE_DIR}/preprocess.h就可以解决,加的位置我上面给了,在#4那个地方
生成的文件,用你生成的那版VS打开,打开后直接编译,按道理debug和release应该都可以编译成功
错误3:无法打开包括文件:"dirent.h":No such file or directory
上面这个编译错误是因为找不到官网说的那个dirent.h文件,这个错误可以直接把路径给到那个附加包含目录中即可解决
编译成功后:
错误4:
[network.cpp::nvinfer1::Network::addScale::737] Error Code 3: API Usage Error (Parameter check failed at: network.cpp::nvinfer1::Network::addScale::737, condition: shift.count > 0 ? (shift.values != nullptr) : (shift.values == nullptr)
)
上面这个错误就是网络结构版本和权重不对应,找到对应的就行了 ,就在作者给的链接里就有
GitHub - wang-xinyu/tensorrtx: Implementation of popular deep learning networks with TensorRT network definition APIImplementation of popular deep learning networks with TensorRT network definition API - GitHub - wang-xinyu/tensorrtx: Implementation of popular deep learning networks with TensorRT network definition APIhttps://github.com/wang-xinyu/tensorrtx
如果你用的最新版本那就是v6
在release根目录打开cmd,把下载的yolov5.wts放在根目录,运行下面的命令产生yolov5.engine文件
yolov5.exe -s yolov5s.wts yolov5s.engine s
成功后运行下面命令测试效果,结果会自动产生在根目录,注意samples文件夹是你已经有的,在下面的文件夹里,复制到realse即可,或者直接给全路径
命令:
yolov5.exe -d yolov5s.engine ./samples