Building an Object Classification Model with Fast.AI

Disclaimer: This post isn’t meant to be an in-depth explanation of machine or deep learning, but rather, provide a practical guide on setting up object classification for projects. This blog post will cover how to use Fast.AI to build an object classification model. I have written another blog post on how to build a custom, multiple object detection model using TensorFlow’s Object Detection API, which is linked here!

Background

While I was researching how to build custom object detection and classification models for use in a web application, I came across Fast.AI. Eventually, our team ended up using TensorFlow because of available documentation on porting TensorFlow models to web applications. Fast AI doesn’t, at the time of writing this blog, have an explicit tutorial on multiple object detection — a desired feature of the web application. However, I wanted to document my work on using Fast.AI to build a model for single object classification due to its speed, accuracy, and ease of use. The current model can distinguish between three Nike shoes.

Fast.AI: A MOOC for Deep Learning

Fast.AI is a project that was started to make deep learning accessible to the masses. Fast.AI has released a free open-source library for deep learning, called fastai, which sits on top of PyTorch and provides a consistent API to important deep learning tools. Its benefit is less code, ease of use, increased accuracy, and increased speed when building models. Fast.AI provides a practical deep learning for coders course, which I took and utilized code from for object classification.

The great part about Fast AI is that most of the tools/tutorials written for it use the jupyter notebook format, which allows you to write documentation and experiment with code in a neat, intuitive manner. In order to run jupyter notebook, first, install it (installation directions here). Then, go to the terminal and type in jupyter noteboook. Next, navigate to the directory that contains the .ipynbfilethat you’re interested in and click on it. It should run a jupyter notebook that lets you interactively run code.

While I have run my code locally, I highly suggest that you use Google Colab for running code related to Fast AI. This is because it offers free GPU and it’s also what I have written this tutorial for. Therefore, if trying to replicate my results, it will be difficult to do so locally. I will write the code assuming that you will use Google Colab/OSX. Additionally, I will be providing my code on GitHub for Fast AI.

Building a Custom ML Model with Fast.AI

Setting up Google Colab

The first step is to register your environment properly to access Colab. This requires you to have a Gmail account. Next, head to the Colab Welcome Page and import from Github (you need to input fastai/course-v3). If you do this method, you’re installing the FastAI python notebook; however, I suggest you actually upload the one that I built because it has more instructions and is project-specific. Therefore, head to the Colab Welcome Page and upload the .ipynb file called CustomClassifier_Ronak_FastAI.ipynb. Now, the python notebook should be displayed. Note: all my code (including the referenced .ipynb file) is available on Github here.

Before running anything, however, you need to tell Google Colab that you’re interested in using a GPU. You can do this by clicking the runtime tab, selecting “change runtime time,” and clicking GPU! See the screenshots below for more clarity.

Finding the setting option to change to GPU:

Runtime -> Change runtime type.

The actual settings you want (make sure to hit save):

Python 3 + GPU are the settings you want. Please click save!

For any more clarity about the above steps, I suggest reading Fast.AI’s documentation here.

Explanation of Code in Google Colab

In the Jupyter Notebook, I edited some of the code (and there exists an explanation in the file itself). The first part of the code is to run import statements that allow you to use Fast AI in Google Colab. To run code in Google Colab, simply click the playbutton that’s to the left of each code block.

The code starts by importing the Fast.AI library, updating the software, and allowing Google to access data on Google Drive. This is the code to do so:

from google.colab import drive
drive.mount(‘/content/gdrive’, force_remount=True)
root_dir = “/content/gdrive/My Drive/”
base_dir = root_dir + ‘fastai-v3/’

This will require you to enter an authorization code (which you get by visiting a specific URL provided after you run the command). I created a directory in Google Drive called fastai-v3that would contain all my work. This code allows your Jupyter Notebook file to access that repository.

Next, you need to acquire training photos from Google Images. This is done by pasting the following code into the browser’s console after scrolling through Google Images:

