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

作者: qizhen816 | 来源:发表于2019-10-31 17:35 被阅读0次

跟着网上(百度)的教程做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。

相关文章

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

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

  • django 中model踩的坑之AttributeError:

    django 中model踩的坑之AttributeError: type object ** has no at...

  • clipChildren="false" 失效

    踩坑随手记 setLayerType(View.LAYER_TYPE_SOFTWARE, null)setLaye...

  • AFNetworking multipart/form-data

    踩坑的地方,自己設置Content-type 覆盖了内部设置的 content-type 和boundary,导...

  • Vue踩坑实录(二)

    在上一篇中说了一下踩过的前三个坑,剩下的坑就在这篇中全部搞定吧。Vue踩坑实录(一) Vue-cli .js?.V...

  • fastlane 踩坑实录

    这个世界是懒人创造的。 人懒了就会发明各种各样的工具,或者寻找各种各样能够给自己偷懒机会的工具,当然我还停留在使用...

  • Mongo踩坑实录

    1.更新数据时,js脚本中没有指定数据类型,int数据被更新成了double,导致线上问题。 原因,js是弱类型,...

  • gitattribute踩坑实录

    工欲善其事,必先利其器。 前一阵子,公司的版本控制从svn迁移到了git,不得不说,git确实比svn要强大好多,...

  • Weex踩坑实录

    1.新组件无法与配合使用。目前遇到的情况主要是loading没法正常...

  • # Flutter 踩坑实录

    [TOC] Another exception was thrown: A dismissed Dismissib...

网友评论

    本文标题:Libtorch踩坑实录:non-scalar type, '-

    本文链接:https://www.haomeiwen.com/subject/xcbtbctx.html