AI Logic

Datasets and Benchmarks

CIFAR-10/100

Updated:

How CIFAR-10/100 were built and what the data looks like. Left: the flow from 80 Million Tiny Images, where humans verified labels and duplicates were removed to build CIFAR-10 and CIFAR-100, each split into 50,000 training and 10,000 test images. Right: the 32×32×3 image shape, the file layout, the label types, and an example of a CIFAR-100 superclass

Overview

CIFAR-10 and CIFAR-100 are datasets of 32×32-pixel color photos, each labeled with one class for the object it shows. Both contain 60,000 images: CIFAR-10 has 10 classes (6,000 images per class), and CIFAR-100 has 100 classes (600 images per class). Each is split into 50,000 training images and 10,000 test images.

They were created in 2009 by Krizhevsky, Nair, and Hinton. They were introduced in Krizhevsky's technical report "Learning Multiple Layers of Features from Tiny Images," and the name comes from the abbreviation of the Canadian Institute for Advanced Research, which funded their creation.

The purpose was to carve out a reliably labeled portion of 80 Million Tiny Images, a large collection of small images gathered from the web, so that object recognition experiments could be evaluated properly. In Tiny Images, the search term used to find each image serves directly as its label, so the labels are not very trustworthy. CIFAR is a subset whose labels were re-checked by humans.

Because the images are small and the distribution files are only about 160 MB, training can be tried with local compute. Sitting between MNIST, which it is harder than, and ImageNet, which it is much lighter than, it is widely used as a place to compare new methods quickly.

Contents: construction, classes, and format

CIFAR was built from 80 Million Tiny Images through the following steps.

  1. Source images: Tiny Images was made by running image searches for concrete English nouns in WordNet (about 79,000 words) and shrinking the top ~3,000 results per word to 32×32.
  2. Humans verify the labels. Students paid by the hour looked at images found with the word for their assigned class and its hyponyms (for example, cargo ship and frigate for ship) and removed images that did not fit.
  3. Criteria: only images for which someone asked "what is in this image?" would readily answer with the class name are kept. Line drawings are removed, as are images showing several equally prominent objects. Images where the object is partly occluded or seen from an unusual angle are kept.
  4. The author verifies. The author checked every label assigned by the students.
  5. Duplicates are removed. Images that are close in pixel-value distance (L2 distance) are removed, as are nearly identical images such as re-saved JPEGs.
  6. Splitting. For CIFAR-10, 5,000 randomly selected images from each class went into the training set, and the remaining 1,000 into the test set.

The CIFAR-10 classes are the 10 classes airplane, automobile, bird, cat, deer, dog, frog, horse, ship, and truck. The classes do not overlap. For example, automobile covers sedans and SUVs, and truck covers only big trucks; pickup trucks belong to neither.

The CIFAR-100 classes are 100 classes that do not overlap with those of CIFAR-10, grouped five similar classes at a time into 20 superclasses. For example, the superclass "reptiles" contains crocodile, dinosaur, lizard, snake, and turtle. Each image has two labels: a fine class label (fine, 0–99) and a superclass label (coarse, 0–19). Classes within the same superclass are similar, which makes them hard to tell apart.

ItemCIFAR-10CIFAR-100
Number of classes10100 (20 superclasses)
Training50,000 images (5,000 per class)50,000 images (500 per class)
Test10,000 images (1,000 per class)10,000 images (100 per class)
Images32×32 pixels, 3 RGB channelsSame
LabelsInteger 0–9Fine label 0–99 and superclass 0–19

In the distributed files, each image is represented by 3,072 (=32×32×3= 32 \times 32 \times 3) integers from 0 to 255. The first 1,024 are the red channel, the next 1,024 green, and the last 1,024 blue, each stored row by row starting from the first row of the image. The CIFAR-10 training data is split into five files of 10,000 images each.

Evaluation protocol

Evaluation uses accuracy (or error rate) on the 10,000 test images. The usual metric is the fraction of images whose top-1 prediction matches the correct class (top-1). For CIFAR-100, accuracy over the 100 fine classes is reported, and the superclass labels are almost never used.

CIFAR does not come with a validation set. The correct procedure is therefore to hold out part of the 50,000 training images (e.g., 5,000) for validation when choosing hyperparameters, then retrain on the full training set once the choice is made and evaluate on the test set only once.

Some papers choose hyperparameters while looking at test set performance. Such numbers come out higher by the amount they were tuned to the test set (a form of overfitting). When comparing with numbers from other papers, check how the validation set was built.

