I have this very simple code snippet using opencv and LibTorch, which does not run for some reason.
#include <iostream>
#include <torch/script.h>
#include <opencv2/core/core.hpp>
int main() {
cv::Mat imgMat = cv::Mat::zeros(640, 640, CV_8UC3);
at::Tensor tensorImg = torch::from_blob(imgMat.data, {1, imgMat.rows, imgMat.cols, imgMat.channels()});
std::cout << tensorImg << "\n"; // problem here
return 0;
}
I have tried to compile it with clang and added undefined behaviour sanitizer, which gives the following errors:
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==11549==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x7fffde2fa000 (pc 0x7fffe4039d08 bp 0x7fffdd7b4ed0 sp 0x7fffdd7b4e20 T11570)
==11549==The signal is caused by a READ memory access.
UndefinedBehaviorSanitizer:DEADLYSIGNAL
UndefinedBehaviorSanitizer:DEADLYSIGNAL
#0 0x7fffe4039d08 in void c10::function_ref<void (char**, long const*, long, long)>::callback_fn<auto at::TensorIteratorBase::loop_2d_from_1d<at::native::AVX2::copy_kernel(at::TensorIterator&, bool)::'lambda'()::operator()() const::'lambda10'()::operator()() const::'lambda'()::operator()() const::'lambda12'()::operator()() const::'lambda'(char**, long const*, long)>(at::native::AVX2::copy_kernel(at::TensorIterator&, bool)::'lambda'()::operator()() const::'lambda10'()::operator()() const::'lambda'()::operator()() const::'lambda12'()::operator()() const::'lambda'(char**, long const*, long) const&)::'lambda'(char**, long const*, long, long)>(long, char**, long const*, long, long) (/home/dani/Desktop/test/build/libtorch/lib/libtorch_cpu.so 0x54bed08) (BuildId: e03155c98263c3ef83236051d8610270872897af)
#1 0x7fffdfecf96f in at::TensorIteratorBase::serial_for_each(c10::function_ref<void (char**, long const*, long, long)>, at::Range) const (/home/dani/Desktop/test/build/libtorch/lib/libtorch_cpu.so 0x135496f) (BuildId: e03155c98263c3ef83236051d8610270872897af)
#2 0x7fffdfecfb2d in void at::internal::invoke_parallel<at::TensorIteratorBase::for_each(c10::function_ref<void (char**, long const*, long, long)>, long)::'lambda'(long, long)>(long, long, long, at::TensorIteratorBase::for_each(c10::function_ref<void (char**, long const*, long, long)>, long)::'lambda'(long, long) const&) (._omp_fn.0) (/home/dani/Desktop/test/build/libtorch/lib/libtorch_cpu.so 0x1354b2d) (BuildId: e03155c98263c3ef83236051d8610270872897af)
#3 0x7fffde41696d (/home/dani/Desktop/test/build/libtorch/lib/libgomp-52f2fd74.so.1 0x1696d) (BuildId: 9afb2d23e5127e68ba5ef6031eefc9d25b9b672b)
#4 0x7fffde79db42 in start_thread nptl/./nptl/pthread_create.c:442:8
#5 0x7fffde82f9ff misc/../sysdeps/unix/sysv/linux/x86_64/clone3.S:81
UndefinedBehaviorSanitizer can not provide additional info.
SUMMARY: UndefinedBehaviorSanitizer: SEGV (/home/dani/Desktop/test/build/libtorch/lib/libtorch_cpu.so 0x54bed08) (BuildId: e03155c98263c3ef83236051d8610270872897af) in void c10::function_ref<void (char**, long const*, long, long)>::callback_fn<auto at::TensorIteratorBase::loop_2d_from_1d<at::native::AVX2::copy_kernel(at::TensorIterator&, bool)::'lambda'()::operator()() const::'lambda10'()::operator()() const::'lambda'()::operator()() const::'lambda12'()::operator()() const::'lambda'(char**, long const*, long)>(at::native::AVX2::copy_kernel(at::TensorIterator&, bool)::'lambda'()::operator()() const::'lambda10'()::operator()() const::'lambda'()::operator()() const::'lambda12'()::operator()() const::'lambda'(char**, long const*, long) const&)::'lambda'(char**, long const*, long, long)>(long, char**, long const*, long, long)
==11549==ABORTING
Any idea what am I doing wrong?
CodePudding user response:
As it turned out I was missing the option parameter at::kByte
from the function torch::from_blob()
.
Edit:
Without this parameter LibTorch could not interpret the tensor, and gave a deadly signal. See the actual reason in @Dan Mašek's comment.
The TensorOptions specify additional configuration options for the returned tensor, such as what type to interpret the data as.
The correct line is:
at::Tensor tensorImg = torch::from_blob(imgMat.data, {1, imgMat.rows, imgMat.cols, imgMat.channels()}, at::kByte);