Examine MNIST Dataset from PyTorch Torchvision
Examine the MNIST dataset from PyTorch Torchvision using Python and PIL, the Python Imaging Library
< > Code:
You must be a Member to view code
Access all courses and lessons, gain confidence and expertise, and learn how things work and how to use them.
or Log In
This video will show how to examine the MNIST dataset from PyTorch torchvision using Python and PIL, the Python Imaging Library.
The MNIST dataset is comprised of 70,000 handwritten numerical digit images and their respective labels.
There are 60,000 training images and 10,000 test images, all of which are 28 pixels by 28 pixels.
First, we import PyTorch.
Then we print the PyTorch version we are using.
We are using PyTorch 0.3.1.post2.
Now that we have PyTorch available, let's load torchvision.
Torchvision is a package in the PyTorch library containing computer-vision models, datasets, and image transformations.
Since we want to get the MNIST dataset from the torchvision package, let's next import the torchvision datasets.
import torchvision.datasets as datasets
First, let's initialize the MNIST training set.
mnist_trainset = datasets.MNIST(root='./data', train=True, download=True, transform=None)
We already have the files downloaded, so that was pretty fast.
Next, we initialize the MNIST test set.
mnist_testset = datasets.MNIST(root='./data', train=False, download=True, transform=None)
Now, let's examine what the MNIST train set and the MNIST test set Python variables have.
First, let's check the number of items in each Python variable.
For the MNIST train set, we use the Python len function to get the number of items.
We see that the variable has 60,000 items which is what we expect as the MNIST training set is comprised of 60,000 images.
For the MNIST test set, we again use the Python len function to get the number of items.
We see that the variable has 10,000 items which is what we expect as the MNIST test set is comprised of 10,000 images.
Next, let's explore what those items actually are.
We'll look at the first item in the MNIST train set variable.
We see two things - a PIL image item, then a comma, and a number.
Remember that Python is a zero-based index language so it uses zero instead of the number one.
This is the zero and this is where the PIL image is.
We can check the type of this item to figure out what it is.
It tells us that this is a Python tuple.
Python tuples are a sequence of immutable Python objects.
In this case, it's a two-element sequence where the first element is a PIL image and the second is an integer.
Let's use the Python variable assignment convention to get the image and integer into two separate Python variables.
train_image_zero, train_target_zero = mnist_trainset
The first element of the tuple goes to the Python variable train_image_zero.
The second element of the tuple goes to the Python variable train_target_zero.
Because we're using Python 3.6.4 and Anaconda, and Conda as our package manager, we already have PIL available to us.
So let's look at the image using PIL show operation.
This opens the image viewer on my Mac and shows the train_image_zero image which does indeed look like the handwritten number five.
We can then confirm what the label or target is for this image by checking to see what the train_target_zero Python variable contains.
We get the number five.
Let's do another check, only this time, let's use the test set to see what we get.
test_image_eighty, test_target_eighty = mnist_testset
We look at the image first.
It looks like a handwritten number seven.
Let's check the target now.
And it's the number seven.
We were able to examine the MNIST dataset from PyTorch torchvision using Python and PIL, the Python Imaging Library.