简体   繁体   English

如何从 libtorch 中的 Tensor 取回图像?

[英]How to get back the image from Tensor in libtorch?

I am trying to get the image back from the tensor I created earlier and visualize it, However, the resulting image is distorted/garbage it seems.我正在尝试从我之前创建的张量中取回图像并将其可视化,但是,生成的图像似乎失真/垃圾。 This is how I convert a CV_8UC3 into the corresponding at::Tensor :这就是我将CV_8UC3转换为对应at::Tensor的方式:

at::Tensor tensor_image = torch::from_blob(img.data, { img.rows, img.cols, 3 }, at::kByte);

and this is how I convert it back to the image:这就是我将其转换回图像的方式:

auto ToCvImage(at::Tensor tensor)
{
    int width = tensor.sizes()[0];
    int height = tensor.sizes()[1];
    try
    {
        cv::Mat output_mat(cv::Size{ height, width }, CV_8UC3, tensor.data_ptr<int>());
        return output_mat.clone();
    }
    catch (const c10::Error& e)
    {
        std::cout << "an error has occured : " << e.msg() << std::endl;
    }
    return cv::Mat(height, width, CV_8UC3);
}

This is how the original image looks like:这是原始图像的样子:

在此处输入图像描述

and this is what I get after conversion:这就是我转换后得到的:

在此处输入图像描述

Now if I use at::kInt instead of kByte during the creation of the tensor:现在,如果我在创建张量期间使用at::kInt而不是kByte

at::Tensor tensor_image = torch::from_blob(img.data, { img.rows, img.cols, 3 }, at::kByte);

I no longer get the distorted image, however, the network output will be off which means something has gone wrong in the input!我不再得到扭曲的图像,但是,网络 output 将关闭,这意味着输入出现问题!

What's the issue here and how should I be going about this?这里有什么问题,我应该怎么做?

When the tensor was created using a c10::kByte for the cast we need to use uchar and not char or uint , etc. so in order to get this fixed I only had to use uchar instead of int :当使用c10::kByte为演员创建张量时,我们需要使用uchar而不是charuint等,所以为了解决这个问题,我只需要使用uchar而不是int

auto ToCvImage(at::Tensor tensor)
{
    int width = tensor.sizes()[0];
    int height = tensor.sizes()[1];
    try
    {
        cv::Mat output_mat(cv::Size{ height, width }, CV_8UC3, tensor.data_ptr<uchar>());
        return output_mat.clone();
    }
    catch (const c10::Error& e)
    {
        std::cout << "an error has occured : " << e.msg() << std::endl;
    }
    return cv::Mat(height, width, CV_8UC3);
}

Side note: In case you created your Tensor with any other type, make sure to use Tensor::totype() effectively and convert to the proper type before hand.旁注:如果您使用任何其他类型创建了张量,请确保有效地使用Tensor::totype()并事先转换为正确的类型。 That is before I feed this tensor to my network, eg I convert it to KFloat and then carry on!那是在我将此张量提供给我的网络之前,例如,我将其转换为KFloat然后继续! its an obvious point that may very well be neglected and cost you hours of debugging!这是一个很明显的点,很可能会被忽略并花费您数小时的调试时间!

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

相关问题 LibTorch C++:将张量转换回图像,结果是 3x3 网格? - LibTorch C++: Converting Tensor back to Image, Result is 3x3 Grid? 你如何在 LibTorch 中转置张量? - How do you transpose a tensor in LibTorch? Libtorch:如何从 tensorRT fp16 半类型指针创建张量? - Libtorch:how to create tensor from tensorRT fp16 half type pointer? 从 openCV 读取 / 到 libtorch 张量 c++ 的签名整数 - reading signed ints from openCV in/to libtorch tensor c++ 如何在 LibTorch 中对张量中的数字使用更大/更小运算符? - How to use a greater / lesser operator for numbers in a tensor in LibTorch? 如何将 cv::Mat 转换为 torch::Tensor 并将其提供给 libtorch 模型? - How to convert cv::Mat to torch::Tensor and feed it to libtorch model? 如何在 libtorch 中堆叠形状为 (n, k) 的张量和形状为 (k) 的张量? - How to stack a tensor of shape (n, k) with tensors of shape (k) in libtorch? libtorch:如何基于 data_ptr 创建一个 gpu 张量? - libtorch : How to create a gpu tensor base on data_ptr? libtorch:为什么我的张量在从一种方法返回到另一种方法时会更改值? - libtorch: Why does my Tensor change value when returned from a method into another method? 如何使用 protobuf 序列化 tuple(std::string, torch::Tensor) 的 libtorch 向量? - How to serialize a libtorch vector of tuple(std::string, torch::Tensor) using protobuf?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM