How do I save keras models so that they don't break between versions?

I have models that I trained in TF2.14 that will not load in TF2.15+.
Currently can’t install 2.14 due to dependency hell (absl-py and protobuf).

Is there a way to load a 2.14 model in 2.15?

Previously reported here, but not really solved.

The error from 2.16 is essentially:

TypeError: Could not deserialize class 'Functional' because its parent module keras.src.engine.functional cannot be imported.

Here is the error text from 2.15:

File ~/work/Sleep/src/hypnos/model.py:87, in Model.process(self)
     63 """
     64 Load and use a pre-trained multiclass model along with a corresponding scaler to make predictions on provided data.
     65 
   (...)
     83     None: The method stores the predictions as an attribute instead of returning them.
     84 """
     86 # Load pre-trained multiclass model and scaler
---> 87 model = load_model(self._model_path)
     89 # Lists to store predictions
     90 self._predictions = []

File ~/work/Sleep/.venv311/lib/python3.11/site-packages/keras/src/saving/saving_api.py:254, in load_model(filepath, custom_objects, compile, safe_mode, **kwargs)
    249     if kwargs:
    250         raise ValueError(
    251             "The following argument(s) are not supported "
    252             f"with the native Keras format: {list(kwargs.keys())}"
    253         )
--> 254     return saving_lib.load_model(
    255         filepath,
    256         custom_objects=custom_objects,
    257         compile=compile,
    258         safe_mode=safe_mode,
    259     )
    261 # Legacy case.
    262 return legacy_sm_saving_lib.load_model(
    263     filepath, custom_objects=custom_objects, compile=compile, **kwargs
    264 )

File ~/work/Sleep/.venv311/lib/python3.11/site-packages/keras/src/saving/saving_lib.py:281, in load_model(filepath, custom_objects, compile, safe_mode)
    278             asset_store.close()
    280 except Exception as e:
--> 281     raise e
    282 else:
    283     return model

File ~/work/Sleep/.venv311/lib/python3.11/site-packages/keras/src/saving/saving_lib.py:269, in load_model(filepath, custom_objects, compile, safe_mode)
    266 else:
    267     asset_store = None
--> 269 _load_state(
    270     model,
    271     weights_store=weights_store,
    272     assets_store=asset_store,
    273     inner_path="",
    274     visited_trackables=set(),
    275 )
    276 weights_store.close()
    277 if asset_store:

File ~/work/Sleep/.venv311/lib/python3.11/site-packages/keras/src/saving/saving_lib.py:466, in _load_state(trackable, weights_store, assets_store, inner_path, skip_mismatch, visited_trackables)
    457     _load_state(
    458         child_obj,
    459         weights_store,
   (...)
    463         visited_trackables=visited_trackables,
    464     )
    465 elif isinstance(child_obj, (list, dict, tuple, set)):
--> 466     _load_container_state(
    467         child_obj,
    468         weights_store,
    469         assets_store,
    470         inner_path=tf.io.gfile.join(inner_path, child_attr),
    471         skip_mismatch=skip_mismatch,
    472         visited_trackables=visited_trackables,
    473     )

File ~/work/Sleep/.venv311/lib/python3.11/site-packages/keras/src/saving/saving_lib.py:534, in _load_container_state(container, weights_store, assets_store, inner_path, skip_mismatch, visited_trackables)
    532 else:
    533     used_names[name] = 0
--> 534 _load_state(
    535     trackable,
    536     weights_store,
    537     assets_store,
    538     inner_path=tf.io.gfile.join(inner_path, name),
    539     skip_mismatch=skip_mismatch,
    540     visited_trackables=visited_trackables,
    541 )

File ~/work/Sleep/.venv311/lib/python3.11/site-packages/keras/src/saving/saving_lib.py:435, in _load_state(trackable, weights_store, assets_store, inner_path, skip_mismatch, visited_trackables)
    428             warnings.warn(
    429                 f"Could not load weights in object {trackable}. "
    430                 "Skipping object. "
    431                 f"Exception encountered: {e}",
    432                 stacklevel=2,
    433             )
    434     else:
--> 435         trackable.load_own_variables(weights_store.get(inner_path))
    437 if hasattr(trackable, "load_assets") and assets_store:
    438     if skip_mismatch:

File ~/work/Sleep/.venv311/lib/python3.11/site-packages/keras/src/engine/base_layer.py:3531, in Layer.load_own_variables(self, store)
   3529 all_vars = self._trainable_weights + self._non_trainable_weights
   3530 if len(store.keys()) != len(all_vars):
-> 3531     raise ValueError(
   3532         f"Layer '{self.name}' expected {len(all_vars)} variables, "
   3533         "but received "
   3534         f"{len(store.keys())} variables during loading. "
   3535         f"Expected: {[v.name for v in all_vars]}"
   3536     )
   3537 for i, v in enumerate(all_vars):
   3538     # TODO(rchao): check shapes and raise errors.
   3539     v.assign(store[f"{i}"])

ValueError: Layer 'conv1d_72' expected 2 variables, but received 0 variables during loading. Expected: ['conv1d_72/kernel:0', 'conv1d_72/bias:0']
1 Like

I am having the same issue but I trained models in TF 2.15 and saved them in .keras format, and now I am not being able to load them in TF 2.16.1. The error raised is the same.

Hi @Alvaro_Solano, As mentioned in the above GitHub comment I have tried to save the model weights in 2.15 version and assigned the weights to model defined in 2.16 version and did not face any issue.please refer to this gist. Thank You.