跟著網(wǎng)上(百度)的教程做libtorch部署廉丽,官方庫正在更新倦微,有很多問題沒有解答,Google了一下把解決的問題列出來
1. CMake error: conversion from ‘torch::jit::script::Module’ to non-scalar type ‘std::shared_ptrtorch::jit::script::Module .
這個(gè)問題是很多教程在聲明模型的時(shí)候使用了:
std::shared_ptr<torch::jit::script::Module> module = torch::jit::load("../xxx.pt");
根據(jù)參考1正压,std::shared_ptr這個(gè)是libtorch測(cè)試版本使用的變量類型欣福,現(xiàn)在已經(jīng)變更,將以上代碼修改為:
torch::jit::script::Module module = torch::jit::load("../xxx.pt");
另外焦履,有些教程里會(huì)出現(xiàn)一個(gè)空指針判斷的斷言:
assert(module != nullptr);
然后會(huì)出現(xiàn)錯(cuò)誤:error: no match for ‘operator!=’ (operand types are ‘torch::jit::script::Module’ and ‘std::nullptr_t’)
根據(jù)官方說法拓劝,現(xiàn)在的Module已經(jīng)不是指針,這個(gè)斷言沒有存在的必要了嘉裤,刪掉就行郑临。
2. error: base operand of '->' has non-pointer type 'torch::jit::script::Module'
問題出現(xiàn)在這一行:
torch::Tensor output = module->forward(std::move(inputs)).toTensor();
原因也很簡(jiǎn)單,module已經(jīng)不是指針屑宠,把代碼修改為:
torch::Tensor output = module.forward(std::move(inputs)).toTensor();
3. Make過程中 undefined reference to `cv::String::allocate(unsigned long)'
出現(xiàn)一堆錯(cuò)誤:
undefined reference to cv::String::allocate(unsigned long)' undefined reference to
cv::imread(cv::String const&, int)'
undefined reference to cv::String::deallocate()' undefined reference to
cv::imread(cv::String const&, int)'
undefined reference to cv::String::deallocate()' undefined reference to
cv::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 to
cv::imwrite(cv::String const&, cv::_InputArray const&, std::vector<int, std::allocator<int> > const&)'
undefined reference to cv::String::deallocate()' undefined reference to
cv::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)
在語義分割等任務(wù)中有可能模型的返回值是一個(gè)list而不是Tensor厢洞,有問題的模型返回值可能是這樣的:
torch::Tensor result = module->forward({tensor_image}).toTensor();
需要修改一下,先 .toTuple()->elements()得到一個(gè)list典奉,我的情況是得到的list第一個(gè)元素是Tensor躺翻,就要修改為:
torch::Tensor result = module->forward({tensor_image}).toTuple()->elements()[0].toTensor().toTensor();
5. 模型softmax輸出之后C++進(jìn)行argmax后處理:
torch::Tensor result_mask = result.argmax(1);
result_mask = result_mask.squeeze();
這樣就可以得到語義分割的分類mask。