Plotting random image in validation set for GANs

Hi,

I’m training a GAN to upscale low resolution image to high resolution image. For my validation set, I want to randomly plot images, but I always get the same one. My dataloader for my validation set has batch size one.

i_val = 0
for _,imgs in enumerate(val_loader):
            i_val +=1
            imgs_lr = Variable(imgs["lr"].type(Tensor))
            imgs_hr = Variable(imgs["hr"].type(Tensor))
            # VISUALIZING when  for loop is done
              if (i_val % len(val_loader) == 0):
                        with torch.no_grad():
                              index = torch.tensor([np.random.randint(imgs_lr.shape[0], size=1)[0]]).to(cfg.device)
                              lr_i = torch.index_select(imgs_lr, 0, index, out=None)
                            


Since your batch size is 1, I assume imgs_lr.shape[0] is also 1.
If that’s the case, np.random.randint(imgs_lr.shape[0], size=1)[0] will always return a 0, which would explain the same plotted image.

Yeah that makes sense. It looks like my if statement will only lead me to plot out the last image. Is there any workaround where I can plot a random image instead?

You could sample some variable from [0, len(val_loader)-1] and compare it directly to i_val inside the data loader loop.
Then just use the same variable to get the corresponding image.

Alternatively, you could also remove the plotting completely from the data loader loop and just sample from the Dataset directly using a random number.

What a splendid idea! Continue being awesome :slight_smile:

1 Like