How to Show Signatures of Tensorflow Saved Model

Yu Ishikawa
3 min readApr 18, 2018

How should we manage the documentation about the specifications of trained models? Something like swagger would help machine learning teams with sharing information about that. However, it seems that there is no defacto standard to generate specifications about tensorflow saved models yet.

How can we understand the signatures from a given tensorflow model? We need a staff to understand given saved tensorflow model. In such cases, tensorflow’s SavedModel CLI would help us by showing the signatures of your saved model. In this article, I will explain how to show the signature of inputs/outputs of a tensorflow saved model.

Build saved_model_cli

First of all, you check out the code of tensorflow from github. Then, you compile saved_model_cli with bazel. If you are using mac OS, you can install it with brew install bazel. At the time when I am writing this artible, the tensorflow version is 1.7.0. If you face something wrong, switching the code to r1.7.0 would probably work.

$ git clone git@github.com:tensorflow/tensorflow.git
$ cd tensorflow
$ bazel build tensorflow/python/tools:saved_model_cli

Building the tool with bazel could take a little long time. If you would like to skip the building step, you can also run it by just executing the python file.

$ python tensorflow/python/tools/saved_model_cli.py -hUse the retry module or similar alternatives.
usage: saved_model_cli.py [-h] [-v] {show,run} ...
saved_model_cli: Command-line interface for SavedModeloptional arguments:
-h, --help show this help message and exit
-v, --version show program's version number and exit
commands:
valid commands
{show,run} additional help

Download Pre-Trained Tensorflow Model

There are many pre-trained tensorflow models on the internet. We can make the best use of them not only just re-using them, but also doing fine-tuning them with our own data.

Tensorflow detection model zoo provides a collection of pre-trained object detection models on COCO dataset, the Kitti dataset, and the Open Images dataset. Take ssd_mobilenet_v1_coco, for instance. It includes a graph meta, checkpoint, frozen graph and saved model. We can just server the saved model with tensorflow serving. But what are the signatures of the saved model?

$ curl -O http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v1_coco_2017_11_17.tar.gz
$ tar zxvf ssd_mobilenet_v1_coco_2017_11_17.tar.gz
$ tree ssd_mobilenet_v1_coco_2017_11_17
ssd_mobilenet_v1_coco_2017_11_17
├── checkpoint
├── frozen_inference_graph.pb
├── model.ckpt.data-00000-of-00001
├── model.ckpt.index
├── model.ckpt.meta
└── saved_model
├── saved_model.pb
└── variables

Show Signatures with SavedModel CLI

We have prepared for the CLI and have downloaded a tensorflow saved model. Now, let’s show the signatures of inputs/outputs of the model. As you can see, the signature_def is serving_default and the key of inputs is input whose shape is (-1, -1, -1, 3). It means we can pass any size of 3 channel images to the model. As well as it seems that the saved model returns detection_boxes , detection_classes , detection_scores and num_detection .

$ python tensorflow/python/tools/saved_model_cli.py show --dir ./ssd_mobilenet_v1_coco_2017_11_17/saved_model --allMetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:signature_def['serving_default']:
The given SavedModel SignatureDef contains the following input(s):
inputs['inputs'] tensor_info:
dtype: DT_UINT8
shape: (-1, -1, -1, 3)
name: image_tensor:0
The given SavedModel SignatureDef contains the following output(s):
outputs['detection_boxes'] tensor_info:
dtype: DT_FLOAT
shape: (-1, 100, 4)
name: detection_boxes:0
outputs['detection_classes'] tensor_info:
dtype: DT_FLOAT
shape: (-1, 100)
name: detection_classes:0
outputs['detection_scores'] tensor_info:
dtype: DT_FLOAT
shape: (-1, 100)
name: detection_scores:0
outputs['num_detections'] tensor_info:
dtype: DT_FLOAT
shape: (-1)
name: num_detections:0
Method name is: tensorflow/serving/predict

Conclusion

Tenfowflow’s SavedModel CLI enables us to show signatures of inputs/outputs of your model. If you don’t know the signature of a given model, it would help with understanding it.

--

--

Yu Ishikawa

Data Engineering / Machine Learning / MLOps / Data Governance / Privacy Engineering