I am working on a GAN and cannot make it work to save images that I transformed into tensors back to “normal” pngs within a loop. The same goes for the tensors that are generated by the Generator.
I applied the following transformation for the original images I am using for the training in the GAN ( I hope i did it the right way):
transform = transforms.Compose(
[
transforms.ToPILImage(),
transforms.Resize(img_size),
transforms.CenterCrop(img_size),
transforms.ToTensor(),
transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5]),
]
)
When trying to save the tensors as png images in a loop with the following code they do not come out the right way:
real_samples = next(iter(train_loader))
for i in range(4):
torchvision.utils.save_image(real_samples[i, :, :, :],
‘Real_Images/real_image{}.png’.format(i))
On the left is an example of the original image after transformation and on the right an example of the “wrongly” saved ones:
Can anyone please help me out with saving the images in the right way?
You apply normalization with mean 0.5 and std 0.5, so your images are transformed from range (0., 1.) to (-1., 1.). You should denormalize them and bring back to the original range before saving them.
In your case, simply doing
real_samples = real_samples * 0.5 + 0.5
before saving should work.