如何在Pythorch中保存灰度图像?

2024-09-25 06:36:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在Pytorch中保存灰度图像,每个图像有四个灰度值,060120和{}。我尝试以下方法保存图像,但保存的图像不是我所期望的。在

for i, (inputs) in enumerate(test_generator):
    pred = modelPl(inputs.float()).detach()
    fig,ax = plt.subplots(1,1,figsize = (5,5))
    ax.imshow(pred[0,:,:], cmap = "gray")
    print(pred.shape)
    torchvision.utils.save_image(pred, saveTestPath + 'img_{0}.png'.format(i)) 

Output:torch.Size([400, 400])

Expected image:

enter image description here

但以下图片不正确:

enter image description here


Tags: 方法intest图像imageforpytorchax
1条回答
网友
1楼 · 发布于 2024-09-25 06:36:08

可能是torchvision.utils.save_image要求值在0到1的范围内。图像的值大于1,因此出现问题。在

您可以通过将张量除以255(或一些适当的数字)来检查这一点。您还可以尝试设置normalize=True,看看它是否可以自动规范化数据。在

相关问题 更多 >