Show the classified images after the Network being trained?

Daniel_Joseph: <p>Hello everyone.<br>
Is it possible to generate the result of the trained network, i.e show the classified images after training on pytorch<br>
Any help much appreciated<br>
Many thanks</p>

ptrblck: <p>Would you like to classify and visualize unseen test images using your trained model?<br>
If so, the <a href="https://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html" rel="nofollow noopener">Training a Classifier Tutorial</a> might give you some ideas to write the code. In the tutorial, images will also be validated, visualized, and the corresponding class printed.<br>
Let me know, if I misunderstood your use case.</p>

Daniel_Joseph: <p>Thanks a lot, Ptrbck , yeah what I mean is that after training the network I would like to visualize the result of the classification (Validation), the images look like this  <img src="https://www.hyspex.no/images/Kube.jpg" alt="" role="presentation" width="" height=""><br>
its kinda pixel-wise classification thus i want to regenerate these results found in some paper :<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/6/60e1182670b1907dace1d7a5bc48ca0451b60f27.png" data-download-href="https://discuss.pytorch.org/uploads/default/60e1182670b1907dace1d7a5bc48ca0451b60f27" title="result%20of%20hsi"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/6/60e1182670b1907dace1d7a5bc48ca0451b60f27_2_690x444.png" alt="result%20of%20hsi" data-base62-sha1="dP26aGK7y5OLXbY2jFhclAyzvVl" width="690" height="444" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/6/60e1182670b1907dace1d7a5bc48ca0451b60f27_2_690x444.png, https://discuss.pytorch.org/uploads/default/original/2X/6/60e1182670b1907dace1d7a5bc48ca0451b60f27.png 1.5x, https://discuss.pytorch.org/uploads/default/original/2X/6/60e1182670b1907dace1d7a5bc48ca0451b60f27.png 2x" data-dominant-color="918A95"><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">result%20of%20hsi</span><span class="informations">763×492 209 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>

ptrblck: <p>It looks like you are dealing with a segmentation task.<br>
So I assume your output would have the shape <code>[batch_size, nb_classes, height, width]</code>, is that correct?<br>
If so, you can get the predicted class index using <code>pred = torch.argmax(output, 1)</code>.<br>
Using the <code>pred</code> tensor, you should be able to create these color images using a mapping from class index to color code.<br>
If you don’t have the mapping yet, I would suggest to create a <code>dict</code> with some random mapping to your desired colors.</p>

Daniel_Joseph: <p>Indeed, it is a segmentation task I do have the groundtruth, is it possible to visualize the output then?</p>

ptrblck: <p>Yes, as I said you can get the predictions using <code>torch.argmax</code> and try to visualize it directly or using a color mapping scheme:</p>
<pre><code class="lang-python">output = torch.randn(1, 10, 24, 24)
pred = torch.argmax(output, 1)
plt.imshow(pred[0])
</code></pre>

isalirezag: <p>Can you tell how did you plot this image in 3D like that? <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"></p>

Daniel_Joseph: <p>where should I add it inside which part, <img src="https://discuss.pytorch.org/images/emoji/apple/shushing_face.png?v=12" title=":shushing_face:" class="emoji" alt=":shushing_face:" loading="lazy" width="20" height="20"> got confused Ptrblck,</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/5/5028338074b4c97b463225c9202a59d1a9c30e3e.png" data-download-href="https://discuss.pytorch.org/uploads/default/5028338074b4c97b463225c9202a59d1a9c30e3e" title="test00001"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/5/5028338074b4c97b463225c9202a59d1a9c30e3e_2_690x213.png" alt="test00001" data-base62-sha1="br6klQAboP6azoPAJCyo4aZA3jE" width="690" height="213" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/5/5028338074b4c97b463225c9202a59d1a9c30e3e_2_690x213.png, https://discuss.pytorch.org/uploads/default/optimized/2X/5/5028338074b4c97b463225c9202a59d1a9c30e3e_2_1035x319.png 1.5x, https://discuss.pytorch.org/uploads/default/original/2X/5/5028338074b4c97b463225c9202a59d1a9c30e3e.png 2x" data-dominant-color="252525"><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">test00001</span><span class="informations">1077×333 42.9 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><br>
<img src="https://discuss.pytorch.org/images/emoji/apple/thinking.png?v=12" title=":thinking:" class="emoji only-emoji" alt=":thinking:" loading="lazy" width="20" height="20"></p>

Daniel_Joseph: <p>you can use Matlab to plot it, its a special type of image called “Hyperspectral”, and these colored images are only the labeled data.</p>

ptrblck: <p>You could add it directly after <code>output = model(data)</code>.<br>
The better approach would probably be to store all predictions in a list and plot some images after the test loop finshes:</p>
<pre><code class="lang-python">preds = []
for data, target in test_loader:
    ...
    output = model(data)
    pred = torch.argmax(output.detach(), 1)
    preds.append(pred)
    ...

nb_imgs_to_plot = 4
fig, axarr = plt.subplots(4)
for idx, img in enumerate(preds[:nb_imgs_to_plot]):
    axarr[idx].imshow(img.numpy())
</code></pre>

Maria_Fernanda_De_La: <p>Hello, in this tutorial the images appear blurry. Is there a way to fix that?</p>

ptrblck: <p>CIFAR10 images have a spatial resolution of <code>32x32</code> pixels, so they are kind of blurry unfortunately. <img src="https://discuss.pytorch.org/images/emoji/apple/confused.png?v=9" title=":confused:" class="emoji" alt=":confused:"><br>
You could try to interpolate them to a higher resolution, but I’m not sure if that’s really useful.</p>