tf.saved_model.load
Stay organized with collections
Save and categorize content based on your preferences.
Load a SavedModel from export_dir
.
tf.saved_model.load(
export_dir, tags=None, options=None
)
Used in the notebooks
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
returned by tf.saved_model.load
is not a Keras object (i.e. doesn't have
.fit
, .predict
, etc. methods). A few attributes and functions are still
available: .variables
, .trainable_variables
and .__call__
.
model = tf.keras.Model(...)
tf.saved_model.save(model, path)
imported = tf.saved_model.load(path)
outputs = imported(inputs)
Use tf.keras.models.load_model
to restore the Keras model.
Importing SavedModels from TensorFlow 1.x
1.x SavedModels APIs have a flat graph instead of tf.function
objects.
These SavedModels will be loaded with the following attributes:
.signatures
: A dictionary mapping signature names to functions.
.prune(feeds, fetches)
: A 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([]))
See tf.compat.v1.wrap_function
for details.
.variables
: A list of imported variables.
.graph
: The whole imported graph.
.restore(save_path)
: A function that restores variables from a checkpoint
saved from tf.compat.v1.Saver
.
Consuming SavedModels asynchronously
When consuming SavedModels asynchronously (the producer is a separate
process), the SavedModel directory will appear before all files have been
written, and 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.
Args |
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
tf.saved_model.save .
|
options
|
tf.saved_model.LoadOptions object that specifies options for
loading.
|
Returns |
A trackable object with a signatures attribute mapping from signature
keys to functions. If the SavedModel was exported by tf.saved_model.save ,
it also points to trackable objects, functions, debug info which it has been
saved.
|
Raises |
ValueError
|
If tags don't match a MetaGraph in the SavedModel.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2024-04-26 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-04-26 UTC."],[],[],null,["# tf.saved_model.load\n\n\u003cbr /\u003e\n\n|--------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/saved_model/load.py#L820-L913) |\n\nLoad a SavedModel from `export_dir`.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.saved_model.load_v2`](https://www.tensorflow.org/api_docs/python/tf/saved_model/load)\n\n\u003cbr /\u003e\n\n tf.saved_model.load(\n export_dir, tags=None, options=None\n )\n\n### Used in the notebooks\n\n| Used in the guide | Used in the tutorials |\n|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Using the SavedModel format](https://www.tensorflow.org/guide/saved_model) - [Extension types](https://www.tensorflow.org/guide/extension_type) - [Import a JAX model using JAX2TF](https://www.tensorflow.org/guide/jax2tf) - [Migrate the SavedModel workflow](https://www.tensorflow.org/guide/migrate/saved_model) - [Ragged tensors](https://www.tensorflow.org/guide/ragged_tensor) | - [Save and load a model using a distribution strategy](https://www.tensorflow.org/tutorials/distribute/save_and_load) - [Load text](https://www.tensorflow.org/tutorials/load_data/text) - [Simple audio recognition: Recognizing keywords](https://www.tensorflow.org/tutorials/audio/simple_audio) - [Transfer learning with YAMNet for environmental sound classification](https://www.tensorflow.org/tutorials/audio/transfer_learning_audio) - [Distributed training with DTensors](https://www.tensorflow.org/tutorials/distribute/dtensor_ml_tutorial) |\n\nSignatures associated with the SavedModel are available as functions: \n\n imported = tf.saved_model.load(path)\n f = imported.signatures[\"serving_default\"]\n print(f(x=tf.constant([[1.]])))\n\nObjects exported with [`tf.saved_model.save`](../../tf/saved_model/save) additionally have trackable\nobjects and functions assigned to attributes: \n\n exported = tf.train.Checkpoint(v=tf.Variable(3.))\n exported.f = tf.function(\n lambda x: exported.v * x,\n input_signature=[tf.TensorSpec(shape=None, dtype=tf.float32)])\n tf.saved_model.save(exported, path)\n imported = tf.saved_model.load(path)\n assert 3. == imported.v.numpy()\n assert 6. == imported.f(x=tf.constant(2.)).numpy()\n\n*Loading Keras models*\n\nKeras models are trackable, so they can be saved to SavedModel. The object\nreturned by [`tf.saved_model.load`](../../tf/saved_model/load) is not a Keras object (i.e. doesn't have\n`.fit`, `.predict`, etc. methods). A few attributes and functions are still\navailable: `.variables`, `.trainable_variables` and `.__call__`. \n\n model = tf.keras.Model(...)\n tf.saved_model.save(model, path)\n imported = tf.saved_model.load(path)\n outputs = imported(inputs)\n\nUse [`tf.keras.models.load_model`](../../tf/keras/models/load_model) to restore the Keras model.\n\n*Importing SavedModels from TensorFlow 1.x*\n\n1.x SavedModels APIs have a flat graph instead of [`tf.function`](../../tf/function) objects.\nThese SavedModels will be loaded with the following attributes:\n\n- `.signatures`: A dictionary mapping signature names to functions.\n- `.prune(feeds, fetches)`: A method which allows you to extract\n functions for new subgraphs. This is equivalent to importing the SavedModel\n and naming feeds and fetches in a Session from TensorFlow 1.x.\n\n imported = tf.saved_model.load(path_to_v1_saved_model)\n pruned = imported.prune(\"x:0\", \"out:0\")\n pruned(tf.ones([]))\n\n See [`tf.compat.v1.wrap_function`](../../tf/compat/v1/wrap_function) for details.\n- `.variables`: A list of imported variables.\n\n- `.graph`: The whole imported graph.\n\n- `.restore(save_path)`: A function that restores variables from a checkpoint\n saved from `tf.compat.v1.Saver`.\n\n*Consuming SavedModels asynchronously*\n\nWhen consuming SavedModels asynchronously (the producer is a separate\nprocess), the SavedModel directory will appear before all files have been\nwritten, and [`tf.saved_model.load`](../../tf/saved_model/load) will fail if pointed at an incomplete\nSavedModel. Rather than checking for the directory, check for\n\"saved_model_dir/saved_model.pb\". This file is written atomically as the last\n[`tf.saved_model.save`](../../tf/saved_model/save) file operation.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|--------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `export_dir` | The SavedModel directory to load from. |\n| `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 [`tf.saved_model.save`](../../tf/saved_model/save). |\n| `options` | [`tf.saved_model.LoadOptions`](../../tf/saved_model/LoadOptions) object that specifies options for loading. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A trackable object with a `signatures` attribute mapping from signature keys to functions. If the SavedModel was exported by [`tf.saved_model.save`](../../tf/saved_model/save), it also points to trackable objects, functions, debug info which it has been saved. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|------------------------------------------------------|\n| `ValueError` | If `tags` don't match a MetaGraph in the SavedModel. |\n\n\u003cbr /\u003e"]]