我想在Pytorch中保存灰度图像,每个图像有四个灰度值,0
,60
,120
和{
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:
但以下图片不正确:
可能是
torchvision.utils.save_image
要求值在0到1的范围内。图像的值大于1,因此出现问题。在您可以通过将张量除以
255
(或一些适当的数字)来检查这一点。您还可以尝试设置normalize=True
,看看它是否可以自动规范化数据。在相关问题 更多 >
编程相关推荐