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.
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,
- https://www.youtube.com/watch?v=2d5LzJNj46w&t=292s
- https://www.youtube.com/channel/UCRH_z-oM0LROvHPe_KYR4Wg
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
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"
}
]
As mentioned above we can invoke REST API from anywhere anytime.
Output
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.
Summary
- We set up GCP using provided links.
- Then we upload Keras model to Google cloud server.
- Then we upload flask_app.py file to Google cloud server.
- we used ‘python3 flask_app.py’ 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