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 <torch/script.h> // One-stop header.
#include <opencv2/opencv.hpp>
#include <iostream>
#include <memory>
#include<string>
//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问题都是以前的,最新的教程还是这样用。

感谢:
这篇文章

  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值