The world’s leading publication for data science, AI, and ML professionals.

Intro to FastAI: Installation and Building our First Classifier

Hint: 98% Accuracy on MNIST with only 5 lines of code!

FastAI is a library that simplifies the training of neural networks. It is based on research in deep learning best practices. Out of the box has support for:

  • Vision
  • Text
  • Tabular data
  • Collaborative filtering
Photo by Jean Gerber on Unsplash
Photo by Jean Gerber on Unsplash

More on each will be covered in the following articles. For now, I just want to say that I’m not the owner of the library, I’m only a user and I don’t intend to market the product in any way, shape or form. Everything you read here will be based on personal experience only. Now back to the article.

Introduction to every deep learning library starts with its capability of handling the MNIST dataset, a standard dataset containing 28×28 pixel photos of handwritten digits. I’ve trained the model in Tensorflow and Keras before and while it’s almost a trivial thing to do (provided you have a basic understanding behind neural nets), there’s no chance it can compete with FastAI, at least implementation speed-wise.

Here’s how to train on the sample of MNIST dataset using the ResNet18 (pre-trained convolutional neural network):

path = untar_data(URLs.MNIST_SAMPLE)
data = ImageDataBunch.from_folder(path)
learn = cnn_learner(data, models.resnet18, metrics=accuracy)
learn.fit(1)

Yes, that was only 4 lines of code (well, we didn’t count a line for imports), but only this will give you an accuracy of 97%. But I’ve gone way ahead of myself. Before we can actually train models and make predictions we need to properly set up the damn thing, so the next section will cover that process.


Installation Options

Owners behind the FastAI library recommend that you have a computer with a GPU. It’s not the end of the world if you don’t have it, as there are a bunch of viable options.

Here are the options you have:

As the GPU on my laptop is pretty decent I will be covering in-depth the first option. The other options are pretty well covered on links provided.


FastAI Local Installation

Here are some details of my machine:

  • Windows 10 x64
  • Nvidia GeForce GTX 1650
  • 16GB RAM

To start out you will need to download Visual Studio, Nvidia CUDA Toolkit, and cuDNN. Luckily I’ve already written an article on that:

An Utterly Simple Guide on Installing Tensorflow-GPU 2.0 on Windows 10

Don’t be confused because it says Tensorflow, you only need to set up those 3 things mentioned above and then you can return back to this article.

Okay, that went well? Let’s proceed.

To start out you will need PyTorch because FastAI is based on it. Because I’m running a Windows machine without Anaconda, and have installed CUDA version 10.1 or newer, I have to execute this in the command prompt:

pip install torch===1.4.0 torchvision===0.5.0 -f https://download.pytorch.org/whl/torch_stable.html

Your’s, however, might be different. You can find it here, just make sure to click on stuff that’s relevant to your configuration:

For me, it was about 750MB to download, so it might take a while, depending on your Internet speed. Once done we can proceed. The only thing needed before installing FastAI itself, are Jupyter Notebook extensions (make sure not to skip those if you are using Jupyter). After that we can install FastAI:

pip install jupyter_contrib_nbextensions
pip install fastai

Great, we now have FastAI installed on our GPU enabled machine. Let’s see how to use it, really shortly.


Example – MNIST classifier with 5 lines of code

Hint – imports are included. Right now we’ll explore how easy it is to train a simple image (digit) classifier with only 5 lines of code.

To start out, it’s recommended to do something which makes me anxious a bit, and that is to import star (*) from a library. Basically it means that we will be able to use functions provided by the library without providing a library name or a prefix before every call. It seems like a good idea, but once your code base grows in size it will be difficult to determine if the function is coming from pure Python, from some of your functions, or whatever place.

Nevertheless, this convention is recommended by the authors so I’ll use it here, but in your project think if that’s something you want to follow. Let’s start with the import:

There’s no point in making an image classifier if we don’t have any images. Luckily, FastAI provides the untar_data() function which will essentially download the URL to a destination if it doesn’t exist there already. For now, only the first parameter is needed, as we are okay with downloading the dataset to the default location.

For the URL we can also benefit from the FastAI’s URLs— it has predefined variables that point to the web location of datasets. To make everything fast for this simple, first-time hands-on experience, we can use the MNIST_SAMPLE dataset, containing only images of digits 3 and 7.

It might seem like a small dataset, but this article is only here to prove a point. Dataset’s path will be stored to a variable named path, and after doing so we can see where the dataset is stored:

Now when that’s done we’ll need to load in the downloaded data. We can do so with the help of a ImageDataBunch class, and call a method from_folder, which will, as the name suggests, load images from a specified folder.

The path variable can be passed in as a parameter, to indicate where the images are stored:

It’s finally time to train our model!

Once again FastAI makes this process utterly simple, as it bases only on a single function call. We will use the function cnn_learner() which expects three parameters:

  • data— your images
  • base_arch— base model architecture, here we’ll use ResNet18
  • metrics— the metric(s) you want to use to evaluate the model (for simplicity I’ll use accuracy here)

Implementation in code:

And now we are ready to train! A function fit will train our model for the desired number of epochs, I’ll set that param to 3:

After only three epochs model achieved an impressive accuracy of 98.8%. Now let’s explore how the input images looked like and where the model made misclassifications.


Model Evaluation

The function show_batch() can be used to display the data that’s going in the model. The implementation in code is utterly simple and needs no further explanation:

There are two other things I want to cover here, and the first one would be how to plot the confusion matrix. To do so we’ll need to make a ClassificationInterpretation object from our CNN learner.

The code is displayed below:

From here we can use the plot_confusion_matrix() function to visualize what the model classified wrongly:

Most of the classifications were correct, as visible from the diagonal line. We’ve misclassified some of the images, but it’s no big deal – still impressive for 5 lines of code.

Another handy thing that FastAI can do is to actually show you the images the model misclassified. We can do so by using the plot_top_loses() function.

I decided to show the top 12 misclassifications:

The first value indicates what the model predicted and the second one indicates what the value actually was.

For some of the instances, we cannot really blame the model, because the images are so similar.


Before you go

In the end, the article was a bit longer than I would like, but I wanted to make an actual demonstration, not just to talk about the theory.

If you’re interested in the FastAI library stay tuned to Towards Data Science and my profile, because there are many more articles like this to come.

Thanks for reading.


Loved the article? Become a Medium member to continue learning without limits. I’ll receive a portion of your membership fee if you use the following link, with no extra cost to you.

Join Medium with my referral link – Dario Radečić


Related Articles