Hi, welcome back in this lesson, we'll use a pre-trained network to extract features from our dataset
and then train a linear model like a logistic regression on those features.
So let's get started.
Open Notebook 24 Call this one year by torch feature extraction CNN, which I already have done here,
and let's get started.
So we're going to go back to our Catskills stocks dataset.
No particular reason, just because it's easy to of us to quit and make sure you're using a GPU as usual,
Hiram, for this, for this lesson as well.
So let's make sure we're using you and hire you.
So now let's download our pre-trained model.
So to download a pitching model, you just basically do models thought.
VIDEO 16.
Robert Moses is from too much vision, said pre-trained equal true, which means we're loading the pre-trained
PyTorch wits.
And then we send that model to you if each of you is available and then we just print out a summary
here below.
So let's do this.
We may have to wait a bit to download our model, but that shouldn't take too long.
OK, so that took about 23 seconds, plus maybe five 10 seconds time.
It took to connect to an instant still.
So now let's examine this network.
You can see there's a lot of convolutional layers.
It is one to try as many, many it's actually 16, to be fair.
And then you can see at the end, there's a max pool and average pooling layer, and then there's this
tree linearly connected, fully connected layers.
So what we're going to do, we're going to remove these seven layers here.
This is really tree linearly is linear, fully connected layers of overlays to reload the drop out.
So we're going to drop all of those down.
So to do that, it's actually quite simple.
We just use an end or sequential here, and we just set this to convert this model to a list.
And we use all this indexing slicing abilities here to just cut the last seven rows out of it.
Whoops.
And let's roll that and then we create a new classifier out of that.
So we just make a model classifier to be equal to that classifier.
So no model has become just the convolutional layers of the video 16 network along with those weights.
So let's take a look at that and you can see it stops right at the adaptive average pulling last layer
there and you can see now we have 14 to 14 million trainable premises here, whereas before we had 138
million trainable parameters because we had a lot of nodes here in these fully connected layers so we
can continue now.
So let's download old Catzavelos Dogs datasets, which we've done before.
This takes a little while and what we'll do is go to go through this again with you guys.
We just set up, we're treating pets here.
We get our files from those directories here, and we just display how many images is in our training
dataset and our test dataset as well.
Then we create our transformers here for video.
We don't need anything complicated.
We just need to resize to 224 by 224 and then convert it to image Tensor.
And then we have a class dataset here where we just get all labels and image out of it.
And then from that, we just have a dataset objects here where we just specify the files, the directory
of the transmissions.
And then finally, we can add using the dataset class to do that, actually.
And then finally, we have our data models at the bottom there.
So I believe this is finished.
Yep, 30 seconds has passed.
So now we can get our display here, 25000 images in the training, 12000 Typekit in the tests.
So now we're ready to extract of features using Figure 16.
So let's take a look at how we do that.
So let's just set up image names here to get the image file names, some that are actually as well as
image parts, which is the full path now.
And then what we do, we set our model to evaluation mode because remember, we're not training this
model, we're using this model as a feature detector.
Well, CNN's numbers remember, CNN filters are basically feature detectors to give effect edges, complex
patterns, and all of those things we can and could to well include or image any image in terms of those
features and then use those features as inputs into another model.
That's effectively what we're doing.
So let's take a look at how we do that.
So we just set the Model T evaluation mode for the model on the GPU and then with touch note.
Read what we do here.
We just create some blank features and levels because we're going to keep appending to those in the
loop so we can we can go through this loop through our training dataset here.
This is returns a batch which is here data and labels.
We send that to you.
This actually is probably bad coding, so let's make sure you have a GP, you enable the device.
This will break.
Then we just get concatenate of features using torture cut cuts here.
And so we just keep accumulating features, accumulating to levels so we know what the what's there.
And then we just reshape our TensorFlow.
This is the output says it's going to be twenty five thousand by 25000 idiot.
Because remember, let's take a look at the work in our Keros lesson.
If you remember correctly, it's going to be this is the output size here.
512.
Yes, I am using code of yes, 512 by seven by seven, which gives us 25 tools and idiot.
All right.
So that's the final size here.
So let's run this.
This may take a while to run.
So let's take a look and see how long it takes.
Yep.
You can see it's incrementing treatise.
We use t2dm here to just get a nice little progress bar.
And you can see this might take about a minute.
So let's all right.
There we go.
It took about two minutes and we've extracted all of our features from a training dataset.
So we now have a very big area here.
We have an array that's 25000 by twenty five thousand eighty eight that's storing but storing all the
encoded features that our CNN extracted from our images.
So let's take a look at the size to make sure and labels just to make sure everything is OK and check
the final shape as it is or as expected.
No, we can use this along with labels here to train our logistic regression.
So let's convert them to lumpia.
Is firstly, suicide features thought CPU doc.
No, a same for the labeled start CPU thought no.
I didn't forget to brackets.
So let's convert those.
We have features on image labels, and by then we import a linear logistic regression from a skeleton's
linear model, as well as to split because we're only going to look at the training data here.
So we just split the training data into 80 20 split.
So that's 20000 in training images and 5000 test images.
And then we can just quickly just run this.
So we take the split.
The data here.
Features and labels set our test size set around them state just to make it replicable, replicable
for other times you run cities, set the random state here to seven to keep the random state constant
can put in any number here.
Actually, if you want, put your favorite number.
And then this would be here.
We just create a logistic regression object and we just fit the model here.
So we have Extrait and Weitering Whiteread mean labels.
So let's start training.
All right, there we go.
So we've completed training in just under a minute, 58 seconds.
In fact, it's quite fast.
And let's take a look at our accuracy.
Ninety seven point one two percent.
That's actually pretty good.
So let's run now, run some inferences on our test data.
So, no, the tests they do the twelve thousand five hundred images that we didn't touch.
Let's take a look at running some inferences on those two samples here.
Sorry to leave that.
So let's create a load of transformers here for the test images.
And then we're going to use random just load a random sample from our test images here.
We're going to look at a random sample of 12 images and then just run those images here with this function
call test image here.
So it takes well, we take the sample to sample here and then we just run it through the prediction
here.
And if it's a dog eat dog, we just create a result as dog.
If else, it's a cat.
When logistic regression returns a probability between zero and one.
So if it's greater than 0.5, it's going to be a dog.
If not, it's going to be a cat.
So let's create this here and now let's get our results.
So we're just getting the results of this 12 images.
The quote random random images here and all of this actually plot them now.
So we're going to plot those in this loop, which I want.
Explain everything here just to show you that we're just loading the image part here of sort of loading
the image path here, resizing it two two two four, then changing it to look from a video to RGV.
And then putting the result that we got previously, which was the results.
Yet prediction results, and we can visualize it now on those images.
So let's take a look at them.
So these were the test samples to sample.
That we're good at, so you can see this as a dog.
But some people down here.
This is also a dog.
This is a dog to dog cat.
Yep, got it right.
Talk, talk, talk, talk got.
So they got all 12 right, actually.
So our 97 percent accurate model is doing fairly well on the training on the test dataset.
So you can load more test at random test images.
Actually, I'll show you how we can do that.
What we do is just run this block of code from here, then run this one and then run this again and
you're going to get 12 new random images here.
See?
So, yeah, so let's just make sure everything is correct, everything does seem to be correct.
Yep.
We've made a very good cut first of classify, you know, as you can see.
So I'll stop there for now.
And in the next section, we'll take a look at Google's deep dream, which is a very cool algorithm.
So stay tuned for that.
Thank you.