Convert CIFAR10 Dataset from PIL Images to PyTorch Tensors
Convert CIFAR10 Dataset from PIL Images to PyTorch Tensors by Using PyTorch's ToTensor Operation
< > 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
Once imported, the CIFAR10 dataset will be an array of Python Imaging Library (PIL) images.
This is useful for some applications such as displaying the images on the screen.
However, in order to use the images in our deep neural network, we will first need to transform them into PyTorch tensors.
Conveniently, the ToTensor function in torchvision.transforms is built for exactly this purpose.
First, we need to import torch.
Then we need to import torchvision.
Then we need to import torchvision.datasets as datasets.
import torchvision.datasets as datasets
Then we need to import torchvision.transforms as transforms.
import torchvision.transforms as transforms
Now, we need to check that our versions of torch and torchvision are current.
As of March 22, 2018, 0.3.1 for torch and 0.2.0 for torchvision are the current versions.
Now, when we are importing our training test sets from torchvision.datasets, instead of leaving it blank, we will want to set the transform parameter to the ToTensor transform.
cifar_trainset = datasets.CIFAR10(root='./data', train=True, download=True, transform=transforms.ToTensor())
cifar_testset = datasets.CIFAR10(root='./data', train=False, download=True, transform=transforms.ToTensor())
Specifically, the root, train, and download parameters were covered in the previous video.
The transform parameter specifies how we want to transform the imported images and the transform parameter indicates that we want the images to be converted to PyTorch tensors during import.
We are now free to use the tensors in a PyTorch model.