Deploy Deep Learning Model Using Docker, Tensorflow Serving, Nginx and Flask (Part 1)

Ashish Kumar
TheCyPhy
Published in
4 min readAug 27, 2020

Hello curious person, in this series named Deploy Deep Learning we are going to deploy two deep learning models one will classify the Images between Cats and Dogs and the other will Detect the Card type i.e King, Queen, Ace, etc. We will be getting our hands on Docker, Tensorflow Serving, Flask, Nginx, gunicorn. So without any further delay let's get started.

We will be deploying our Models via Tensorflow serving using Docker containers. We are going to create Three Services using Three Docker containers i.e 1. Web, 2. Tensorflow Serving, 3. Nginx

So without any further delay, let us create our first service,

  1. Web

Our Web application will be accessible by user via a web browser. User will be able to create a account in our application, so that he can login and use our application.

Create a folder named “web” and inside that “web” folder create another folder named “flask”

Now create a file named “app.py” inside “flask” folder. Below is the code for “app.py”

Now let us understand our “app.py” line by line,

from Line 1 - 6, We are importing the python libraries we will be using in our flask application

Line 8 - 10, We are importing other helper python scripts which will be created by us. I will explain about them later

Line 12 - 20, We are initializing our flask app, basically attaching it to a sqlite database that will store our User name and Password and defining a login manager for our app which will take care of the login procedures for our users. Note: ‘SECRET_KEY’ should be something complex. I have given an example ‘SECRET_KEY’ here.

Line 24, create_tables() function creates data2.db file which stores Username and Password. flask_app.before_first_request decorator executes this request before any other request to flask

Line 27- 82, We define the functions which will handle the login and logout operations of our Users. At line 58, we are using generate_password_hash() function to hash the Password as storing the password as plain text is a big security mistake.

Line 86, predict() function allows the User to upload an Image into our Web application and gets back the prediction result, i.e whether the uploaded image is of a Cat or a Dog. login_required decorator makes the predict() function available only if the User is logged in.

Line 106, object_detection() function allows the User to upload an Image into our Web application and gets back the prediction result, i.e bounding boxes for the detected cards in the Images. Note: User will not upload the Image via web browser for object_detection() function, but via API endpoint

Line 113, load_user() function tells Flask-Login how to find a specific user from the ID that is stored in their session cookie.

We are done with our “app.py” file. Now we will create “model.py” file in the “flask” folder. “model.py” file contains the functions which will send the User uploaded image to our trained Deep Learning Models which are deployed via Tensorflow Serving

Now let us understand our “model.py” line by line,

Line 1- 10, We import required python libraries

Line 13- 14, “MODEL_URI” defines the URL location for the Image Classifying model which is deployed using Tensorflow Serving. “CLASSES” defines the two classes i.e. Cat and Dog.

Line 16, non_max_suppression() function removes overlapping bounding boxes that are detected by our Object Detection Model in a given Input image. Note: Tensorflow has also inbuilt method for it, i.e. tf.image.non_max_suppression

Line 66, get_prediction() function will take the User uploaded image and sends it to our image classifying model which is deployed via Tensorflow Serving. It gets back the prediction result and interprets the result into one of the Classes i.e. a Cat or a Dog. Please Note, I have pre-trained a model to classify between images of a Cat and a Dog

Line 85, get_objects() function will take the user uploaded image and sends it to our Object Detection model which is also deployed via the same Tensorflow Serving as above. It gets back the prediction result and interprets the result into bounding boxes coordinates and the Type of Card detected for the respective bounding boxes. In this function, from Line 90- 97, I have written codes to send the User uploaded image to Object Detection model via gRPC request from Line 98- 124 I have written code to interpret the prediction result. At Line 110, I am calling the non_max_suppression() function to remove overlapping bounding boxes detected by our Object Detection model. Please note, I have pre-trained my model to detect the following types of Card- Nine, Ten, Jack, Queen, King, Ace. The Dataset which I used to train my model can be found in this link.

We are done with our “model.py” file. Now we will create “dbmodels.py” file in the “flask” folder. “dbmodels.py” file contains the Class User which will create the structure for our database where username and password will be stored.

Now we will create “manage.py” file in the “flask” folder. “manage.py” file allows us to add command-line interface for our flask application.

Now we will create “database.py” file in the “flask” folder. “database.py” file initializes our SQL database. It is created in a separate file as it is imported in various other files created by us

We have done a lot of work until now. It’s time to relax and have a cup of coffee. In the next part of this series, we will be writing HTML pages for our web application and of course our Dockerfile for our first service i.e. Web

Till then enjoy your day :)

If you are ready to go ahead, here is the link for Part 2

References-

  1. https://www.digitalocean.com/community/tutorials/how-to-add-authentication-to-your-app-with-flask-login
  2. https://www.pyimagesearch.com/2015/02/16/faster-non-maximum-suppression-python/

--

--

Ashish Kumar
TheCyPhy

I write and share interesting articles. Follow me if you are an avid reader. Connect with me at https://topmate.io/ashish_kumar17