Image Classification with Fastai v1

Overview of how to get started with image classification using Fastai v1 with minimal code.

In this blog, I will give a overview of how to use the Fastai v1 library to train a model which is able to classify images with only a few lines of code within a Jupyter notebook. The full source code of the Jupyter notebook is available here.

Simple Image Classifier

Common Practices

Start each notebook with the following code:

  • %matplotlib inline – ensures matplotlib will plot to output cell in notebook
  • %reload_ext autoreload, %autoreload 2 – automatically reloads modules if edited
  • Import relevant Fastai libraries
%matplotlib inline
%reload_ext autoreload
%autoreload 2
from fastai import *
from import *


bs = 64 # batch size
img_size = 24 # image size

Getting the Dataset

Fastai has a few built-in methods to get existing datasets using the URLs module. Alternatively, you can skip the data download and extraction and specify your own dataset path. In this example we will be using the CIFAR10 dataset which contain 10 classes of small images.

Approach 1: Getting CIFAR10 dataset using fastai

path = untar_data(URLs.CIFAR);
path # output >>> PosixPath('/home/jupyter/.fastai/data/cifar10')
# dataset folder structure # output >>> ['test', 'labels.txt', 'train']

Approach 2: Specifying your own dataset path

Here we specify the path of the dataset location which is in `/home/jupyter/.fastai/data/cifar10` with the directory structure with a test folder, training folder and a text file with the label names.

path = Path('/home/jupyter/.fastai/data/cifar10') # output >>> ['test', 'labels.txt', 'train']

Within our “test” and “train” folder, there are sub-directories containing folders for each of the classes. These folders each contain images of the corresponding classes.

!ls /home/jupyter/.fastai/data/cifar10/train
# output >>> airplane  automobile  bird  cat  deer  dog  frog  horse  ship  truck

Creating an Image Data Bunch

There are several ways in which we can create an Image Data Bunch. See the documentation for the full details of methods available in ImageDataBunch. Example methods include getting class splits from folders, csv, dataframes, custom functions, regular expressions and from lists.

Since our example is from CIFAR10, the classes are divided into separate folders with each folder containing images for that particular class. The directory structure:


Therefore we will use the from_folder method to extract the image data bunch.

data = ImageDataBunch.from_folder(path, ds_tfms=tfms, valid='test', size=img_size, bs = bs) 
# normalising the dataset using the same normalisation applied to the imagenet dataset

Visualising our data

We can extract the data and visualise a sample of a batch using Fastai built-in methods to ensure that the classes are correct and images are properly rendered.

Training our Model

We can then apply transfer learning using a pre-trained resnet-50 model and train the model for a specified number of epochs.

Here we trained the model for about 12 minutes and for 10 epochs to reach an accuracy of just under 80%. To improve the accuracy we can apply other tuning methods which will be explained in later blog posts.

Evaluating our Model

Interpreting our classification results with ClassificationInterpretation. We can plot a visualisation of our results to show which areas our model performed poorly with various built-in Fastai methods. These include:

  • plotting the images which had the greatest loss
  • plotting a confusion matrix
  • plotting the class pairs in which the model got mixed up between.


Fastai v1 library provides a quick approach get started with image classification which are able to reach high quality results with minimal code.

from fastai import *
from import *

path = untar_data(URLs.CIFAR);
data = ImageDataBunch.from_folder(path, ds_tfms=tfms, valid='test', size=24 , bs = 64 )
learn = ConvLearner(data, models.resnet50, metrics=error_rate)

The above code snippet is a summary of all the code that is required to train a model for image classification.

Source: Deep Learning on Medium