As a reference point, the authors' official page lists error rates of 18% for a CNN without data augmentation and 11% with data augmentation. Current methods reach far lower error rates on CIFAR-10, and differences between methods are often below 1%.

Usage and historical significance

CIFAR has been valued because it allows classification on natural photos to be tested with modest compute. MNIST is too easy for differences to show, and ImageNet requires large compute for training; CIFAR fills the gap between them.

Common uses include:

  • Comparing architectures: ResNet, DenseNet, and others report results on CIFAR-10/100 alongside ImageNet.
  • Comparing regularization and data augmentation: it is the standard place to measure methods that curb overfitting, such as Shake-Shake, ShakeDrop, Stochastic Depth, Cutout, and Mixup. With a relatively small training set of 50,000 images, differences between techniques for curbing overfitting show up easily.
  • Neural architecture search: as with DARTS, architectures are searched on CIFAR-10 to keep search costs down, and the architecture found is then transferred to ImageNet.
  • Semi-supervised learning: methods such as FixMatch are compared in a setting where labels are kept for only a few images per class in the CIFAR-10 training set and the rest are used as unlabeled data.
  • Adversarial robustness: it is also used to evaluate attacks and defenses such as FGSM.

CIFAR-100 has the same number of images as CIFAR-10 but one tenth as many per class. Because it requires distinguishing many classes from little data, it is used to compare methods that have become hard to separate on CIFAR-10.

Practical notes

License: the official page does not state the permitted scope of use (license). It asks users to cite the technical report. The images were originally collected from the web and their copyright status is unclear, so check before using them as data in a product.

80 Million Tiny Images, from which CIFAR was derived, was found to contain offensive terms among its labels, and its authors withdrew it in 2020. CIFAR-10/100 consist only of images from human-selected classes and are not subject to this issue, but it is important to be aware of biases stemming from how the original data was collected.

Overlap between test and training data: Barz and Denzler (2020) showed that for 3.3% of CIFAR-10 test images and 10% of CIFAR-100 test images, a nearly identical image (differing only slightly in contrast, color tone, position, and so on) exists in the training data. Duplicate removal based on pixel distance cannot catch such slightly different images. A test set called ciFAIR, in which all duplicates are replaced with new images, has also been released.

Label errors: Northcutt et al. (2021) report that in the test sets, 54 images (0.54%) of CIFAR-10 and 585 images (5.85%) of CIFAR-100 are mislabeled. CIFAR-100 has more errors because it has many similar classes.

Suspected overfitting to the test set: Recht et al. (2019) replicated the original procedure to build a new CIFAR-10 test set (CIFAR-10.1, 2,000 images) and showed that the accuracy of many models drops by 3–15%. However, the ranking of models was largely preserved, and the authors conclude that the cause lies less in tuning to the test set than in the new images being slightly harder.

Small images: at 32×32, fine textures are barely visible. Methods that work on CIFAR do not necessarily work equally well on larger images, so it is common to back up claims with results on ImageNet or similar datasets as well.

Comparison with similar datasets

Datasets commonly used for image classification, compared from the same viewpoints:

DatasetImagesNumber of classesTraining / testSuited for
MNISTHandwritten digits, 28×28 grayscale1060,000 / 10,000Sanity checks, getting started
CIFAR-10Photos of objects, 32×32 color1050,000 / 10,000Quickly comparing methods on small images
CIFAR-100Photos of objects, 32×32 color100 (20 superclasses)50,000 / 10,000Comparing the ability to distinguish many classes from little data
CIFAR-10.1Photos re-collected with the same procedure as CIFAR-10102,000 for evaluation onlyChecking for tuning to the test set
ImageNet (ILSVRC 2012)Photos of objects, color, various sizes1,0001,281,167 / 50,000 (validation)Serious performance comparison, pretraining

Rules of thumb for choosing:

  • To quickly check whether a new method works, try it on CIFAR-10 first.
  • When CIFAR-10 shows no difference, switch to CIFAR-100 with the same code (just change CIFAR10 to CIFAR100 and make the output 100 classes).
  • When the results will inform a paper or a product decision, also verify them on a dataset with larger images, such as ImageNet.

PyTorch example

Show code
python
import torch
from torch.utils.data import DataLoader, random_split
from torchvision import datasets, transforms

'''
Load CIFAR-10 with torchvision and split it into training, validation, and test sets
'''

root = './data'

