Hi and welcome back.
In this section, we'll take a look at using PyTorch without PyTorch Lightning to implement transfer
learning by feature extraction, as well as fine tuning.
So it's a very important lessons, very nice lesson.
So let's begin to open the book tree here already have it open.
And that's because I'm creating the network because it takes a bit of a while to train.
So let's begin the lesson.
So firstly, run all of your inputs here.
These are all the libraries will be using for this project, as well as download your data set.
So you may notice this is a different data said this is to Hymenoptera.
I really can't pronounce that we're probably not doing it justice, but basically it ends with these
data sets.
So I thought you may have been getting tired of the cats with dogs datasets.
So now let's go into the ends of this piece dataset to visualize some of those images shortly.
But for now, the first thing we do is let's set up what transforms.
Now you may notice something here.
You may notice that this line looks a bit weird, doesn't it?
Well, that's because we're subtracting the mean and then dividing by the standard deviation here.
This is the image net, so the deviations for the RGV images.
So these are the means for the rugby, as well as the standard deviations.
So because we're going to use a pre-trained network, a resonant it, in fact, that was trained on
the image now dataset, we have to pre process it in the same way that the image net data was pre-processed.
So we have a normal, we have an augmentation here or a random horizontal flip, as well as a random
resize crop and over sending it to the tenses which we've done before.
This is for the training data and for the validation data.
We're doing something a bit different.
Firstly, we're resizing it here to 256 and then we're doing a sinta crop to 224 afterward.
Don't worry about it.
That's just how we have to do it.
When we apply it, when we're loading a pre-trained image net network and then again, we converted
to Tensor and then we normalize it again as well.
So there's a standard, relatively simple transformations.
Feel free to add in other transformations here.
If you want random augmentations, there's random brightness, contrast, random rotations, a bunch
of them you can use from pay torch.
So have fun with them and see if they improve your results.
So now we have to create our digital orders.
So what we do, we're going to use a function called Image Folder.
Image Folder expects to data set in this name and this organize for the structure.
So you have the dataset name here, which is a hymenoptera.
And then we have the trained data here and then we have a labels we have and the images here notice
that these are all images in the territory and these are all the bins in the beta, actually.
This is a very nice and simple way to organize your data sets if you have your own custom data set.
Feel free to just add create a folder structure like this where you have the train and Val, or if you
want to have one big data set and then use a different function to do the splits up to you.
But this is usually the way I do it.
So what we do here when creating or data loaders set the data directory path.
Then we use image for to point to the full dataset here.
That's the train involved.
Full list is a filenames.
And then we just use some list or dictionary comprehension is here to create our data loader for port
train and Val in one line.
Normally you might see this done in Tulane's, but we were using Python, so let's take advantage of
some of the many, many abilities.
Python has to do a lot in one line, which is quite cool.
Next, what we'll do with just ghetto dataset sizes as well as well as a low class Nims.
So you can see we only have 224 training images, one image, 50 tree validation images.
So this is a relatively small dataset, and then we have only two classes and some BS.
So we should you would think we should get some fairly good results, given that it's a simple dataset.
However, notice that we don't have much data.
OK, so now let's move on to visualizing some images here so you can see these are some BS here.
I wish I had an example of them.
And but this is calling random images from the training dataset, and it just happened to pick a bunch
of BS.
So you can run this again on your own and see if you get some new images here.
Some nuance?
No, let's create a training function.
So we're also introducing two things here.
We're going to use a learning read scheduler.
If you recall, we're learning, which is is a it uses a different learning rate every epoch according
to a preset algorithm or the preset list of values.
And also, we're going to introduce check pointing, which serves the best model and returns the best
model.
And of this loop.
So that's pretty cool.
And let's take a look at a screening loop.
So I know creating loops.
Well, tuning functions here in PyTorch can be a bit confusing because there's a lot going on.
So I'll do my best to step through this good with you.
So this is just time logging because we log into times for training programs.
So don't don't worry too much about that here.
We actually use copy and deep copy to basically save the state of the initial model here, and then
we keep track of this.
This is how we saved the best model.
It's a PyTorch function and I'll by touch object that you'll be using to return the best model in the
end, as well as just keep track of the best accuracy here.
So now for the number of ebooks we specify, and in this case model we have specified 25, which we'll
see below.
We just print the e-book, the currency book, as well as as well as the number of ebooks we have left
to get.
Feel like we want over 24 25.
And then we just use this here with a dash just to add some separators here so we can have a nice,
clean printing output for training.
So next to what we do, we just basically for each value get in this really creating value.
Remember, the sequence we do is true for each book as we train the model and then we test and evaluate
the validation dataset.
So for cheating first, we just set model to training mode.
Otherwise, it's going to be an evaluation mode here.
We keep track of all lost and no predictions that are correct.
Then we just get the inputs and levels from there tomorrow.
So this is a batch right here.
Send that back to the GPU or CPU, whatever devices.
Then we just zero the parameter gradients.
And then if it's in training mode, it would.
This is what the gradient enabled.
What you're going to do here is it's going to get random, run your input, your batch through the model,
get two predictions, get lost as well.
Again, if it's in the training mode, then you do.
The back propagation here would Optimize Optimizer being whatever you want it to be, whatever you set
it to be, to be like stochastic gradient or Adam or one of those, then we just keep track of those
two training statistics here.
So we just keep track of the first one.
We keep track of this loss.
And then a second one is how much of these predictions were actually correct.
If we just get a total using torture of some number of predictions that are equal to the levels in the
data in that match?
And then again, if if it's in training mode here, let's just skip the going at this point.
We just do this every little step.
That's for the learning rate adjustments.
So we just skip you, just increment that by one and we just go to the next value that you'll use for
the optimizer, the leading rate selection next.
We just keep track of the epoch loss as well as debug accuracy.
And then the split would print it out here.
And then if it's in validation mode and our epoch accuracy is greater than the best accuracy that we
were keeping track of, then we just create a new copy of the model.
So this is how we actually keep track of the best model.
And if you notice at the end here, when the training is finished, this training loop here, this literally
this conditional here would have kept track of the best models.
Nice little programming tool here.
So and this printed just a blank line so we can skip lines between epochs.
And these are more these are summary statistics in the end.
And so that's it.
That's the whole block of code explained.
Hopefully, you understood everything.
And if you don't, because I know too much can be confusing because of its low level of style, then
please message me on the Udemy forums next.
What we have here, we just have a quick function called Visualize Predictions, where we can take a
number of images here and the difficulties six to the model, we set the Model S Model two evaluation
mode.
If and then we just it propagate the images to batch whatever that is, and we just prepare to subplots
at that point here so you can do whatever mode of slot supports you want.
These be a six year, and that's it.
That's a cool visualizing function that we can do.
We can use to just plot the images along with their predictions.
So we'll stop there for no, actually, no, sorry, we're actually about to trying to train them that
network.
Forget about that.
So this is how we fine tune this called convolutional neural network.
So remember, in fine tuning, we lower a pre-trained network, but we don't freeze any of the layers
or we can freeze some layers as well as leaving some unfrozen.
In this case, we leave.
We don't freeze any place in the network, so we lowered Resident 18 here.
Then we just get the number of features here from the model that we just floated.
And then we set that to our last update for the connected layer here for the number of features.
This is the previous number of features that but that definitely going to lay ahead.
And then we just said that I would set our sample size only to set has two classes who have said that
the final output, the linear layer to me to say this is the input for that layer and this is the output
here, and this input came from the last final.
The last fully connected layer features the number of down here.
This is just a number of features, you know, who just send them all to the GP.
You set the criterion here, which is the loss criterion, cross entropy loss.
And then we use sarcastic the gradient descent.
We use this default integrated here with momentum.
However, these are being is up from the linear rate scheduler, which is all of the above to see here.
So this is our this is how we configured to living, which you take a look at this.
So we create a optimizer here.
However, what we do, we then create this doesn't use this other function from pie torches, which
we imported in the beginning of this good learning rate scheduler, doorstep learning rate.
That means every step or whatever step, it's that that's actually the algorithm we're using in this
case for this scheduler.
The step one, we pass this optimizer because this this can no work with any optimizer.
You can use Adam or the others as well.
And we the subsidize of seven and the gamut is point one.
You can take a look at the function to see how it's configured.
You can see exactly what gamma controls.
I believe gamma controls the decrement at every step and things step size means that you change the
learning rate every seven ebooks.
So that's it.
And then now, how do we use that in the model?
Well, remember we we can use trained model here.
So the model that we we defined here, we created, that was this final model that we loaded.
It's quite simple.
Set the last criterion set to optimize a set learning rate scheduler.
So the number of ebooks and there we go.
So I have a training right now.
It takes a little while, takes roughly about maybe five minutes, actually less than five minutes in
your book, maybe about a minute or two an epoch, and it's training for 25 ebooks that starts at zero.
You can add more than +1 to this.
If you wanted to keep it at 25, I actually would have preferred that.
And then we go so we can keep track of your accuracy.
So at the end of each podcast, take a look at default nine.
You can see our training accuracy 83 percent training versus point training of validation.
This is quite good point to do, and validation accuracy is ninety one point five percent.
That's pretty good.
And you can see we go to pick one of ninety three.
I believe that's what any other sort of you know, 93 was actually 95 initially fell in the first epoch.
OK, so that basically shows you that we don't have enough data in this model to really take advantage
of transfer learning here because you can see after one epoch, we actually got the best results coincidentally,
and that's due to the random initialization of weights that give us that coincidental best results.
But the fact that it's not getting better after this, this money box means that this is a bit of an
overkill for this dataset.
What you can do, though, we can perhaps free some lives if you wanted and experiment like that.
So that's actually what we're going to do next.
But in the meantime, let's take a look at our visualizing or predictions here so you can see it predicted
ants.
And I don't.
Yeah, they are and sound asleep.
This is a B.
This is and this is a really do not have to be.
This is an ant and this is ants as well.
So that's cool.
So know what we'll do.
We'll take a look at using a convolutional neural net as a fixed fidget extractor so opposed to listen,
know and resume in the next section because this video has been going on for almost 15 minutes.
So you think you may want to break and I'll raise my voice a bit too as well.
And when we resume, we'll take a look at how we can use or confirm that as a fixed feature, the extractor
and apply play transplanting, which is quite easy.
Actually, it's not going to be a very long section.
Thank you, and I'll see you then.
Bye.