Introduction to RESTful API with Tensorflow Serving

I described how to serve trained tensorflow models with tensorflow serving in Serving Pre-Modeled and Custom Tensorflow Estimator with Tensorflow Serving before. In the article, I explained how to make tensorflow models with estimator and how to serve the models with tensorflow serving and docker. And tensorflow serving starts supporting the RESTful API feature at the version 1.8 in addition to gRPC API. So, I would like to describe how to server RESTful APIs with tensorflow serving.
In this article, I will give you a hands-on about the RESTful API feature. The goal is to serve an iris classifier with HTTP/REST API with tensorflow model serving. To do that, we have the three main steps as below:

  • Create a tensorflow model for iris data with tensorflow estimator.
  • Launch tensorflow model server with HTTP/REST API on docker.
  • Request to the HTTP/REST API.

I extended the repository which was used to explain gRPC API with tensorflow serving before in order to support RESTful API.

Train Model

First of all, we will train a model to classify iris data. I explained how to train a tensorflow model for tensorflow serving before. So, I would like to focus on making the RESTful API.

First but not least, designing the input is the most important. As you probably know, iris data has fourfeatures: sepal length, sepal width, petal length and petal width. The four features should be the input items of RESTful API. Its serving_input_receiver_fn can be like below. We define the input items withreceiver_tensors . That is, the inputs are sepal_length , sepal_width, petal_length and petal_width. Each feature can be a list of float values. And to keep the format of features consistent with the training data, those four features are concatinated before putting into tensorflow graph. The full code is iris_premodeled_estimator.py.

The following command enables us to train models. The checkpoints and exported models will be created under. /models/iris_premodeled_estimator . As well as a model for tensorflow model serving is under a directory whose path is like. /model/iris_premodeled_estimator/export/models/1529121297 .

As I described how to show signatures of tensorflow trained model before, saved_model_cli.py enables us to show the specification of API. The result should be like below.

That is, the request body of HTTP/REST will be like below.

Offer RESTful API with Tensorflow Serving

As we offer the gRPC server with docker, we will offer HTTP/REST API with docker as well. All we have to extend the Dockerfile is just to add options of tensorflow_model_server. Tensorflow serving 1.8 supports — rest_api_port which is used to specify the port number of REST API. The default value is 0. That is, it doesn’t offer REST API by default. The Dockerfile is here.

After building a docker image with the Dockerfile, we will prepare for the served model. First, we make a directory to share between the local machine and a docker container. Next, we choose a trained model to serve and then copy them to the directory.

Now we are ready to offer not only gRPC API, but also HTTP/REST API with docker. The following command is an example to run a docker container, sharing the model directory between the local machine and the docker container. Where, MODEL_NAME is used to set model name and MODEL_PATH is used for specifying the path to trained models.

Request to RESTful API with Tensorflow Serving

You can see the documentation about RESTful API specification of tensorflow serving in here. TensorFlow ModelServer running on host:port accepts following REST API requests:

We have launched the docker container to offer the RESTful API to classify iris data. Now let’s request to the HTTP/REST API with curl . Where the model name is iris and the version is 1. The JSON object is in the -d options. As a result, we can get the class IDs, probabilities to each class and so on.

Summary

Thanks to tensorflow serving, once we got great models with tensorflow, tensorflow serving enables us to create not only gRPC API, but also RESTful API automatically. That is, we machine learning guys can more focus on building models.

If you would like to get familiar with it, the official document describes the request and response body of JSON object to request.

Written by

Machine Learning Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store