urls=Array.from(document.querySelectorAll(‘.rg_i’)).map(el=> el.hasAttribute(‘data-src’)?el.getAttribute(‘data-src’):el.getAttribute(‘data-iurl’));
window.open(‘data:text/csv;charset=utf-8,’ + escape(urls.join(‘\n’)));

The next step is creating directories and uploading the URLs of the photos from Google Images into the server. All this is explained in the Python Notebook. The important part is that “you will need to run this cell once per each category,” which means that you need to run a folder cell, and then the path cell code, then another folder cell, then the path folder cell, then the path cell code, and the path folder cell. There is then code to download the images from the URLs to the file paths created, removing images that can’t be opened, and viewing the data.

If everything goes well, by the time you reach the data.show_batch(rows=3,figsize=(7,8)) cell, you would get the following output (your screen will look different depending on what images you’ve utilized for training):

Then, you need to train the model, which utilizes transfer learning via the Resnet34 model. The code will run fit_one_cycle, which fits the model by using large, cyclical learning rates to train models quickly with higher accuracy. Cyclical learning rates are a method to let the learning rate continuously oscillate between reasonable bounds. We then save this to a stage, freeze the model, and check out our results. If things work correctly, we have the following:

Next is running lr_find which explores the learning rate to fit the optimal rate. You then plot this learning rate and see where you get the least amount of loss — which for me is usually between 3e-5 and 3e-4. You input that information into another call to fit_one_cycle. This will look like:

And like this, respectively:

You’ll then be able to interpret your results like the following:

After this, you need to clean up the code (e.g. removing images, prune top losses). The notebook will walk you through deleting duplicate images. Once this step is completed, the model can be put into production. The command learn.export() enables you to export the content of the learner object for production as an export.pkl file. You can then create a routing for web applications to utilize this app.

Deploying Code in the Current Repository

The previous steps were for building the custom, single-object classifier using Fast.AI. But what about deploying the model in a web application? This repo can be used as a starting point to deploy fast.ai models on Render. Render is one way to host the .pklfiles on the web; it’s a fast and quick way to test deployment (and doesn’t require money initially). The first step is uploading the .pkl file on Google Drive and making the link publicly accessible. This is accomplished by using the following website. Now, before I continue, it’s probably best that you run stuff locally before running it through Render. If you insist on running through Render, I’d continue with the tutorial/guide linked below to Fast AI’s blog. The next instructions are assuming that you want to run locally.

To continue with running things locally, you should run the following commands in terminal:

python3.7 -m pip install aiohttp
pip install uvicorn
pip install torch torchvision
sudo pip install http://download.pytorch.org/whl/cpu/torch-1.0.0-cp36-cp36m-linux_x86_64.whl
sudo pip install fastai
pip install starlette
pip install aiofiles
pip install python-multipart — user

If there are still issues, it’s because of problems with PyTorch and OSX. Try installing Fast AI via Anaconda:

conda install -c pytorch -c fastai fastai jupyter

If you’re getting issues regarding permissions, try running the pip command again with the — usertagline!

Now, edit the file server.py inside the app directory and update the export_file_url variable with the URL from the Google Drive download link. In the same file, update the line classes = [‘black’, ‘grizzly’, ‘teddys’] with the classes you expect from your model. Your code should look something like this:

Now, run the following code and head to http://0.0.0.0:5000/:

python app/server.py serve

If everything goes well, your code should look like the following:

The guide for production deployment to Render is here as well if needed.

References/Acknowledgments

Jeremey Howard, Rachel Thomas, and Sylvain Gugger’s Fast.AI code and MOOC (massive open online course) were incredibly useful and very well put together. I utilized their code and instructions from the first two lessons of their online course to create one of the object classification models. Additionally, I’d like to thank Allison Youngdahl for help with troubleshooting and proofreading. Finally, I’d like to thank Tarun Bhatia for his help in helping me set up Google Colab properly to work with Fast AI.

Engineer (HMC ’19), DJ, DDR Addict, Cheese Aficionado, and Polyglot. Interested in the intersections of technology and society. Views are my own.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store