0

I am trying to implement a simple GAN in google collaboratory, 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?

enter image description here

The above image shows the output I get, there are two problems here.

  1. The image normalization, rendered the image indistinguishable, it is just all black.
  2. The images are not coming frame by frame in the same place like a video, instead, it is getting printed in a new line.

What approach do I take to solve these issues?

amalp12
  • 185
  • 2
  • 10

2 Answers2

1

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

  • Found the solution to problem 1, I didn't denormalize. ` pixels = ((x *.5)+.5)*255 ` The above formula did the trick, to covert it back to the range [0,255]. Thanks a lot for your answer. I didn't find any solution for problem 2 yet. – amalp12 Jun 01 '21 at 09:00
0

Found that I didn't denormalize.

def denormalize(x):
  # Denormalizeing 
  pixels =  ((x *.5)+.5)*255
  return pixels

The above function did, to convert it back to the range [0,255].

I didn't find any solution for problem 2 yet.

amalp12
  • 185
  • 2
  • 10