Building an image classifier
Hi everyone. I’m going to review the steps in creating and deploying a basic pet breed identifier for cats and dogs.
The guide is mostly based off fast.ai’s chapter 2: Deployment and Dr. Tanishq’s blog. Several changes had to be made due to Gradio’s deprecation of several APIs.
The deployed app follows through these steps:
- Training pet classifier model
- Set up Hugging Faces space repository
- Deploy
Model training
Our model is based on a ResNet50 image classifier trained on Oxford Pets dataset.
from fastai.vision.all import *
path = untar_data(URLs.PETS)
dls = ImageDataLoaders.from_name_re(path, get_image_files(path/'images'), pat='(.+)_\d+.jpg', item_tfms=Resize(460), batch_tfms=aug_transforms(size=224, min_scale=0.75))
learn = vision_learner(dls, models.resnet50, metrics=accuracy)
learn.fine_tune(1)
learn.path = Path('.')
learn.export()
This code creates an export.pkl file. Files that end with .pkl are called pickle and represent preserved Python object. In our case, export.pkl represents our image classifier model.
Hugging Faces space
The next step is to create a Hugging Faces account. Create a space. From my understanding, a hugging space space acts as a repository for your app files and automatically deploys (launches the app) for you.
For those of you who aren’t familiar with repositories, treat it like a file storage for your app file components. Once you place all your files in there, our deployment knows to look for files inside that repository to spin together your app. Another point to take note of is that not only do you have to define app files, you may also need to include the file dependencies (Python packages in this case) as well. This concept has to do with software development and reproducibility. To define these package dependencies, just simply include a requirements.txt file that names your package (and maybe version, I’ll come back to this later). In my case, I had to add fastai
and scikit-image
.
To see the files in my pet identifier app, check here. While it may be more efficient to clone the repository and work locally, I simply uploaded the files through the Hugging Face UI in my space repository (see below). Feel free to download the files in my repository and upload to your own.
Deployment
Gradio provides API interfaces for our app. Gradio allows us to conveniently set up ways for users to upload their own images and see a prediction output. Several API interfaces were deprecated in the 2024 version from the guides I used. Specifically, gr.inputs.Image(shape=(512, 512)
had to be changed to gr.Image(type = 'pil')
, gr.outputs.Label(num_top_classes=3)
changed to gr.Label(num_top_classes=3)
, and interpretation
, enable_queue
were deprecated and were not longer valid arguments. By the time you read this post Gradio may have changed its API again. You can likely fix deployment issues by using an older Gradio version (requirements.txt) or referring to updated documentation. The space will automatically try to launch your app everytime you commit (save) your changes.
Summary
This was an overview of how to deploy an image classifier using Hugging Face and Gradio. You can view my demo here. Feel free to contact me for questions and feedback!