Loading keras tensorflow model from .h5 file
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
A .h5 file can store a full Keras model, including architecture, weights, and sometimes optimizer state. Loading it is straightforward, but real-world issues usually come from custom layers, compile settings, and the fact that HDF5-based saving is a legacy format compared with the newer TensorFlow-native formats.
The Standard Load Path
If the .h5 file contains a full saved Keras model, use load_model:
After loading, the model is ready for inference and, if it was saved with compile information, usually ready for continued training as well.
A Complete Save and Load Example
This example creates a tiny model, saves it to HDF5, and loads it back.
That is the normal workflow when the file contains a full model and the runtime environment matches the original save environment closely enough.
Loading for Inference Only
Sometimes you only want prediction, not training. In that case, compile=False can simplify loading:
This is useful when:
- You do not need optimizer state.
- The original training metrics are unavailable.
- The file was saved in an environment with compile-time objects you do not want to recreate.
For deployment jobs, compile=False is often the cleaner option.
Custom Layers and custom_objects
If the model uses custom layers, losses, metrics, or activation functions, Keras needs to know how to rebuild them.
Without custom_objects, loading can fail because the HDF5 file references symbols that do not exist in the current process.
.h5 Versus Newer Save Formats
The .h5 format still works, but it is older than the newer TensorFlow and Keras save formats. In modern projects, you may also see directory-based model exports or the newer .keras format.
That matters because some features and serialization details behave better in the newer formats. If you control the save side and do not need HDF5 compatibility, newer formats are generally the better long-term choice. But if you already have a .h5 artifact, loading it is still a standard Keras workflow.
Common Recovery Pattern When Loading Fails
If a model does not load cleanly, debug in this order:
- Try
compile=False. - Provide
custom_objectsfor any custom classes or functions. - Confirm the file is a full model file, not just weights.
- Match the TensorFlow and Keras environment as closely as practical.
That sequence resolves a large fraction of real .h5 loading problems.
Common Pitfalls
- Assuming every
.h5file is a full model when some files contain weights only. - Forgetting
custom_objectsfor custom layers, losses, or metrics. - Loading for inference with compile settings you do not actually need.
- Treating
.h5as the only Keras save format and ignoring newer options. - Mixing significantly different runtime environments and expecting serialization to be frictionless.
Summary
- Use
tf.keras.models.load_model("model.h5")to load a full Keras model from HDF5. - '
compile=Falseis often the simplest choice for inference-only workloads.' - Supply
custom_objectswhen the model references custom code. - Confirm whether the file contains a full model or only weights.
- '
.h5is still usable, but newer save formats are often a better default for new projects.'
Related reading
- Loading PNG files into TensorFlow
- Loading SavedModel is a lot slower than loading a tf.train.Saver checkpoint
- Loading two models from Saver in the same Tensorflow session
- Log accuracy metric while training a tf.estimator
- Loading Model only once in fastAPI
- Loading sentence transformer model in streamlit taking FOREVER
- Logging requests being served by tensorflow serving model
- Logging training and validation loss in tensorboard
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.