Downloading CIFAR-10 with PyTorch

For our first PyTorch program, we are going to test downloading CIFAR-10 according to the official PyTorch documentation. As we did with TensorFlow, we again download both the training and test datasets. Here is a related part of the code that we are going to use in the next section:

import torch
import torchvision
import torchvision.transforms as transforms
normalize = transforms.Compose(
[transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]
)
trainset = torchvision.datasets.CIFAR10(root='./pytorch', train=True, download=True, transform=normalize)

The new dataset files will be downloaded to the same location where you ran the preceding code. root='./pytorch' is how you can customize the download. In this case, a directory named pytorch will be created:

Downloading https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz to ./pytorch/cifar-10-python.tar.gz
100.0%Files already downloaded and verified
Process finished with exit code 0

As we saw in the case of the Fashion-MNIST dataset, the local download location of the dataset files is your PyCharm project folder itself. Here, our PyCharm project subfolder is PyTorch, which has our pytorch dataset files:

~/PyCharmProjects/HandsOnGPUComputingWithPython/PyTorch/pytorch

It will be immediately reflected when you download the CIFAR-10 datasets for the first time with PyTorch:

Now, let's dive into our first GPU-accelerated machine learning experience!