深度学习-推荐系统-CV-NLP人工智能/模式识别/机器学习精华专题深度学习·神经网络·计算机视觉

Libtorch踩坑实录:non-scalar type, '-

2019-10-31  本文已影响0人  qizhen816

跟着网上(百度)的教程做libtorch部署,官方库正在更新,有很多问题没有解答,Google了一下把解决的问题列出来

1. 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");

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

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

另外,有些教程里会出现一个空指针判断的断言:

assert(module != nullptr);

然后会出现错误:error: no match for ‘operator!=’ (operand types are ‘torch::jit::script::Module’ and ‘std::nullptr_t’)
根据官方说法,现在的Module已经不是指针,这个断言没有存在的必要了,删掉就行。

2. error: base operand of '->' has non-pointer type 'torch::jit::script::Module'

问题出现在这一行:

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

原因也很简单,module已经不是指针,把代码修改为:

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

3. Make过程中 undefined reference to `cv::String::allocate(unsigned long)'

出现一堆错误:
undefined reference to cv::String::allocate(unsigned long)' undefined reference tocv::imread(cv::String const&, int)'
undefined reference to cv::String::deallocate()' undefined reference tocv::imread(cv::String const&, int)'
undefined reference to cv::String::deallocate()' undefined reference tocv::line(cv::InputOutputArray const&, cv::Point<int>, cv::Point_<int>, cv::Scalar_<double> const&, int, int, int)'
undefined reference to cv::line(cv::_InputOutputArray const&, cv::Point_<int>, cv::Point_<int>, cv::Scalar_<double> const&, int, int, int)' undefined reference tocv::imwrite(cv::String const&, cv::_InputArray const&, std::vector<int, std::allocator<int> > const&)'
undefined reference to cv::String::deallocate()' undefined reference tocv::imwrite(cv::String const&, cv::_InputArray const&, std::vector<int, std::allocator<int> > const&)'
多半原因是CMakeLists.txt文件里忘了加Opencv的链接:

target_link_libraries( your_app_name ${OpenCV_LIBS} )

4. Expected Tensor but got Tuple (toTensor at /libtorch/include/ATen/core/ivalue_inl.h:86)

在语义分割等任务中有可能模型的返回值是一个list而不是Tensor,有问题的模型返回值可能是这样的:

        torch::Tensor result = module->forward({tensor_image}).toTensor();

需要修改一下,先 .toTuple()->elements()得到一个list,我的情况是得到的list第一个元素是Tensor,就要修改为:

        torch::Tensor result = module->forward({tensor_image}).toTuple()->elements()[0].toTensor().toTensor();

5. 模型softmax输出之后C++进行argmax后处理:

    torch::Tensor result_mask = result.argmax(1);
    result_mask = result_mask.squeeze();

这样就可以得到语义分割的分类mask。

上一篇 下一篇

猜你喜欢

热点阅读