Hi and welcome back in this lesson, we'll take a look at using cameras to train our one CNN model on
the Catzavelos Dogs dataset, which is basically something you can apply to your own datasets as well.
So open this notebook and we'll get started in this lesson.
So there's a lot going on in this lesson.
So I may be a bit a bit quick with some things, however.
I'll try my best to be slow and explain these things line by line and block by block.
So firstly, in this first section, we're loading all of the necessary functions we're going to be
using.
Take note that we're using the skills tree and test split function.
So it's no that now that's downloadable images and unzip them.
This takes about 30 seconds, roughly.
So while that's how well that works, well, that runs.
I'll talk about this.
This is where we just define our image width and height and image size, which takes more to do this
width and height as inputs.
And then we just set the number of channels because it's a color image we're using.
So it's 60 by 60 by trees, the dimensionality of this.
And next, we'll wait for that to finish.
Yeah, here we go.
So next, forget the error below.
Let's run this and get rid of it.
What we're doing here, we're going to load our data and we're going to store the file name and its
class in a data frame.
And the reason for that for us, because Keros has an automatic function that allows you to pull information
like this out of the data frames, which are which is convenient because data scientists like myself
and I'm assuming many of you would be familiar with pandas.
And this is a very, very good tool.
So Keros integrates directly with pandas.
So once you have your file name and your class here, it's a very good way to separate it, in my opinion.
You can actually do that.
So to do this or to build this, all we have to do is actually just get the file names for all of the
images in the directory.
Split them by the DOT because if it's but that's how the clusters are named, it's not always going
to be this way.
Just remember that this is specific to this dataset, which is let's actually look at the test data
actually again.
And remember, you won't see it, but the images are named something like, Let's do this here.
So if there's a dog in the image, it's going to be dog dot.
Some no jpg.
So if you split on the dot here and if a category, if you get if you grab that first element and split
the fishery, you will probably get you can get whether it's a cat or dog in that sense.
And then you can just use pandas after the reframe here to create the use.
Those are easier.
So you get the file names here and the categories, and you just create this data frame out of this.
So that's quite convenient.
And then you can do things like value counts and plot and bob plot to get the codes that each category.
And you can even view a sample image here using the lower image function.
This is not related to the pandas part of it.
This is just a way for us to get a random image is random.
The choice in the file names here we get our sample image name because it's in this.
This is our actual pet.
And we just use my plot lib to show the image.
So it's quite simple and you get a different random image every time you run this code.
So now this will create a model which we've have done many times before, and this is the same model
we used to pay to listen to.
It's a very simple model 16 filters, 3D filters, 64 filters, three battery filters, really activation,
max pooling here.
Then we just have to fully connected layers, 256 nodes, 128 nodes up to finally two nodes, which
are where we have to solve Max.
This gives us the probability we use Adam as the optimizer with a very low learning rate that first
point zero zero five, you're probably going to have to go that low, but that's just keep it that way
for now.
And then we just find our model has four hundred and sixty six thousand parameters, not that much.
Now what we're going to do is we're going to create our data generators.
So let's take a look at how we do that to firstly, we need to replace all the classrooms, the ones
and zeros that we saw previously in the data frame with the cat or dog.
So let's do that.
Let's move our convenience.
We could have left it at one and zero, but we'll change it to cat and dog because it's nicer to look
out for us, for human to and to it.
If we see the grass next, we'll use the scale and train test bed function under the F dataset here
to create training data sets and validation data frames are in here and data frame here.
And then we just reset the index and drop trou just to get back a nice data frame like this.
So let's run this and then we go.
So we have that one day, let's take a look at our validation dataset, which is going to be.
Oops, didn't start properly.
So there we go, so you can see those are the frames here.
This this data frame contains all the image names with the validation data.
This one contains all the ones with the training data.
Next, this is what is cool about Keros.
There is this flow from data frame function that exists in the train to generate the function that we
create with the image data generated here.
This is similar to PyTorch, where we have our transmissions.
We can reskill the image here, so played at risk of transformation here.
It's not exactly the same thing, but it's quite similar.
However, this is the cool part here with trained data gender flow from data frame, we can set the
part to the data frame.
That's the train.
The data frame here said the part with the images where they're located.
Then we just put the column names here with the X column name is found in what the white labels name
is going to be class.
Then we have an image size, which we defined before class melodies categorical that size here and there
we go.
So and now we do the same for the validation data generated exactly the same thing, actually.
The only difference here is that we're using a different data frame.
Otherwise, everything else remains the same.
And here's an example of how it works.
So this is just an example, this generator here.
So let's take a look at this.
So example the f here is just this is just a random line from this.
Here's one image, basically.
And what we do is if you just run this here, it's found one image one in this class.
That's because it's only one image was set here, but we can then preview that image.
So how do we use it out there?
Well, for example, generate the returns, put the image and label.
So you can actually get the image out of it here.
Images stored in a multidimensional array.
So you just index and the first one.
And you can display that random image.
For that example, image had its first image in our training dataset.
So that's how this works.
So you can see now that all you have to do now in fitting the model is to model that fit set pointed
to a train that they generate through the generator, which contains our training images set to part
four, set to a validation data generator to be equal to the validation data part of it.
Remember, these are the things we created above here validation generator.
And this is just a function that points basically to where the data is using the pandas data frame we
created and we can train.
Also remember, we have to do the validation steps and steps we book.
This is a validation site.
Abide by the VAT status of the training administered data size divided by the body size.
So let's run this.
It's going to take a little while to train roughly about a minute for each epoch.
So while the trends continue with the lesson.
And since most of this code has been run before the outputs exist here, so I can we can.
I can teach you from those so you can see this model is being trained to see immediately.
It's a 55 percent accuracy, which is a good sign because in a binary classification problem, 50,
if it starts at 50 percent and it continues at 50 percent, it means it's not learning.
But now we can see it's learning, so it's getting better, even though it's not perfect yet.
Obviously, it's just started.
So let's continue with this lesson to the next block.
Here's where we saved the model, and here's where we thought the results here.
So you can see in the first graph we have treating this with the legend, isn't he actually not sure
why it's not OK?
And take a look at occlude?
Yeah, we need to have another legend above, but that's OK, so we can see this as the training accuracy
which is going up here.
Validation accuracy is going around 80 percent.
Not too bad.
And similarly, we can see this is the validation loss here, which you can see it's sort of a Fitbit
and training dataset because the validation loss is going up already.
And that's because it's probably just a very small network anyway.
Even the small networks tend to generalize better.
They don't always work too well, especially if you will between them, because you can see the training
loss this blue line went down.
Now let's take a look at this.
Let's get the predictions for validation images here.
So we just use the validation data generated here, and we use models that predict generator and steps
is what we have before.
Five divided that's about size.
We use ceiling just to make sure it's not to make sure it's an integer that point.
So we get the model's predictions out of it here.
So then we can actually concatenated with our data frame here.
This could I'm not going to go into too much depth with this code, but just look at the output to see
what it does.
So know what a.
Both in know, you can see we have to ground your class here in the middle column and then the predicted
class for my model, so you can see for all of these, it got rough.
No, it didn't get that much, right?
This one, in particular, the dog, when it's really a cat.
This is not a mistake.
I believe as silly of these two here and this one here.
So it got a few them wrong.
So at 80 percent accuracy, that's what you would expect generally.
So no, we can inference and a bunch of images here and get the probabilities out of it.
So you can see this.
Here's a cat image here when to cut the probability in the brackets.
That's what the title is giving us.
We're getting a category out of it category is predicted.
Finally, but this rule?
So we're printing category here into the image as long as well as the file name, so you can see the
file name as a ground shirt.
And this is a prediction ground truth prediction.
Good so far.
Ground truth prediction or good or these?
This one isn't good, though it's a cat, but it predicted a dog.
And let's see if there are any more mistakes.
No, but this is a mistake.
Very obvious mistake here.
This is definitely not a cat.
So is this one definitely not a cat as well?
This one is definitely not a cat.
It's actually that it's a cat.
And yeah, so you can see at 80 percent accuracy was still making quite a few mistakes.
But generally, this cat, this dog classifier works fairly well.
What's with such a simple model that's actually pretty good?
So now let's take a look at implementing checkpoints and Keros checkpoints are actually super simple
to implement.
You would have seen some of the examples in the slides previously.
So all you do, basically, you just look at the checkpoint you want to use.
We're going to use model checkpoints.
This saves animal waits after every epoch and early stopping, which is another one where we do it.
We use it to monitor the devaluation, loss or validation accuracy, whichever one you want to monitor.
You can specify whatever parameter you want.
Here we are looking at validation, loss both of them and basically we set the parameters here.
This is the value that it's required to change before we stop.
This is no the box we wait before we stop.
This is to open settings and we can restore best weights to be equal to true.
So once we stop it, it saves the model with the best weights, and that specified actually just saves
them the best, which is the model.
So whatever model it keeps, it returns at the end is the best model in this case here.
Similarly, for model check pointing here, you can set a pattern for where you want the model to be
saved.
So I'll just take off this part because this path, as it exists in this directory and this is the saving
it to the best start for here.
Again, that's it.
Also, let's take a look at the lending rate on the tool so we can just set the parameters here as well.
I wouldn't go into details with this because we did have in the slides.
So what we do know, we have those three checkpoints we created here model check point, early stopping
and reduced lending rates.
So we put all of those check points in an array here.
So we have recorded callbacks.
And then all we have to do when we're doing model of fitting tariffs is put callbacks equal to the callbacks
that we set here.
And that's it.
And then you start creating your model, and all of us basically obeys all of those rules.
And you can see here that in this example, it's stopped trading after the sixth iteration because at
that point, violation was, I believe, stopped it and improve from point sixty five.
So it implemented it, really stopping and return the model with the best suites here.
So the model now has those best suites based on the sixty bucks here, which most likely would have
been the last one either.
Actually, this will be this one.
The third one point six point seven four, which is the one with the lowest suites.
So that's it.
For this lesson, I notice a bit lengthy and it was a lot going on in this lesson.
I know things are getting a bit more difficult.
However, I'm hoping you can keep up, and if you don't, please feel free to message me on Udemy support
forums and I'll try to get back to you as soon as possible.
Thank you, and we'll stop there for now and then after we'll take a look at PyTorch lightning.
OK.
So stay tuned for that.
Thank you.