How to display incorrect samples predicted by the model?

username: HarshRangwala
cooked: <p>I have written a snipet that should output 10 images that were misclassified by my model.</p>
<pre><code class="lang-auto">fig = plt.figure(figsize=(20, 8))

for idx in np.arange(10):
    ax = fig.add_subplot(2, 10/2, idx+1, xticks=[], yticks=[])
    #std = np.array([0.229, 0.224, 0.225])
    #mean = np.array([0.485, 0.456, 0.406])
    img = incorrect_examples[idx][idx]
    img = img/2 + 0.5
    img = np.clip(img, 0, 1)
    ax.imshow(img)
    ax.set_title(f"{classes[pred[idx]]}: x%\n (label: {classes[target[idx]]})",
                 color=("green" if pred[idx] == target[idx].item() else "red"))
</code></pre>
<p>I get the following error:</p>
<pre><code class="lang-auto">---&gt; 10     ax.imshow(img)
     11     ax.set_title(f"{classes[pred[idx]]}: x%\n (label: {classes[target[idx]]})",
     12                  color=("green" if pred[idx] == target[idx].item() else "red"))

4 frames
/usr/local/lib/python3.7/dist-packages/matplotlib/image.py in set_data(self, A)
    697                 or self._A.ndim == 3 and self._A.shape[-1] in [3, 4]):
    698             raise TypeError("Invalid shape {} for image data"
--&gt; 699                             .format(self._A.shape))
    700 
    701         if self._A.ndim == 3:

TypeError: Invalid shape (1, 3, 224, 224) for image data
</code></pre>

username: Epoching
cooked: <aside class="quote no-group" data-username="HarshRangwala" data-post="1" data-topic="125373">
<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/harshrangwala/48/25088_2.png" class="avatar"> HarshRangwala:</div>
<blockquote>
<p><code>Invalid shape (1, 3, 224, 224) for image data</code></p>
</blockquote>
</aside>
<p>That first dimension should be squeezed out as an image should have 3 dimensions: number of channels, height, and width. (i.e. <code>(3, 224, 224)</code>).</p>
<p>Try <code>img = img.squeeze()</code> before calling <code>ax.imshow(img)</code></p>

username: HarshRangwala
cooked: <p>Tried it. I get this:</p>
<pre><code class="lang-auto">img = incorrect_examples[0][idx]
----&gt; 9     img = img.squeeze(0)
     10     img = img/2 + 0.5
     11     img = np.clip(img, 0, 1)

ValueError: cannot select an axis to squeeze out which has size not equal to one
</code></pre>

username: Epoching
cooked: <p>What’s the shape? <code>print(img.shape)</code></p>
<p>Ideally you want something like this:<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/3/6/361a69e85b0e7333a835c27a86c2a522a11380f0.png" alt="image" data-base62-sha1="7ICndzDJhSG6MlAQ1cUZBChP5e0" width="275" height="212"></p>

username: HarshRangwala
cooked: <p>Okay thanks I got rid of the problem.</p>

username: HarshRangwala
cooked: <p>But I still cant get the images to be disp[layed in group. Can you provide me with a boiler code that will help me display 10 images.<br>
The <code>incorrect_examples</code> is a (list) array of images which can be accessed by <code>incorrect_examples[0][idx]</code></p>

username: Epoching
cooked: <p>Be sure to mark the correct answer in case others run into this too!</p>

username: Epoching
cooked: <p>If you have a list of image tensors you can use this handy util from <code>torchvision</code>: <code>torchvision.utils.make_grid</code> <a href="https://pytorch.org/vision/stable/utils.html#torchvision.utils.make_grid" rel="noopener nofollow ugc">torchvision.utils — Torchvision 0.10.0 documentation (pytorch.org)</a>.</p>
<p>It takes in a list of image tensors and makes a nice grid of images for you, one of the most convienent utils I’ve used for displaying multiple images.</p>

username: Epoching
cooked: <p>Here’s an example:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/6/b688ee33b69d4cc5bf8aa38df69e90fb387ccf9e.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/b688ee33b69d4cc5bf8aa38df69e90fb387ccf9e" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/b/6/b688ee33b69d4cc5bf8aa38df69e90fb387ccf9e_2_585x500.jpeg" alt="image" data-base62-sha1="q2MgQR4DWAw8SFhDbutvLiMg3Bs" width="585" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/b/6/b688ee33b69d4cc5bf8aa38df69e90fb387ccf9e_2_585x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/b/6/b688ee33b69d4cc5bf8aa38df69e90fb387ccf9e_2_877x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/b/6/b688ee33b69d4cc5bf8aa38df69e90fb387ccf9e.jpeg 2x" data-dominant-color="BDBDBD"><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">997×852 628 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: HarshRangwala
cooked: <p>Cool, I will check it out. Thank you.</p>