Hy guys, I am having a runtime error with libtorch
. I am getting a video stream and I want to get the probability out for my classification problem. The program runs for the first 4/5 frames and then it crashes.
This is what I am doing:
void MyClass::prob(const cv::Mat& img, const bool cuda){
torch::Tensor img_tensor = torch::from_blob(img.clone().data, { 1, 1, img.rows, img.cols }, torch::kByte);
img_tensor = img_tensor.to(torch::kFloat) / 255;
bool use_cuda = cuda && torch::cuda::is_available();
torch::DeviceType device_type;
if (use_cuda)
device_type = torch::kCUDA;
else
device_type = torch::kCPU;
torch::Device device(device_type);
_model->to(device);
img_tensor = img_tensor.set_requires_grad(false);
auto out = _model->forward(img_tensor.to(device));
_mProb = out[0].squeeze(0); // writing to a field [H, W]
}
It seems that the error refers to a to
function, input tensor and memory location. I just started with pytorch
and I’ve seen examples that seems to do the same. Don’t know what can be wrong. I attach the error trace that may be interesting:
what(): unsupported operation: some elements of the input tensor and the written-to tensor refer to a single memory location. Please clone() the tensor before performing the operation.
Exception raised from assert_no_partial_overlap at ../aten/src/ATen/MemoryOverlap.cpp:75 (most recent call first):
frame #0: c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) + 0x69 (0x7f9798b88b89 in /home/ldg/source/libraries/libtorch/lib/libc10.so)
frame #1: <unknown function> + 0xb24bb2 (0x7f9788759bb2 in /home/ldg/source/libraries/libtorch/lib/libtorch_cpu.so)
frame #2: at::TensorIterator::compute_mem_overlaps(at::TensorIteratorConfig const&) + 0xc5 (0x7f9788c425e5 in /home/ldg/source/libraries/libtorch/lib/libtorch_cpu.so)
frame #3: at::TensorIterator::build(at::TensorIteratorConfig&) + 0x4e (0x7f9788c4b4ce in /home/ldg/source/libraries/libtorch/lib/libtorch_cpu.so)
frame #4: at::TensorIterator::TensorIterator(at::TensorIteratorConfig&) + 0xdd (0x7f9788c4bc9d in /home/ldg/source/libraries/libtorch/lib/libtorch_cpu.so)
frame #5: <unknown function> + 0xd3f173 (0x7f9788974173 in /home/ldg/source/libraries/libtorch/lib/libtorch_cpu.so)
frame #6: at::native::copy_(at::Tensor&, at::Tensor const&, bool) + 0x53 (0x7f9788976093 in /home/ldg/source/libraries/libtorch/lib/libtorch_cpu.so)
frame #7: at::Tensor& c10::Dispatcher::callWithDispatchKey<at::Tensor&, at::Tensor&, at::Tensor const&, bool>(c10::TypedOperatorHandle<at::Tensor& (at::Tensor&, at::Tensor const&, bool)> const&, c10::DispatchKey, at::Tensor&, at::Tensor const&, bool) const + 0x1e7 (0x7f97891665d7 in /home/ldg/source/libraries/libtorch/lib/libtorch_cpu.so)
frame #8: at::Tensor::copy_(at::Tensor const&, bool) const + 0xcd (0x7f978928e0ed in /home/ldg/source/libraries/libtorch/lib/libtorch_cpu.so)
frame #9: <unknown function> + 0x32cecd8 (0x7f978af03cd8 in /home/ldg/source/libraries/libtorch/lib/libtorch_cpu.so)
frame #10: at::Tensor& c10::Dispatcher::callWithDispatchKey<at::Tensor&, at::Tensor&, at::Tensor const&, bool>(c10::TypedOperatorHandle<at::Tensor& (at::Tensor&, at::Tensor const&, bool)> const&, c10::DispatchKey, at::Tensor&, at::Tensor const&, bool) const + 0x1e7 (0x7f97891665d7 in /home/ldg/source/libraries/libtorch/lib/libtorch_cpu.so)
frame #11: at::Tensor::copy_(at::Tensor const&, bool) const + 0xcd (0x7f978928e0ed in /home/ldg/source/libraries/libtorch/lib/libtorch_cpu.so)
frame #12: at::native::to(at::Tensor const&, c10::ScalarType, bool, bool, c10::optional<c10::MemoryFormat>) + 0x2ff (0x7f9788c2039f in /home/ldg/source/libraries/libtorch/lib/libtorch_cpu.so)
I really appreciate some help,
Thanks for your time