libtorch部署过程opencv读取图片以及遇到的错误解决方法

下载最新编译好的libtorch

从官网下载最新的libtorch

CMakeList

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(custom_ops)

#include_directories(/home/weipenghui/Lib-dev/opencv_3.4.3_contrib/opencv-3.4.3/build/install_cv/include)
# set(CMAKE_PREFIX_PATH "/home/weipenghui/Lib-dev/opencv_3.4.3_contrib/opencv-3.4.3/build/install_cv")
find_package(OpenCV REQUIRED)

set(CMAKE_PREFIX_PATH
        /home/user0/glq/trt/torch/libtorch
        /usr/local/)

find_package(Torch REQUIRED)
add_executable(example-app test.cpp)
target_link_libraries(example-app ${TORCH_LIBRARIES} ${OpenCV_LIBS})
set_property(TARGET example-app PROPERTY CXX_STANDARD 11)

cpp源码

#include  // One-stop header.
#include 
#include 
#include 
#include
//https://pytorch.org/tutorials/advanced/cpp_export.html

int main(int argc, const char* argv[]) {

    // Deserialize the ScriptModule from a file using torch::jit::load().
    torch::jit::script::Module module = torch::jit::load("/home/user0pse/detemodel.pt");
    std::string image_path = "/home/user/torch/pse/04.jpg";
    //assert(module != nullptr);
    std::cout << "ok\n";
    //输入图像
    auto image = cv::imread(image_path,cv::ImreadModes::IMREAD_COLOR);
    cv::Mat image_transfomed;
    cv::resize(image, image_transfomed, cv::Size(800, 384));
    cv::cvtColor(image_transfomed, image_transfomed, cv::COLOR_BGR2RGB);
    // 图像转换为Tensor
    torch::Tensor tensor_image = torch::from_blob(image_transfomed.data, {image_transfomed.rows, image_transfomed.cols,3},torch::kByte);
    tensor_image = tensor_image.permute({2,0,1});
    tensor_image = tensor_image.toType(torch::kFloat);
    tensor_image = tensor_image.div(255);
    tensor_image = tensor_image.unsqueeze(0);
    tensor_image = tensor_image.to(at::kCUDA);
    std::cout<<tensor_image<<std::endl;
    module.to(at::kCUDA);
    // 网络前向计算
    // Execute the model and turn its output into a tensor.
    at::Tensor output = module.forward({tensor_image}).toTensor();
    std::cout<<output<<std::endl;
}

遇到的主要问题

CMake error: conversion from ‘torch::jit::script::Module’ to non-scalar type ‘std::shared_ptrtorch::jit::script::Module .

将官方教程里给的这句`

std::shared_ptr<torch::jit::script::Module> module = torch::jit::load("../xxx.pt");`

std::shared_ptr这个是libtorch测试版本使用的变量类型,现在已经变更,将以上代码修改为:

 torch::jit::script::Module module = torch::jit::load("../xxx.pt");

no match for ‘operator!=’ (operand types are ‘torch::jit::script::Module’ and ‘std::nullptr_t’)

根据官方说法,现在的Module已经不是指针,这个断言没有存在的必要了,删掉就行。

assert(module != nullptr);

error: base operand of ‘->’ has non-pointer type ‘torch::jit::script::Module’

出现这句错误的原因是module已经不是指针,

torch::Tensor output = module->forward(std::move(inputs)).toTensor();

把代码修改为:

torch::Tensor output = module.forward(std::move(inputs)).toTensor();

Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same(解决)

图片和模型都应该显示放到cuda上
加上

tensor_image = tensor_image.to(at::kCUDA)

而不是

tensor_image.to(at::kCUDA)

这里出错了 google Can’t load the tensor image to the GPU devices 才搜索出来结果。
!!
吐槽一下官方教程,都1.4了该更新的还没有更新。1,2问题都是以前的,最新的教程还是这样用。

感谢:
这篇文章

你可能感兴趣的:(libtorch部署过程opencv读取图片以及遇到的错误解决方法)