Keras
TensorFlow
Machine Learning
Model Loading
H5 File

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.

Practice ML system design

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:

python
1import tensorflow as tf
2
3model = tf.keras.models.load_model("model.h5")
4print(model.summary())

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.

python
1import numpy as np
2import tensorflow as tf
3
4x = np.random.rand(100, 4).astype("float32")
5y = (x.sum(axis=1) > 2).astype("float32")
6
7model = tf.keras.Sequential(
8    [
9        tf.keras.layers.Input(shape=(4,)),
10        tf.keras.layers.Dense(8, activation="relu"),
11        tf.keras.layers.Dense(1, activation="sigmoid"),
12    ]
13)
14
15model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
16model.fit(x, y, epochs=2, verbose=0)
17model.save("model.h5")
18
19loaded = tf.keras.models.load_model("model.h5")
20pred = loaded.predict(x[:3], verbose=0)
21print(pred)

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:

python
import tensorflow as tf

model = tf.keras.models.load_model("model.h5", compile=False)

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.

python
1import tensorflow as tf
2
3class SquareLayer(tf.keras.layers.Layer):
4    def call(self, inputs):
5        return tf.square(inputs)
6
7model = tf.keras.models.load_model(
8    "custom_model.h5",
9    custom_objects={"SquareLayer": SquareLayer},
10)

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:

  1. Try compile=False.
  2. Provide custom_objects for any custom classes or functions.
  3. Confirm the file is a full model file, not just weights.
  4. 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 .h5 file is a full model when some files contain weights only.
  • Forgetting custom_objects for custom layers, losses, or metrics.
  • Loading for inference with compile settings you do not actually need.
  • Treating .h5 as 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=False is often the simplest choice for inference-only workloads.'
  • Supply custom_objects when the model references custom code.
  • Confirm whether the file contains a full model or only weights.
  • '.h5 is still usable, but newer save formats are often a better default for new projects.'

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.