A simple guide to deploy deep learning models as a web api
This blog will explain you how to create your own image dataset, train a model on it and deploy it as a web api using binder.
Table Of Contents
· Gathering Data
· From Data to Dataloaders
· Data Augmentation
· Training the model
· Turning the model into an online application
· Deploying the app using MyBinder
· Future Work
· Reference
Gathering Data
For many types of projects, you may be able to find the data on Kaggle or on any other website . The project we’ll be completing in this blog is a shoes classifier. It will classify between four types of shoes — Nike Air Jordan, Adidas Yeezy, Gucci and Balenciaga . We just need a way to find them and download them . I’ll be using image scrapper library in python to scrap images from the internet. You can install the library from anaconda terminal through pip just by typing pip install -q jmd_imagescraper .
The above code will first create a directory test_images in the current working directory and it will download 100 images for every search term we mentioned in a list and save them in a sub directory which will be named after our search term.
From Data to Dataloaders
I’ll be using fastai’s dataloaders to provide the data for the model. Here’s what we need to create a dataloader for the the dataset we just downloaded.
Let’s look at each argument one by one. First , I’m providing a tuple which specifies the independent and dependent variable. In this case our independent variable is the set of images and dependent variable are the categories. Also we don’t have the separate validation/test set so I’m randomly splitting our images into train and test set by calling splitter. For labeling the images, I’m passing get_y = parent_label that simply gets the name of the folder a image is in. We put our shoes images into folders based on different types of shoes so this is going to give us the label we need. In the end , I’m resizing every image to 128 pixels. Now our dataloader is set and i just need to give the path of our data.
A dataloader includes both training and validation dataloaders. In the above code i called the validation dataloaders that will provide us eight images in two rows.
Data Augmentation
As all the images are now of the same size(128 pixels), i’ll be augmenting the images such as flipping them , changing contrast and brightness etc. All this can be easily done by calling the fastai’s aug_transform function. To tell fastai that we have to use this transformation on a batch of images, I’ll be using batch_tfms parameter.
Training the model
We are all set to finally train the model as we’ve gathered all the data, labeled themand called the fastai’s dataloaders to load them. I’ll now create the learner and fine tune ResNet for our training.
I ran the fine tuned model for four epochs on Google Colab and it took less than 5 seconds for every epoch as we don’t have a lot of data (100 images for every class). But despite that our model performed very well and debunked the myth that neural networks require a lot of data to perform well. You just need good labeled data instead of more data.
In the above pic you can clearly see our validation loss keeps on reducing after every epoch and error rate also keep on decreasing. Certainly we can run the model for even more epochs to increase its performance.
Now let’s see what mistakes our model is making. To visualize it, I created a confusion matrix. The rows represent all the different types of shoes in our dataset and the columns represent the images that our model predicted as adidas yeezy, balenciaga , gucci and nike air jordan . The diagonals of the confusion matrix shows the images that were correctly classified. With the color coding, the goal is to have white everywhere except the diagonals. Our shoe classifier isn’t making many mistakes. We can go even one step further and check the top n losses i.e fastai library allows us to see the images in which the loss was higher .
In the above image you can see top 5 shoe images in which the loss was highest. The top left shoe belongs to the class adidas yeezy but our model predicted it to be balenciaga with a probability of 0.76 and hence the loss in this image was 3.89.
Turning the model into an online application
We are now going to convert this model into a working online application. The first step will be to save the model architecture and its trained parameters. It is very easy to do it, the code below does just that.
The export method saves both the architecture and the trained parameters of the model. Before deploying the model, I’ll be first showing you how to create a similar app in the jupyter notebook. To do so, I’ll be using ipywidgets.
The above code converts our model into a simple application . The output will be the predicted shoe class and the probability for the same .
The above image is the output of the function we created to convert our model into a simple application. On clicking the upload button , you’ll have to upload an image and after clicking on the classify button , we’ll get three outputs — Image which you uploaded, predicted shoe class for it and the probability for the same . We have done 95% of the work . Now we just need to save our code in the Github repository and use mybinder to convert the jupyter notebook into an application.
Deploying the app using MyBinder
To publish our web app on Binder we just have to follow these steps —
- Upload the notebook to a Github repository .
- Paste the URL of that repository into Binder’s URL field
- Change the file drop -down to URL.
- In the ‘URL to open’ field enter voila/render/notebook_name.ipynb (replace notebook_name with the name of your jupyter notebook).
- Click launch and copy the url provided .
It will take around 5 minutes to build the site and afterwards it will navigate the browser to your new web app. And that’s it , deploying a deep learning model as a web app is very easy.
Future Work
In this blog we learned how to deploy it on binder without any customization. In the next blog we’ll see how to deploy any deep learning model on Heroku and how to use html tools to make the web app more attractive .
Reference
You can check out all the details and code from my github repo —
Connect with me on Linkedin -