YOLOv5 deployment to C++ for Windows (using LibTorch)

foreword

Due to project needs, we want to deploy YOLOv5 to C++ programs on Windows. This article uses LibTorch to deploy YOLOv5 to Visual Studios 2019.

preparation content

First, we download and prepare the required content:

Configuration Environment

Configure the OpenCV and LibTorch environment, the configuration content of the two is roughly the same (the following path address is adjusted according to the personal installation path).

  1. Configure system environment variables:
    Add the paths of OpenCV and LibTorch respectively in the system environment variables
    Opencv:
    D:\opencv\build\x64\vc15\bin
    Libtorch:
    D:\libtorch\lib

  2. VS2019 creates a new C++ empty project;

  3. Project Properties -> VC++ Directories -> Include Directories:
    Opencv:
    D:\opencv\build\include
    D:\opencv\build\include\opencv2
    Libtorch:
    D:\libtorch\include
    D:\libtorch\include\torch\csrc\ api\include

  4. Project Properties -> VC++ Directories -> Library Directories:
    Opencv:
    D:\opencv\build\x64\vc15\lib
    Libtorch:
    D:\libtorch\lib

  5. Project Properties -> Linker -> Input -> Additional Dependencies:
    Opencv: Put the .lib
    file in the D:\opencv\build\x64\vc15\lib file into the additional dependencies, the two versions and your project version Consistent: opencv_world452d.lib( Debug ) opencv_world452.lib( Release ) Libtorch: Put all the .lib files in D:\libtorch\lib into the additional dependencies.



  6. Project Properties -> C/C++ -> Additional Include Directories:
    D:\opencv\build\include
    D:\libtorch\include
    D:\libtorch\include\torch\csrc\api\include

  7. Project Properties -> C/C++ -> General SDL Check to No

  8. Project Properties -> C/C++ -> Language -> Conform to Mode Conform to Mode Change to No

  9. Project Properties -> Debug -> Environment:
    Opencv:
    D:\opencv\build\x64\vc15\lib;
    Libtorch:
    D:\libtorch\lib;

  10. Test OpenCV:
    Run the following code to check whether OpenCV is configured correctly:

    #include <opencv2/opencv.hpp>
    int main() {
          
          
    	cv::Mat img = cv::imread("图片路径");
    	cv::imshow("", img);
    	cv::waitKey(0);
    	return 0;
    }
    
  11. Test LibTorch:
    Run the following code to check that LibTorch is configured correctly:

    #include <torch/torch.h>
    #include <iostream>
    int main() {
          
          
    	std::cout << "cuda::is_available():" << torch::cuda::is_available() << std::endl;
    	torch::Tensor tensor = torch::rand({
          
           3, 4 });
    	std::cout << tensor << std::endl;
    }
    

compile

  1. Open CMakeLists.txt in Libtorch-Yolov5, give the path of OpenCV and LibTorch in the following two lines:
    set(OpenCV_DIR D:/opencv/build)
    set(Torch_DIR D:/libtorch/share/cmake/Torch/)
  2. open cmd
    mkdir build	# 建立build文件夹
    cd build		# 进入build文件夹
    cmake ..		# 编译
    

YOLOv5 detection

  • Change the category name in coco.names in D:/libtorch-yolov5/weights to your own dataset category name, and the trained weight file can be placed in this folder (.pt weight file first uses export.py of yolov5 into a .torchscript.pt file);
  • The files in D:/libtorch-yolov5/src are the detection function detector.cpp and the main function main.cpp, and the specific content can be changed according to individual needs.

possible problems

  • Unable to open source file opencv2/opencv.hpp or torch/torch.h: The environment is not configured well.
  • OpenCV or LibTorch files cannot be found during compilation: Check whether the path address in CMakeLists.txt is filled in correctly, pay attention to the direction of the slash, whether there are comment symbols, and whether it is added to the system environment variable.
  • CUDA not found when compiling: CUDA was not installed correctly or was not added to the system environment variables.
  • Slow inference speed or slow model loading: Check whether the pytorch version and CUDA version are correspondingly correct.

Guess you like

Origin blog.csdn.net/lucifer479/article/details/120743124