Saving and Loading Keras model using JSON and YAML files | by Renu Khandelwal | Towards Data Science

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 pyyamlpip install h5py
I am using Tensorflow 1.14.0
#Importing required libarariesimport osimport tensorflow as tffrom 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 datasettrain_images = train_images[:1000].astype('float32') / 255test_images = test_images[:1000].astype('float32') / 255# Reshaping the data for inputing into the modeltrain_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 modeldef 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 instancemodel = create_model()model.summary()
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
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 modelmodel.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 jsonjson_model = model.to_json()#save the model architecture to JSON filewith open('fashionmnist_model.json', 'w') as json_file:    json_file.write(json_model)#saving the weights of the modelmodel.save_weights('FashionMNIST_weights.h5')#Model loss and accuracyloss,acc = model.evaluate(test_images,  test_labels, verbose=2)
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 filewith 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 modelmodel_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)
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
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 fileyaml_model= model.to_yaml()# writing the yaml model to the yaml filewith 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 filewith open('fashionmnist_yamlmodel.yaml', 'r') as yaml_file:    yaml_savedModel=yaml_file.read()# Load the saved Yaml modelfrom keras.models import model_from_yamlmodel_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 filemodel_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)
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
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