简体   繁体   English

pytorch将4D张量的小批量保存为图像

[英]pytorch save a minibatch of 4D tensor as images

I have set of 8 tensors in 4D tensor of shape (B, C, H, W). 我有4个张量的4个张量(B,C,H,W)。 More specifically, the shape of my tensor is (8,3,64,64) ie 8 images with [3x64x64] format. 更具体地说,我的张量的形状是(8,3,64,64),即具有[3×64×64]格式的8个图像。

I would like to save these in 1.png, 2.png ..., 8.png etc. When I try torchvision.utils.save_images(my_tensor) , this is working fine. 我想在1.png,2.png ...,8.png等中保存这些。当我尝试使用torchvision.utils.save_images(my_tensor) ,这很好用。 However, that is saving the images in a single grid instead of individual images. 但是,这是将图像保存在单个网格中而不是单个图像中。

How can I fix this issue? 我该如何解决这个问题?

I tried below code but that did not work. 我试过下面的代码但是没有用。

for i in range(tensor.size(0)):

        np_data = tensor.cpu().numpy()
        imgplot = plt.imshow(np_data)

If you want to save individual images you could use: 如果要保存单个图像,可以使用:

for i in range(tensor.size(0)):
           torchvision.utils.save_image(tensor[i, :, :, :], '{}.png'.format(i))

which will save as : 1.png, 2.png ..., 8.png 这将保存为:1.png,2.png ...,8.png

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

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