In this article, we’ll look at how we can save a model that we have trained in Keras.

First, we’ll create a very simple CNN model using Keras and train that model on the Fashion MNIST data set. Then I will show you how to save and subsequently restore that trained model and perform the prediction.

I understand that saving a model for a very basic problem like fashion MNIST might sound trivial.

However, think of a situation where we are training a very complex model which takes a tremendous amount of time, like hours, or even days, to train.

In such cases, it is often useful to save the model. So, in the future, if we need to make predictions, we can simply load the trained model and make our predictions instead of training our model again from scratch.

In Keras, models are saved in the following three formats.

Let’s see how to save the model in each of these formats.

HDF5:

Generally, the models are saved in this format. HDF5 is short for the Hierarchical Data Format. The number five indicates the version.

The HDF format will store our entire model. It saves all the information about our model, including the architecture, model weights, trained parameters, optimizer details etcetera.

In Keras, saving a model in the HDF5 format is quite simple. Keras has a save() function that allows us to easily save the model in this format.

h5 is the extension used to save the model in HDF format.

YAML OR JSON:

Instead of saving the entire model Keras also gives us the flexibility to save the models architecture and weights separately.

We can use either of these two formats to save only the model’s architecture without the weights, parameters, loss, or optimizer settings.

We can use the following functions to save the model in JSON or YAML format.

Since these formats save only the architecture we need to save the model’s weight separately.

Weights can be stored in the h5 format. To save the weights we can use the save_weights() method.

Now let’s do some coding.

Firstly, we’ll import all the necessary packages and the dataset. We’ll use the fashion MNIST dataset.

Next we’ll create our CNN model. The below code snippet is taken from Keras Github Repo.

Finally, we can train our model using the model.fit() function.

Since we now have our trained model, let’s see how to save our model in the HDF5 format.

As we have already seen, it is super simple. We just need to call model.save() method.

That’s it. The model will be saved in your current working directory.

In most cases, we will not load a saved model in the same session. However, for demonstration purposes, let’s load our model.

Now we can use our restored model to make predictions.


Similarly, let’s look at how to save the model in JSON and YAML formats.

Save model in JSON and YAML formats:

Firstly we’ll see how to save the model in json format. The following code snippet will save our model in the json format.

The model is converted into a JSON string on the first line, and afterward, on the second and third line, the JSON string is stored into a file named model.json.

Since JSON saves only the architecture, we need to save the model’s weight separately.

Weights can be stored in the h5 format. The save_weights () method can be used to save the weights.

Since we now have our weights and architecture, let’s reconstruct our model from the JSON file.

The code above is pretty self-explanatory. Firstly, we are importing the class model_from_json.

On the second and third lines, we open the file and read its contents.

Finally, we pass the contents of the json_string to the model_from_json to reconstruct the model.

Now, if we need to make predictions, we need to load the weights and compile our model. To load the weights into our model, we can use the load_weights () function.

We can now make predictions with our model.


Saving a model in YAML is very similar to saving it in JSON. We just need to make a couple of changes.

Replace model.to_json with model.to_yaml and import model_from_yaml instead of model_from_json.

There is also another method to save the model in Keras. It is by using the Callbacks.

Save model in Keras using Callbacks:

Keras callbacks are functions that are executed during the training process. According to Keras Documentation, A callback is a set of functions to be applied at given stages of the training procedure.

I have written an article explaining some of the commonly used callbacks. You can read the article Keras Callbacks to learn more about callbacks.

Now for this post let’s see how to save a model using Model Checkpoint callback.

By using model checkpoint callback, we can save our model at regular intervals.

The model checkpoint callback saves the weights of the model along with the structure once we made progress, like hitting a new validation accuracy or loss. We could also save the model for every epoch.

The signature of the callback is as follows

  • monitor: quantity to be monitored
  • save_best_only: if TRUE saves only the best model according to the quantity monitored
  • Save_weights_only: if TRUE only the weights of the model are saved
  • mode:  one of {auto, min, max}. In min mode, training will stop when the quantity monitored has stopped decreasing; in max mode, it will stop when the quantity monitored has stopped increasing;  auto mode, the direction is automatically inferred from the name of the monitored quantity.
  • period: after how many epochs you need to checkpoint the model

The code is same up until the model_compile() part. Add the following after the model.compile().

In the above code snippet, we have created the Model Checkpoint callback and passed it to the fit function. It will save our model in the h5 format every time our validation loss improves.

Summary:

In this article, we looked at different ways to save a model and reconstruct it again in Keras.

Firstly, we saw how to save an entire model, including architecture, parameters trained, weights, loss, and optimizer states, etcetera.

Second, we discussed the JSON and YAML formats, which only store the architecture of the model.

Finally, we used the Keras Callback Model Checkpoint to save the model at regular intervals whenever we hit a better validation loss.

Visit this Github Repo to get the complete code.



Source link

author-sign