Saving and Loading Models in TensorFlow & Keras

Saving and Loading model is one of the key components of building Deep Learning Solutions. Not only they are used in model deployments, but also in Transfer Learning. The already trained model from Millions or Billions of records can be saved and used by others who want to just deploy and use the model or do not have access to huge amount or training data.

There are different ways in which models are saved in Keras and Tensorflow, which are outlined below

Saving and loading Models in Tensorflow

Saving and Loading Model in Keras

Keras models can be saved is json and yaml format with weights saved separately in .h5 format.

the code is below


from keras.models import model_from_json
from keras.models import model_from_yaml
model_json = model.to_json()
with open("model.json","w") as json_file:
    json_file.write(model_json)
model.save_weights("model.h5")

Load Models

json_file = open("model.json","r")
loaded_model_json = json_file.read()
json_file.close()

loaded_model = model_from_json(loaded_model_json)


loaded_model.load_weights("model.h5")
print("Loaded model from disk")

The Code is posted here

Written on May 19, 2018
[ ]