PyTorch Stack: Turn A List Of PyTorch Tensors Into One Tensor 3:03

Examine the MNIST dataset from PyTorch Torchvision using Python and PIL, the Python Imaging Library

< > Code:

Transcript:

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.

import torch

Then we print the PyTorch version we are using.

print(torch.__version__)

We are using PyTorch 0.3.1.post2.

Now that we have PyTorch available, let's load torchvision.

import 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.

len(mnist_trainset)

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.

len(mnist_testset)

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.

mnist_trainset[0]

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.

type(mnist_trainset[0])

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[0]

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.

train_image_zero.show()

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.

print(train_target_zero)

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[79]

We look at the image first.

test_image_eighty.show()

It looks like a handwritten number seven.

Let's check the target now.

print(test_target_eighty)

And it's the number seven.

Brilliant!

We were able to examine the MNIST dataset from PyTorch torchvision using Python and PIL, the Python Imaging Library.