Hi and welcome back to the course in this lesson.
It's going to be a quick, good run through.
Hi, welcome back to the course.
In this lesson, we'll do a quick run-through of training your very own cats versus dogs.
Classify it here.
So let's get started.
So open this notebook and let's begin.
So we'll know that all of these functions or libraries that will be using.
So I've already done that and download and unzip the images here, which I've already done and will
just set our training pets here to the parts that the images were downloaded to.
We can inspect them there so you can see what this is a cat image and let's see what a dog image looks
like.
Let's keep going.
Actually, we want we won't be able to see a dog image because Dog Images has dog in a title, but it's
that we don't have the ability to see all of them there, unfortunately.
But that's OK.
So let's set the pets here.
Print the number of images.
This is a quite large datasets, 25000 images and training get a set and twelve thousand five hundred
in the test dataset, which is quite a lot for a test dataset.
And you can see when you just look at a pet, one of the images here, dog images have dog in a title.
That's how we separated out.
There's not always the best way to separate your image classes.
I tend to use directories for it.
It's usually a good standard to use.
So you have one directory called dogs when they're actually called.
Thank you.
Let me show you what what I mean.
It's better to have it in your training dataset.
You have a subdirectory with dogs and a subdirectory with cuts and keras and PyTorch can load directly
from that, that memory structure as well, which I'll probably demonstrate that in a little less than
actually.
So now we can print that part, you can see Dog in the title.
No, it's created transformations, which you're accustomed doing that would pay to it, though this
is a part I want to discuss with you.
This part could seem a bit tricky to beginners.
So what's happening here is we're creating a class basically as a dataset class that Pikachu uses directly.
It's a function that we use in Python, actually, it's a python type style of coding.
This class that we're creating here is actually quite simple, it might seem a bit confusing to people
who are new to Python or actually even if you're not new to Python.
I remember I was coding Python for two years and I wasn't accustomed to building classes like this type
of structure here.
But what this does, it allows us to basically point to what the dataset that we establish here.
So this is how we get a data set of pots right here and the transformation path and allows us to get
the image label out of it.
So if this dog in the image per title like we saw here, this dog in the title, it returns a one if
not written of zero.
And if self transform is known, it will do this.
If not, it wouldn't.
And it returns, the image transformed and the label here.
So it's quite a simple class.
It's more for class.
It's a multiple object that does allows us to access the image, and it's label quite easily.
So PyTorch can utilize this class directly.
So we create that dataset here.
So we create a training class creating object.
I should say I know about this, an object here, which is outlined there, and it can show you an example
of it.
How it works.
So you can use Trin get item zero.
And what is this place here?
This is the image tensor after the transom has been applied, so you can see how it works.
You can immediately see this is a way we can access it and access to data and as well as label here.
So you can print this out and you can actually see get the shape of that.
That was tenses.
You can see its true dimensions, which treatment, but emotional depth, which means is a color image
60 by 60 is the size.
So that's quite cool.
So now what we'll do.
We're going to use our dataset to create a random split here.
So the reason we create a random split here is because during the training process, we're not going
to test our model on the test to separate only going to use it training data set that we will split
the 20000 and 5000 images.
And this allows us to basically remember when we're training models, it's good to have a test dataset
the outside of the of the training process, because we're going to be passing the training validation
is validation is going to be what we evaluate on during training and the training.
That is what the model is going to be trained on.
And then the final test of the said is what the model is evaluated on afterward.
That's the the benchmark.
That's the that's it's the whole Kaggle does a lot of these competitions where they give you a test
dataset that you only get to perform on after you train your model.
So after your training model.
So basically, when training your model like that, you should split that 25000 into a subset of 20
and five and this is trained out here.
So we just use this to utilities function that does that for you automatically by to which has a number
of convenient functions that don't.
It's impossible to get a full 12 pie touch and teach you like that.
There are courses and books that try to avoid doing the device you use them.
My advice for you is, I mean, I don't advise you, don't use them.
What I'm seeing is my advice to you to get the hang of PyTorch is to experiment on your own.
Basically, just looking at a lot of example codes of how other people have used PyTorch and then trying
to implement them on their own, trying to understand them and then applying them.
It's also good to look at the documentation, don't like to see what it's capable of, but there are
hundreds of functions and pie torching cars, so it's not easy to grasp all of them by just reading
at reading them.
That's a way that you can fool yourself, that you're living in something, but you quickly forget it
the next day.
So there's almost no point to doing that.
So learn by doing it is what I see.
So let's just say just to double check that it's worked.
Now let's create an array of old labels using the get item, and we just get the first the second sorry
item of that array.
So this is our validation set labels, and this takes a little while to run a remember correctly.
And then what we'll do next.
We'll split the plot.
Sorry, the classes here so you can see if you're imbalanced or not amounts at all.
You can see in the test data such class it as we're just five dozen images.
It's 25 25, which is good.
Always good to check that.
So this is a random dose and it's sort of worked well, but assume that's assuming the initial classes
were balanced correctly.
So now we can create our data leaders here, which you've seen before.
So we specified to train and objects which were created above specified, but size set roughly equal.
Shrew, truffle and evolution actually should be false.
Not true.
So let's change that.
And now we can just print out some sample images using towards vision make grid that function, which
you've seen a few times before, and we just used MATLAB to and it showed us in the end.
So we transposed to get a nicer shape grid.
And there we go.
There's all cats and dogs images.
This one has some people in it, some children.
This looks like a cat.
Yeah, it is a cat.
Just checking.
So now we've create a model, and this is a quite simple model that we're creating here.
It's a three by three filter with 16 filters.
This one that's 22 filters, 64 filters.
And then we have to we have 256 units here, 128 units here.
And then another 128 units here.
And then we just compile network like this trick on players to FC layers here and applied itself max
at the end.
So let's create or high touch model.
And no, let's display a summary of our model with the input size specified 366.
So that's the image size and the image depth.
So there we go.
So we see we have relatively small model, four hundred and sixty thousand premises, so it's not that
big.
Next, let's take a look at defining our last criterion and optimizing and setting out epochs to 10.
And what we're going to do here.
This is just going to type to make sure it's a data loader, and it is just to be sure.
And what they're going to, what I'm going to do here is we're going to use t2dm, sorry to basically
output a progress bar during our training, which is quite nice.
It helps visualize this training, and it's not a career style way of visualizing the training process.
This is the same training loop you've seen before.
We keep track of things here so we can display them on the output of after after it's finished its epoch
and then we just print sorry after the finish this epoch here.
This is just the training accuracy.
So let's run this, and let's take a look at the nice outputs we get from this.
You can see we get a nice progress bar using TQM here.
All you have to do is just use Tilikum with tell him, I'm sorry, and then you just put in what you
want as as the epochs we call a T epoch.
And we just set the description here and we just set this criteria to display.
And it's usually it's quite simple to use it.
I wouldn't go into detail what it's here because it could distract you from the course because this
is just a nice way to display our training.
So I'll leave this to trim and I'll come back to it afterward.
Thank you.
So while one that were twins, I actually can go through the rest of the book Gratuitous Kendra at life,
so it's not going to be a big deal.
It's just not going to be running the court life because this has been this is running here.
So this we still have to wait for nine more books to train all nine and a half, actually eight million
bucks to train now.
But let's take a look at this so you can see it's printing the training accuracy here at the end, when
it's finished to go into the academic training dataset.
So you can see we're at 58 percent accuracy, which is quite good for the first epoch.
And validation is even better at it's 68 percent accuracy.
So actually, let's wait for the second epoch to display our validation, accuracy and validation loss.
So it should be better because the law scores better here and we can see, yeah, we're getting 73 percent
accuracy.
So for our fairly simple model, we're getting pretty decent accuracy right now.
I mean, I know it's just two classes, so it's basically what you can do with them to 50 50 on this.
But after just such two epochs with such a simple model, that's quite good.
So what I'm going to show you here, the train loss parameter will train or some parameter.
The train loads variable we're using to keep track of triangles as it progresses.
You can actually use that to visualize it at the end here, which is what we've done before.
Rob's actually tried to run.
That could, but anyway, the output is here and you can see this is this here is displaying the trading
accuracy, which is under this blue line I believe is trading after.
See, this is the test set.
Sorry, this is the test.
No, this is the training accuracy.
This is the test accuracy.
This is the training loss.
This is the test loss and you can plot those values.
That would have been very useful if I put a legend in here, so I wouldn't have been guessing.
So and then we save on model here can save it and give it any name you want.
And what we're going to do here, we're going to just plot the results of our model here.
So this is a primitive way of doing it and don't actually do it this way because you can tell what to
predict with what prediction is to his image.
So there's a better way to do that.
So what do we do?
We just use our model again with a little model that we've saved before.
It's always a good habit to just load your saved model just to make sure you can reload it in the future,
because sometimes things do go wrong when saving models.
Not that it would happen randomly, but it happens when you define models and you try to load it back.
It doesn't always load correctly because there was something missing in the class definition, so it's
always good just to make sure it works.
So what we're going to do here, we're going to get some random samples were not random samples, but
samples from the validation dataset using the DOT next iterate to pass them to the GPU.
If it's if you're using AGP, you.
And then what we're going to do here, we're going to get the probability outputs here from the model
and change this.
It is variable.
Is a dictionary here.
Sorry?
So we're just going to use that to put a title in the image here.
So once we took then this allows us to plot this nice plot here where you can see the predictions over
the images, which I quite like.
So you can see that the catalog dog that got this one wrong, look at it.
It says it's a dog, but it's a cat.
This is also a cat, but it thinks it's a dog.
So you can see it's doing.
It's getting about three quarters of these images, right?
So that's pretty decent.
So now you have so you basically seen here how you can set up PyTorch to be trained to create a Python
Watch CNN model on your own dataset.
So later on, we'll take a look at how we do that using more advanced training methodologies such as
fine tuning and translating.
But for now, we'll stop there.
Your model should still be training right now.
Let's take a look and see what it's getting.
It is getting 77 percent accuracy after 40 books.
I believe this model.
Let's check out the graph.
Actually, on the test accuracy got about ninety two percent sort of just under ninety two percent accuracy,
which is quite good, actually.
Let's take a look at what our training accuracy is at this point seventy seven.
So it's actually quite similar to the test accuracy, the validation accuracy, I should say.
So that's pretty good.
Overall, decent results.
We're getting there.
So I'll stop there for now.
And in the next section, we'll take a look at how we do this with Keros and with the Keros module here.
I'll also be teaching you Kovac's how to implement them in class.
We can do those things in PyTorch.
However we'll be using.
We'll be using the PyTorch Lightning Library to implement these features here.
So stay tuned for those lessons.
Thank you.