About Fastai - why is it so great?

If you took a look at the 'HTC Education Series: Getting Started with Deep Learning' post on monday, you will have noticed that we're building the course on top of fastai and the 2020 Part 1 fastai Deep Learning course lecture series, with additional extra material provided by HTC.

So why did we pick fastai for our course?

Originally we intended to build the course around python coding using Keras.  And Keras is certainly great, but nowhere near as great as fastai.  So right away we switch from Keras to PyTorch under the hood, with fastai on top of PyTorch (hiding it while still making it accessible if you need to make specific calls to it's libraries).

Fastai is designed around 2 main goals, to try to be easy to comprehend to allow you to be extremely productive, but also to be extensible if you want to do that.  We'll talk more about what that means in a minute, but first let's look at some sample code for building and training a complete deep learning neural net system.

path = untar_data(URLs.PETS)/'images'

def is_cat(x): return x[0].isupper()
dls = ImageDataLoaders.from_name_func(
    path, get_image_files(path), valid_pct=0.2, seed=42,
    label_func=is_cat, item_tfms=Resize(224))

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

What's happening here in the 5 lines of code?

1:  We need a database for our deep learning net to train on.  The first line grabs a database of images of pets (from the fastai datasets collection.  This particular database is composed of images of dogs and cats, and is based on the Oxford-IIIT Pet Dataset, which contains 7349 images of cats an dogs from 37 different breeds.  Under the hood, the dataset is downloaded to the GPU server you are using, and then extracted.

2:  We define a simple function called is_cat() that is used to distinguish between cat and dog images by a name label convention inherent in this particular database where cat images are uppercase names are dogs, lower case names are cats.

3:  We use the supercool ImageDataLoaders function in fastai to do all of the usually annoying work associated with taking all of the images in the database and converting them into a form we can actually use in the particular neural network we will be training them from.

Contrast this incredibly simple way of interacting with all kinds of different data sets and associated data very simply with what we were originally planning to do, doing all of that work manually with custom python code.  Plus, all of the get the data set ready for the model work is done on the GPU for added speed benefit.

4:  We then setup a convolutional neural net learner to use our fastai customized data set.  The particular deep learning neural net we define in this single programming step is based on the pre-trained resnet34 deep learning model (trained to solve ImageNet).  

5: We now use transfer learning based off of the pre-trained resnet 34 model to fine tune that pretrained model for our newly defined task (in this example recognizing cats and dogs).  We only do one epoch of training in this example, typically you are going to be doing more epochs.

Wow, that is about as simple as it could possible be.

What can we do with our new custom trained neural net?

We can now deploy it for however we want to use it.

To run the trained model on a new image, we only need one line of code.

is_cat,_,probs = learn.predict(img)

This will give us a probability that the image is a cat.

And we're done.  Like i said, wow.

Ok, but what about something like image segmentation?  Surely that must be harder then detecting cats vs dogs?

Nope.  You can do that particular task just as easily.

path = untar_data(URLs.CAMVID_TINY)
dls = SegmentationDataLoaders.from_label_func(
    path, bs=8, fnames = get_image_files(path/"images"),
    label_func = lambda o: path/'labels'/f'{o.stem}_P{o.suffix}',
    codes = np.loadtxt(path/'codes.txt', dtype=str)

learn = unet_learner(dls, resnet34)

Running this new custom trained deep learning neural net is just as easy as before, a single line of code.

Ok, but what about training a recursive deep learning neural net for natural language processing. You only need 3 lines of code for this example.

dls = TextDataLoaders.from_folder(untar_data(URLs.IMDB), valid='test')
learn = text_classifier_learner(dls, AWD_LSTM, drop_mult=0.5, metrics=accuracy)
learn.fine_tune(2, 1e-2)

Running this new custom trained deep learning neural net is just as easy as before, a single line of code.

I could continue, but i think you get the message.

Fastai's applications all use the same basic steps and code:

  • Create appropriate DataLoaders
  • Create a Learner
  • Call a fit method
  • Make predictions or view results.

This is so much simpler then what you have to do if you are working directly with Keras or PyTorch.

As i pointed out above, getting a data set configured to be actually useable as input to a particular net model is often a tedious job if you have to hand code it.  And it will run on the cpu if you use Python to hand code it.  Fastai's data loaders handle all of that work, and can run it on the GPU as well for extra speed.

You can access the full online documentation for the fastai api here.

I should point out that you don't have to use transfer learning if you don't want to.  You can define a custom deep learning model in fastai, and then train it from the ground up.

But to be honest, for many tasks there really is no need to do that, since you gain huge speed and training time advantages by using transfer learning based on some pre-existing deep learning model zoo net model

The fastai course is also built around Jupyter notebooks.

This lets you do all of your work in a Jupyter notebook inside of a web browser. 

And it's all pre-configured.  

I should point out that setting up an individual computer for working with GPUs and other associated code libraries is oftentimes a mind numbing experience.  You search online for how to do it and get 20 different explanations, each one incorrect for your particular computer setup.  Then you spend hours, days trying to get everything installed you need to do work with the set of libraries required to do the actual work.

Let's quickly take a look at fastai's design.

The fastai api uses a layered architecture, which expresses common underlying patterns of many deep learning and data processing techniques in terms of decoupled abstractions. These abstractions can be expressed concisely and clearly by leveraging the dynamism of the underlying Python language and the flexibility of the PyTorch library. 

Fastai includes:

1:  A new type dispatch system for Python along with a semantic type hierarchy for tensors
2:  A GPU-optimized computer vision library which can be extended in pure Python
3:  An optimizer which refactors out the common functionality of modern optimizers into two basic pieces, allowing optimization algorithms to be implemented in 4–5 lines of code
4:  A novel 2-way callback system that can access any part of the data, model, or optimizer and change it at any point during training
5:  A new data block API
6:  And much more...

If you are curious, you can learn more about the design and motivation of the fastai library in this paper.


Popular posts from this blog

Pix2Pix: a GAN architecture for image to image transformation

CycleGAN: a GAN architecture for learning unpaired image to image transformations

Smart Fabrics