Hi, welcome back in this lesson, we'll take a look at using PyTorch Lightning to implement transfer
learning, which you should have a deeper understanding of know having watched those introductory slides
I presented previously.
So open this notebook notebook 20.
And let's begin this lesson.
So I really have some stuff running, so you wouldn't have to wait.
So let's install and set up to pay to watch lightning module.
So a library.
So I've done it already here you can run that could have already imported these packages that we'll
be using in this lesson and I've already done with it.
Unzip the data set to be using so we can skipped all of that because we've done this before.
This is a world class or data asset class that returns to images and label, and we just set it up here
and create a file on the train and our dataset objects and then the data loaders here as well.
So let's run all of this.
And no, no, let's see how we set up our transfer learning.
So we're doing transfer learning by feature extraction.
And what that means, of course, you forgot, is that we're loading a pre-trained image net network.
In this case, we're using a resonant 50.
And what we do, we keep all the convolutional is all of Emilia's in the network frozen and we just
replace the header with a different layer and retrieve that.
So let's take a look at how we do that here.
So Verizon here def definitely part of the class.
We create we just followed the model here, so we load resonate here.
We alluded to prettiness true and we call it the backbone here.
So now we can access after fully connected layers here and we just get the number of number of filters
on the output of that fully connected live.
Next, we just get to let's get all of the layers here in that would accept the last one because the
last one has the details and outputs, and we create this feature extractor here using it and then sequential
here.
This just combines the best basically rebuilds the model effectively here.
And because our model has two classes now, because of the Catalyst Store classifier, we just set it
to the last year and the network to be a number of classes here.
So it takes no filters from the previous layer here, the fully connected layer and then ups it to the
fullest, the last two output nodes, which we have.
And then secondly, we have noticed in the forward loop, we don't have a network like the layers individually
as defined.
We have this.
We have a feature extracted, which is basically a model node that we haven't thought so often feature
to.
This sort of command allows us to use this in different parts of the class so we can use this in the
forward loop here.
So we just set this now to evaluation mode.
That's basically how we freeze the way it's there and then we use touch with no grid.
And what this does, basically, this allows us to use this frozen model here where we just flatten
the output and then feed it now to the final layer here.
And then we have the software updates to the soft max layer here.
So it seems a bit complicated, but this is actually boilerplate code that you have to change and you
can interchange different resonant networks here.
So this is basically the methodology for doing feature extraction, transfer, learning.
Yeah, so that's true.
So everything else is the same.
You would have seen these things before in the previous lesson.
So let's run that block of code and we're now ready to implement our transfer learning.
So let's start training.
So this is going to take a while to train.
It doesn't and download the way it's here, but let's take a look and see how long it takes anyway.
You can see here we have 23 million parameters here.
However, we're only going to be treating the 4.1 key parameters in the model.
So let's wait for that.
And you can see we're training right now.
So while the strings, we can take a look at the tensor board of.
But down here, however, this doesn't have it saved for the previous training that I did, unfortunately.
So we'll have to wait for this to be finished to complete training.
However, that's it for this lesson.
That's basically all there is to implementing transfer learning with PI to its lightning.
What we can do can stop this now.
Hopefully, it stops.
And he did stop there.
And we can no run our tenser border and look at the first two epochs that we've trimmed, so give it
a wealth or attend to board to load.
OK, so there it goes.
It takes a little while to start up.
So when you see this blank screen here, it doesn't mean it didn't load yet.
It's just taking its time.
There we go.
You can see it's coming up now.
And now we can take a look at our training.
Let's take a look at our training and validation accuracy.
So let's take a look at this one first.
And you can see it's a little spiky, but however you can see immediately we we're at 84 percent accuracy,
actually 87 percent accuracy.
So that's quite good.
And let's see how our validation one performed.
Actually, this is the wrong one.
Let's look at the epoch.
Actually, it's the same one I looked at.
OK.
We have one value orderly, and we're at 78 percent accuracy right there.
So that's pretty good.
So we apparently we're not logging it.
The validation accuracy is in the steps.
Actually, that makes sense because it's not in the training loop.
There can take a look and see.
So that's it for this lesson, and I'll join you now in the adolescent where we'll now use carrots and
carrots is actually a lot more, a lot easier to implement transfer learning, in my opinion.
I will also use CNN as a feature extractor increase, which I mentioned in the slides.
We will use a logistic regression at the end of that of the end of the CNN network using the features,
which as we extract from the convolutional, is.
So stay tuned for that and also will do the same.
Those two same things and pi touch without using Pi Touch Lightning.
So if you want to use transfer learning in PI touch and fine tuning, you can take a look and see how
we do that there.
Thank you.