Deploy Keras model to Flask App REST APIs

Source: Deep Learning on Medium

Deploy Keras model to Flask App REST APIs

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: https://towardsdatascience.com/deploying-keras-models-using-tensorflow-serving-and-flask-508ba00f1037

Part 3

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

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 flask_app.py

You can find code for flask_app.py, here.

## Initialise Flask
app = flask.Flask(__name__)
## load model
@app.before_first_request
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:
pass
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"))
app.run(host='0.0.0.0', port=5001, debug=True, threaded=True)

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

python3 flask_app.py
“python3 flask_app.py” command to start flask app

REST-API description

Request URL: http://34.93.214.159:5001/pingserver

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

Request URL: http://34.93.214.159:5001/predict
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"
}
]