• Home
Name Date Size #Lines LOC

..--

model_utils/03-May-2024-1,8171,341

BUILDD03-May-202417.8 KiB645606

README.mdD03-May-20244.3 KiB9475

builder.pyD03-May-20241.2 KiB309

builder_impl.pyD03-May-202433.3 KiB798528

constants.pyD03-May-20244.6 KiB13483

function_deserialization.pyD03-May-202421.7 KiB541360

function_serialization.pyD03-May-20247.3 KiB177125

load.pyD03-May-202438.4 KiB921696

load_options.pyD03-May-20242.3 KiB5831

load_test.pyD03-May-202475.6 KiB2,2011,689

load_v1_in_v2.pyD03-May-202412.3 KiB281216

load_v1_in_v2_test.pyD03-May-202429.3 KiB701618

loader.pyD03-May-20242.5 KiB7041

loader_impl.pyD03-May-202417.9 KiB457352

loader_test.pyD03-May-202411.8 KiB296210

main_op.pyD03-May-20241.1 KiB299

main_op_impl.pyD03-May-20242.8 KiB7346

method_name_updater.pyD03-May-20246 KiB150110

method_name_updater_test.pyD03-May-202410.2 KiB378342

nested_structure_coder.pyD03-May-202417 KiB560391

nested_structure_coder_test.pyD03-May-202413.9 KiB344301

revived_types.pyD03-May-20247.4 KiB182133

revived_types_test.pyD03-May-20244 KiB11179

save.pyD03-May-202457.5 KiB1,274985

save_context.pyD03-May-20241.9 KiB7140

save_context_test.pyD03-May-20243.2 KiB8852

save_options.pyD03-May-20247.6 KiB183139

save_test.pyD03-May-202438.2 KiB985791

saved_model.pyD03-May-20241.6 KiB3917

saved_model_test.pyD03-May-202465.2 KiB1,5801,070

signature_constants.pyD03-May-20244.9 KiB14688

signature_def_utils.pyD03-May-20241.8 KiB3818

signature_def_utils_impl.pyD03-May-202414 KiB396302

signature_def_utils_test.pyD03-May-202418.8 KiB479349

signature_serialization.pyD03-May-202413.2 KiB312216

simple_save.pyD03-May-20244.1 KiB9268

simple_save_test.pyD03-May-20244.8 KiB10865

tag_constants.pyD03-May-20241.8 KiB5729

utils.pyD03-May-20241.2 KiB289

utils_impl.pyD03-May-202410.2 KiB281207

utils_test.pyD03-May-20248.2 KiB185147

README.md

1# TensorFlow SavedModel
2
3[TOC]
4
5## Overview
6
7SavedModel is the universal serialization format for
8[TensorFlow](https://www.tensorflow.org/) models.
9
10SavedModel provides a language-neutral format to save machine-learning models
11that is recoverable and hermetic. It enables higher-level systems and tools to
12produce, consume and transform TensorFlow models.
13
14## Guides
15* [Using the SavedModel Format](https://www.tensorflow.org/guide/saved_model)
16* [Save and load Keras models](https://www.tensorflow.org/guide/keras/save_and_serialize)
17* [Save and load with checkpointing in Keras](https://www.tensorflow.org/tutorials/keras/save_and_load)
18* [Training checkpoints](https://www.tensorflow.org/guide/checkpoint)
19* [Save and load a model using a distribution strategy](https://www.tensorflow.org/tutorials/distribute/save_and_load)
20
21
22## [Public API](https://www.tensorflow.org/api_docs/python/tf/saved_model)
23* [`tf.saved_model.save`](https://www.tensorflow.org/api_docs/python/tf/saved_model/save)
24* [`tf.saved_model.load`](https://www.tensorflow.org/api_docs/python/tf/saved_model/load)
25* [`tf.saved_model.SaveOptions`](https://www.tensorflow.org/api_docs/python/tf/saved_model/SaveOptions)
26* [`tf.saved_model.LoadOptions`](https://www.tensorflow.org/api_docs/python/tf/saved_model/LoadOptions)
27* [`tf.saved_model.Asset`](https://www.tensorflow.org/api_docs/python/tf/saved_model/Asset)
28* [`tf.saved_model.contains_saved_model`](https://www.tensorflow.org/api_docs/python/tf/saved_model/contains_saved_model)
29
30### Related Modules and Functions
31* [`tf.keras.models.save_model`](https://www.tensorflow.org/api_docs/python/tf/keras/models/save_model)
32* [`tf.keras.models.load_model`](https://www.tensorflow.org/api_docs/python/tf/keras/models/load_model)
33* [`tf.train.Checkpoint`](https://www.tensorflow.org/api_docs/python/tf/train/Checkpoint)
34
35
36## The SavedModel Format
37A SavedModel directory has the following structure:
38
39```
40assets/
41assets.extra/
42variables/
43    variables.data-?????-of-?????
44    variables.index
45saved_model.pb
46```
47
48*   SavedModel protocol buffer
49    *   [`saved_model.pb`](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/protobuf/saved_model.proto)
50        or `saved_model.pbtxt`
51    *   Includes the graph definitions as `MetaGraphDef` protocol buffers.
52*   Assets
53    *   Subfolder called `assets`.
54    *   Contains auxiliary files such as vocabularies, etc.
55*   Extra assets
56    *   Subfolder where higher-level libraries and users can add their own
57        assets that co-exist with the model, but are not loaded by the graph.
58    *   This subfolder is not managed by the SavedModel libraries.
59*   Variables
60    *   Subfolder called `variables`.
61        *   `variables.data-?????-of-?????`
62        *   `variables.index`
63
64---
65
66#### Stripping Default valued attributes
67The SavedModelBuilder class allows users to control whether default-valued
68attributes must be stripped from the NodeDefs while adding a meta graph to the
69SavedModel bundle. Both `SavedModelBuilder.add_meta_graph_and_variables` and
70`SavedModelBuilder.add_meta_graph` methods accept a Boolean flag
71`strip_default_attrs` that controls this behavior.
72
73If `strip_default_attrs` is `False`, the exported MetaGraphDef will have the
74default valued attributes in all it's NodeDef instances. This can break forward
75compatibility with a sequence of events such as the following:
76
77* An existing Op (`Foo`) is updated to include a new attribute (`T`) with a
78  default (`bool`) at version 101.
79* A model producer (such as a Trainer) binary picks up this change
80  (version 101) to the OpDef and re-exports an existing model that uses Op `Foo`.
81* A model consumer (such as Tensorflow Serving) running an older binary
82  (version 100) doesn't have attribute `T` for Op `Foo`, but tries to import
83  this model. The model consumer doesn't recognize attribute `T` in a NodeDef
84  that uses Op `Foo` and therefore fails to load the model.
85
86By setting `strip_default_attrs` to `True`, the model producers can strip away
87any default valued attributes in the NodeDefs. This helps ensure that newly
88added attributes with defaults don't cause older model consumers to fail loading
89models regenerated with newer training binaries.
90
91TIP: If you care about forward compatibility, then set `strip_default_attrs`
92to `True` while using `SavedModelBuilder.add_meta_graph_and_variables` and
93`SavedModelBuilder.add_meta_graph`.
94