简体   繁体   English

将 Pytorch 4D 张量另存为图像

[英]Save Pytorch 4D tensor as image

I have a 4-d Pytorch tensor that I would like to save to disk as a .jpg我有一个 4-d Pytorch 张量,我想以 .jpg 格式保存到磁盘

My tensor is the following size:我的张量大小如下:

print(image_tensor.size())

>>>torch.Size([1, 3, 400, 711])

I can view the entire tensor as one image within my IDE:我可以在 IDE 中将整个张量视为一个图像:

ax1.imshow(im_convert(image_tensor))

Since I am able to view the entire tensor as one image, I am assuming there is a way to also save it as such.由于我能够将整个张量视为一个图像,因此我假设有一种方法也可以将其保存为这样。 However, when I try to save the image, it looks like it only saves the blue color channel.但是,当我尝试保存图像时,它看起来只保存了蓝色通道。 I would like to save the entire tensor as a single image.我想将整个张量保存为单个图像。

img1 = image_tensor[0]
save_image(img1, 'img1.jpg')

In PyTorch this snippet is working and saving the image:在 PyTorch 中,此代码段正在运行并保存图像:

from torchvision.utils import save_image
import torch
import torchvision

tensor= torch.rand(2, 3, 400, 711) 

img1 = tensor[0]
save_image(img1, 'img1.png')

Before saving the image can you check the shape of the img1 in any case something happened.在保存图像之前,您可以检查img1的形状,以防万一发生了什么。

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

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