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

Ashok Tankala
Coinmonks
4 min readJul 15, 2018

--

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

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

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.

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

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.

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.

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.

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

--

--

Ashok Tankala
Coinmonks

I help aspiring & emerging leaders gain clarity & reach their potential so they can build a fulfilling life both personally and professionally - http://tanka.la