Invalid image shape when using plt.imshow

username: Jonathon_Smith
cooked: <p>I am following the Pytorch tutorial on the DCgan, and was trying to view the output images from the model individually, instead as grid. This is the code used:</p>
<h1>
<a name="generating-new-images-1" class="anchor" href="#generating-new-images-1"></a>generating new images</h1>
<h1>
<a name="sets-a-random-seed-2" class="anchor" href="#sets-a-random-seed-2"></a>sets a random seed</h1>
<p>manualSeed = random.randint(1, 10000) # use if you want new results<br>
print("Random Seed: ", manualSeed)<br>
random.seed(manualSeed)<br>
torch.manual_seed(manualSeed)</p>
<p>fake = gen(torch.randn(1, n_z, 1, 1, device=‘cpu’)).detach()<br>
plt.figure(figsize=(15, 15))<br>
plt.imshow(fake)<br>
…</p>
<p>I am able to view the output as a grid using the following function, but that doesn’t serve my purpose since I would like to save each image later using a loop function.</p>
<p>plt.imshow(np.transpose(vutils.make_grid(fake, padding=2, normalize=True), (1,2,0)))</p>
<p>I was also viewing the tutorial of <a href="https://github.com/ayan-aji-nair/art-generation-gan/blob/main/Notebooks/DCGAN_128.ipynb" rel="noopener nofollow ugc">https://github.com/ayan-aji-nair/art-generation-gan/blob/main/Notebooks/DCGAN_128.ipynb</a> which is an adaptation of Pytorch’s version.</p>

username: ptrblck
cooked: <p><code>make_grid</code> will create a grid using all samples in the current batch and transform it into an image format.<br>
Based on your code I guess you are seeing an error claiming that the input shape is invalid.<br>
If so, this error is expected since <code>matplotlib</code> expects input arrays in an image formar as <code>[height, width, channels]</code> where the <code>channels</code> dimension might be missing in case of a grayscale image.<br>
Your model output should have the shape <code>[batch_size, channels, height, width]</code> so you would have to index the sample you would like to visualize in the batch dimension and <code>permute</code> the image to the aforementioned channels-last format.</p>

username: Jonathon_Smith
cooked: <p>Hi! Thanks for the reply, I permuted the image and am still getting this error:</p>
<p>This is my code:</p>
<p><em># generating new images</em></p>
<p><em># sets a random seed</em></p>
<p><em>manualSeed = random.randint(1, 10000) # use if you want new results</em></p>
<p><em>print("Random Seed: ", manualSeed)</em></p>
<p><em>random.seed(manualSeed)</em></p>
<p><em>torch.manual_seed(manualSeed)</em></p>
<p><em>fake = gen(torch.randn(1, n_z, 1, 1, device=‘cpu’)).detach()</em></p>
<p><em>plt.figure(figsize=(4, 200))</em></p>
<p><em>fake.shape</em></p>
<p><em>plt.imshow(fake.permute(1,2,0))</em></p>
<p>This is the error message:</p>
<h2>
<a name="random-seed-5002-1" class="anchor" href="#random-seed-5002-1"></a>Random Seed:  5002</h2>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in ()<br>
13 plt.figure(figsize=(4, 200))<br>
14 fake.shape<br>
—&gt; 15 plt.imshow(fake.permute(1,2,0))<br>
16<br>
17 <span class="hashtag">#plt</span>.imshow(np.transpose(vutils.make_grid(fake, padding=2, normalize=True), (1,2,0)))</p>
<p><strong>RuntimeError: number of dims don’t match in permute</strong></p>

username: ptrblck
cooked: <p>It seems you forgot to index the sample first before permuting.<br>
Something like this should work: <code>plt.imshow(fake[0].permute(1,2,0))</code></p>

username: Jonathon_Smith
cooked: <aside class="quote no-group" data-username="ptrblck" data-post="4" data-topic="132088">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>[0].</p>
</blockquote>
</aside>
<p>Thanks alot! I can now see the image but it is in black and no information is available. Also, I am unable to find a way to remove the X and Y numbers plotted as I want to generate art using pytorch and the grids are not going away</p>
<p>Code:</p>
<p><em># sets a random seed</em></p>
<p><em>manualSeed = random.randint(1, 10000) # use if you want new results</em></p>
<p><em>print("Random Seed: ", manualSeed)</em></p>
<p><em>random.seed(manualSeed)</em></p>
<p><em>torch.manual_seed(manualSeed)</em></p>
<p><em>plt.imshow(np.transpose(fake[0], (1, 2, 0)))</em></p>
<p>Output</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/6/0/60a1de76823ccb17cc7baba6221c2015204f5923.png" data-download-href="https://discuss.pytorch.org/uploads/default/60a1de76823ccb17cc7baba6221c2015204f5923" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/6/0/60a1de76823ccb17cc7baba6221c2015204f5923_2_690x188.png" alt="image" data-base62-sha1="dMQDFJ1SecsOHwOMfv8QAlC7Vfl" width="690" height="188" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/6/0/60a1de76823ccb17cc7baba6221c2015204f5923_2_690x188.png, https://discuss.pytorch.org/uploads/default/original/3X/6/0/60a1de76823ccb17cc7baba6221c2015204f5923.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/6/0/60a1de76823ccb17cc7baba6221c2015204f5923.png 2x" data-dominant-color="DBDBDB"><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">854×233 38.8 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>

username: ptrblck
cooked: <p>You are receiving a warning that your data will be clipped, which could create the black image.<br>
Check the values in the tensor you would like to visualize and make sure they are either floating point values in <code>[0, 1]</code> or <code>uint8</code> values in <code>[0, 255]</code>.<br>
To remove the axes you could use <code>plt.axis('off')</code> and refer to the <code>matplotlib</code> docs for more information.</p>