Simple Flask Web Service for Consuming Keras Model

Rizal Setya Perdana
Work in playground
Published in
3 min readJan 24, 2019

In this note, I will show you the most straightforward way (in my opinion) how to implement a mechanism of consuming a deep learning model for prediction task. Instead of using TensorFlow Serving, this note gives you a simple and clean step by step implementation. First of all, a question arises in my mind when I spend my times more in deep learning, how to consume the model that has been build simply and commonly? I think one of the most used protocols on the internet today, web protocol. In this note, I will build a simple web service that provides a prediction task of images using a pre-trained model, ResNet (Residual Network) based on this paper, using Keras and Flask on Python.

Preparation

  1. Install the following dependency of Python libraries: Tensorflow, Keras, Pillow, Numpy, Flask, and Flask-Cors
  2. Download and install Postman for simulating of consuming web service

Main Python Script for Running Flask

This script is the main file for running Flask that will expose your URL and port and make it accessible from clients. In my design, this script is reusable when you have a new another task just add several lines of code and re-run this script.

The last part, you can modify the hostname where this script is running and can be accessed. By default, this script run on localhost port 5000.

ResNet Image Prediction Script

As I mentioned before, if you have a new task, it is possible to you by adding your own class. In this note, I create a very simple ResNetPredict class that preprocess and predict the image file to get the categorize of class by ResNet. I use ResNet50 from Keras Applications for the simple case. This script imported by the first main script which running Flask (run_flask_service.py).

From your console, run the script run_flask_service.py

Test Endpoint using Postman

The last, to make sure all is running well, for the simplest one, we show you how to use Postman.

  1. Create a new request
  2. Change the request method to POST, but if you just expect your request using the GET method do not make a modification
  3. Set the URL to the URL that appears when you run run_flask_service.py script. In this case, my http://godal.sys.cs.tut.ac.jp:5000/predict_resnet.
  4. On the Body of the request, select form-data
  5. Fill the KEY as the parameter POST name, and change the type of parameter (text or file) by clicking the KEY name
  6. Click on Choose Files, upload your image here
  7. The last, Click on the blue Send button
  8. Voila… the response will be shown on the JSON format
Postman Request Demo

Notes: the biggest probability give me the label “mountain bike” after I send this image

Me on a scary face (photo by Anom Harya)

Access using Javascript Ajax Request

After success on requesting the web service using Postman, I will show you my experiment to prove that the web service endpoint also accessible by Javascript.

Notes: By default, accessing directly on Ajax request will be failed due to cross-origin resource sharing (CORS). Instead of change the web server configuration, I chose by using Flask-Cors for simplicity.

A web-based simple demo for image prediction

Please let me know if you have any questions.

Notes: By default, accessing directly on Ajax request will be failed due to cross-origin resource sharing (CORS). Instead of change the web server configuration, I chose by using Flask-Cors for simplicity.

--

--