Win10 c++调用yolov5的pytorch模型

环境:win10 + vs2019+cuda11.0+pytorch1.7.1

1. vs2019配置opencv4.4和libtorch1.7.1

https://download.pytorch.org/libtorch/cu110/libtorch-win-shared-with-deps-1.7.1%2Bcu110.zip

libtorch的GPU release版本。

下载完成后解压在任意地址即可,这里假定文件在F:\libtorch中。libtorch文件夹内容如下。

 

1)环境变量设置

安装完成后,右键“此电脑”->“属性”->“高级系统设置”->点击系统变量中的Path->添加dll路径。(lib中有.lib和.dll两种文件,安装过opencv的小伙伴都知道,这两种文件一般是分到两个文件夹中的,.lib路径要在vs中进行设置,.dll一般要添加到环境变量中。)

加入"F:\libtorch\lib",“F:\opencv\build\x64\vc15\bin”(具体前缀地址以你实际放置的地址为准)

2)VS2019的环境配置

新建项目,“项目”->“属性”,将配置改为“Release”,平台设为“x64”。

选择“调试”->“环境”,添加

    PATH=%PATH%;F:\libtorch\lib

选择“包含目录”,添加

    F:\libtorch\include\torch\csrc\api\include

    F:\libtorch\include

    F:\opencv\build\include

选择“库目录”,添加

    F:\libtorch\lib

    F:\opencv\build\x64\vc15\lib

选择“链接器”->“附加依赖项”,添加

    F:\opencv\build\x64\vc15\lib\opencv_world440.lib

    F:\opencv\build\x64\vc15\lib\opencv_world440d.lib(用于debug版) (具体地址与opencv_world后缀以实际为准)

    c10.lib

    torch.lib

    torch_cpu.lib

    torch_cuda.lib

“链接器”->“命令行”,在其他选项中输入 /INCLUDE:?warp_size@cuda@at@@YAHXZ

这个命令大概是用来链接cuda库的,cpu版无需输入。

3) vs测试

torch::cuda::is_available()输出为1即为成功。

// for gpu version test

#include <opencv2/opencv.hpp>
#include <torch/script.h>
#include <torch/torch.h>
#include <iostream>


int main()
{

    std::cout << torch::cuda::is_available() << std::endl;

    torch::DeviceType device_type = at::kCUDA;

    std::cout << device_type << std::endl;
 
    torch::Tensor tensor = torch::rand({ 5,3 }).to(device_type);

    std::cout << tensor << std::endl;

    cv::Mat img = cv::imread("D:/test.png");
    cv::imshow("", img);
    cv::waitKey(1000);
    return 0;

}

2. pytorch模型转化为torch script

Export-gpu.py将pt模型转化为torchscript.pt

3. 在c++中使用libtorch调用yolov5模型进行测试

    在Python环境下对训练好的模型进行转换以后,咱们须要C++环境下的PyTorch来读取模型并进行编译部署。这种C++环境下的PyTorch就是libtorch。由于libtorch一般用来做为PyTorch模型的C++接口,libtorch也称之为PyTorch的C++前端。

    下载GitHub - ncdhz/YoloV5-LibTorch: 一个 C++ 版本的 YoloV5 封装库在自己的项目中新建main.cpp, YoloV5.cppYoloV5.h分别将YoloV5-LibTorch工程中的test文件夹中的test.cpp, src文件夹中的YoloV5.cpp和include文件夹中的YoloV5.h的代码复制到对应的main.cpp,YoloV5.cppYoloV5.h中。Main.cpp中设置的pt文件和coco.txt文件分别修改为上步转化的torchscript.pt和自己训练的类别。

  • 3
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
YOLOv5 PyTorch模型转换为TensorRT模型需要以下步骤: 1. 安装TensorRT和PyTorch。 2. 下载并安装yolov5。 3. 使用PyTorchyolov5模型转换为ONNX格式。 ``` python models/export.py --weights yolov5s.pt --img 640 --batch 1 --include onnx # yolov5s ``` 4. 安装ONNX-TensorRT。 ``` git clone https://github.com/onnx/onnx-tensorrt.git cd onnx-tensorrt git submodule update --init --recursive mkdir build && cd build cmake .. -DTENSORRT_ROOT=/path/to/tensorrt -DCMAKE_CXX_COMPILER=g++-7 make -j sudo make install ``` 5. 使用ONNX-TensorRT将ONNX模型转换为TensorRT模型。 ``` import onnx import onnx_tensorrt.backend as backend model = onnx.load("yolov5s.onnx") # Load the ONNX model engine = backend.prepare(model, device="CUDA:0") # Prepare the TensorRT model with open("yolov5s.engine", "wb") as f: # Serialize the TensorRT engine f.write(engine.serialize()) ``` 6. 测试TensorRT模型的性能和准确性。 ``` import pycuda.driver as cuda import pycuda.autoinit import numpy as np import time # Load the TensorRT engine with open("yolov5s.engine", "rb") as f: engine = cuda.Context().deserialize_cuda_engine(f.read()) # Create the TensorRT inference context context = engine.create_execution_context() # Allocate the input and output buffers input_shape = engine.get_binding_shape(0) output_shape = engine.get_binding_shape(1) input_buffer = cuda.mem_alloc(np.prod(input_shape) * 4) output_buffer = cuda.mem_alloc(np.prod(output_shape) * 4) # Prepare the input data input_data = np.random.rand(*input_shape).astype(np.float32) # Copy the input data to the input buffer cuda.memcpy_htod(input_buffer, input_data) # Run inference start_time = time.time() context.execute_v2(bindings=[int(input_buffer), int(output_buffer)]) end_time = time.time() # Copy the output data to the output buffer output_data = np.empty(output_shape, dtype=np.float32) cuda.memcpy_dtoh(output_data, output_buffer) # Print the inference time and output data print("Inference time: {} ms".format((end_time - start_time) * 1000)) print("Output shape: {}".format(output_shape)) print("Output data: {}".format(output_data)) ```
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值