Implementation:Tensorflow Serving SavedModelBuilder Export
| Knowledge Sources | |
|---|---|
| Domains | Model_Serialization, Deployment |
| Last Updated | 2026-02-13 17:00 GMT |
Overview
Wrapper for TensorFlow's SavedModelBuilder class to serialize trained models with signatures to the SavedModel directory format.
Description
SavedModelBuilder is TensorFlow's builder pattern API for constructing SavedModel artifacts. In TensorFlow Serving's MNIST example, it:
- Creates the export directory at
<export_dir>/<model_version>/ - Adds the trained session's MetaGraph with serving tags and signature definitions
- Writes the serialized protobuf and checkpoint files to disk
The builder validates that the export path does not already exist (to prevent accidental overwrites) and supports multiple MetaGraphs with different tag sets in a single SavedModel.
Usage
Use this after defining SignatureDefs and completing model training. The builder is the bridge between a live TensorFlow session and the on-disk format that TensorFlow Serving can load.
Code Reference
Source Location
- Repository: tensorflow/serving
- File: tensorflow_serving/example/mnist_saved_model.py
- Lines: L99-153
Signature
# Constructor
tf.compat.v1.saved_model.builder.SavedModelBuilder(
export_dir: str # Path where SavedModel will be written
)
# Add MetaGraph with variables
builder.add_meta_graph_and_variables(
sess: tf.Session, # Trained session with variables
tags: list[str], # Tags identifying this MetaGraph (e.g., ["serve"])
signature_def_map: dict[str, SignatureDef], # Named signatures
main_op: tf.Operation = None, # Op to run on load (e.g., table initializer)
strip_default_attrs: bool = False # Remove default-valued attributes for forward compat
)
# Serialize to disk
builder.save() -> None
Import
import tensorflow as tf
# Access via: tf.compat.v1.saved_model.builder.SavedModelBuilder
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| export_dir | str | Yes | Full path including version subdirectory |
| sess | tf.Session | Yes | Trained TF session with variable values |
| tags | list[str] | Yes | Tags for MetaGraph (typically ["serve"]) |
| signature_def_map | dict[str, SignatureDef] | Yes | Named signatures for serving |
| main_op | tf.Operation | No | Op to run when model is loaded |
| strip_default_attrs | bool | No | Default False; strip default attributes |
Outputs
| Name | Type | Description |
|---|---|---|
| saved_model.pb | File | Serialized MetaGraphDef protobuf |
| variables/ | Directory | Checkpoint files with trained weights |
Usage Examples
Export MNIST Model
import os
import tensorflow as tf
export_path = os.path.join('/tmp/mnist_model', str(1)) # version 1
builder = tf.compat.v1.saved_model.builder.SavedModelBuilder(export_path)
# Add MetaGraph with signatures
builder.add_meta_graph_and_variables(
sess,
[tf.compat.v1.saved_model.tag_constants.SERVING],
signature_def_map={
'predict_images': prediction_signature,
tf.compat.v1.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
classification_signature,
},
main_op=tf.compat.v1.tables_initializer(),
strip_default_attrs=True
)
builder.save()
print(f'Model exported to {export_path}')
Command Line Usage
# Export model version 1
python tensorflow_serving/example/mnist_saved_model.py /tmp/mnist_model
# Export model version 2 (for version management)
python tensorflow_serving/example/mnist_saved_model.py \
--model_version=2 \
--training_iteration=2000 \
/tmp/mnist_model