How to get individual image in DCGAN tutorial

Koon: <p>I’m trying to run DCGAN tutorial. Great guide, but I have a problem.</p>
<p>I want to use the image which G generated. But the tutorial show a grid combine with 64 individual image.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/0/07ef902225b18cc027e506d5a931adc56d00c3a9.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/07ef902225b18cc027e506d5a931adc56d00c3a9" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/0/07ef902225b18cc027e506d5a931adc56d00c3a9_2_500x500.jpeg" alt="image" data-base62-sha1="18cBqChjyU5zRNIelr5R3J7xaLn" width="500" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/0/07ef902225b18cc027e506d5a931adc56d00c3a9_2_500x500.jpeg, https://discuss.pytorch.org/uploads/default/original/2X/0/07ef902225b18cc027e506d5a931adc56d00c3a9.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/2X/0/07ef902225b18cc027e506d5a931adc56d00c3a9.jpeg 2x" data-dominant-color="77645B"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">540×539 192 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>How can I get the individual image in original resolution?</p>
<p>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.</p>
<pre><code class="lang-auto"># 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))
</code></pre>
<p>Thanks for your help!!</p>

ptrblck: <p>The generated images are returned in <code>fake</code>, which should hold all images in dim0.<br>
Try to slice this tensor to get individual images:</p>
<pre><code class="lang-python">image0 = fake[0]
image1 = fake[1]
</code></pre>
<p>Alternatively, if you just want to generate one image, you could just change the batch size of <code>fixed_noise</code> to 1.</p>

Koon: <p>Thanks for your help.</p>
<p>I have tried to show the image by the code below.</p>
<pre><code class="lang-auto">image0 = fake[0]

plt.imshow(np.transpose(image0,(1,2,0))) 
plt.show()
</code></pre>
<p>It shows a image with the warning</p>
<blockquote>
<p>Clipping input data to the valid range for imshow with RGB data ([0…1] for floats or [0…255] for integers).</p>
</blockquote>
<p>and the image doesn’t seems like the correct image. Most value of the tensor are negative value. Did I do any thing wrong?</p>

ptrblck: <p>In your example the images are normalized, which is what you would need to do.<br>
You could use <code>torchvision.transforms.ToPILImage</code> or alternatively normalize your generated image by yourself. Basically you would have to subtract the minimum and divide by the maximal pixel value.<br>
<code>make_grid</code> uses it <a href="https://github.com/pytorch/vision/blob/4c9c40747feae3fead77d06adb0dd60ff9f61710/torchvision/utils.py#L61">here</a>.</p>

hahaau2020: <p>How can I generate more images, e.g., 1000 fake images from the DCGAN?</p>

Jared_Bance: <p>Use this line in that if statement:</p>
<pre><code class="lang-auto">vutils.save_image(vutils.make_grid(fake[0], padding=2, normalize=True), "image" + str(iters) + ".jpg")
</code></pre>

Muhammad_Muneeb_Saad: <p>Hi,<br>
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 ?</p>
<p>Thanks in anticipation…</p>