简体   繁体   中英

PyTorch C++ convert data to tensor fails

I am trying / using the torch_script tool to run my PyTorch trained NN in C++. The below code fails with linker errors

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);

When I uncomment the from_blob line my code compiles well, enable it and it fails with below error. I'm following examples on the web that claim this works. Where shall I look?

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

Found the proper solution from this bugreport

https://github.com/pytorch/pytorch/issues/32500

Seems find_package was returning bad paths to libraries.

$TORCH_LIBRARIES contains a mix of library names and absolute paths to libs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM