Saving and Loading Keras model
By: Renu Khandelwal
Tags: Keras, Python, Deep Learning, Save, Json

Saving and Loading Keras model using JSON and YAML files

Photo by Victor Freitas on Unsplash

How can you share a deep learning model you trained that is giving awesome results with other team members working in a different part of the world or How to save a deep learning model and it’s trained weights during or after training or How to resume training of a model from where you left off?

To answer the above questions we need to save the

Model architecture

Trained weights or Parameters of the model

Sharing the saved files will allow others to recreate the model to make inferences or to resume further training from you left off. The model can be recreated by loading the model, and it’s trained weight from the saved files containing the model architecture and the pre-trained weights.

Different methods to save and load the deep learning model are using

JSON files

YAML files

Checkpoints

In this article, you will learn how to save a deep learning model developed in Keras to JSON or YAML file format and then reload the model.

To save the model, we first create a basic deep learning model. I have used the Fashion MNIST dataset, which we use to save and then reload the model using different methods.

We need to install two libraries : pyyaml and h5py

pip install pyyaml

pip install h5py

I am using Tensorflow 1.14.0

#Importing required libararies

import os

import tensorflow as tf

from tensorflow import keras #Loading Fashion MNIST dataset

(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.fashion_mnist.load_data() #creating a smaller dataset

train_labels = train_labels[:1000]

test_labels = test_labels[:1000] #Normalizing the dataset

train_images = train_images[:1000].astype('float32') / 255

test_images = test_images[:1000].astype('float32') / 255 # Reshaping the data for inputing into the model

train_images = train_images.reshape((train_images.shape[0], 28, 28,1))

test_images = test_images.reshape((test_images.shape[0], 28, 28,1)) #Defining and compiling the keras model

def create_model():

model = tf.keras.Sequential()

# Must define the input shape in the first layer of the neural network

model.add(tf.keras.layers.Conv2D(filters=64, kernel_size=2, padding='same', activation='relu', input_shape=(28,28,1)))

model.add(tf.keras.layers.MaxPooling2D(pool_size=2))

model.add(tf.keras.layers.Dropout(0.3))

model.add(tf.keras.layers.Conv2D(filters=32, kernel_size=2, padding='same', activation='relu'))

model.add(tf.keras.layers.MaxPooling2D(pool_size=2))

model.add(tf.keras.layers.Dropout(0.3))

model.add(tf.keras.layers.Flatten())

model.add(tf.keras.layers.Dense(256, activation='relu'))

model.add(tf.keras.layers.Dropout(0.5))

model.add(tf.keras.layers.Dense(10, activation='softmax'))



#Compiling the model

model.compile(loss='sparse_categorical_crossentropy',

optimizer='adam',

metrics=['accuracy'])



return model # Create a basic model instance

model = create_model() model.summary()

Saving and loading the model architecture using a JSON file

Steps for saving and loading model to a JSON file

Fit the train data to the model

The model architecture will be saved to a JSON file using to_json() . The returned string will be saved in a JSON file

. The returned string will be saved in a JSON file Save the trained weights using save_weights()

Reload the model architecture from the JSON file by reading the JSON file and then reload the model architecture to a new model using model_from_json()

The trained weights will be reloaded using and load_weights()

Fitting the train data to the model

#Fit the train data to the model

model.fit(train_images,

train_labels,

batch_size=64,

epochs=100,

validation_data=(test_images,test_labels))

Saving the model architecture and weights to JSON file

Write the model architecture to a JSON file using to_json() and save the weights of the trained model using save_weights()

from keras.models import model_from_json # serialize model to json

json_model = model.to_json() #save the model architecture to JSON file

with open('fashionmnist_model.json', 'w') as json_file:

json_file.write(json_model) #saving the weights of the model

model.save_weights('FashionMNIST_weights.h5') #Model loss and accuracy

loss,acc = model.evaluate(test_images, test_labels, verbose=2)

Original model’s loss and accuracy

Reloading the model architecture from JSON File to a new model

We have created a new model: model_j, which will have the same architecture as the saved model

I added the glorot_uniform library as I was getting a warning for the initializer

from keras.initializers import glorot_uniform

#Reading the model from JSON file

with open('fashionmnist_model.json', 'r') as json_file:

json_savedModel= json_file.read() #load the model architecture

model_j = tf.keras.models.model_from_json(json_savedModel)

model_j.summary()

The new model has the same architecture as the previously saved model

Reloading the trained weights to the new model

Use load_weights() to load the pre-trained weights to the new model

model_j.load_weights('FashionMNIST_weights.h5')

Only the model architecture and pre-trained weights are loaded to the new model, but the model compilation details are missing, so we need to compile the model.

We can specify different optimizers to compile the model, like changing the optimizer from Adam to “Rmsprop” or “SGD.”

#Compiling the model

model_j.compile(loss='sparse_categorical_crossentropy',

optimizer='SGD',

metrics=['accuracy'])

We can check the loss and accuracy of the new model to confirm if the same trained weights were loaded

loss,acc = model_j.evaluate(test_images, test_labels, verbose=2)

Model loss and accuracy on test data for the new model using a JSON file

Saving and loading the model architecture using a YAML file

Steps for saving and loading model to a YAML file

Fit the train data to the model

The model architecture will be saved to a YAML file using to_yaml() . The returned string will be saved in a YAML file

. The returned string will be saved in a YAML file Save the trained weights using save() in an H5 file.

Reload the model architecture from the YAML file by reading the YAML file and then reload the model architecture to a new model using model_from_yaml()

The trained weights will be saved using save() and loaded to a new model using load_model()

We will use the previous model for saving to a YAML file. We will not fit the model again as it was already trained.

Saving the model architecture to YAML file

Write the model architecture to a YAML file using to_yaml()

#saving the model to a YAML file

yaml_model= model.to_yaml() # writing the yaml model to the yaml file

with open('fashionmnist_yamlmodel.yaml', 'w') as yaml_file:

yaml_file.write(yaml_model)

Reloading the model architecture from YAML File to a new model

Read the model architecture from the YAML and restore it to a new model, model_y using model_from_yaml()

We get the same model architecture as the original model

#Read the model architecture from YAML file

with open('fashionmnist_yamlmodel.yaml', 'r') as yaml_file:

yaml_savedModel=yaml_file.read() # Load the saved Yaml model

from keras.models import model_from_yaml

model_y= tf.keras.models.model_from_yaml(yaml_savedModel)

model_y.summary()

Save the weights using Save() in HDF5 format in a single file/folder

The architecture of the model so that we can recreate it anywhere anytime

Trained weights of the model

Training configuration like loss function, optimizer, etc.

State of the optimizer

Save() helps to export a model and recreate the entire model or resume training from where you left off.

The model could be reinstated using load_model(), which also takes care of compiling the model using the saved training configurations.

#saving the smodel's architecture, weights, and training configuration in a single file/folder. model.save('fashionmnist.h5')

Reinstating the model

# loading the model from the HDF5 file

model_h5 = tf.keras.models.load_model('fashionmnist.h5')

Let’s evaluate the new reinstated model on test images. Note we are not compiling the model here

loss,acc = model_h5.evaluate(test_images, test_labels, verbose=2)

Model loss and accuracy on test data for the new model using a YAML file

We get approximately the same loss and accuracy as we have not set the seed here.

code for saving the model and reloading model using Fashion MNIST

Conclusion:

We can now save weights and reload them into a new model using the JSON file or YAML file that will allow you to share your excellent deep learning work with the world or resume your training for better performance.

Saving and loading the model using checkpoint is discussed in the next article

References:

https://keras.io/getting-started/faq/#how-can-i-save-a-keras-model