In our previous section, we've built a deep learning model based on convolutional neural networks to
help detect the presence of malaria in blood cells.
Nonetheless, in the real world, we are not always going to be using our models on a lab notebook like
this.
Hence, we need to be able to save this model so it could be used externally.
In the section we'll learn how to save and load the model and also do the same process with Google Drive.
That is, we'll be able to save our model in our Google Drive.
And then later on, when we want to use this model, we'll just load it from our Google drive.
That's it.
Don't forget to subscribe and hit the notification button so you don't miss amazing content like this.
We've built this very performant model, though we could improve on it, but then once we close this,
we do not save this model's current state.
And so if we have to come next time, the model will have randomly initialized weights, which will
be different from the weight we've got now after training on this data set and other issues in case
we want to use this model in another scenario or in another environment like, say, on a browser or
on a mobile phone.
We'll need to find a way to export this model from your.
And so TensorFlow allows us to save our model.
Now we'll have to differentiate between a model's configuration and a model's weights.
So a model like this, let's suppose we have a model which is defined as such.
We have the input which will pass into account layer, and then we have batch normalization, we have
pooling for subsampling and then we flatten and after flattening we pass to a dense layer and we have
our output.
So suppose we have this small model.
Now all the parameters for the creation of this model are known as the model's configuration and the
model configuration.
Women have it that the model, for example, like in this case here, the model starts with a curve
layer with six filters, canal size, three batch norm and all this.
So these are our model's configurations.
But this model's configurations are different from the model's weights.
The model's weights are those filters.
We have for example in the case of the conf two D, so we have the model weights and the models configuration.
And upon summarizing the model, we see clear that we have this connectivity and then we have this number
of parameters.
And so whenever we want to save a model, we have to take into consideration this configuration and
the weights, because for this same configuration, we could have different weights.
And so there are actually two main options.
The first option is to save the full model, that is to save the model configuration and the model weights.
Another option will be to save only the model weights.
So we could save all of the model weights.
Now, this option is used when, for example, we don't want to or we don't even know this model configuration
upfront.
So we have used this year we've defined the model's configuration with trained it, we've got a new
weights and this the current model state.
But if we take this to another environment where we don't have this configuration, then if we've saved
this model's configuration and weights, all we need to do is just to load this configuration and weights
which have been packaged as the full model.
Now, in another case where we are able to get the configuration and all we need is just the weights,
then we'll just save the weights and then reload these weights since we already have the configuration.
Either ways we will always need the configuration and the weights.
Nonetheless, it's important to note that the most important part of this is actually the weights since
working with a randomized or randomly initialized weights.
After we've trained, our model isn't very useful and sometimes we may take many days to train this
model.
So imagine you train your model for like ten days and then you want to reuse that model and the weights
have been randomly initialized.
You find that those ten days have been wasted both time wise and monetary wise.
So you have to ensure that you save your weights properly such that you could reuse them.
And then the great thing with TensorFlow is you could also continue training from the state.
So this means that at this point where we've gotten this model's performance, here's where we have
94%.
We could keep training from years so that we could get even to say 99%.
So you have to ensure that your saving is done properly.
Now let's get into that.
But before getting to that one last point, also note that with the first method here, with this first
method, apart from this model configurations, we also have information like the Matrix.
So the matrix you use, like the accuracy, the loss, you use the optimizer, so the optimizer information
you use and all that.
So this kind of hyper parameter information has been saved here.
So next time all you need to do is just to load your model and then make use of it.
Whereas here all you're saving is just the weights.
That said, let's see if our model, this case we have in that model, that safe actually then that
model are safe and we give it a name.
So we say the net saved model, for example.
That's it.
Now we have this little net saved model or we run that cell and we check this out here.
So check out those files.
And what do you see?
You have this net saved model folder.
In this folder you have the assets, which in this case is empty.
You have the variables.
Which actually contains the weights.
So you could download this from yours, we could download this and then upload it next time.
So from here we see you click on download.
That's fine.
We have the variables which contains the weights.
That's it.
And then we have this saved model that put about file here, which actually contains our configuration.
We've had our configuration saved and our weights saved.
Now let's load this.
So we've saved this and now we can now load it.
Note that you could always download this, so you could download the weights right here, download this.
So let's click on download, get download all this and then next time all you need to do is just to
load it.
Now let's go ahead and load this.
The loading is quite simple.
Yeah, we will define a new model net load that we have loaded.
Model equals tier dot cross dot models, dot load model.
There we go.
And now we specify this exact same name.
We have the net saved model.
Now what we do is we're going to do the net load at model and then summary.
So that's it.
We're going to run that and.
We get this arrow here, which is unusual.
Changing this name actually makes this work.
So, Lynnette, and then your.
So we have this little net and let's run that again.
We save that and then we load this and we have our model right here.
So this means if you have to come back to this notebook, all you need is to load this model which has
been saved right here in this little neck folder.
And so just like with this, we will replace this little net model by net load that model.
So let's load this.
Let's use this loaded model and do some predictions.
There we go.
Yes, we'll get you.
You, you p p p here we have an error.
You, you, you, you and p p.
So it's kind of similar to what we have with the original model.
From here.
We could also evaluate this model.
We have learned load that model, we evaluate that and let's see what we get.
Recall previously we had 95.16%, so now we expect to have something around that value.
There we go.
We have exactly the same output as previously.
Now we are going to look at how to load and save with the HD five format.
Now, the PDF format is a lightweight version of this TensorFlow model saving method.
Here there's only this slight difference.
All we need to do is to say include this file extension so we have your HD hd f five and then we save
that.
Now you check this out, you should have the HD five appearance.
So here we have the net hd F five and then you could see it's weight like 52 re megabytes.
S Now let's load this model to load it.
What we have here is the same code we had previously, and then here we specify h, d, f five.
So there we go, we run that and we have exactly the same summary.
So that's it.
Now we're yet to work with custom layers that you have to note that and the case where you build custom
layers, then those configurations aren't start when you're dealing with this HD file format.
And so that's why generally it's preferable for you to use this first format in which we presented that
set.
We're done with this first method where we save the configurations and the weights.
Now let's look at this next method where we save only the weights.
So in this case, for example, where we're having this notebook, what we could do is simply just save
the weights, given that we already have the model's configuration defined in here.
So let's get straight away into looking at the save Weights method, which comes with TensorFlow.
So take that off.
And then right here we have the net model, which we defined already, and then we save this weights
as the net weight.
So here we've saved this weights, let's put it in the folder.
So we have the weights folder.
We run that again so we could see clearly our weights.
Click on that.
And there we go.
We have our weights and this weights, we have the check pointing, we have the weights.
Now notice how this weights here that's in this variables.
If you click on this variables.
Okay, so notice how there is some similarity between what we had here and this.
Notice how this is the same as this.
And then this index here is the same as this.
Here we call we said that this variables contain the weights and then we have this check points.
Subsequently, we're going to look at checkpoints and with TensorFlow.
So for now, just know that this is how we save the weights and then upon defining your model, so you
define your model, you can now load just this weights and not the whole model, but loading the weights.
You saying that you don't want the optimization or the optimizer configuration, you don't want the
metrics and you don't want the last configurations.
So that said, let's look at how to load these weights.
Now here is all we need to load the weights.
We have the learned model, the load weights, and then we load this weights.
Let's do this.
So you see clearly that this loading actually works.
So the first thing we do is we are going to re initialize our model, so we will rerun this.
So we run this will compile our model and then we run this evaluation right here.
So we evaluate the model.
And so you see that when the models which are randomly initialized, we have very poor results.
So there we go.
After random initialization, we have.
This.
Now, what we'll do is we'll take that model and then we load the weights.
So we're going to load the weights and then pass in our weights, slash the net weights, and that's
fine.
Let's run this again and then get a new model's performance.
There we go.
We see that our model now gets back to the 94.16% accuracy we had initially after doing the training.
At this point, we've been able to load and save our model right here on Google Lab.
But as we know, at the end of the session or after closing my notebook, all this information will
be lost.
So let's see how to save this information in Google Drive.
We'll start by imparting this drive.
So we have from Google Dot Drive from Google Lab.
We're going to import the drive.
Let's run that.
And then the next thing we'll do is to mount this drive.
So we have drive that mount and then we specify the location.
So we have your drive and running that you will be asked to put in an authorization code right here
to get this authorization code.
Suffice it to click on this right, this link given to us here.
So we click on that link and then once this pops up, we have this.
You select your account.
Once you select that account, you now go to connection.
So you've connected and then you copy your code.
So your code is copy it.
Now you put it in your and then you press enter one.
That's fine.
You see we have your mounted at content slash drive.
So mount in this location you could see clearly from here.
And this tells us we are in this directory content and in this directory content we've created this
directory drive.
Now we click this open and then from this I can get access to my own Google Drive.
If now I want to copy this little net folder into my Google drive so that next time I could just load
it from my Google drive.
I'll make use of this CP command right here.
So what we'll have is CP some option and then we have the source and the destination.
So here we are going to specify or we are going to use this are so recursive, so we're going to use
this to copy directory recursively.
And that said, we run the command, we have this here and then we specify this folders directory here.
So we have content and then net.
So we're copying this net and then to what destination to my drive.
So we specify my drive, we have my drive and then in here I have Loonette all I just hit Loonette collab.
So that's it.
From here I'm going to run this my drive and then I'll search for the net collab.
So that's what I have now.
I have this Loonette collab right here and then our next step will be to copy from the my drive to the
Google Collab Search that next time in case where we have not, for example, saved this year, we'll
be able to just quickly get that information from the drive onto the Google collab.
Also know that this is really used to with data sets.
So what we can have here is data set and our collab and then we could transfer that data set to our
drive and vice versa.
Now let's do the same thing.
So here we are going to copy this back, but this time around we're going to copy this into Loonette
collab, so we're going to create a new photo, the Net collab and then take this information.
So this time around we're copying from our drive and to Loonette collab.
So that's it.
And then Loonette collab, we run that and let's click on this click again and guess what we see we
have our Loonette collab right here.
Thank you very much for following up to this point and see you next time.