简体   繁体   English

LibTorch C++:将张量转换回图像,结果是 3x3 网格?

[英]LibTorch C++: Converting Tensor back to Image, Result is 3x3 Grid?

I am working on some code that takes a matrix object (assume this behaves as cv::Mat), converts to tensor, does a forward pass through my model, and converts back to my matrix object.我正在处理一些代码,它采用矩阵 object(假设这表现为 cv::Mat),转换为张量,通过我的 model 进行正向传递,然后转换回我的矩阵 ZA8CFDE6331BD49EB2AC96F8661 However I have one issue persisting.但是我有一个问题仍然存在。 That is, my resulting matrix is a 3x3 grid of the result.也就是说,我得到的矩阵是结果的 3x3 网格。 I tested my conversion codes (Matrix to Tensor and back) by just passing the image through both, and the resulting image is correct.我通过将图像传递给两者来测试我的转换代码(矩阵到张量并返回),结果图像是正确的。 This leads me to believe it is something with how the forward pass creates your output tensor?这让我相信这与前向传递如何创建您的 output 张量有关? How should I work with the output tensor to fix the problem?我应该如何使用 output 张量来解决问题?

Code for context上下文代码

Matrix to Tensor:矩阵到张量:

int numel = rows * cols * depth;
assert(numel > 0);

tensor_image = torch::zeros({ rows, cols, depth }, torch::kFloat);

std::memcpy(tensor_image.data_ptr<float>(), Image.GetConstDataPtr(), sizeof(float) * numel);

tensor_image = tensor_image.permute({ 2, 0, 1 }).unsqueeze(0);

Forward Pass:前传:

at::Tensor output = torch::zeros({ tensor_image.sizes()[0], tensor_image.sizes()[1], tensor_image.sizes()[2], tensor_image.sizes()[3] }, torch::kFloat);

device = torch::kCUDA;

// Move model to GPU
module.to(device);

// Move target to gpu 
tensor_image = tensor_image.to(device);
// Execute the model and turn its output into a tensor.
at::Tensor output = module.forward({ tensor_image }).toTensor();

output = output.to(torch::kCPU);

Tensor to Matrix:张量到矩阵:

int numel = height * width * depth;
assert(numel > 0);

Image.Resize(height, width, depth);

at::Tensor tensor_image_cpy = tensor_image.squeeze(0).permute({ 1, 2, 0 });

std::memcpy(Image.GetDataPtr(), tensor_image_cpy.data_ptr<float>(), sizeof(float) * numel);

I have found the solution to my problem.我找到了解决我的问题的方法。 Citing yugo's answer to this question:引用 yugo 对这个问题的回答:

Convert pytorch tensor to opencv mat and vice versa in C++ 在 C++ 中将 pytorch 张量转换为 opencv 垫子,反之亦然

You need to make sure to reshape the data:您需要确保重塑数据:

tensor_image_cpy = tensor_image.reshape({ width * height * depth });

Hope this helps others who run into this issue.希望这可以帮助遇到此问题的其他人。

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

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