![](/img/trans.png)
[英]How to convert PyTorch tensor to C++ torch::Tensor vice versa?
[英]PyTorch C++ convert data to tensor fails
我正在嘗試/使用torch_script
工具在 C++ 中運行經過 PyTorch 訓練的神經網絡。 以下代碼因鏈接器錯誤而失敗
cv::Mat amplitudePatch = Assigned somewhere above in code;
auto options = torch::TensorOptions().dtype(torch::kF32);
torch::from_blob((void*)amplitudePatch.data,
{1, amplitudePatch.size().height, amplitudePatch.size().width, amplitudePatch.channels()},
options);
當我取消注釋from_blob
行時,我的代碼編譯得很好,啟用它並且失敗並出現以下錯誤。 我正在關注網絡上聲稱此方法有效的示例。 我應該去哪里看?
In function `c10::tensorTypeIdToBackend(c10::TensorTypeId)':
/usr/local/lib/python3.6/dist-packages/torch/include/c10/core/Backend.h:107: undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
SunlightNoiseClassifier/libSunlightNoiseClassifier.a(SunlightNoiseClassifier.cpp.o): In function `c10::backendToDeviceType(c10::Backend)':
/usr/local/lib/python3.6/dist-packages/torch/include/c10/core/Backend.h:169: undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/local/lib/python3.6/dist-packages/torch/include/c10/core/Backend.h:171: undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
SunlightNoiseClassifier/libSunlightNoiseClassifier.a(SunlightNoiseClassifier.cpp.o): In function `c10::scalarTypeToTypeMeta(c10::ScalarType)':
/usr/local/lib/python3.6/dist-packages/torch/include/c10/core/ScalarType.h:125: undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
SunlightNoiseClassifier/libSunlightNoiseClassifier.a(SunlightNoiseClassifier.cpp.o): In function `c10::typeMetaToScalarType(caffe2::TypeMeta)':
/usr/local/lib/python3.6/dist-packages/torch/include/c10/core/ScalarType.h:151: undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
SunlightNoiseClassifier/libSunlightNoiseClassifier.a(SunlightNoiseClassifier.cpp.o):/usr/local/lib/python3.6/dist-packages/torch/include/c10/core/Device.h:93: more undefined references to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' follow
SunlightNoiseClassifier/libSunlightNoiseClassifier.a(SunlightNoiseClassifier.cpp.o): In function `at::Context::getDeviceFromPtr(void*, c10::DeviceType)':
/usr/local/lib/python3.6/dist-packages/torch/include/ATen/Context.h:47: undefined reference to `c10::DeviceTypeName[abi:cxx11](c10::DeviceType, bool)'
/usr/local/lib/python3.6/dist-packages/torch/include/ATen/Context.h:47: undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
SunlightNoiseClassifier/libSunlightNoiseClassifier.a(SunlightNoiseClassifier.cpp.o): In function `at::from_blob(void*, c10::ArrayRef<long>, c10::ArrayRef<long>, std::function<void (void*)> const&, c10::TensorOptions const&)':
/usr/local/lib/python3.6/dist-packages/torch/include/ATen/Functions.h:1099: undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
SunlightNoiseClassifier/libSunlightNoiseClassifier.a(SunlightNoiseClassifier.cpp.o): In function `torch::autograd::make_variable(at::Tensor, bool, bool)':
/usr/local/lib/python3.6/dist-packages/torch/include/torch/csrc/autograd/variable.h:524: undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
SunlightNoiseClassifier/libSunlightNoiseClassifier.a(SunlightNoiseClassifier.cpp.o): In function `c10::intrusive_ptr<c10::StorageImpl, c10::detail::intrusive_target_default_null_type<c10::StorageImpl> >::retain_()':
/usr/local/lib/python3.6/dist-packages/torch/include/c10/util/intrusive_ptr.h:183: undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
SunlightNoiseClassifier/libSunlightNoiseClassifier.a(SunlightNoiseClassifier.cpp.o): In function `c10::intrusive_ptr<c10::TensorImpl, c10::UndefinedTensorImpl>::retain_()':
/usr/local/lib/python3.6/dist-packages/torch/include/c10/util/intrusive_ptr.h:183: undefined reference to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
SunlightNoiseClassifier/libSunlightNoiseClassifier.a(SunlightNoiseClassifier.cpp.o):/usr/local/lib/python3.6/dist-packages/torch/include/c10/util/intrusive_ptr.h:183: more undefined references to `c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' follow
collect2: error: ld returned 1 exit status
從此錯誤報告中找到了正確的解決方案
https://github.com/pytorch/pytorch/issues/32500
似乎find_package
返回了錯誤的庫路徑。
$TORCH_LIBRARIES
包含庫名稱和庫的絕對路徑的組合。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.