英文:
How to convert a four dimensional Tensor to image by PIL?
问题
例如,如果我的张量是:
t1 = torch.randn(1, 3, 256, 256) # batch_size/ch/height/width
可以使用 squeeze()
轻松将其转换为一张图像:
import torchvision.transforms as T
transform = T.ToPILImage()
one_img = transform(t1.squeeze())
one_img.save("test1.jpg")
问题是,如果批处理大小大于一,我想知道是否有类似于以下的PyTorch函数:
t1 = torch.randn(5, 3, 256, 256)
print(t1.shape)
for i in range(t1.size(0)):
one_tensor = t1[i] #(3, 256, 256)
one_img = transform(one_tensor)
one_img.save(str(i) + ".jpg")
英文:
For example, if my tensor is
t1 = torch.randn(1,3,256,256) #batch_size/ch/height/width
it is easily to convert to one image by squeeze()
import torchvision.transforms as T
transform = T.ToPILImage()
one_img = transform(t1.squeeze())
one_img.save("test1.jpg")
The problem is if batch_size is is more than one, I was wondering if there is for function in pytorch like,
t1 = torch.randn(5,3,256,256)
print(t1.shape)
for i in range(t1[0]):
one_tensor = t1[i] #(3,256,256)
one_img = transform(one_tensor)
one_img.save(i + ".jpg")
答案1
得分: 0
只需执行以下操作:
for i in range(len(t1)):
one_tensor = t1[i]
one_img = transform(one_tensor)
one_img.save(str(i) + ".jpg")
英文:
Just do this:
for i in range(len(t1)):
one_tensor = t1[i]
one_img = transform(one_tensor)
one_img.save(str(i) + ".jpg")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论