Develop an Image Classifier Using Fastai

Zora Hirbodvash
3 min readNov 12, 2020

This post is based on the course offered by Jeremy Howard and Rachel Thomas (https://course.fast.ai/). The material for this course is a book named Deep Learning for Coders with fastai and PyTorch. Sincere thanks to the book authors, Jeremy Howard and Sylvain Gugger. I also used https://docs.fast.ai/ information in this blog.

Here, we start by providing an example to classify cat and dog breeds. We are using a dataset available in the fastai library. The name of the dataset is Oxford-IIIT Pet Dataset, and it has 37 different breeds types of cats and dogs images.

Load the necessary libraries & the dataset

We load the necessary libraries, and download our dataset:

from fastai.vision.all import *
path = untar_data(URLs.PETS)
or
path=Path(“/notebook/storage/data/oxford_iii_pet”)

To check what is inside of the dataset, we can use the ls() method:

path.ls()

Besides, we use a fastai function called get-images_files to grab all images files in one folder:

files = get_image_files(path/"images")
len(files)

Labelling the dataset and train the model

Next, the data needs to be labelled. There are two ways to label our data generally. We can use a simple function or a regular expression.

Most of the time labels are hidden in the file names. So, Let’s check the file names first:

files[16].name

Here, the class is all the thing before underline, and it follows by some digits.

Therefore:

pat= r’^(.*)_\d+.jpg’

Data needs to be ready for a model by putting it in a DataLoaders object. You can find a piece of comprehensive information about the different factory of DataLoader class here:

https://docs.fast.ai/vision.data#ImageDataLoaders

We use ImageDataLoaders.from_name_re in this example:

dls = ImageDataLoaders.from_name_re(path, files, pat, item_tfms=Resize(224))

Let’s have a look at our data by using the show_batch() method:

dls.show_batch()
Results from the show_batch method. Dogs and Cats images with the breeds.

Then we have to create a learner. A learner is an object combining the data and a model for training. The learner uses transfer learning to fine-tune a pretrained model. Here, we use a convolutional neural network (CNN).

learn = cnn_learner(dls, resnet34, metrics=error_rate)
learn.fine_tune(4, 3e-3)

Prediction

Now, it is time to check the performance of the model by looking at some predictions using the show_results method:

learn.show_results()
Some predictions with the show_results method.

We can also check the model performance by checking to predict a new image. First, we can download an image. We execute the cell with the uploader define.

from ipywidgets import widgets
uploader=widgets.FileUpload()
uploader

Then we can print our image:

import io
from PIL import Image
img = PILImage.create(uploader.data[0])
img
Persian Cat

Finally, we will use the predict() method to check the model performance.

learn.predict(img)
Out: ('Persian',
tensor(7),
tensor([9.0535e-11, 6.6253e-11, 3.6857e-08, 4.4284e-10, 2.5143e-07, 1.1042e-09,
5.9379e-10, 9.9999e-01, 4.3883e-08, 6.8854e-12, 9.3967e-12, 9.8016e-10,
3.2754e-09, 4.6902e-11, 6.9504e-11, 1.9017e-11, 3.0089e-09, 3.0767e-11,
2.3352e-10, 2.7608e-10, 2.4781e-11, 9.3027e-11, 3.8694e-08, 2.4465e-08,
2.5688e-10, 5.1831e-11, 6.8533e-12, 7.7815e-10, 6.0541e-06, 1.1127e-09,
5.4246e-11, 3.3597e-09, 3.1433e-09, 9.5952e-12, 1.2015e-11, 5.9862e-10,
5.2314e-10]))

--

--