The Soup Classifier : FastAI Course Lesson 1

Trent Lucier
6 min readNov 29, 2022

--

This article is a summary of my experience building a simple image classifier app based on learnings from the first 1 & ½ lessons from FastAI’s “Practical Deep Learning For Coders” course. The class is totally free and provides excellent project resources, so I will not rehash all the details here. Rather, I want to highlight the stumbling blocks I encountered and hopefully make the process more speedy for others. The issues were almost entirely related to environment setup and not deep learning / ML programming.

What I Built

Since winter is coming here in New England, I had hot soup on my mind. Specifically, Korean and Vietnamese soup. This made me wonder: could AI distinguish between these two types of soup just by looking at photos? Below is the final UX of my experiment. You can play with it here:

https://huggingface.co/spaces/TrentL/KoreanVietnameseSoup

Development Pipeline

The tools I used for this project were:

  • Kaggle: Create a Python notebook to build, train, and export an image classifier model as a .pkl file.
  • HuggingFace Spaces and Gradio: This popular ML model site can also host simple web apps useful for demonstrating models. Since I wanted to develop locally first, I also used…
  • Visual Studio Code / Python / Windows: My local development environment was Visual Studio Code on Windows, and the coding was done in Python. This proved to be the biggest stumbling block as you’ll see, but I think I ironed out most of the details.

Kaggle

Using Kaggle was very straightforward and I did not have any problems. I have basic experience with Python Notebooks, so creating one in Kaggle was not entirely new to me.

My code was almost entirely copy-and-pasted from the course’s “Is It a Bird?” notebook. I strongly recommend starting with that project and just changing the subject to something that interests you. All I changed were some strings and variable names to better relate to my domain.

The end result was to export a trained model that could distinguish between Korean and Vietnamese soup. The model is in a Python pkl file, which you will need to download and add to your HuggingFace project.

Unknown to me, some part of the process seems to add in platform-dependent path formatting to the model. I will return to this problem later in this article when I talk about VSCode, Python, and Windows.

HuggingFace Spaces & Gradio

This is where things started to get new for me. In the past I have downloaded some Stable Diffusion models from HuggingFace, but I never used it for anything more complicated than that. I certainly had never heard of Gradio before.

HuggingFace Spaces is a convenient tool for hosting a web-based ML app. This makes it very easy to make demos and get feedback from the community. I chose Gradio as the UI library to implement my app in.

I created a new Space with Gradio and the end result was an empty app that is also a Git repo. The next step was to clone this repo locally so I could build my app.

Visual Studio Code, Python, Gradio

Here is where the “fun” begins 🙃

To tell you the truth, when I started this step, I still had no idea what I was even trying to do. What was Gradio? Did I need it locally? Am I supposed to have my app retrain all my data in HuggingFace first? This article may describe my experience in linear steps, but there was a considerable amount of jumping around for a few evenings. Let this be a lesson to just keep pushing on when you have doubts.

The goal of this step is to develop our Gradio app locally so we can quickly and easily test it. Once it works, all we have to do is push it to the HuggingFace Spaces repo and voila! The rest of the world can play with it, too.

The first step is to install VSCode and add the Python plugin. After that, clone your HuggingFace app by simply using its URL:

Create a new app.py file in your workspace with the following code:

import gradio as gr

def greet(name):
return "Hello " + name + "!!"

iface = gr.Interface(fn=greet, inputs="text", outputs="text")
iface.launch()

Run it and observe the nice error message:

This error message shouldn’t be a surprise since we haven’t installed gradio. But hold your horses and don’t go firing off your “pip install” commands willy nilly. First we want to setup a project-specific Python environment.

In order to prevent your Python workspaces from devolving into dependency hell, Python supports the concept of an “environment” which is an isolated set of project-specific packages and a Python interpreter. Instructions for creating an environment in Visual Studio can be found here.

Unfortunately, even after creating your Python environment, it is very likely that your new environment still won’t be used when you actually run your script. This is due to some wonky Windows permissions issues. My solution was to use the full path of the Python interpreter from my project’s environment to run pip install for the gradio package. All the tutorials I read implied that opening a terminal in VSCode should automatically use your new Python environment, but that didn’t seem to be the case for me. So to install gradio, I needed to do something like:

PS C:\Code\TrentTestSpace> c:/Code/TrentTestSpace/.venv/Scripts/python.exe  -m pip install gradio

If you have trouble with permissions, I used the following commands (use your project folder instead of TrentTestSpace):

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process

& c:/Code/TrentTestSpace/.venv/Scripts/Activate.ps1

Now if you run your app.py file, hopefully you will see the server start and a URL printed to the console. Paste this URL into a web browser and verify that basic “Hello World” functionality is working.

One more thing: remember to update the requirements.txt file with the packages you added so the HuggingFace server knows to install them. You can see my example requirements.txt.

From here, I added the fastai library (install it with pip same as above) to load my model’s pkl file. This proved to be one final challenge, as apparently the file contains platform specific path formatting. I added the following code to get around it:

plt = platform.system()
print(f"Platform : {plt}")
if plt == 'Windows':
pathlib.PosixPath = pathlib.WindowsPath

You can find my full project code here.

After getting my app to run locally, I committed and pushed it to my HuggingFace app. After a few minutes, your app should be running on HuggingFace:

And that concludes my first mini project for the FastAI course. Hopefully this article will unblock someone who runs into similar issues.

--

--