How to get individual image in DCGAN tutorial

I’m trying to run DCGAN tutorial. Great guide, but I have a problem.

I want to use the image which G generated. But the tutorial show a grid combine with 64 individual image.

How can I get the individual image in original resolution?

I guess I can get the image somewhere in the [Train the Generator] part. Listed below. But I can not figure out the exactly variable that image stored. I want to get the G’s output.

# Check how the generator is doing by saving G's output on fixed_noise
        if (iters % 500 == 0) or ((epoch == num_epochs-1) and (i == len(dataloader)-1)):
            with torch.no_grad():
                fake = netG(fixed_noise).detach().cpu()
            img_list.append(vutils.make_grid(fake, padding=2, normalize=True))

Thanks for your help!!

The generated images are returned in fake, which should hold all images in dim0.
Try to slice this tensor to get individual images:

image0 = fake[0]
image1 = fake[1]

Alternatively, if you just want to generate one image, you could just change the batch size of fixed_noise to 1.

Thanks for your help.

I have tried to show the image by the code below.

image0 = fake[0]


It shows a image with the warning

Clipping input data to the valid range for imshow with RGB data ([0…1] for floats or [0…255] for integers).

and the image doesn’t seems like the correct image. Most value of the tensor are negative value. Did I do any thing wrong?

In your example the images are normalized, which is what you would need to do.
You could use torchvision.transforms.ToPILImage or alternatively normalize your generated image by yourself. Basically you would have to subtract the minimum and divide by the maximal pixel value.
make_grid uses it here.

How can I generate more images, e.g., 1000 fake images from the DCGAN?

Use this line in that if statement:

vutils.save_image(vutils.make_grid(fake[0], padding=2, normalize=True), "image" + str(iters) + ".jpg")

How can I save generated images individually in the local folder (like 100 images… not in a grid) using the same above DCGAN_pytorch code on Colab please ?

Thanks in anticipation…