Hello, and welcome.
In this Colab,
we will learn how to take a trained model,
save it, and then load it back up,
so we can either keep training it or use it to perform predictions.
We will be using transfer learning to train
a classifier to classify images of cats and dogs,
just like we did in the previous lesson.
So let's get started.
As always, we begin by importing the packages we're going to use.
In this Colab, we're going to use TensorFlow 2.0 Beta,
TensorFlow Hub for Transfer Learning,
and TensorFlow datasets to download our dogs versus cats dataset.
Then we proceed to import the packages as we've done before.
We are now ready to load the cats versus dogs dataset.
We will use TensorFlow datasets to load our data, as we've done before.
Notice, that we're going to split our dataset such
that 80 percent of the data will be used for training,
and 20 percent of the data will be used for validation.
Here, we use TensorFlow datasets to load the cats versus dogs dataset.
We'll use the options with info equals True,
so we can get the metadata from our dataset.
We'll use the option as supervised equals true,
so we can get the corresponding labels over images,
and we use the option split equals splits so that
TensorFlow datasets can split the data as we defined here,
which again, is 80 percent of the data for training,
and 20 percent of the data for validation.
Now, remember that the images in our dogs vs cats dataset are not all the same size.
Therefore, we need to resize all the images to the resolution expected by our model.
In this Colab, our model is going to be MobileNet,
which expects images to be 224 by 224.
To reformat our images,
we're going to use a function called Format Image,
that will take the images from our dataset and resize them to the correct size,
and then divide them by 255 to normalize
the pixel values so that they are in the range from 0-1.
Next, we create our input pipeline just as we've done before.
Notice that in this case,
we're using map before batch because TensorFlow can't batch tensors of different sizes.
Therefore, we must first resize all their images to be the same size before we can batch.
Next, we're going to build our model using transfer learning as we've done before.
We begin by downloading the feature vector from
their pre-trained MobileNet model from TensorFlow Hub.
Next, we set the variables of our pre-trained model to non-trainable.
Then we create a Keras sequential model with a new classification head with two outputs.
We'll use only two outputs because we only have two classes in this case,
namely a cat or a dog.
Here, we can see a summary of our model.
Now, let's go ahead and train our model as
usual by first calling compile and specifying our optimizer,
our loss function, and our metrics,
and then running model.fit to train our model.
Now that we have a trained model,
let's go ahead and make some predictions.
We begin by getting the class names from the metadata of our dataset,
and as we can see, the labels are cat and dog.
Next, we create a batch of images that we're going to use to test our model.
Here, we run the batch of images through our model and make some predictions.
Here, we can see the predictions of our model compared to the true labels.
We see that all the labels are marked blue,
which means they are the same label as in the dataset.
So we can see that our model is pretty accurate.
Now that we've trained the model,
we're going to save it as an HDF5 file,
which is the format used by Keras.
Our HDF5 file will have the extension.h5.
In this particular case,
we've chosen to name the file with
the corresponding timestamp that we get from the time module.
We've done this so that every time we save a file,
it has a unique name.
However, you can choose to name your SavedModels however you like.
In order to save the file,
we use the model that save method.
As you can see,
all we need to provide is the path to the file.
If you click in this little arrow,
you will see a menu that tells you the table of contents,
code snippets, and a files menu.
The files menu will display all the files in your current working directory.
As you can see, we only have one folder named sampled data.
When we run the model.save command,
this will save the model in this working directory.
So if I run this cell,
we see that it has saved the model with the corresponding timestamp.
If we press "Refresh" over here,
we can now see our model in the current working directory.
We can see the files that are in our current working directory
from the command line in the Colab by using bang.ls.
So if I run this cell,
you can see that I see the same files that I see over here.
Now that we have a saved Keras model,
we can load it up to make predictions.
We can load a Keras model using the tf.keras.models.load_model function.
All we need to provide is the path to the file containing the Keras model.
In this particular case,
we also need the custom objects parameter.
We need the custom objects parameter in this case because this parameter tells Keras
how to load the hub.kerasLayer from the feature extractor we used in transfer learning.
We're going to load our model into a variable called reloaded,
enabled to distinguish or reloaded model from our original model.
If we run a summary on this reloaded model,
we can see that it's exactly the same as the original model.
Now, let's check that our reloaded model makes the same predictions as an original model.
This should be true because we haven't done anything new to the reloaded model.
So at this point,
the reloaded model and the original model,
should be exactly the same, and therefore,
should yield the same predictions on the same images.
To test this, we're going to run the same batch of images to both models,
and compare their predicted results.
We start by making predictions with the original model on the batch of images,
and then we make predictions with the reloaded model on the same batch of images,
then we compare the results.
As we can see, is zero,
which means that both models made the same predictions on the same images.
Besides making predictions, we can also take a reloaded model and keep training it.
This often happens when you get new data and you want
to train your model on these new data, to make it better.
To train a reloaded model is very easy.
You just train it in the same way as you will do with a regular model,
but using the fit method,
as we can see here.
Now, let's take our original model and save it as a TensorFlow SavedModel.
To do this, we will use the tf.saved_model.save function.
This function takes in the model we want to
save and the path to the folder where we want to save our model.
As before, we will name our folder according to the current timestamp.
Again, we do this to guarantee that every folder will have a unique name.
Again, we can click on this little arrow and take
a look at the current files in our current working directory.
If I run this cell,
you'll see that it has created a folder with the current timestamp.
If I press "Refresh",
you'll see this folder in our current working directory.
If I look into this folder,
you will see an assets folder,
of our variables folder,
and that's saved_model.pb file.
Again, you can take a look at the files inside the SavedModel folder
by running bang ls with the export path of the SavedModel in the Colab.
As you can see, you can see the folder assets,
the saved_model.pb, and the variables folder.
Now that we have our model saved as a TensorFlow SavedModel,
and it's loaded backup and user to make predictions.
To load a SavedModel,
we use the tf.saved_model.load function.
As you can see, the only thing you need to provide is
the path to the directory where the model is saved.
Again, let's check the predictions of
reloaded SavedModel with the predictions of our original model.
Again, since we haven't done anything to our reloaded SavedModel,
then the predictions from our SavedModel and the original model, should be the same.
Again, we take our reloaded SavedModel
and pass the same batch of images we did previously.
We use the training equals false flag to indicate that we're not training.
Finally, we convert this into a NumPy array,
then we compare the predictions from
the original model to the predictions for my reloaded SavedModel.
As you can see, the result is again zero,
which indicates that the predictions from
the original model and our SavedModel are the same.
We should note that the object returned by the
tf.saved_model.load function is not a Keras object.
These means that it doesn't have a fit method,
or a.predict method, or a.summary method.
Therefore, we can't simply take our reloaded SavedModel,
and keep training it by running.fit as we did before.
To be able to get back a full Keras model from the TensorFlow SavedModel format.
We must use the tf.keras.models.load_model function.
The only thing we need to provide is the path of the folder where the SavedModel is in,
and again, we need to provide the custom objects parameter in this case.
I should note that at the time of recording this lesson,
there is a bug in the Keras API that is preventing
us from directly loading a SavedModel into a Keras model.
Therefore, we need this code as a workaround.
This code clones the original model to undo the.com pile function,
and then receives our model as our SavedModel.
This code allows this code to run.
But in the future, when this bug is fixed,
you won't need this code at all.
One of the great advantages of saving your model as a TensorFlow SavedModel,
is that you can use it later for deployment on different platforms.
To do this, you want to download your TensorFlow SavedModel to your local disk.
We can do this by creating a zip file of the folder that contains your SavedModel.
If we run this code,
we will create a model that zip file that
contains all the contents of the folder that contains your SavedModel.
As before, we can take a look at the contents of
our current working directory by running bang ls in the Colab.
Here, you can see the models.zip file.
Once we seek the file,
we can actually download it to our local disk by using this cell.
That's it for now.
Now, you know how to save and load models using TensorFlow.