So we've really started to evaluate how our DOD vision model is going.
By visualizing our models predictions should I give you a little challenge before we get into this video
which is saving and reloading a model.
What an interesting thing might be is now that we've visualized the images the predicted label the predicted
probability the true label and the top 10 predictions how could you make a confusion matrix to figure
out where your model is confused with what we've got.
So remember a confusion matrix is comparing the predicted labels to the true labels and then seeing
which ones the model gets confused because that would probably be really helpful with this sort of problem
and helpful with any kind of multi class classification.
So that's a little challenge to you a little bit of extra curricular.
I'll put it here before we save and load a model.
Let's go here.
Challenge how would you create a confusion matrix with our models predictions and true labels.
So if you do figure it out make sure you share it in the discord chart you can share your work and help
others if they're trying to figure it out as well.
With that being said good luck with the challenge.
Let's create a little title for saving and reloading a model.
Now this is helpful.
Of course it's taken us a while to get here so it makes sense to want to save our progress after we've
tried to model let's figure out how we would do that.
A trained model.
And it also helps if you wanted to share your model.
So say we wanted to export our dog vision prediction capability to someone else.
We wanted to put it in a web application.
We're not might need to get it out of our car lab notebook.
So that's what I'm gonna do here.
TENSOR flow.
How to Save a model so this is what I would do.
Oh that guy's really excited just like me just like us.
Let's save and load models.
Again we could read through here.
But what we're gonna do is we're just going to write the code first and then if you want to read up
on a little bit more you can read through the documentation here.
So we'll create a function to do it so we can call it on any model.
So create a function to save a model and do it nice and simple.
Def save model pass on a model and suffix equals now and you'll see what suffix means in a second.
So just like how log directory in dog vision we might create a new folder called models.
And what we're going to do is as you might have guessed is save.
We've got our training logs here and save our models to dog vision so we keep it all within that one
project folder.
Sticking with what we've got in our projects set up create a project folder.
So this is going to go.
Saves a given model given model in a model's directory and appends a suffix string.
Nice and simple.
So if we go here let's create a model directory path name with current time.
So just like our tend to flow logs callback that we created a few videos ago.
We want to first of all save our model with the current time so we can track our experiments.
And we're gonna give it a little suffix so we also know a little bit about our model.
So we're not just opening our models folder and it's just timestamps.
We'd also like to know how many images was it trained on and stuff like that.
So that's what we'll do.
We'll go model day equals US path the joint just like our tensor board my drive slash dog vision slash
models and then we'll append will join it up with the current time which we can use date time for that.
So daytime daytime now that string from time.
And we're going to go year month to month day our month on minute second.
OK beautiful.
But now we also want model path where we want to combine the model directory with our suffix so model
the plus hyphen plus suffix plus dot height 5.
Now if you're wondering what height 5 is that's the same format of model.
It's just a file specification just like JPEG and if you read through the documentation it's somewhere
here.
It's got a huge five
there we go save whites with a page five ext.
There we go so that's gonna be the extension and we just want to print a little update for ourselves.
If saving model to we'll go and model path Dr. Dot just to let us know that it's in progress and we'll
go to model dot save.
So there's a method called Save that you can call on a model just like model not predict and you posit
a file path.
Let's see if the documentation will come up.
There we go.
Saves the model to tend to flow save model or single HD F5 file.
So that's what I meant by page 5.
So that's what we're doing.
And then return model path because we want to see the model path we can use that later if we want okay.
Now we've got a function to save a model while we're here.
We'll build a function to load a model because if we save the model we want to potentially load that
back in.
So it makes sense to load our trained model and then we'll test them both at the same time create a
function to load a trained model so this one is gonna be very similar to what we do.
So we go.
Def load model I want to pass in a model path.
So it's just like hey we've saved a model this low return the model path we've saved a model to this
file path which is going to be our models folder somewhere here and so to load model we want to go.
Okay what can we call we can go loads a saved model from specified path and then we go print f loading
saved model from where is it loading it from.
So just this is just for our own sanity to make sure that we're loading the right model model not t
after carers models dot load model again saving and loading models you would find this load model here.
So load model.
There we go recreate the exact same model including its white and the optimizer.
So because we've used tens of fly hub we actually need to do a little special thing here.
So model path custom objects so this documentation a lot of carriers intensify.
Documentation is of the simplest use case but since we're not using the simplest use case we're using
a test flight hub model we need to pass it this little custom objects dictionary so carer's layer we
just need to tell it hey we've got an irregular layer so a layer you wouldn't have seen before in our
model and that's all we have to do so all we're doing is loading a model from model path and saying
hey we've got a custom little piece of equipment in our model in particular where is it right back up
here look how much we've done you should be really proud of yourself you're watching this hub dot carer's
layer that's all we have to do.
So instead of just using these kind of layers which are the traditional carer's layers tend to flow
we're using this special one.
So that's what we're telling this load model function
and then we want to return the model.
So now the ultimate test let's put it here.
Now we've got functions to save and load a trained model let's make sure they work so what we'll do
is we'll save our model
trained on a thousand images.
So this is where the suffix comes in.
So because we traded on a thousand images we want people to know that this is only a thousand image.
We want ourselves to know that this is only a thousand image model suffix equals a thousand images and
we'll probably put Adam there for Adam optimizer in case you wanted to change.
You could really put whatever suffix you want here.
Thousand images.
The mobile Net V2.
Adam.
That's probably nice and simple.
So let's see what this does.
And again this is just a communicative part not 100 percent necessary but I found in practice it's very
good to communicate just even if it's to yourself as well as if you wanted to share with someone else
they go they wouldn't just look in your models director and go hey which model is which they could go
Oh there's a suffix here and then maybe you could tell them about that suffix
saving up unable to create file.
What's happened here.
You know what.
That's exactly what I thought.
Look at that.
What's wrong with that classic.
So these trust me you're going to run into these errors these and reshape errors getting your data into
the right shape.
Going to plague you for your entire machine learning career.
Typos are a program is worst nightmare.
So we go here up there we go.
Saved.
So there's the file path to the model.
And now if we come up into the models directory there we go.
There's our saved model.
So dot Hage five.
Wonderful.
So if we X out of that let's now test our load model so load try and model so all we have to do is we're
going to go loaded thousand image model long nine there.
But just for clarity and we go load model.
This is why we return that so we can just copy that they're will like that shouldn't take too long.
Now.
Probably gonna have some warnings here.
Yes.
So these warnings.
What is it saying.
Sequential models without the input shape past the first line cannot reload the optimizer state as a
result your model is instantiating that's a fairly long warning.
But if you come up to here and check warning and I think it's one of the top ones.
There we go.
This may generate warnings if you load a model related to the saving State of the optimizer these warnings
and similar warnings throughout this notebook are in place to discourage outdated usage and can be ignored
so these warnings can be ignored according to the documentation.
So be helpful if they just remove them rather than just have them printed out.
But aside from that let's just see if it works if it works then that's where all gravy.
So evaluate the pre saved model.
Now another function you can call on your train models is evaluate and pass it the data set value data.
And so we're going to run that.
So this is our model before saving it.
And this is our model after loading it.
So evaluate the loaded model the model dot evaluate Oh no I only nearly messed up.
We're on autopilot now and started typing about the cell above image model dot evaluate Val data.
So if all is swell they should return the exact same thing.
Wonderful.
So this is just an evaluate function to quickly evaluate your models performance on the validation set
and we can say here the loss is one point 2 6 0 7.
Same as this.
And the accuracy is the exact same.
So there we go how to save and load a model now is saved and loaded a model.
I think it's time.
I think it is time to step it up and train a model.
We've got it all working.
Let's train one on the full dataset.
So get yourself ready get yourself excited.
I'll see you in the next video.
We're going to train a model on the full 10000 images.