@@ -183,8 +183,9 @@ def __init__(self, checkpoint_view, options, wrapped_functions=None):
183
183
"""
184
184
self .options = options
185
185
self .checkpoint_view = checkpoint_view
186
- trackable_objects , node_ids , slot_variables = (
187
- self .checkpoint_view .objects_ids_and_slot_variables ())
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
188
189
self .nodes = trackable_objects
189
190
self .node_ids = node_ids
190
191
self .captured_tensor_node_ids = object_identity .ObjectIdentityDictionary ()
@@ -1029,15 +1030,40 @@ def serve():
1029
1030
May not be called from within a function body.
1030
1031
@end_compatibility
1031
1032
"""
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
+ """
1032
1057
options = options or save_options .SaveOptions ()
1033
1058
# TODO(allenl): Factor out some subset of SavedModelBuilder which is 2.x
1034
1059
# compatible (no sessions) and share it with this export API rather than
1035
1060
# making a SavedModel proto and writing it directly.
1036
1061
saved_model = saved_model_pb2 .SavedModel ()
1037
1062
meta_graph_def = saved_model .meta_graphs .add ()
1038
1063
1039
- _ , exported_graph , object_saver , asset_info = _build_meta_graph (
1040
- obj , signatures , options , meta_graph_def )
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 ))
1041
1067
saved_model .saved_model_schema_version = constants .SAVED_MODEL_SCHEMA_VERSION
1042
1068
1043
1069
# Write the checkpoint, copy assets into the assets directory, and write out
@@ -1077,6 +1103,8 @@ def serve():
1077
1103
# constants in the saved graph.
1078
1104
ops .dismantle_graph (exported_graph )
1079
1105
1106
+ return saved_nodes , node_paths
1107
+
1080
1108
1081
1109
def export_meta_graph (obj , filename , signatures = None , options = None ):
1082
1110
"""Exports the MetaGraph proto of the `obj` to a file.
@@ -1103,7 +1131,7 @@ def export_meta_graph(obj, filename, signatures=None, options=None):
1103
1131
"""
1104
1132
options = options or save_options .SaveOptions ()
1105
1133
export_dir = os .path .dirname (filename )
1106
- meta_graph_def , exported_graph , _ , _ = _build_meta_graph (
1134
+ meta_graph_def , exported_graph , _ , _ , _ , _ = _build_meta_graph (
1107
1135
obj , signatures , options )
1108
1136
1109
1137
file_io .atomic_write_string_to_file (
@@ -1122,7 +1150,8 @@ def export_meta_graph(obj, filename, signatures=None, options=None):
1122
1150
def _build_meta_graph_impl (obj ,
1123
1151
signatures ,
1124
1152
options ,
1125
- meta_graph_def = None ):
1153
+ meta_graph_def = None ,
1154
+ raise_metadata_warning = True ):
1126
1155
"""Creates a MetaGraph containing the resources and functions of an object."""
1127
1156
if ops .inside_function ():
1128
1157
raise AssertionError (
@@ -1170,7 +1199,7 @@ def _build_meta_graph_impl(obj,
1170
1199
saveable_view , asset_info .asset_index )
1171
1200
meta_graph_def .object_graph_def .CopyFrom (object_graph_proto )
1172
1201
1173
- if saved_object_metadata :
1202
+ if saved_object_metadata and raise_metadata_warning :
1174
1203
tf_logging .warn (
1175
1204
'FOR KERAS USERS: The object that you are saving contains one or more '
1176
1205
'Keras models or layers. If you are loading the SavedModel with '
@@ -1186,13 +1215,15 @@ def _build_meta_graph_impl(obj,
1186
1215
'metadta field will be deprecated soon, so please move the metadata to '
1187
1216
'a different file.' )
1188
1217
1189
- return (meta_graph_def , exported_graph , object_saver , asset_info )
1218
+ return (meta_graph_def , exported_graph , object_saver , asset_info ,
1219
+ saveable_view .nodes , saveable_view .node_paths )
1190
1220
1191
1221
1192
1222
def _build_meta_graph (obj ,
1193
1223
signatures ,
1194
1224
options ,
1195
- meta_graph_def = None ):
1225
+ meta_graph_def = None ,
1226
+ raise_metadata_warning = True ):
1196
1227
"""Creates a MetaGraph under a save context.
1197
1228
1198
1229
Args:
@@ -1205,6 +1236,8 @@ def _build_meta_graph(obj,
1205
1236
options: `tf.saved_model.SaveOptions` object that specifies options for
1206
1237
saving.
1207
1238
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.
1208
1241
1209
1242
Raises:
1210
1243
AssertionError: If `export_meta_graph` is executing inside a `tf.function`.
@@ -1218,4 +1251,5 @@ def _build_meta_graph(obj,
1218
1251
"""
1219
1252
1220
1253
with save_context .save_context (options ):
1221
- return _build_meta_graph_impl (obj , signatures , options , meta_graph_def )
1254
+ return _build_meta_graph_impl (obj , signatures , options , meta_graph_def ,
1255
+ raise_metadata_warning )
0 commit comments