简体   繁体   English

在 C++ 中使用 LibTorch 访问 at::Tensor 时出错

[英]Error when accessing at::Tensor using LibTorch in C++

I have this very simple code snippet using opencv and LibTorch, which does not run for some reason.我有这个使用 opencv 和 LibTorch 的非常简单的代码片段,由于某种原因它没有运行。

#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:我试图用 clang 编译它并添加了未定义的行为消毒剂,它给出了以下错误:

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?知道我做错了什么吗?

As it turned out I was missing the option parameter at::kByte from the function torch::from_blob() .事实证明,我在函数torch::from_blob()中缺少at::kByte的选项参数。 Without this parameter LibTorch could not interpret the tensor, and gave a deadly signal.如果没有这个参数,LibTorch 将无法解释张量,并发出致命信号。

Based on the documentation: 根据文档:

The TensorOptions specify additional configuration options for the returned tensor, such as what type to interpret the data as. TensorOptions 为返回的张量指定额外的配置选项,例如将数据解释为什么类型。

The correct line is:正确的行是:

at::Tensor tensorImg = torch::from_blob(imgMat.data, {1, imgMat.rows, imgMat.cols, imgMat.channels()}, at::kByte);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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