SavedModel export

TensorFlow · Practical · 12 min

SavedModel is the format TF Serving, LiteRT conversion and most Google Cloud tools consume.

A SavedModel is a directory containing the computation graphs (as tf.function traces), the variable values and signatures: named entry points with fixed input specs, like serving_default. Loading it does not need the Python class that created it, which makes it the hand-off format for production.

Two ways to produce one:

  • For any tf.Module, call tf.saved_model.save(obj, path). Methods decorated with @tf.function(input_signature=...) are saved; if exactly one exists, it becomes the default signature. You can pass signatures= explicitly.
  • For Keras 3 models, use model.export(path), which writes an inference-only SavedModel with a serve endpoint. (Use model.save("m.keras") for the Keras format if you want to keep training later.)

tf.saved_model.load(path) returns an object whose saved functions and variables are restored, and loaded.signatures["serving_default"] gives a function taking keyword tensors and returning a dict.

Inspect a SavedModel from the shell with saved_model_cli show --dir path --all, which lists signatures with input and output names, shapes and dtypes. Always check this before deploying, since clients must send exactly those inputs.

Example

import tensorflow as tf

class Scaler(tf.Module):
    def __init__(self):
        super().__init__()
        self.w = tf.Variable(2.0)

    @tf.function(input_signature=[tf.TensorSpec([None], tf.float32)])
    def __call__(self, x):
        return x * self.w

tf.saved_model.save(Scaler(), "scaler_model")

loaded = tf.saved_model.load("scaler_model")     # no Scaler class needed
print(loaded(tf.constant([1.0, 3.0])).numpy())  # [2. 6.]
print(list(loaded.signatures.keys()))           # ['serving_default']

# Keras 3 models:  model.export("export_dir")  -> SavedModel for serving
# Inspect from the shell:  saved_model_cli show --dir scaler_model --all

Remember

  • SavedModel = graphs + variables + signatures, no Python class needed
  • tf.saved_model.save for modules, model.export for Keras models
  • Check signatures with saved_model_cli show

Watch out for

  • Saving a tf.function without an input signature and finding no serving signature in the export.

Check yourself

  1. What does a SavedModel signature define?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: www.tensorflow.org/guide/saved_model