43
43
from tensorflow .python .ops import array_ops
44
44
from tensorflow .python .ops import control_flow_ops
45
45
from tensorflow .python .ops import resource_variable_ops
46
- from tensorflow .python .platform import tf_logging
47
46
from tensorflow .python .saved_model import builder_impl
48
47
from tensorflow .python .saved_model import constants
49
48
from tensorflow .python .saved_model import function_serialization
@@ -183,9 +182,8 @@ def __init__(self, checkpoint_view, options, wrapped_functions=None):
183
182
"""
184
183
self .options = options
185
184
self .checkpoint_view = checkpoint_view
186
- trackable_objects , path_to_root , node_ids , slot_variables = (
187
- self .checkpoint_view .objects_ids_and_slot_variables_and_paths ())
188
- self .node_paths = path_to_root
185
+ trackable_objects , node_ids , slot_variables = (
186
+ self .checkpoint_view .objects_ids_and_slot_variables ())
189
187
self .nodes = trackable_objects
190
188
self .node_ids = node_ids
191
189
self .captured_tensor_node_ids = object_identity .ObjectIdentityDictionary ()
@@ -752,17 +750,14 @@ def _serialize_object_graph(saveable_view, asset_file_def_index):
752
750
if serialized is not None :
753
751
proto .concrete_functions [name ].CopyFrom (serialized )
754
752
755
- saved_object_metadata = False
756
753
for obj , obj_proto in zip (saveable_view .nodes , proto .nodes ):
757
- has_saved_object_metadata = _write_object_proto (
758
- obj , obj_proto , asset_file_def_index , saveable_view .function_name_map )
759
- saved_object_metadata = saved_object_metadata or has_saved_object_metadata
760
- return proto , saved_object_metadata
754
+ _write_object_proto (obj , obj_proto , asset_file_def_index ,
755
+ saveable_view .function_name_map )
756
+ return proto
761
757
762
758
763
759
def _write_object_proto (obj , proto , asset_file_def_index , function_name_map ):
764
760
"""Saves an object into SavedObject proto."""
765
- has_saved_object_metadata = False # The metadata field will be deprecated.
766
761
if isinstance (obj , tracking .Asset ):
767
762
proto .asset .SetInParent ()
768
763
proto .asset .asset_file_def_index = asset_file_def_index [obj ]
@@ -798,14 +793,11 @@ def _write_object_proto(obj, proto, asset_file_def_index, function_name_map):
798
793
if registered_type_proto is None :
799
794
# Fallback for types with no matching registration
800
795
# pylint:disable=protected-access
801
- metadata = obj ._tracking_metadata
802
- if metadata :
803
- has_saved_object_metadata = True
804
796
registered_type_proto = saved_object_graph_pb2 .SavedUserObject (
805
797
identifier = obj ._object_identifier ,
806
798
version = versions_pb2 .VersionDef (
807
799
producer = 1 , min_consumer = 1 , bad_consumers = []),
808
- metadata = metadata )
800
+ metadata = obj . _tracking_metadata )
809
801
# pylint:enable=protected-access
810
802
proto .user_object .CopyFrom (registered_type_proto )
811
803
@@ -818,7 +810,6 @@ def _write_object_proto(obj, proto, asset_file_def_index, function_name_map):
818
810
# documentation.
819
811
if hasattr (obj , "_write_object_proto" ):
820
812
obj ._write_object_proto (proto , options ) # pylint: disable=protected-access
821
- return has_saved_object_metadata
822
813
823
814
824
815
def _export_debug_info (exported_graph , export_dir ):
@@ -1016,7 +1007,8 @@ def serve():
1016
1007
instances with input signatures or concrete functions. Keys of such a
1017
1008
dictionary may be arbitrary strings, but will typically be from the
1018
1009
`tf.saved_model.signature_constants` module.
1019
- options: `tf.saved_model.SaveOptions` object for configuring save options.
1010
+ options: Optional, `tf.saved_model.SaveOptions` object that specifies
1011
+ options for saving.
1020
1012
1021
1013
Raises:
1022
1014
ValueError: If `obj` is not trackable.
@@ -1030,40 +1022,15 @@ def serve():
1030
1022
May not be called from within a function body.
1031
1023
@end_compatibility
1032
1024
"""
1033
- save_and_return_nodes (obj , export_dir , signatures , options ,
1034
- raise_metadata_warning = True )
1035
-
1036
-
1037
- def save_and_return_nodes (obj , export_dir , signatures = None , options = None ,
1038
- raise_metadata_warning = False ):
1039
- """Saves a SavedModel while returning all saved nodes and their paths.
1040
-
1041
- Please see `tf.saved_model.save` for details.
1042
-
1043
- Args:
1044
- obj: A trackable object to export.
1045
- export_dir: A directory in which to write the SavedModel.
1046
- signatures: A function or dictionary of functions to save in the SavedModel
1047
- as signatures.
1048
- options: `tf.saved_model.SaveOptions` object for configuring save options.
1049
- raise_metadata_warning: Whether to raise the metadata warning. This arg will
1050
- be removed in TF 2.5.
1051
-
1052
- Returns:
1053
- A tuple of (a list of saved nodes in the order they are serialized to the
1054
- `SavedObjectGraph`, dictionary mapping nodes to one possible path from
1055
- the root node to the key node)
1056
- """
1057
1025
options = options or save_options .SaveOptions ()
1058
1026
# TODO(allenl): Factor out some subset of SavedModelBuilder which is 2.x
1059
1027
# compatible (no sessions) and share it with this export API rather than
1060
1028
# making a SavedModel proto and writing it directly.
1061
1029
saved_model = saved_model_pb2 .SavedModel ()
1062
1030
meta_graph_def = saved_model .meta_graphs .add ()
1063
1031
1064
- _ , exported_graph , object_saver , asset_info , saved_nodes , node_paths = (
1065
- _build_meta_graph (obj , signatures , options , meta_graph_def ,
1066
- raise_metadata_warning ))
1032
+ _ , exported_graph , object_saver , asset_info = _build_meta_graph (
1033
+ obj , signatures , options , meta_graph_def )
1067
1034
saved_model .saved_model_schema_version = constants .SAVED_MODEL_SCHEMA_VERSION
1068
1035
1069
1036
# Write the checkpoint, copy assets into the assets directory, and write out
@@ -1103,8 +1070,6 @@ def save_and_return_nodes(obj, export_dir, signatures=None, options=None,
1103
1070
# constants in the saved graph.
1104
1071
ops .dismantle_graph (exported_graph )
1105
1072
1106
- return saved_nodes , node_paths
1107
-
1108
1073
1109
1074
def export_meta_graph (obj , filename , signatures = None , options = None ):
1110
1075
"""Exports the MetaGraph proto of the `obj` to a file.
@@ -1131,7 +1096,7 @@ def export_meta_graph(obj, filename, signatures=None, options=None):
1131
1096
"""
1132
1097
options = options or save_options .SaveOptions ()
1133
1098
export_dir = os .path .dirname (filename )
1134
- meta_graph_def , exported_graph , _ , _ , _ , _ = _build_meta_graph (
1099
+ meta_graph_def , exported_graph , _ , _ = _build_meta_graph (
1135
1100
obj , signatures , options )
1136
1101
1137
1102
file_io .atomic_write_string_to_file (
@@ -1150,8 +1115,7 @@ def export_meta_graph(obj, filename, signatures=None, options=None):
1150
1115
def _build_meta_graph_impl (obj ,
1151
1116
signatures ,
1152
1117
options ,
1153
- meta_graph_def = None ,
1154
- raise_metadata_warning = True ):
1118
+ meta_graph_def = None ):
1155
1119
"""Creates a MetaGraph containing the resources and functions of an object."""
1156
1120
if ops .inside_function ():
1157
1121
raise AssertionError (
@@ -1195,35 +1159,17 @@ def _build_meta_graph_impl(obj,
1195
1159
for fdef in func ._stateless_fn ._function_cache .all_values (): # pylint: disable=protected-access
1196
1160
function_aliases [fdef .name ] = alias
1197
1161
1198
- object_graph_proto , saved_object_metadata = _serialize_object_graph (
1199
- saveable_view , asset_info .asset_index )
1162
+ object_graph_proto = _serialize_object_graph (saveable_view ,
1163
+ asset_info .asset_index )
1200
1164
meta_graph_def .object_graph_def .CopyFrom (object_graph_proto )
1201
1165
1202
- if saved_object_metadata and raise_metadata_warning :
1203
- tf_logging .warn (
1204
- 'FOR KERAS USERS: The object that you are saving contains one or more '
1205
- 'Keras models or layers. If you are loading the SavedModel with '
1206
- '`tf.keras.models.load_model`, continue reading (otherwise, you may '
1207
- 'ignore the following instructions). Please change your code to save '
1208
- 'with `tf.keras.models.save_model` or `model.save`, and confirm that '
1209
- 'the file "keras.metadata" exists in the export directory. In the '
1210
- 'future, Keras will only load the SavedModels that have this file. In '
1211
- 'other words, `tf.saved_model.save` will no longer write SavedModels '
1212
- 'that can be recovered as Keras models (this will apply in TF 2.5).'
1213
- '\n \n FOR DEVS: If you are overwriting _tracking_metadata in your class,'
1214
- ' this property has been used to save metadata in the SavedModel. The '
1215
- 'metadta field will be deprecated soon, so please move the metadata to '
1216
- 'a different file.' )
1217
-
1218
- return (meta_graph_def , exported_graph , object_saver , asset_info ,
1219
- saveable_view .nodes , saveable_view .node_paths )
1166
+ return meta_graph_def , exported_graph , object_saver , asset_info
1220
1167
1221
1168
1222
1169
def _build_meta_graph (obj ,
1223
1170
signatures ,
1224
1171
options ,
1225
- meta_graph_def = None ,
1226
- raise_metadata_warning = True ):
1172
+ meta_graph_def = None ):
1227
1173
"""Creates a MetaGraph under a save context.
1228
1174
1229
1175
Args:
@@ -1236,8 +1182,6 @@ def _build_meta_graph(obj,
1236
1182
options: `tf.saved_model.SaveOptions` object that specifies options for
1237
1183
saving.
1238
1184
meta_graph_def: Optional, the MetaGraphDef proto fill.
1239
- raise_metadata_warning: Whether to raise a warning when user objects contain
1240
- non-empty metadata.
1241
1185
1242
1186
Raises:
1243
1187
AssertionError: If `export_meta_graph` is executing inside a `tf.function`.
@@ -1251,5 +1195,4 @@ def _build_meta_graph(obj,
1251
1195
"""
1252
1196
1253
1197
with save_context .save_context (options ):
1254
- return _build_meta_graph_impl (obj , signatures , options , meta_graph_def ,
1255
- raise_metadata_warning )
1198
+ return _build_meta_graph_impl (obj , signatures , options , meta_graph_def )
0 commit comments