Deep learning based web application: from data collection to deployment
By: Davide Burba
Tags: Free Gpu, Web App Development, Data Science, Deep Learning, Image Classification

The second technique consists in using pre-trained models, in our case ResNet34. This network has been trained on the ImageNet dataset to detect thousand of classes. We can therefore expect that the representation of the image given by the last hidden layer “captures” a large variety of complex patterns, i.e. it provides smart features.

Even if these concepts may not seem completely clear, this shouldn’t stop us to deploy our app. Indeed, the fast.ai library allows for these techniques to become extremely accessible.

In order to have free access to a GPU without the need for a credit card, we can exploit Google Colab, which is a service provided by Google and hence highly integrated with Google Drive.

Creating and running a Jupyter Notebook in Google Colab is pretty straightforward. To enable GPU press “runtime”, “runtime type” and set the “hardware accelerator” to GPU.

However, we also need to:

Connect with Drive Communicate with Git/GitHub

Once we put our data in Google Drive, to access it from Colab Notebook we may simply run the following code:

Now we can load data from “path”.

To use Github from Drive we basically use a Colab Notebook as command line, as described here. Personally I have used a Colab notebook outside the projects which I have called git_comunicator.ipynb.

So, to clone a Github repository we simply have to run the following from a notebook in Colab:

Once we are done with modifications, we can commit and push modifications running the usual git commands (preceeded by “!”) from a Colab notebook cell.

Once we have created a GitHub repository for our project, we are able to clone it on Google Drive, run existing notebooks in Colab exploiting a free GPU and commit and push the changes!

Now that we’ve solved technicalities, it’s time to train our classifier. Again, fast.ai makes it super easy. First, let’s load data:

This code creates a DataBunch object:

keeping 20% of data for validation applying standard data augmentation techniques applying the ImageNet dataset normalization

Good, now we would like to use the ResNet34 network architecture with pre-trained weights, and train only the connections of the last hidden layer with the output layer. This can be done in two lines of code:

This code uses the fit-one-cycle policy, which is a smart technique to train a network in a short amount of time by using a learning rate which progressively increases at first, and later progressively decreases. This led us to the following results:

We reached almost 93% of accuracy on validation set in less than 6 minutes. However, we can observe that the training loss is still greater than the validation loss. This means that there’s still space for improving!

It may be worth to train the whole network now, not only the last layer. This can be achieved by, again, two lines of code:

Setting a slice instead of a single value for the maximum learning rate in the policy allows to train the network with different learning rates. The first layers will be trained with smaller learning rates, since they “detect” simple patterns that should not change much amongst different image data distributions.

So let’s have a look at the confusion matrix we obtained now:

Wow, this is 98.73 % of accuracy on the validation set!

If we were to use this as a professional application we would have to provide a separate test set to estimate the performance of our classifier. However, for a toy project like ours this result is more than enough!

Since we are satisfied with our model, we can export it for deployment by running:

learn.export()

Everything you need to run the model will be stored in a file named export.pkl, placed into “data” directory in our case.

3. Deployment

If training was easy, deployment is even easier, thanks to Docker and Render.

Docker is a tool which allows to easily deploy and run applications by using containers. In simple words it is a sort of Virtual Machine that, rather than virtualizing the entire OS, uses the same Linux kernel as the system that is running on, resulting in improved performance.

Render is a web-service that allows to build and run web applications extremely easily (among other things).

Again, we don’t even need to understand how these things actually work. We can simply use the sample code provided here by fast.ai by adding it to our repository (or forking it from the beginning).

To deploy on Render it is enough to:

Create a Render Account and connect it to Github Upload the trained model file export.pkl in Google Drive and make it downloadable Edit model_file_url variable with the URL to export.pkl and update the line classes in the file server.py Edit index.html to change website text and layout (Optional) Start a new Service

All these steps are described in details here.

Finally you should now have a working deep-learning based web application! It should look something like this: