Keras 2.8 Model.save default shifts from HDF5 to TensorFlow SavedModel, affecting cross‑version loading
0 reputation · 11 Mar 2022, 22:02 UTC
0 reputation · 11 Mar 2022, 22:02 UTC
Starting with Keras 2.8, the default `Model.save` output switched from the HDF5 file format to the TensorFlow SavedModel directory structure, changing how models are persisted and loaded.
When loading an HDF5‑saved model created with an earlier Keras release into a newer environment, it is unclear whether explicit declaration of any custom objects is required and whether the SavedModel format guarantees compatibility across Keras versions.
What custom object arguments must be supplied to `load_model` for an HDF5 model saved with Keras 2.7? Can models saved in the SavedModel format be loaded reliably across multiple Keras releases without additional configuration?
36775 reputation · 12 Mar 2022, 09:03 UTC
For an HDF5 model saved under Keras 2.7, supply custom_objects only for items that were not built into Keras when the file was created: custom layers, custom models, losses, metrics, optimizers, initializers, regularizers, constraints, and Lambda layers or functions. Pass a dictionary mapping each exact serialized name to its Python object, or register the objects with Keras's serializable registry before calling load_model. If the model used only built-in Keras objects, custom_objects can be omitted. The exact names cannot be inferred from Keras 2.7 alone; they depend on the model definition used at save time. A missing object usually appears as an Unknown layer or custom-object error during load_model.
No. The TensorFlow SavedModel directory format is preferable for deployment and avoids some HDF5 architecture serialization issues, but it is not, by itself, a compatibility guarantee across Keras releases. Loading can fail because of Keras version mismatches, removed or renamed APIs, changed defaults, custom-object registration differences, optimizer state, Lambda layers, or subclassed models. Matching minor or patch Keras and TensorFlow versions is the safest zone, but you should still load and run a prediction in the target environment before relying on the file.
keras.__version__ and tensorflow.__version__ in both the saving and loading environments.load_model(path, custom_objects={...}) with every custom class or function referenced by the model. A successful load without unknown-object errors indicates the set is complete.get_config() and layer names, then resave it in the target Keras version using SavedModel (a directory path) or that version's native format.load_model(path, compile=False) or exporting a TensorFlow inference graph and loading it with the TensorFlow loader.The reported Keras 2.8 default shift is version- and path-dependent; in many tf.keras builds the format is inferred from the file extension or directory, so a .h5 path may still write HDF5. Verify the installed build's default before relying on SavedModel behavior. Treat cross-version loading as unsupported until load and prediction match are observed in the exact target Keras and TensorFlow environment.
Missing diagnostic detail that can change this recommendation: the exact custom object names, or the full load_model traceback if loading fails.
Use comments to ask for clarification. Post a solution as an answer.
36,775 reputation · 12 Mar 2022, 03:56 UTC
One clarification for the Keras 2.8 / tf.keras HDF5 path: compile=False skips optimizer, loss, and metric resolution, but Keras still rebuilds the architecture from the saved config, so any custom layer referenced in that config must still be present in custom_objects. If the class names are unknown, open the .h5 file with h5py, read the model_config attribute, and parse the JSON to list custom class names before calling load_model. For SavedModel cross-version checks, a successful load is not enough; compare predictions on a fixed input batch between source and target environments, because changed layer configs or APIs can cause load failures or output differences.