pytorch
  1. pytorch-cifar-10-and-cifar-100-datasets

CIFAR-10 and CIFAR-100 datasets - ( Image Classification with PyTorch )

Heading h2

Syntax

CIFAR-10

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))])

trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
                                        download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,
                                          shuffle=True, num_workers=2)

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 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))])

trainset = torchvision.datasets.CIFAR100(root='./data', train=True,
                                         download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,
                                           shuffle=True, num_workers=2)

testset = torchvision.datasets.CIFAR100(root='./data', train=False,
                                        download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=4,
                                          shuffle=False, num_workers=2)

Example

CIFAR-10

import torch
import torchvision
import torchvision.transforms as transforms

transform = transforms.Compose(
    [transforms.RandomHorizontalFlip(),
     transforms.RandomCrop(32, padding=4),
     transforms.RandomRotation(15),
     transforms.ToTensor(),
     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
                                        download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,
                                          shuffle=True, num_workers=2)

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)

classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')

CIFAR-100

import torch
import torchvision
import torchvision.transforms as transforms

transform = transforms.Compose(
    [transforms.RandomHorizontalFlip(),
     transforms.RandomCrop(32, padding=4),
     transforms.RandomRotation(15),
     transforms.ToTensor(),
     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

trainset = torchvision.datasets.CIFAR100(root='./data', train=True,
                                         download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,
                                           shuffle=True, num_workers=2)

testset = torchvision.datasets.CIFAR100(root='./data', train=False,
                                        download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=4,
                                          shuffle=False, num_workers=2)

classes = ('aquatic mammals', 'fish', 'flowers', 'food containers', 'fruit and vegetables', 'household electrical devices',
           'household furniture', 'insects', 'large carnivores', 'large man-made outdoor things', 'large natural outdoor scenes',
           'large omnivores and herbivores', 'medium-sized mammals', 'non-insect invertebrates', 'people', 'reptiles', 'small mammals',
           'trees', 'vehicles 1', 'vehicles 2')

Output

CIFAR-10

Files already downloaded and verified
Files already downloaded and verified
[ Training Data ]
Batch size: 4
Number of train batches: 12500

[ Test Data ]
Batch size: 4
Number of test batches: 2500

CIFAR-100

Files already downloaded and verified
Files already downloaded and verified
[ Training Data ]
Batch size: 4
Number of train batches: 12500

[ Test Data ]
Batch size: 4
Number of test batches: 2500

Explanation

CIFAR-10 and CIFAR-100 are popular image classification datasets containing 10 and 100 classes respectively. Both datasets contain images of size 32x32, which makes them small and easy to train.

We use the torchvision library to download, load and transform the dataset. Data augmentation techniques like random crop, random flip and random rotation are applied to the images to increase the size of the dataset and prevent overfitting.

The data is divided into training and testing sets. The chosen batch size of 4 is used to speed up the training process, and the data loader is used to load the data efficiently during training and testing.

Use

CIFAR-10 and CIFAR-100 can be used for image classification tasks, such as recognizing objects in photographs. They are commonly used as benchmark datasets in computer vision research.

Important Points

  • CIFAR-10 and CIFAR-100 are popular image classification datasets containing 10 and 100 classes respectively.
  • Data augmentation techniques like random crop, random flip and random rotation can be applied to the images to increase the size of the dataset and prevent overfitting.
  • The data is divided into training and testing sets, and a batch size of 4 is used to speed up the training process.

Summary

In conclusion, CIFAR-10 and CIFAR-100 are popular datasets used for image classification tasks. They contain 10 and 100 classes respectively, and contain images of size 32x32. Data augmentation techniques like random crop, random flip and random rotation can be applied to the images to increase the size of the dataset. The data is then divided into training and testing sets, and a batch size of 4 is used to speed up the training process.

Published on: