‫So we've really started to evaluate how our dog vision model is going by visualizing our models predictions.
‫Just want to 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 ten 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 multiclass classification.
‫So that's a little challenge to you, a little bit of extracurricular.
‫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 chat, 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 trained the model.
‫Let's figure out how we would do that.
‫I trained model and it also helps if you wanted to share your model.
‫So so we wanted to export our dog vision prediction capability to someone else.
‫We wanted to put it in a web application.
‫We might need to get it out of our CoLab notebook.
‫So that's what I'm going to do here.
‫TensorFlow How to Save a Model.
‫So this is what I would do.
‫Oh, that guy's really excited.
‫Just like me.
‫Just like us.
‫Save and load models.
‫Again, we could read through here.
‫But what we're going to 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 we'll do it nice and simple def save model pass it.
‫A model and suffix equals none.
‫You'll see what suffix means in a second.
‫So just like our log directory in dog vision, we might create a new folder.
‫Cold 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 project 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 TensorFlow 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 going to 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 time stamps.
‫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 dir equals Oh's path to join just like our tensor board, my drive slash dog vision slash
‫models and then we'll append.
‫We'll join it up with the current time which we can use date time for that.
‫So date time.
‫Date time.
‫But now the string from time and we're going to go.
‫Yeah.
‫Month or month day.
‫Our.
‫Month or minute second.
‫Okay.
‫Beautiful.
‫But now we also want model path where we want to combine the model directory with our suffix.
‫So model der plus hyphen.
‫Plus suffix plus dot h five.
‫Now, if you're wondering what five 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.
‫Let's go.
‫Five.
‫There we go.
‫So if whites with ah5 extension.
‫There we go.
‫So that's going to be the extension.
‫And we just want to print a little update for ourselves.
‫If saving model two, we'll go model path, dot, dot, dot, just to let us know that it's in progress
‫and we'll go model dot save.
‫So there's a method called save that you can call on a model just like model to predict and you pass
‫it a file path.
‫Let's see if the documentation will come up.
‫There we go.
‫Saves the model to TensorFlow save model or single HD free file.
‫So that's what I meant by H5.
‫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 a 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's going to 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 is why we return the model path.
‫We've saved a model to this file path, which is going to be our models folder somewhere here.
‫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 if.
‫Loading.
‫Saved model from where's it loading it from.
‫So just this is just for our own sanity to make sure that we're loading the right model model.
‫TF Cara's 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 weights and the optimizer.
‫So because we've used TensorFlow hub, we actually need to do a little special thing here.
‫So model path.
‫Custom objects.
‫So this documentation, a lot of carers and testify documentation is of the simplest use case.
‫But since we're not using the simplest use case, we're using a TensorFlow 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 here.
‫Watching this hub scarers layer.
‫That's all we have to do.
‫So instead of just using these kind of layers, which are the traditional carriers layers, TensorFlow,
‫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 trained it on 1000 images, we want people to know that this is only 1000 image or we
‫want ourselves to know that this is only a thousand image.
‫Model suffix equals 1000 images and we'll probably put atom there for atom optimizer.
‫In case you wanted to change it, you could really put whatever suffix you want here.
‫1000 images mobile net v2 atom.
‫That's probably nice and simple.
‫So let's see what this does.
‫And again, this is just a communicative part, not 100% 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 directory 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 and unable to create file.
‫What's happened here?
‫You know what?
‫It'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 are going to plague you for your entire machine learning career.
‫Typos are a programmer's worst nightmare.
‫So we go here.
‫Up there we go.
‫Saved.
‫So there's our file path to the model.
‫And now if we come up here into the models directory.
‫There we go.
‫There's our saved model.
‫So, dot age five.
‫Wonderful.
‫So if we exit out of that, let's now test our load model.
‫So load a trained model.
‫So all we have to do is we're going to go loaded thousand image model long name there.
‫But just for clarity.
‫And we go load model.
‫This is why we returned that.
‫So we can just copy that there.
‫Although that shouldn't take too long.
‫Now probably going to have some warnings here.
‫Yes.
‫So these warnings, what is it saying?
‫Sequential models without an input shape past the first layer cannot reload their optimize estate.
‫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 print it 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 validator.
‫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 model.
‫Evaluate.
‫Oh, no, I nearly, nearly messed up, went on autopilot there and started typing above 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 model's performance on the validation
‫set.
‫And we can see here the loss is 1.2607, same as this, and the accuracy is the exact same.
‫So there we go.
‫How to save and load a model.
‫Now.
‫We 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 data set.
‫So get yourself ready.
‫Get yourself excited.
‫I'll see you in the next video.
‫We're going to try to model on the full 10,000 images.