In the DCGAN example, there has the code as follows:
fake = netG(fixed_noise)
vutils.save_image(fake.data,
‘%s/fake_samples_epoch_%03d.png’ % (opt.outf, epoch),
normalize=True)
The range of the output of the netG is [-1,1], however, the source code of the save_image function is as follows:
def save_image(tensor, filename, nrow=8, padding=2,
normalize=False, range=None, scale_each=False, pad_value=0):
“”"Save a given Tensor into an image file.
Args:
tensor (Tensor or list): Image to be saved. If given a mini-batch tensor,
saves the tensor as a grid of images by calling make_grid.
**kwargs: Other arguments are documented in make_grid.
“”"
from PIL import Image
tensor = tensor.cpu()
grid = make_grid(tensor, nrow=nrow, padding=padding, pad_value=pad_value,
normalize=normalize, range=range, scale_each=scale_each)
ndarr = grid.mul(255).clamp(0, 255).byte().permute(1, 2, 0).numpy()
im = Image.fromarray(ndarr)
im.save(filename)
Obviously, the negative values in the fake(fake = netG(fixed_noise)) will be set to 0 based on the vutils.save_image function. I think this operation could cause a mistake. In my view, the correct code is as follows:
fake = netG(fixed_noise)
fake=(fake+1)/2.0
vutils.save_image(fake.data,
‘%s/fake_samples_epoch_%03d.png’ % (opt.outf, epoch),
normalize=True)