Deploy Keras model to Flask App REST APIs

Pranay Sawant
Jan 1 · 4 min read

Hello again,

This blog is a continuation of the previous blog name “Memes Detection Android App using Deep Learning”. In this blog, we are going to deploy the model on a Google Cloud server.

Please go through my previous blog to understand the business problems and machine learning formulation for this problem.

image borrows from:

Part 3

As we have deployed TFlite model on an android platform, now we are going to deploy it on Flask app.

Click here for part1 and here for part2

This easiest task than deploying model on android platform.

Firstly we will create a VM instance on Google cloud server. If you are not familiar with GCP then we can recommend you, please watch below videos,

Note: It is mandatory to open TCP port. We consider 5001 port for a flask. You can find Firewall rules from here.

After GCP VM instance ready to use. We assume that you have set up VM instance.

If you remember at the end of part1 we stored Keras fine-tuned model. pick up that saved model and store it to Google VM instance.

For those of you who don't know anything about flask, please refer below URL here1. also here, also here

Also, upload

You can find code for, here.

## Initialise Flask
app = flask.Flask(__name__)
## load model
def load_model_keras_model():
global model
model = load_model('/home/pranay/model.v1.h5')

Then predict method as follows,

def predict_label(f):class_lable = ''try:
img = image.load_img(f, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
prediction = model.predict(x)
class_lable = CATEGORIES[int(prediction[0][0])]
except Exception as e:
print("Exception---->", e)
return class_lable

Then we can initialize flask as follows

if __name__ == "__main__":
print(("* Loading Keras model and Flask starting server..."
"please wait until server has fully started"))'', port=5001, debug=True, threaded=True)

we can start flask app using below command in a terminal,

“python3” command to start flask app

REST-API description

Request URL:

"response_code": "200",
"response_message": "Pong from server",
"response_root": "Success"

Request URL:
Request Params: Multipart, file

Success Response for 'NOT MEME'.
"imagePath": "51890381_2377844502226231_5633013788123856896_n.jpg",
"memeStatus": "Not Meme",
"response_code": "200",
"response_message": "Valid File",
"response_root": "Success"
Success Response for 'MEME'.[
"imagePath": "3gknn7.jpg",
"memeStatus": "Meme",
"response_code": "200",
"response_message": "Valid File",
"response_root": "Success"

Below is an error response.

"response_code": "400",
"response_message": "No file part in the request",
"response_root": "Error"
## invalid type ## error response[
"response_code": "400",
"response_message": "incompatible file extension part in the request",
"response_root": "Error"

As mentioned above we can invoke REST API from anywhere anytime.


1. Check Single Image on Server(Flask REST API)

It means fine-tuned VGG19 model is running a flask GCP server and test only a single image.

2. Check Multiple Image on Server(Flask REST API)

It means fine-tuned VGG19 model is running flask GCP server and testing on multiple images.

An image that is marked as Meme is denoted using this STAMP image.

Meme images denoted by “Meme STAMP”


  • We set up GCP using provided links.
  • Then we upload Keras model to Google cloud server.
  • Then we upload file to Google cloud server.
  • we used ‘python3’ command to start flask app.
  • We can invoke REST-API as mentioned in above.

So anyone can take advantage of the above REST API.

Important Links

You can check out the similar interesting blog here and here.

Github source code can be found here.

Thank you for your time.

Please appreciate our efforts if you like this blog.

Special thanks to

Pranay Sawant

Written by

I am the one who wants to deep dive into the AI (Artificial Intelligence ) ocean.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade