Shows image with specific index from MNIST dataset

Neda: <p>I am trying to show an image from MNIST dataset. I found in Keras is like the following. How can I do it in Pytorch?</p>
<pre><code class="lang-auto">import matplotlib.pyplot as plt

%matplotlib inline # Only use this if using iPython
image_index = 7777 # You may select anything up to 60,000
print(y_train[image_index]) # The label is 8
plt.imshow(x_train[image_index], cmap='Greys')

</code></pre>

ptrblck: <p>In case you didn’t use any transformations, you’ll get <code>PIL.Images</code> and can show them directly.<br>
If you transformed the images into <code>tensors</code>, you could also use <code>matplotlib</code> to display the images.<br>
Here are the two ways to show the images:</p>
<pre><code class="lang-python"># As PIL.Image
dataset = datasets.MNIST(root='PATH')
x, _ = dataset[7777]
x.show() # x is a PIL.Image here

# As torch.Tensor
dataset = datasets.MNIST(
    root='PATH',
    transform=transforms.ToTensor()
)

x, _ = dataset[7777] # x is now a torch.Tensor
plt.imshow(x.numpy()[0], cmap='gray')
</code></pre>

redtailedhawk: <p>hi, first time writing in this forum. <a class="mention" href="/u/ptrblck">@ptrblck</a> , I love your solutions, you’ve always got great ones, this one included. Here’s a question - who or what on earth does .numpy() belong to? I can’t find any documentation on it, and it works without numpy being imported, and most of all - what is it doing so that I can show my image with imshow? what is the 0th element?</p>
<p>Second part to this question: If I leave out cmap=‘gray’ I get this green and purple thing. What am I seeing there?</p>
<p>Thank you</p>

ptrblck: <p>I’m glad you like it here. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>
<p>You can find the documentations for <code>tensor.numpy</code> <a href="https://pytorch.org/docs/stable/tensors.html?highlight=numpy#torch.Tensor.numpy">here</a>.<br>
It gives you a numpy array sharing the same storage as your tensor, which is really cheap and does not include a copy (at least if your tensor is already on the CPU).</p>
<p>PyTorch uses numpy internally as a dependency to provide this method, so you don’t need to manually import it.</p>
<p>The 0th element is used to get the “0th channel”, since <code>dataset[7777]</code> will return the sample as <code>[channels, height, width]</code>. In the case of MNIST images, we are using a single channel (<code>[1, 28, 28]</code>), so we just index it with 0. Otherwise <code>matplotlib</code> will be confused.</p>
<p><code>matplotlib</code> uses a default colormap for single channel images (I think ‘viridis’ if I’m not mistaken).<br>
This is supposed to give you better visual information about your matrix (smaller changes should be better visible than using a grayscale colormap).<br>
However, since we are dealing with grayscale images, we can just force <code>matplotlib</code> to plot the matrix as we want. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

redtailedhawk: <p>Ah OK, so it does come from PyTorch. Great, I was wondering how it was working.</p>
<p>Also thank you for that bit of info about the colormap. I actually like the way it looks.</p>
<p>Thanks for always have a good answer!</p>

Lightning_Blade: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a>. What is the purpose of the underscoe “_” in your code if you don’t mind?</p>

harsha_g: <p><code>_</code> is a convention used in python for storing values that are not used (aka <em>throwaway</em> variables). More specifically, in this case, <code>dataset[7777]</code> returns two values the first of which is assigned to <code>x</code> and the second is put into <code>_</code> as it is not needed.</p>

cad: <p>Is there a way to extract each grayscale value of the mnist images. I couldn’t find a library function or anything in the tensor that can extract the pixel values, sorry if this is a dumb question but I can’t find an answer right now.</p>

ptrblck: <p>You can directly index the tensor to access the pixel values.</p>

cad: <p>I have a tensor with each image in the training set and I want to look at the pixel values on each of those images</p>

ptrblck: <p>Could you post a (slow) pseudo code showing what exactly you want to visualize and why the suggested indexing might not work?</p>