|TensorFlow 1 version||View source on GitHub|
Load a SavedModel from
tf.saved_model.load( export_dir, tags=None )
Used in the guide:
Used in the tutorials:
Signatures associated with the SavedModel are available as functions:
imported = tf.saved_model.load(path) f = imported.signatures["serving_default"] print(f(x=tf.constant([[1.]])))
Objects exported with
tf.saved_model.save additionally have trackable
objects and functions assigned to attributes:
exported = tf.train.Checkpoint(v=tf.Variable(3.)) exported.f = tf.function( lambda x: exported.v * x, input_signature=[tf.TensorSpec(shape=None, dtype=tf.float32)]) tf.saved_model.save(exported, path) imported = tf.saved_model.load(path) assert 3. == imported.v.numpy() assert 6. == imported.f(x=tf.constant(2.)).numpy()
Loading Keras models
Keras models are trackable, so they can be saved to SavedModel. The object
tf.saved_model.load is not a Keras object (i.e. doesn't have
.predict, etc. methods). A few attributes and functions are still
model = tf.keras.Model(...) tf.saved_model.save(model, path) imported = tf.saved_model.load(path) outputs = imported(inputs)
tf.keras.models.load_model to restore the Keras model.
Importing SavedModels from TensorFlow 1.x
tf.estimator.Estimator or 1.x SavedModel APIs have a flat
graph instead of
tf.function objects. These SavedModels will have functions
corresponding to their signatures in the
.signatures attribute, but also
.prune method which allows you to extract functions for new
subgraphs. This is equivalent to importing the SavedModel and naming feeds and
fetches in a Session from TensorFlow 1.x.
imported = tf.saved_model.load(path_to_v1_saved_model) pruned = imported.prune("x:0", "out:0") pruned(tf.ones())
tf.compat.v1.wrap_function for details. These SavedModels also have a
.variables attribute containing imported variables, and a
representing the whole imported graph. For SavedModels exported from
tf.saved_model.save, variables are instead assigned to whichever attributes
they were assigned before export.
Consuming SavedModels asynchronously
When consuming SavedModels asynchronously (the producer is a separate
process), the SavedModel directory will appear before all files have been
tf.saved_model.load will fail if pointed at an incomplete
SavedModel. Rather than checking for the directory, check for
"saved_model_dir/saved_model.pb". This file is written atomically as the last
tf.saved_model.save file operation.
export_dir: The SavedModel directory to load from.
tags: A tag or sequence of tags identifying the MetaGraph to load. Optional if the SavedModel contains a single MetaGraph, as for those exported from
A trackable object with a
signatures attribute mapping from signature
keys to functions. If the SavedModel was exported by
it also points to trackable objects and functions which were attached
to the exported object.
tagsdon't match a MetaGraph in the SavedModel.