Chapter 16 Chapter 16: Appendix
Data Sets
Torchvision is a popular library in the PyTorch ecosystem that provides various tools and utilities for working with computer vision tasks, including the management and loading of standard datasets. The library simplifies the process of accessing and preprocessing datasets commonly used in computer vision research and applications.
Torchvision includes a collection of pre-built datasets that can be easily accessed and utilized for training, validation, and testing purposes.
List of the Torchvision Datasets
- MNIST
- CIFAR-10
- CIFAR-100
- ImageNet
- COCO
- Fashion-MNIST
- SVHN
- STL-10
- CelebA
- Pascal VOC
- Places365
MNIST Data Set
import torchvision.datasets as datasets
# Load the training dataset
train_dataset = datasets.MNIST(root='data/', train=True, transform=None, download=True)
# Accessing the data and labels
data, labels = train_dataset[0] # This will retrieve the first image and its label
CIFAR10
import torch
import torchvision
import torchvision.transforms as transforms
transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
testset = torchvision.datasets.CIFAR10(root='./data', train=False,
download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=4,
shuffle=False, num_workers=2)
CIFAR-100
import torchvision.datasets as datasets
import torchvision.transforms as transforms
# Define transform to normalize data
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
])
# Load CIFAR-100 train and test datasets
trainset = datasets.CIFAR100(root='./data', train=True, download=True, transform=transform)
# Create data loaders for train and test datasets
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)
Fashion MNIST
import torch
import torchvision
import torchvision.transforms as transforms
# Define transformations
transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))])
# Load the dataset
trainset = torchvision.datasets.FashionMNIST(root='./data', train=True,
download=True, transform=transform)
# Create data loaders
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,
shuffle=True, num_workers=2)
SVHN (Street View House Numbers)
import torchvision
import torch
# Load the train and test sets
train_set = torchvision.datasets.SVHN(root='./data', split='train', download=True, transform=torchvision.transforms.ToTensor())
# Create data loaders
train_loader = torch.utils.data.DataLoader(train_set, batch_size=64, shuffle=True)
STL-10
import torchvision.datasets as datasets
import torchvision.transforms as transforms
# Define the transformation to apply to the data
transform = transforms.Compose([
transforms.ToTensor(),
# Convert PIL image to PyTorch tensor
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) # Normalize the data
])
# Load the STL-10 dataset
train_dataset = datasets.STL10(root='./data', split='train', download=True, transform=transform)
CelebA
import torchvision.datasets as datasets
import torchvision.transforms as transforms
transform = transforms.Compose([
transforms.CenterCrop(178),
transforms.Resize(128),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
celeba_dataset = datasets.CelebA(root='./data', split='train', transform=transform, download=True)