Fastai — the deep learning library that sits on top of Pytorch

Original article can be found here (source): Deep Learning on Medium

How fast is Fastai?

Trying Fastai — the deep learning library that sits on top of Pytorch

A walk trough on a image classification task

The most popular deep learning libraries of this moment are TensorFlow and Pytorch.

On top of TensorFlow runs Keras as high level deep learning framework. Pytorch has a level deep learning library called Fastai.

Fastai promises a fast development cycle for common deep learning tasks. How fast is Fastai? I tried the library for a simple image classification task to check if it is really that fast.

Image data helpers

Fastai has a few useful helpers for working with image data, like download_images, verify_images, and open_image.

download_images('chairs.csv', 'data/chairs')
download_images('tables.csv', 'data/tables')

I exported the urls that resulted for searches for images of chairs and images of tables into text files. download_images downloads the images into two directories, data/chairs and data/tables.

Most of the time we want to apply some augmentations on image data. And we want to split the data into train, validation, and test sets. The fastai library offers the ImageDataBunch class which does just that and a bunch of other tasks that usually need to be handled when working with images. This includes normalization and resizing.verify_images('data/chairs', delete=True, max_size=2000)
verify_images('data/tables', delete=True, max_size=2000)

Some images may be corrupted; verify_images deletes those files. Also it resizes the images that exceed max_size in pixels.

open_image('data/chair/00000000.jpg')
Notebook representation of the Image object

open_image returns a fastai.vision.image.Image object. In a notebook environment the image file is automatically loaded and rendered in the notebook.

Data loaders

For most deep learning tasks we want to apply some transformation to the data. Typically we want to augment it too. And we want to split the data into train, validation, and test sets.

Fastai offers the ImageDataBunch class which does just that. It performs a few common tasks, including normalization and resizing.

data = ImageDataBunch.from_folder(path, train=".", valid_pct=0.2,
ds_tfms=get_transforms(), size=224,
num_workers=4).normalize(imagenet_stats)

Also the ImageDataBunch comes with handy visualization methods for the Juptyer notebook. No need to write your own matplotlib code if you want to inspect some examples in the training set

data.show_batch(rows=3, figsize=(7,8))
Sample from the training set

More information on the training and validation set is easily available.

ImageDataBunch;

Train: LabelList (1032 items)
x: ImageList
Image (3, 224, 224),Image (3, 224, 224),Image (3, 224, 224),Image (3, 224, 224),Image (3, 224, 224)
y: CategoryList
desk,desk,desk,desk,desk
Path: data;

Valid: LabelList (258 items)
x: ImageList
Image (3, 224, 224),Image (3, 224, 224),Image (3, 224, 224),Image (3, 224, 224),Image (3, 224, 224)
y: CategoryList
desk,chair,chair,chair,chair
Path: data;

Test: None

The summary shows that the training set contains 1032 items. The validation set contains 258 items. By default there is no test set. Further we see that the image size is 224 pixels, as desired

Loading a pre-trained model

The ccn is the standard workhorse for image classification. A resnet architecture is never a bad choice as a baseline.

Fastai works with learner objects that provide standard models, together with ways to train them.

learn = cnn_learner(data, models.resnet34, metrics=error_rate)

The functionality for downloading and training models is wrapped by the learner class. By default, the learner downloads pre-trained models. The resnet34 model is pre-trained on ImageNet.

For transfer learning, a cyclic learning schedule works well in most cases. This is the learning method that fast ai offer by default.

learn.lr_find()

The method lr_find() helps to determine a good learning rate.

Tuning the learning rate with lr_find()

In this case 1e-2 seems a good choice.

Training

learn.fit_one_cycle(4, max_lr=1e-2)
Training results

After just one minute, the error_rate (1-accuracy) is below 9%; that’s pretty quick.

learn.recorder.plot_losses()
Train and validation loss

Plots of the loss indicate more room for improvement; which is not strange after just one minute of training.

GPU utilization

CPU utilization of 96%

It is not always easy to get the GPU utilization up, so I checked the utilization of my K80. A utilization of 96% based on default settings is good.

Conclusion

With Fastai a simple classification task can be done in half an hour. That’s pretty fast. I am particularly impressed with the default choices that work well out of the box.

The helper functions cut time writing boiler plate code. Splitting data sets is one of those tasks we repeat again and again.

Plotting a grid of sample images with labels is also a common tasks. It should not take not take much time, but in practice it often does. With fastai it takes only a minute to get the plot on your screen.

Over all I am positive about this library for deep learning. Once question that I have concerns flexibility. For standard tasks, fastai offers a efficient work flow. But what about tasks that are not so common? Is it hard to adapt Fastai to specific needs? Perhaps this will be the topic of a future post.