JoelKronander / TensorFlask

A simple webservice API for classifying MNIST digits from HTTP POST requests built using Flask and TensorFlow/Layer
Apache License 2.0
23 stars 6 forks source link

TensorFlask

A simple web service classifying MNIST digits from HTTP POST requests built using Flask, TensorFlow and TensorLayer

The API uses HTTP POST operations to classify images of handwritten MNIST digits that is sent in the request. The single POST request available is /mnist/classify. The API uses JSON for both requests and responses, see below for a detailed specification of the JSON data format. Currently the API only supports 28 by 28 grayscale images and only handles a set maximum batch size of images in each request (the max batch size is currently set as a constant in server_application.py).

JSON request format

The HTTP POST request /mnist/classify expects a JSON request. Example JSON data for the request:

{
  "requests":[
      {
        "image":"/9j/7QBEUGhvdG9...image contents...eYxxxzj/Coa6Bax//Z"
      },
      {
        "image":"/9j/7QBEUGhvdG9...image contents...eYxxxzj/Coa6Bax//Z"
      }
  ]
}

JSON response format

{
  "responses":[
      {
        "class":1,
        "probability":0.98
      },
      {
        "class":8,
        "probability":0.99
      }
  ]
}

Installing requirements

The API uses python3 and the requirements can be installed by

    $pip3 install -r requirements.txt

Running the server

The Flask application can be deployed using e.g. gunicorn using:

    $gunicorn server_application:app

Running the simple test client

After starting the server requests can be sent using the test client. For detailed use of the test client see:

    $python3 simple_client.py --help

Example use, downloading 10 MNIST images and submitting them as a request for classification to a local server:

    $python3 simple_client.py --download_mnist --server=http://127.0.0.1:8000/

Training new model parameters

For convinience pretrained model parameters for the convolutional neural network is supplied in the model/ directory. However, the model can also be retrained by running the train_model.py script in the model directory.

    $python3 python3 train_model.py