Deploy your First Deep Learning Neural Network Model using Flask, Keras, TensorFlow in Python

Recently I built a deep learning model for my company predicting whether the user buys a car or not if yes then which car with good accuracy. Gave
demo to CEO & business team everyone got excited about it. We did testing also and it gave promising results.

Everyone pumped up and asked me to share API for the model so that we can spend our company focus on the probable buyers and also can plan to give a better consumer experience.

First, we used the Tornado framework to deploy our deep learning model. It used to load the model every time it gets API hit due to that response time is
very slow. We are new python so we did bit googling but didn’t find any solution. Then I saw Flask framework easy to use and we can declare a global variable and can load only once which can be reused whenever we want.

Let me share steps to you guys how to do it & how simple it is.

I am assuming you built your deep learning model already if not please refer my previous article to learn how to build deep learning model.

Install flask for deploying your deep learning model using following command

pip3 install flask

Note: I didn’t used Scaling technique on the data at the time of training the model so that when we give data on API we don’t need to transform anything. So I skipped Step3 in my previous article.

Let’s get into the beautiful part — coding part

import flask
import numpy as np
import tensorflow as tf
from keras.models import load_model

Step1: Usual Imports. flask for API server. numpy for input parameters array reshaping. tensorflow for running the deep learning model. load_model from karas.models for loading keras model.

# initialize our Flask application and the Keras model
app = flask.Flask(__name__)
def init():
    global model,graph
    # load the pre-trained Keras model
    model = load_model('models/gotCharactersDeathPredictions.h5')
    graph = tf.get_default_graph()

Step2: For initializing our Flask application and to load the Keras model.

# Getting Parameters
def getParameters():
    parameters = []
    return parameters

Step3: Extracting parameters which required for prediction(male, book1, book2, book3, book4, book5, isMarried, isNoble, numDeadRelations, boolDeadRelations, isPopular, popularity) of a character and appending them in the array.

# Cross origin support
def sendResponse(responseObj):
    response = flask.jsonify(responseObj)
    response.headers.add('Access-Control-Allow-Origin', '*')
    response.headers.add('Access-Control-Allow-Methods', 'GET')
    response.headers.add('Access-Control-Allow-Headers', 'accept,content-type,Origin,X-Requested-With,Content-Type,access_token,Accept,Authorization,source')
    response.headers.add('Access-Control-Allow-Credentials', True)
    return response

Step4: Jsonifying the response object. Then adding header parameters for supporting cross-origin. Usually, we don’t deploy API in the same domain where we need to use so cross-origin support must at that time that’s why these header parameters required.

# API for prediction
@app.route("/predict", methods=["GET"])
def predict():
    nameOfTheCharacter = flask.request.args.get('name')
    parameters = getParameters()
    inputFeature = np.asarray(parameters).reshape(1, 12)
    with graph.as_default():
        raw_prediction = model.predict(inputFeature)[0][0]
    if raw_prediction > 0.5:
        prediction = 'Alive'
        prediction = 'Dead'
return sendResponse({nameOfTheCharacter: prediction})

Step5: Route for the Prediction API.

First, we are taking the name of the character then the rest of the parameters through getParameters method.

Then reshaped them to ndarray which we can use as input for our model.

Then we are finding the prediction through our model.

Then we are coming to conclusion whether the character is alive or dead.

Then we are preparing response object using sendResponse function and returning it.

# if this is the main thread of execution first load the model and then start the server
if __name__ == "__main__":
    print(("* Loading Keras model and Flask starting server..."
"please wait until server has fully started"))

Step6: This code is for just to make sure If this is the main thread of execution first load the model and then start the server

To run the application you just need to run below command that’s it.


Once the server is up I hit below URL in the browser.

http://localhost:5000/predict?name=Viserys II Targaryen&male=1&book1=0&book2=0&book3=0&book4=0&book5=0&isMarried=0&isNoble=0&numDeadRelations=11&boolDeadRelations=1&isPopular=1&popularity=0.605351170568561

Congratulations!!! Your first deep learning model deployed. You will find this example & couple of others at my GitHub repository.

Peace. Happy Coding.

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *