Keras
HDF5
Model Loading
Deep Learning
Python

How to load a model from an HDF5 file in Keras?

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

In the world of deep learning, models are often trained in a particular environment and then deployed or used in another. To facilitate this, we need a way to serialize our models and save them to disk. The Hierarchical Data Format (HDF5) is widely used for storing large amounts of data, especially model weights, in a compact and efficient way. In Keras, a deep learning library built on top of TensorFlow, loading a model from an HDF5 file is straightforward. In this article, we’ll delve into the details of how this is done, complete with examples and explanations.

Understanding HDF5 in Keras

HDF5 is a file format ideally suited for managing large datasets. It is flexible and allows for organized hierarchical storage, which makes it perfect for saving complex entities like deep learning models. Keras models can be saved to HDF5 files using the .h5 extension with the model.save() method.

What Gets Saved in an HDF5 File?

An HDF5 file can store:

  • The model architecture (layers, inputs, outputs)
  • The model weights
  • The optimizer configuration (if applicable)
  • Any user-defined parameters or metrics

This ensures that when a model is loaded back, it's ready for evaluation or further training without any reconfiguration.

Loading a Model from an HDF5 File

To load a model saved in an HDF5 file, Keras provides the keras.models.load_model() function. This function reads the file and reconstructs the model, weights, and optimizer settings.

Basic Example

Here is a simple example demonstrating how to save a Keras model and then load it from the HDF5 file:

python
1from keras.models import Sequential
2from keras.layers import Dense
3import numpy as np
4from keras.models import load_model
5
6# Generate dummy data
7X_train = np.random.random((1000, 20))
8Y_train = np.random.randint(2, size=(1000, 1))
9
10# Define a simple sequential model
11model = Sequential()
12model.add(Dense(64, input_dim=20, activation='relu'))
13model.add(Dense(1, activation='sigmoid'))
14model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
15
16# Train the model
17model.fit(X_train, Y_train, epochs=10, batch_size=32)
18
19# Save the entire model to a HDF5 file
20model.save('my_model.h5')
21
22# Later, load the model
23new_model = load_model('my_model.h5')
24
25# Verify that the loaded model is identical
26new_model.evaluate(X_train, Y_train)

Important Considerations

  • Custom Objects: If your model uses custom loss functions, layers, or other elements, you'll need to provide them to the load_model() function. Specify the custom_objects parameter:
python
1    from keras.models import load_model
2
3    # Assuming 'my_custom_loss' is a function defined elsewhere
4    new_model = load_model('my_model_with_custom_loss.h5', custom_objects={'my_custom_loss': my_custom_loss})
  • File Path: Ensure that the file path is correctly specified, and the file exists at the given location.

Extended Use-Cases

Saving Only Weights

If you don't need to save the entire model architecture, you can just save the weights using model.save_weights('my_weights.h5').

To load weights into a model later, you need the model architecture available, and you can load the weights using:

python
model.load_weights('my_weights.h5')

Summary of Key Operations

Let's summarize the key operations for saving and loading Keras models with HDF5 in the following table:

OperationMethodDetails
Save Modelmodel.save('path_to_file.h5')Saves architecture, weights, and optimizer info.
Load Modelload_model('path_to_file.h5')Loads everything, model ready to use or train.
Save Weightsmodel.save_weights('path_to_weights')Saves only weights, not architecture.
Load Weightsmodel.load_weights('path_to_weights')Requires model architecture already in place.
Custom Objectsload_model(..., custom_objects={})Needed if custom elements were used in training.

Conclusion

Loading a model from an HDF5 file in Keras is a straightforward process that allows you to easily manage and move models across different environments, enhance scalability, and ensure development reproducibility. By understanding the essentials described in this article, you will be well-equipped to integrate model loading and saving processes into your Keras workflow efficiently. This functionality offers the flexibility to save an entire model or just the weights, making it a versatile tool for machine learning practitioners.


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.