Keras
model saving
machine learning
neural networks
deep learning

How to save final model using keras?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In the machine learning lifecycle, saving the final model is a critical step. It allows you to deploy your model in various applications, re-use it later without re-training, or share it with collaborators. When using Keras, a popular deep learning library in Python, saving and loading models is straightforward but somewhat nuanced. This article provides a detailed guide on how to effectively save and load models using Keras, including technical explanations and examples.

Saving a Keras Model

Why Save a Model?

  1. Deployment: Once a model is trained and performing well, it must be integrated into applications for prediction tasks, whether it's a web app, mobile app, or embedded system.
  2. Reusability: Re-running the training process every time you need predictions is inefficient, especially with large datasets.
  3. Reproducibility: Saving models allows experiments to be replicated and validated.

Methods for Saving Models

Keras provides multiple ways to save models:

  1. Model Checkpoints: Save the model during or after training.
  2. HDF5 File Format: Save both architecture and weights.
  3. TensorFlow SavedModel Format: A robust format for saving TensorFlow models.

Saving with Model Checkpoints

Model checkpoints are useful for saving the model at certain points, allowing recovery at a later stage.

python
1from keras.callbacks import ModelCheckpoint
2
3# Define the checkpoint
4checkpoint = ModelCheckpoint('best_model.h5', monitor='val_loss', verbose=1, save_best_only=True, mode='min')
5
6# Assuming `model` is your Keras model and `training_data` and `validation_data` are your datasets
7model.fit(training_data, validation_data=validation_data, epochs=50, callbacks=[checkpoint])

HDF5 File Format

Keras can save model architectures and weights in a single file with the HDF5 format, which is easy to deploy.

python
1# Save the model
2model.save('model.h5')
3
4# Load the model
5from keras.models import load_model
6
7loaded_model = load_model('model.h5')

TensorFlow SavedModel Format

This format is preferred for deploying models in a production system and supports custom objects.

python
1# Save the model
2model.save('saved_model/my_model')
3
4# Load the model
5loaded_model = tf.keras.models.load_model('saved_model/my_model')

Choosing Between HDF5 and SavedModel

When to use one over the other can depend on specific requirements:

CriteriaHDF5SavedModel
File FormatSingle .h5 fileDirectory with assets and variables
CompatibilityLimited to TensorFlow/KerasDesigned for TensorFlow 2.x and beyond
Feature SupportBasic model architecture & weightsComplete model functionality including custom layers and inference
DeploymentSuitable for quick prototyping and simple use casesRecommended for production and serving
Ease of UseEasy to use in PythonComprehensive support for multiple languages through TensorFlow Serving

Best Practices for Saving Models

  1. Automate Checkpoints: During training, automate the saving of checkpoints to capture the best version of your model based on validation metrics.
  2. Versioning: Use versioning in file names or directories to manage multiple models and track changes.
  3. Document Custom Objects: If using custom objects like layers or activation functions, ensure you have means to reload them, possibly using custom_objects parameter in load_model().
  4. Storage Management: Compress and organize storage to manage disk space, especially if saving multiple model versions.
  5. Validation: After loading, always validate models on unseen data to ensure integrity.

Additional Subtopics

Custom Objects

If your model has custom layers or loss functions, ensure they are reloadable:

python
1# Save model with a custom object
2model.save('custom_model.h5')
3
4# Load model with the custom object
5custom_objects = {'CustomLayer': CustomLayer}
6loaded_model = load_model('custom_model.h5', custom_objects=custom_objects)

Using JSON and YAML for Model Serialization

You may choose to save the architecture only, using JSON or YAML, which requires separate management of weights:

python
1# Save architecture to JSON
2json_string = model.to_json()
3
4# Save architecture to YAML
5yaml_string = model.to_yaml()
6
7# Load architecture from JSON or YAML
8from keras.models import model_from_json, model_from_yaml
9
10loaded_model_json = model_from_json(json_string)
11loaded_model_yaml = model_from_yaml(yaml_string)
12
13# Manually load weights
14loaded_model_json.load_weights('model_weights.h5')

Saving Optimizer States

To resume training with the exact state, save optimizer states:

python
1# Save model along with optimizer state
2model.save('full_model.h5')
3
4# Load model and continue training
5model = load_model('full_model.h5')

Conclusion

Saving and loading models in Keras is a foundational skill for any machine learning practitioner. Understanding the nuances between different saving methods ensures that you can efficiently integrate your models into applications, share them with peers, and build on them for future research. Whether you're using the simple HDF5 file, or robust TensorFlow SavedModel format, Keras provides the flexibility needed to manage your deep learning models effectively.


Course illustration
Course illustration

All Rights Reserved.