Shows image with specific index from MNIST dataset

In case you didn’t use any transformations, you’ll get PIL.Images and can show them directly.
If you transformed the images into tensors, you could also use matplotlib to display the images.
Here are the two ways to show the images:

# 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')
3 Likes