Solving the Famous problem of Puppy vs Muffin using a simple Web application on Google App…

Original article was published on Deep Learning on Medium


Solving the Famous problem of Puppy vs Muffin using a simple Web application on Google App Engine/Fastai

Using Fastai , Flask and deploy on Google Cloud Platform for free

I had just started doing the awesome Deep Learning course on www.fast.ai by Jeremy Howard and was looking to practice what I learned.That’s when one of my friends shared the above image in a Whatsapp group.

At first sight, it looked very interesting. But my curiosity started picking up. Would this actually confuse a machine learning algorithm or the AI ? That triggered my journey for this project. I wanted to answer my curiosity and then show it to my friends too ( without the common man knowing how to use AI , what’s the use of it ? ). So once I learned the machine learning does not get confused, I created a simple web application too for this.

I will walk you through each of the steps I followed. ( I am a beginner as well , I understand the pain 🙂 ) . I have gone through this as much as detailed as I can. I hope you all find this useful.

1. Create a Deep Learning model to train the data.

My first step in the process is to create a deep learning model. Thanks to Jeremy and fastai , this can be achieved with minimal lines of code.

For any deep learning model to work, you need to have GPUs. GPUs can be extremely costly. So buying one and setting up on your machine can be daunting. Don’t worry — Google got your back. Google offers free GPU in colabs. You can quickly start creating your app.

I used colab for training my model. You just need to have a google account. Colab also provides you a JUPYTER style notebook. You can see the notebook in my GitHub Repo — Notebook

  1. Colab is free and can be accessed using your google account
  2. Do not forget to turn on GPU under Runtime >Change Runtime Type > Select Hardware accelerator as GPU. This will be automatically shutdown if you dont use it for few minutes. Since Google is providing this free, it would be a good idea to stop when we do not use it 🙂 .

2. Export the Model

I use the below command to export my model. Pickling is a method of saving the object by serializing and de-serializing a Python object which can be reconstructed later. We will be using the same approach for this model.

If you had mounted the google drive, this file should be stored in your google drive. For mounting use the below script. It will take you to your google account and do authorization using a key.

3. Create the Flask API Python program, HTML files and save on Github

This was my most toughest step as I was doing all this along with deploying it on the Google cloud platform since my local machine was not working with the fast ai libraries. So I was testing everything out in production 🙂 .

  1. Create the entry Python (main.py) file as below. It has the below functionalities
  • Load the pickled object which we saved after modelling.
  • Accept the input image file from HTML using request.files
  • Call the predict_single function for predicting the output
  • Passing the output back to the results.html file.

If you notice, I have added debug statements since it took many trial and error before I could get it working on GCP (Google Cloud).

2. Create the index and results.html file

The index.html file is the first web page where we input our prediction image. The app accepts any image in any format (JPEG, PNG etc.)

Once the file is loaded and “Predict the image” is hit, it shows the results.

3. Save all the files in GitHUB :

  • Upload the main.py to the main folder.
  • Create a folder “templates” and add the index.html and results.html to this folder. The Flask API looks for HTML files in the “templates” folder by default.
  • Create a Model folder and upload the trained_model.pkl file over there. Please note this file is of a big size and you cannot upload it using normal git add,commit etc. You need to install git LFS before uploading this file.
  • Create requirements.txt which has all the libraries required for executing this project and upload it to the root.
  • Create app.yaml file. This is needed for the Google Cloud Platform to set up the environment. We mention the Python version here. The instance_class has to be the highest . This link will give you an idea of various instances. Every other instance failed for me.

4. Setup the GCP ( Google Cloud Platform)

Similar to Colab, Google cloud platform can be accessed with your google account. Google gives $300 as free credit for the first year.

  • Sign into GCP and click on Console. If you see the below screen, thats where you have to be.
  • When you first login Google will ask if you want to follow the tutorial -Click Yes and follow the steps in the tutorial. By the end of the tutorial, you should have created a project and know how to open a Google CLI.
  • Click on the highlighted monitor shell image on top right corner of the console to start the CLI.
  • Clone GitHUB repo created earlier to the local folder. Once again, do not forget to install git lfs. Else, it will not copy the repo completely.
  • Once the repo is cloned and file is created, enter the folder by giving “cd <your-project-name>
  • Type ‘gcloud app deploy’ and hit enter. This will deploy your app on cloud. It should take few minutes. You should be able to see something like below. Give “Y” for continuing.
  • If it fails, go to the logs. Either, it will be displayed there or you can go to Main Menu > Operations > Logging.
  • Within few minutes, you app should be up and running. You can access the link for app by giving the command “gcloud app browse”. Click on the link and you can test it out by giving a file of your

Conclusion:

I was able to conclude the machine learning model does not get confused and it predicts the Puppy/Muffin with 100 % accuracy.

I have just finished lesson 1 out of 7 www.fast.ai deep learning course. I am highly satisfied with the learning.

I was able to build the deep learning model pretty easy for this. Most of my time was spent on setting up the web application on Google Cloud as I did not have any prior experience with FLASK,Google Cloud nor HTML files. This was a great learning experience for me. I hope you all enjoy this.

  • If you liked this, please do clap as this will motivate me to write more :).
  • If you have any comments, please do let me know. I will answer to the best of my knowledge.

My Git HUB repo for this project can be found in this link Git hub Repo

Thanks to the below articles

Without Google and the below articles, I would have found it difficult to do this project .

1. Excellent piece by Aakansha on how to deploy as FAST ai model on render.com,firebase — Link

2. How to Deploy Flask APIs to Google App Engine

3. Deploy ML App with Flask