Image Classification using Fastai v2 on Colab

Original article was published by C Kuan on Deep Learning on Medium


Image Classification using Fastai v2 on Colab

Step by step guide to train and classify images in just a few lines of code

Photo by Ryan Carpenter on Unsplash

Introduction

Fastai is a library built on top of PyTorch for deep learning applications. Their mission is to make deep learning easier to use and getting more people from all backgrounds involved. They also provide free courses for Fastai.

Fastai v2 was released in August, I will use it to build and train a deep learning model to classify different sports fields on Colab in just a few lines of codes.

Data Collection

First, I need to collect images for the model to learn. I wish to have a model classifying different sports fields: baseball, basketball, soccer, tennis, and American football. I searched and downloaded the images and saved them in separate folders and uploaded to Google Drive.

Folders contain sport field images. (image by author)

Setup Colab Environment

Once the dataset is ready, I can start the work on Colab.

First upgrade fastai,

!pip install fastai --upgrade -q

and import fastai.vision,

from fastai.vision.all import *

then mount the Google Drive and setup the path

from google.colab import drive
drive.mount(‘/content/gdrive’, force_remount=True)
root_dir = ‘gdrive/My Drive/Colab Notebooks/’
base_dir = root_dir + ‘ball_class’
path=Path(base_dir)

Now we are ready to go.

DataBlock and DataLoader

Fastai provide a mid-level API: DataBlock to deal with data, it is quite easy to use.

fields = DataBlock(blocks=(ImageBlock, CategoryBlock),
get_items=get_image_files,
get_y=parent_label,
splitter=RandomSplitter(valid_pct=0.2, seed=42),
item_tfms=RandomResizedCrop(224, min_scale=0.5),
batch_tfms=aug_transforms())

Different blocks can be used, in this case, we used ImageBlock as the x and CategoryBlock as the label. It is also possible to use other blocks such as MultiCategoryBlock, MaskBlock, PointBlock, BBoxBlock, BBoxLblBlock for different applications.

I used get_image_files function in fastai to get the path of the images as the x and used parent_label method to find the folder names as the label. There are some built-in functions in fastai, you can also write your own function to do this.

Then I used RandomSplitter to split the training and validation dataset.

Then used RandomResizedCrop to resize the image to 224 and aug_transforms() for data augmentation.

This is kind of a template of Fastai DataBlock, it is quite flexible, you can change it based on your situation. You can find the detailed tutorial here.

Once we have the DataBlock ready, we can create dataloader:

and check the labels using vocab

and show some of the images with lables

Train

Now we are ready for training.

I created a CNN learner using resnet34:

learn = cnn_learner(dls, resnet34, metrics=error_rate)

and used lr_find() to find a suitable learning rate

then we can train the model using fit_one_cycle

Only after 5 epochs, we can achieve >90% accuracy.

Now we can unfreeze the network and train the whole network.

Let’s it, only after a few epochs, we achieve 95% accuracy.

Interpretation

We can also interpret the model. The confusion matrix can be plotted using this :

We can also use this to see the image with the highest loss.