Imagenette

When fast.ai first started there were three main datasets that people used for building and testing computer vision models:

  • ImageNet:: 1.3 million images of various sizes around 500 pixels across, in 1,000 categories, which took a few days to train
  • MNIST:: 50,000 28×28-pixel grayscale handwritten digits
  • CIFAR10:: 60,000 32×32-pixel color images in 10 classes

The problem was that the smaller datasets didn’t actually generalize effectively to the large ImageNet dataset. The approaches that worked well on ImageNet generally had to be developed and trained on ImageNet. This led to many people believing that only researchers with access to giant computing resources could effectively contribute to developing image classification algorithms.

We thought that seemed very unlikely to be true. We had never actually seen a study that showed that ImageNet happen to be exactly the right size, and that other datasets could not be developed which would provide useful insights. So we thought we would try to create a new dataset that researchers could test their algorithms on quickly and cheaply, but which would also provide insights likely to work on the full ImageNet dataset.

About three hours later we had created Imagenette. We selected 10 classes from the full ImageNet that looked very different from one another. As we had hoped, we were able to quickly and cheaply create a classifier capable of recognizing these classes. We then tried out a few algorithmic tweaks to see how they impacted Imagenette. We found some that worked pretty well, and tested them on ImageNet as well—and we were very pleased to find that our tweaks worked well on ImageNet too!

There is an important message here: the dataset you get given is not necessarily the dataset you want. It’s particularly unlikely to be the dataset that you want to do your development and prototyping in. You should aim to have an iteration speed of no more than a couple of minutes—that is, when you come up with a new idea you want to try out, you should be able to train a model and see how it goes within a couple of minutes. If it’s taking longer to do an experiment, think about how you could cut down your dataset, or simplify your model, to improve your experimentation speed. The more experiments you can do, the better!

Let’s get started with this dataset:

In [ ]:

  1. from fastai.vision.all import *
  2. path = untar_data(URLs.IMAGENETTE)

First we’ll get our dataset into a DataLoaders object, using the presizing trick introduced in <>:

In [ ]:

  1. dblock = DataBlock(blocks=(ImageBlock(), CategoryBlock()),
  2. get_items=get_image_files,
  3. get_y=parent_label,
  4. item_tfms=Resize(460),
  5. batch_tfms=aug_transforms(size=224, min_scale=0.75))
  6. dls = dblock.dataloaders(path, bs=64)

and do a training run that will serve as a baseline:

In [ ]:

  1. model = xresnet50(n_out=dls.c)
  2. learn = Learner(dls, model, loss_func=CrossEntropyLossFlat(), metrics=accuracy)
  3. learn.fit_one_cycle(5, 3e-3)
epochtrain_lossvalid_lossaccuracytime
01.5834032.0643170.40179201:03
11.2088771.2601060.60156801:02
20.9252651.0361540.66430201:03
30.7301900.7009060.77781901:03
40.5857070.5418100.82524301:03

That’s a good baseline, since we are not using a pretrained model, but we can do better. When working with models that are being trained from scratch, or fine-tuned to a very different dataset than the one used for the pretraining, there are some additional techniques that are really important. In the rest of the chapter we’ll consider some of the key approaches you’ll want to be familiar with. The first one is normalizing your data.