I am trying to implement a simple GAN in Google Colaboratory, After using transforms to normalize the images, I want to view it at the output end to display fake image generated by the generator and real image side by in the dataset once every batch iteration like a video.
transform = transforms.Compose(
[
# Convert a PIL Image or numpy.ndarray to tensor. This transform does not support torchscript.
# Converts a PIL Image or numpy.ndarray (H x W x C) in the range
# [0, 255] to a torch.FloatTensor of shape (C x H x W) in the range [0.0, 1.0]
transforms.ToTensor(),
# Normalize a tensor image with mean and standard deviation.
transforms.Normalize((0.5,),(0.5,))
])
dataset = datasets.MNIST(root="dataset/", transform=transform, download=True)
loader = DataLoader(dataset, batch_size=batch_size, shuffle=True)
After applying transforms on the dataset it is not in the range of [0,255] anymore. How do we denormalize it and use cv2_imshow to show that series of real and fake images frame by frame in the same place?
The above image shows the output I get, there are two problems here.
What approach do I take to solve these issues?
Problem 1
Assuming torch_image is a torch.FloatTensor of shape (C x H x W) in the range [0.0, 1.0]:
numpy_image = torch_image.permute(1, 2, 0).numpy() * 255
You can then display numpy_image with cv2.
Problem 2
If you want to refresh the printed images instead of printing new ones, you might try the solution provided here: https://stackoverflow.com/a/52866695/12463260