TensorFlow serving with canned Estimator

I’ve had a few attempts at getting TensorFlow estimators into a serving host and a client I can use to query them. Finally getting it working, I thought I’d write up the steps for reproduction.

Assumptions and Prerequisites

The first assumption is that you have already trained your estimator (say the tf.estimator.DNNRegressor) and this is now in the variable estimator. You also have a list of feature columns as is standard in a variable feature_columns. The example here is basically the same as my trained estimator. You will also have the tensorflow serving source code locally. If not, you can grab it and all the submodules by running git clone --recursive https://github.com/tensorflow/serving.git --depth=1. n.b. you will need the path to this later (mine is at ~/developer/serving). Finally, you will also need to have the gRPC tools installed and can be installed by running pip3 install grpcio-tools

Exporting the estimator

First step is to export your trained model with the graph, variables and input variable spec for serving. Firstly the code and then an explanation afterwards:

The feature_spec tells TensorFlow how to deserialize an incoming TFRecord for the request. Using the feature_columns here we can generate this to reflect our feature columns we setup for training our estimator. Next the export_input_fn defines the entrypoint for an incoming request to your model and is mainly about the deserializing of inputs and adding tensors to your graph to be executed. Finally we export this to an ‘exports’ folder locally.

Hosting the TF Serving Server

I setup a Dockerfile to copy the export outputs and serve the model. I also found that the recommended Dockerfile to have a lot of dependencies I didn’t end up requiring. My final Dockerfile was:

It uses the tensorflow-model-server apt package rather than building from source and copies in the export outputs to have a self-contained host for our currently trained model. We can then build and run our docker host by:

Creating a python client

Our python client needs to query the server via gRPC and we will need a few proto files from the serving repository to generate some code for us. Using the grpc tools we mentioned in prequisites, run this command to generate the client libraries from the tf serving source (n.b. current directory is alongisde my python estimator source):

Above we create the prediction service client stub for connecting to localhost:8500 as we setup in our docker file above. We also define a helper function to serialize our values to a TFRecord feature used to pass to our host. And the request (my DNNRegressor was from a movie recommendation tutorial so we take two integers ‘userId’ and ‘movieId’ and produce a predicted rating):

Firstly we setup the request and define the model name and signature for the request. You can find your model name from the docker logs and mine looked like “Approving load for servable version {name: default version: 1509419744}” and hence used the name ‘default’. You can find all the information about the signature_name using the saved_model_cli. Next we construct and serialize our feature to be passed to the host and make the request. Finally we just compare it with our training data from a pandas dataframe.

Summary

TensorFlow serving still has a way to go until it’s a 1, 2, 3 step setup but once completed, this could be automated in a CI system. Also my client was responding in 6.3ms running on my MacBook Pro, so performance is not an issue for my circumstances. Next step will be to get this running with a custom estimator by setting the export_outputs appropriately.