Write deep learning models in a few lines of code with fastai

Published 15 Jul 2021

By Julian Garratt



Have you ever considered getting started with deep learning but instead got frustrated by the steep learning curve and huge body of knowledge required to even start hacking up a simple “hello world” program in Pytorch? Thanks to like minded individuals, fastai was developed to provide high-level api access, making the creation of state of the art deep neural networks as easy as a couple of lines of code.

If you believe that I’m over embellishing, I’m not kidding. To give a hint of how high level fastai is, consider these two snippets of code, I’ll let you guess which one is from fastai and which one is from Pytorch.

Exhibit A

Code

Exhibit B

Code

As you may or may not have guessed, Exhibit B was code from the fastai package. Specifically, both pieces of code are used to fit a model and although this might just be one facet, fastai comes prepared with easy data loading including tokenisation for NLP problems and automatic image transformations for vision data as well as highly customisable architectures for neural networks including GANs and RNNs.

A high level overview of fastai

Although fastai comes pre-packed with numerous features, taking a look under the hood reveals a bottom-up framework ready to tackle the most common deep learning problems. It’s also important to note that although fastai leverages packages like Pytorch and Scapy, the core developers (most notably one of the founders Jeremy Howard) note that any packages that were not up to the standard that they required, the company re-designed and re-developed itself. For more info check out this super instructional overview of fastai.

However, if you’re like most people who glanced at the above diagram and yawned, getting a perfect understanding of a machine learning api may not be extremely important. Instead, take these next few paragraphs as a tl;dr, and if you’re interested I’ll add more resources at the end of the page for those who are keen to get into the weeds.

Speaking of high level overview, in the above diagram, sitting at the top is the holy-grail of the fastai package, their “ready-to-go” applications for data in vision, text and tabular (collab refers to collaborative filtering which is effectively a sub-application of tabular). However, applications are only nice if you don’t know how to use them.

If you think of the main procedures of model building as: collecting data, training, and predicting (with preprocesssing, cross-validation etc. all sprinkled in between), fastai designed their software for universality being highly adaptable for most applications with a modular feel. Hopefully, these next few sections will help demonstrate this.

Loading data

At it’s core, fastai’s API takes the difficulty away from manually splitting data up into batches as well as preprocessing. Initally, the API might seem daunting but things become easier if you realise that the API was created for the sole purpose of flexibility, let’s look at an example (note that there are many ways to load data into fastai, this is just one of them).

Example DataBlock Code (taken directly from fastai documentation)


Like learning any new language, it’s confronting at first so let’s break it down. Firstly, you’ll notice that the data set in question is the famous MNIST dataset. With this in mind, take a look at the “blocks” parameter on the first line. You’ll notice that it’s getting passed an “ImageBlock” that contains the class (cls) PILImageBW which defines explicitly that we’re going to pass in an image object where PIL = Python imaging library. Likewise, “CategoryBlock” tells the api that we’re also going to pass some sort of label (in this case a number). This is like defining the columns of a dataset (something we’re more familiar with) which could be anything meaning that we could have also told the api that we’re going to pass in text.
The next parameter, “get_items” explicitly states where to get the images from, in this case it’s a file path (defined as get_image_files), however, it could also have been from a Pandas data frame. The final two parameters are more geared towards transformation of the data. The “splitter” param defines how to create a validation data set and “get_y” tells the api, post transformation of the data, what is the “y” or outcome variable called. Note that this is just one example of vision data, however, similar steps apply to pass in text, tabular, and even audio data.
However, a data block is just as it sounds, you’re effectively just defining the structure of your data (which is still vital). To take the next step, we can create a dataloader which is arguably where most of the action happens (e.g. defining batch size). The following code initialises a dataloader mnist.dataloaders(source) (where source is where our data is located, e.g. Pandas DataFrame, TAR file etc.).

Defining a model, training, and prediction

Defining a model for a specific problem is as easy as one line of code. On a high level, fastai works off “learner” modules. For example, for text classification we can use the “text_classifier_learner” to define a Recurrent Neural Network that uses AWD-LSTM (Average SGD Weight-Dropped Long Short-Term Memory). In the example below, observe that at a minimum we can pass in a data loader (dls) and the architecture (AWD_LSTM), however, consulting the documentation you’ll discover appreciably more parameters.

Example Learner


Once you have a model, training is as simple as writing (assuming you have a learner called “learn”) learn.fit(n_epoch), where “n_epoch” is the number of epochs (amount of cycles through the full training dataset). Thankfully, fastai handles all cross-validation, metrics, and if there’s a GPU available (you can use Kaggle for GPU ready Jupyter Notebooks for free), fastai automatically uses cuda to pass memory over to the GPU for those sweet efficiency gains.

Finally, as simple as fitting a model, predicting on a new given dataset is as easy as `learn.predict(item)’ where item could be an image or text.

Conclusion

In the overall evolution of machine learning, fastai is not the “be all end all” of neural networks, just like sci-kit learn will not remain the king of machine learning, and (perhaps controversially) Python will not remain the best language for Data Science (check out Julia for instance). However, fastai pushes Data Science accessability bounds ahead, providing the benchmark for future AI/ML packages. The speed and ease to which one can learn fastai is a testiment to the educational prowess of their founders and notably Jeremy Howard and Sylvain Gugger. Speaking of which if you’ve found this article fascinating please check out fastai and get started with their legendary course Deep Learning for Coders. Or, if you’re more interested in diving straight into things check out their GitHub repo which contains their book “Deep Learning for coders” (worth $40 on Amazon) for free as interactive Jupyter notebooks. I have definitely only scratched the surface of fastai and you will most likely be confused so be sure to check out the aforementioned superior tutorials. Otherwise, if you wish be sure to get in contact with me on Facebook to use me as a personal Stack Overflow.


Tags: data science deep learning python artificial intelligence