|
||
---|---|---|
.. | ||
model_utils | ||
BUILD | ||
builder_impl.py | ||
builder.py | ||
constants.py | ||
function_deserialization.py | ||
function_serialization.py | ||
load_options.py | ||
load_test.py | ||
load_v1_in_v2_test.py | ||
load_v1_in_v2.py | ||
load.py | ||
loader_impl.py | ||
loader_test.py | ||
loader.py | ||
main_op_impl.py | ||
main_op.py | ||
method_name_updater_test.py | ||
method_name_updater.py | ||
nested_structure_coder_test.py | ||
nested_structure_coder.py | ||
README.md | ||
revived_types_test.py | ||
revived_types.py | ||
save_context_test.py | ||
save_context.py | ||
save_options.py | ||
save_test.py | ||
save.py | ||
saved_model_test.py | ||
saved_model.py | ||
signature_constants.py | ||
signature_def_utils_impl.py | ||
signature_def_utils_test.py | ||
signature_def_utils.py | ||
signature_serialization.py | ||
simple_save_test.py | ||
simple_save.py | ||
tag_constants.py | ||
utils_impl.py | ||
utils_test.py | ||
utils.py |
TensorFlow SavedModel
[TOC]
Overview
SavedModel is the universal serialization format for TensorFlow models.
SavedModel provides a language-neutral format to save machine-learning models that is recoverable and hermetic. It enables higher-level systems and tools to produce, consume and transform TensorFlow models.
Guides
- Using the SavedModel Format
- Save and load Keras models
- Save and load with checkpointing in Keras
- Training checkpoints
- Save and load a model using a distribution strategy
Public API
tf.saved_model.save
tf.saved_model.load
tf.saved_model.SaveOptions
tf.saved_model.LoadOptions
tf.saved_model.Asset
tf.saved_model.contains_saved_model
Related Modules and Functions
The SavedModel Format
A SavedModel directory has the following structure:
assets/
assets.extra/
variables/
variables.data-?????-of-?????
variables.index
saved_model.pb
- SavedModel protocol buffer
saved_model.pb
orsaved_model.pbtxt
- Includes the graph definitions as
MetaGraphDef
protocol buffers.
- Assets
- Subfolder called
assets
. - Contains auxiliary files such as vocabularies, etc.
- Subfolder called
- Extra assets
- Subfolder where higher-level libraries and users can add their own assets that co-exist with the model, but are not loaded by the graph.
- This subfolder is not managed by the SavedModel libraries.
- Variables
- Subfolder called
variables
.variables.data-?????-of-?????
variables.index
- Subfolder called
Stripping Default valued attributes
The SavedModelBuilder class allows users to control whether default-valued
attributes must be stripped from the NodeDefs while adding a meta graph to the
SavedModel bundle. Both SavedModelBuilder.add_meta_graph_and_variables
and
SavedModelBuilder.add_meta_graph
methods accept a Boolean flag
strip_default_attrs
that controls this behavior.
If strip_default_attrs
is False
, the exported MetaGraphDef will have the
default valued attributes in all it's NodeDef instances. This can break forward
compatibility with a sequence of events such as the following:
- An existing Op (
Foo
) is updated to include a new attribute (T
) with a default (bool
) at version 101. - A model producer (such as a Trainer) binary picks up this change
(version 101) to the OpDef and re-exports an existing model that uses Op
Foo
. - A model consumer (such as Tensorflow Serving) running an older binary
(version 100) doesn't have attribute
T
for OpFoo
, but tries to import this model. The model consumer doesn't recognize attributeT
in a NodeDef that uses OpFoo
and therefore fails to load the model.
By setting strip_default_attrs
to True
, the model producers can strip away
any default valued attributes in the NodeDefs. This helps ensure that newly
added attributes with defaults don't cause older model consumers to fail loading
models regenerated with newer training binaries.
TIP: If you care about forward compatibility, then set strip_default_attrs
to True
while using SavedModelBuilder.add_meta_graph_and_variables
and
SavedModelBuilder.add_meta_graph
.