Creates keras_option_scope in legacy saving to ensure that SavedModel logic uses old serialization format throughout stack trace when saving/loading.
@@ -26,6 +26,7 @@ from keras.saving.legacy import serialization
from keras.saving.legacy.saved_model import load as saved_model_load
from keras.saving.legacy.saved_model import load_context
from keras.saving.legacy.saved_model import save as saved_model_save
+from keras.saving.legacy.saved_model.utils import keras_option_scope
from keras.utils import io_utils
from keras.utils import traceback_utils
@@ -161,6 +162,9 @@ def save_model(
)
else:
with serialization.SharedObjectSavingScope():
+ with keras_option_scope(
+ save_traces=save_traces, in_tf_saved_model_scope=True
+ ):
saved_model_save.save(
model,
filepath,
@@ -216,6 +220,9 @@ def load_model(filepath, custom_objects=None, compile=True, options=None):
"""
with serialization.SharedObjectLoadingScope():
with object_registration.CustomObjectScope(custom_objects or {}):
+ with keras_option_scope(
+ save_traces=False, in_tf_saved_model_scope=True
+ ):
with load_context.load_context(options):
filepath_str = io_utils.path_to_string(filepath)
if isinstance(filepath_str, str):
@@ -231,8 +238,8 @@ def load_model(filepath, custom_objects=None, compile=True, options=None):
else:
if h5py is None:
raise ImportError(
- "Filepath looks like a hdf5 file but h5py is "
- "not available."
+ "Filepath looks like a hdf5 file but h5py"
+ "is not available."
f" filepath={filepath_str}"
)
return hdf5_format.load_model_from_hdf5(