# 1. Compute the mean and std for normalization from the training data (do not use the test data)
raw = datasets.CIFAR10(root, train=True, download=True)   # train=True gives the 50,000 training images
pixels = torch.tensor(raw.data, dtype=torch.float32) / 255  # [50000, 32, 32, 3] (channels last)
mean = pixels.mean(dim=(0, 1, 2)).tolist()
std = pixels.std(dim=(0, 1, 2)).tolist()
print(mean, std)

# 2. Augment only during training (pad 4 pixels, random crop, horizontal flip)
train_transform = transforms.Compose([
  transforms.RandomCrop(32, padding=4),
  transforms.RandomHorizontalFlip(),
  transforms.ToTensor(),          # 0-255 image -> [3, 32, 32] tensor in 0-1
  transforms.Normalize(mean, std),
])
eval_transform = transforms.Compose([
  transforms.ToTensor(),
  transforms.Normalize(mean, std),
])

train_full = datasets.CIFAR10(root, train=True, download=True, transform=train_transform)
test_dataset = datasets.CIFAR10(root, train=False, download=True, transform=eval_transform)  # train=False gives the test set
print(len(train_full), len(test_dataset))  # 50000 10000
print(train_full.classes)                  # ['airplane', 'automobile', ..., 'truck']

# 3. There is no validation set, so hold out 5,000 training images
#    No augmentation for validation, so take the same indices from a non-augmented dataset
generator = torch.Generator().manual_seed(0)
train_idx, val_idx = random_split(range(len(train_full)), [45000, 5000], generator=generator)
train_dataset = torch.utils.data.Subset(train_full, list(train_idx))
val_dataset = torch.utils.data.Subset(
  datasets.CIFAR10(root, train=True, download=True, transform=eval_transform), list(val_idx))

train_loader = DataLoader(train_dataset, batch_size=128, shuffle=True, num_workers=4)
val_loader = DataLoader(val_dataset, batch_size=256)
test_loader = DataLoader(test_dataset, batch_size=256)

images, labels = next(iter(train_loader))
print(images.shape, labels.shape)  # torch.Size([128, 3, 32, 32]) torch.Size([128])

# For CIFAR-100, just switch to datasets.CIFAR100 (labels are the 100 fine classes)

# Loading with Hugging Face datasets
# from datasets import load_dataset
# ds = load_dataset('uoft-cs/cifar10')     # splits are 'train' and 'test'; columns are 'img' and 'label'
# ds100 = load_dataset('uoft-cs/cifar100') # columns are 'img', 'fine_label' (0-99), and 'coarse_label' (0-19)

FAQ

Q. Are CIFAR-10 and CIFAR-100 the same images split with different labels?
A.

No. They are different sets of images. The CIFAR-100 classes were chosen so as not to overlap with the CIFAR-10 classes. For example, pickup trucks are excluded from CIFAR-10's automobile and truck, while CIFAR-100 has a class called pickup truck.

Q. What are the CIFAR-100 superclasses (coarse labels) used for?
A.

They can be used for 20-class classification, but most papers report only accuracy over the 100 fine classes. The superclasses are used, for example, when analyzing confusions between similar classes.

Q. If accuracy on CIFAR-10 improves by 0.5%, can the method be said to be better?
A.

Not on that alone. The test set has 10,000 images, so 0.5% is a difference of 50 images. Report the mean and spread over several runs with different random seeds, confirm that no tuning was done on the test set, and then compare.

References

  1. Krizhevsky, A. (2009). Learning Multiple Layers of Features from Tiny Images. Technical Report, University of Toronto.
  2. Krizhevsky, A., Nair, V., & Hinton, G. The CIFAR-10 and CIFAR-100 datasets (official page).
  3. Barz, B., & Denzler, J. (2020). Do We Train on Test Data? Purging CIFAR of Near-Duplicates. Journal of Imaging.
  4. Recht, B., Roelofs, R., Schmidt, L., & Shankar, V. (2019). Do ImageNet Classifiers Generalize to ImageNet? ICML 2019.
  5. Northcutt, C. G., Athalye, A., & Mueller, J. (2021). Pervasive Label Errors in Test Sets Destabilize Machine Learning Benchmarks. NeurIPS 2021 Datasets and Benchmarks.
  6. Birhane, A., & Prabhu, V. U. (2021). Large image datasets: A pyrrhic win for computer vision? WACV 2021.
  7. torchvision.datasets.CIFAR10 (PyTorch documentation).