Welcome back in this section, we're going to do something quite cool, we're going to load a lot of
those complicated big of CNN's that we have seen previous leaders classical.
Hi, welcome back to the course in this section, we're going to do something that I think is quite
cool.
We're going to load those famous scenes to CNN's we discussed previously.
Some of them are here big resonant inception and squeeze net, mobile net and some some others.
We're going to load them in PyTorch and inference, some image and some test images with them and see
how they perform.
So let's get started.
So open this notebook here, which I've already done.
And these are the networks we're going to mess with now.
You can see that they are a lot of other networks available in pie to which you can check them out here.
So what you can do as an experiment, as a homework lesson.
Load some of these networks.
I would suggest trying to mess with efficient net and maybe as next as well.
There's a quite good and experiment and run the inference on those networks.
So let's see how we lured pre-trained network or network in to watch because it doesn't have to be prefent.
In fact, we can set pre-trained equal false and it just loads of model architecture with random weights.
We don't know those around them which can be used.
We can use that network now to train on our own dataset if you so choose so to do so.
However, we're going to load the network that has already been trained on the image net dataset, which
you are familiar with that million image dataset.
That's what that's what these networks have been trained on.
So we have.
So they perform quite well.
So let's do this.
Look how easy this is.
So firstly, from towards vision models, which we import as models, we just do models that Vigilia
16 pre-trained equal true and preclinical true means that it's loading the image nitwits because that's
the only set of weights applied to watch has, and it just looks set into model.
So now our model is that.
VIDEO 16 already trained Windows Image Net.
So let's do this and we'll have to download the model of this, which does take some time because video
isn't a small model, it's quite big.
So let's just wait for this to load.
All right, there we go.
It didn't take that long, took about 12 seconds.
So now we can take this model, this model object, and we can explore it so you can rest run the model
here in the similarly, if you want to just do this, you can do print and put the model inside that
print function and you can see all the layers here.
So Richard U16s 16, convolution Liz and some fully connected layers at the end are actually it's 218
Cornelia's and tree fully connected layers at the end, which is done here, so you can see the architecture.
Inspect it yourself.
Similarly, you can actually use towards summary, which is a very cool function that gives us a Keros
like, similar style display of the model.
So you can see let's just bring this here.
So you can display it here, and you can see the number of parameters pelea the output shape.
This is a much better way, in my opinion, of observing the model.
And you can see the total number of parameters here, which is 128 million.
That is quite big, and you can see there's a forward pass size, parameter size.
These are quite good steps here as well.
And notice we have to put an input size specified input size.
That's understandable because it does change the output shapes as we go through network imaging that
these networks are trained on emission of data size of two 24 by 224, at least most of these networks.
So that's the size we use here.
So let's run that again.
Yeah, well, I did that before.
You don't need to do it twice.
So to run, to run or test images onto this limited network, we have to create a transform function
in by torch.
So similarly, we've done this before.
So you import to the transform module from to watch vision and we set out transforms here.
We're going to resize to 24 by 224.
Change it to Tensor now that I've come into these lines here.
However, these lines are basically the image, not normalized, mean and standard deviation, as you
can see here.
That's what that's a that was a pre-processing technique that was used on image net, and we don't always
need to load this in.
Some for some networks are doing some networks you do for this one.
Individually, they didn't use this normalized width, so we don't know that put him in here.
However, I did leave them in just so you know and put a note here, just so you're aware of them in
the future because some networks do need them.
So let's create a transforms call.
The test transforms, which is probably not the best name for it, should have clear inference transforms,
but that's OK.
So now we're going to use that the development of the model no dotted values, basically like a switch
that allows you to switch from inference and training because the network does behave differently when
training and when inference mode, things like the drop out layers, an inference mode you don't want
to turn, you don't want to turn off layers, you want oilier layers on.
However, doing training when you want to use drop out, you will have to set something for the model
to know that it's going to be either training or inference mode because these layers to behave differently.
So that's basically set it here.
So let's run this and we've set our Model two evaluation mode.
So let's get some images.
But before we get the images, let's download our image net classes.
So I have this on my GitHub.
So we're just done with that file and you can take a look.
The file is here and you can open this file and club.
It opens on the right hand panel and you can see all 1000 classes here for the image now dataset.
So many different things.
So many different weird categories that I don't even know what they are sometimes, to be fair.
This must have been pain to label, but it was human annotators.
So let's close those windows.
And so now we create some helper functions.
These helper functions basically allow or model to get a load, an image and then pasted image to show
them, and the network info propagates it and gives us and returns a class and also does some plotting
here.
So let's take a look at how this works.
So for no, you know what device you know about, this is sensitive to the CUDA GPU or the CPU.
We were just opening this this class name as JSON file and creates, including it using JSON.
That load will look.
So we have class names as a variable that's towards all of those class names that have showed you above.
So let's take a look at the actually, let's take a look at the get images function for this.
So remember, if we point to a directory here which will download these images at these test images
in this block of code afterward?
But for now, let's assume the images are in this images directory.
And what do we do here?
We just use data sets dot image folder specifying that.
Directory in the directory here applied transforms to that, and that's no data.
So now we have the number of images, so we just get lean into data to get the number of images and
then we use this torchlight utilized or detailed or digital order, which is our data loader.
And we specified a bad size, which is number of images here.
And as I said before, we can use two items.
I talked about function in Python to basically extract information from this loader.
So we get data later here and get this data to dot next to get images and levels out of it.
Now it seems quite simple if you think about it, and it is so just stuck to this code slowly.
If you want to understand it, I've done it line by line for you.
However, when code is new and these Python functions can do quite a few different things, that's sometimes
confusing to look at its best.
You take your time and try to understand what each line is doing in a way.
So now let's move up to the predict image function.
So this function is quite simple.
Actually, it just takes two inputs, two images and the class names, which is what the image that
class name is.
Oh, it uses some of the pilou, which is another image library.
It uses some of that.
Those functions here to transform the image.
That's basically for displaying it, but for displaying purposes.
So anyway, so we take the images that we lower so we can just take an array of images.
So once we have the image inside of this, because remember, we're looping true, we're iterating through
an array of images.
We converted to the pill image format for this year and then from that format because PyTorch can directly
operate on some of those in that image format.
We converted to a float using dot floats here and that that basically transforms the image.
Here we're using test transform.
So we applied transformations to the image that needs to be in a pill format, and then we converted
into a float.
And then we used a PyTorch unscreened method here to basically make this a one dimensional tensor.
And then we convert that tensor to apply to each variable here.
This allows us to now fold propagate that image into the network that we specify that it's going to
input Dot to device.
That's right that this we're sending the input to the device.
Sorry for that mistake.
And then we propagate it to the model here.
Finally.
So this does seem like a lot of steps, and it could be quite confusing.
It was confusing to me.
Actually, it's still confusing to me sometimes because PyTorch is a low level language and you do have
to be aware of the image types, the requirements and the sequence of operations you do before you do
inference on the model.
So let's get back to this.
So now that we get the up until we send or not, we can the output to an umpire.
We're using the data dot CPU, the Nampai and then using the number of MAX function.
We get the index that represents the highest probability class.
And then we use that index here to basically do a lookup using the class names dictionary out reloaded.
That's why we converted to string.
So we just get the actual name, the actual word, that representation instead of the number so that
we can then plot the class name using the map for live functions here, which I won't go into detail
because you would have seen this many times before.
So and or just so you know, one thing to note is that this is creating a so many slots for only images
that are in that folder.
So it's quite cool for too many images that would just be one big, long subplot.
So be careful with how you're plotting this.
So we're ready.
So let's create these functions that stomatal images get image net classes, which I think we were already
done before we did.
That line is redundant and we just delete this.
And no, that's not going to spoil the surprise here yet.
Let's predict the images from using the functions we did before.
So it's going to be quite simple.
Let's just run this code.
This could above here.
So we get images first.
And then we passed those images to the predict image function with the class names.
And voila, in a few seconds, we'll get that nice generated subplot that shows you the inference on
all these images using Viji.
So let's zoom into this and we can see this was a burrito and was predicted to be a burrito.
This is predicted to be a bathtub, which was actually a floor mat, not a bathtub.
This basketball is fairly easy, so they've got a basketball.
German Shepherd was a very, very easily as well.
So it's a pretty long description for the German Shepherd, but that's fine.
Christmas stocking.
Which is that it's a critical category, but nevertheless, I did it in this one, I got wrong.
It thinks it's a popsicle and it's clearly a beer.
This one got it.
Got right?
This one, they got right again.
Callie Limousin and Spider Web.
So it's working quite well, but not that great.
Let's see if Resonate can do any better.
So let's do the same thing we did before Lola Resnick model, which takes that didn't take that long
at all.
And then we create we preview a model using the summary function, and you can see it has 11 million
parameters.
It's quite small in comparison to big.
And let's just print out this story.
Doesn't print outlets convinced just convert the Model T valuation would just so, you know, let's
go back up here.
This resident polluting as resonant 18.
This is the smallest resonant model that is available on phytoestrogens.
Resonate City for fifty one, resonate a 50th anniversary and 151.
So there's a bunch of resonance we can use as resonant a one to one as well gemstones.
So let's take a look at the network architecture.
We're not going to go into detail because you've seen it before.
Now let's run our predictions and see if Resonate 18, which is the smallest business model, does better
or worse.
So immediately I can see it got the first one wrong.
It's a burrito, not a mortar, which is the big mistake.
This one you got right the doormat.
When you got that wrong, then it got this, as well as an ice lolly when it's clearly a beer.
But it's understandable because it is kind of shaped like a lolly somewhat.
They got this one wrong to the Colleen, not a similar year.
Whatever that is, that might be a strawberry under the limit.
So now let's do inception.
So we're going to load Inception Vietri, which is a quite good model.
And notice the input image sizes to 99.
That's because Inception was trained on the larger image dataset, so we have to change that to two
219 19:9 here, and we can see the number of parameters with the inception vitreous.
Twenty seven million.
It's quite a bit.
Let's set our Model T evaluation mode, and let's run those predictions here.
Let's see if this gets all right.
And no, it got it says it's an envelope and it's a floor mat to a welcome mat.
But it got very tall, right?
It also got this beer jug mug her own, and it got the color on as well.
So slightly disappointing.
Let's see how mobile that Vision two performs, which is a tiny model, relatively tiny that has some
really good performance stuff.
So it's only 3.5 million parameters, as you can see.
So let's set that to evaluation mode and run our predictions, and we can see it is doing quite poorly.
It's not a show, a cap, it's a Barrientos envelope.
It's a format.
Should have got this one wrong and correct.
Got it wrong to basketball Artic Wolf.
I guess it's kind of similar, but it's not an Arctic wolf, the German Shepherd.
This is a Christmas stocking, so I got that right.
I don't know how this is a Band-Aid, but it got it wrong.
Limousin Limousine Collie Weber.
So they got maybe half of them wrong.
Half of them.
Correct?
Let's take a look at squeezing that lets you squeeze not us any better.
So we'll create that.
Get the model.
SUMMARY 1.1.
1.2 million.
Sorry.
That's very tiny.
So let's see how this performs, given that it's such a small model.
So reiterate.
Got this wrong?
Got this wrong.
But limo right politics.
So meet again, which is not sure what it is or look it up after and then got a spiderweb, right?
So not that great.
What about wider, isn't it?
So why residents are better than residents?
Generally, it's wider, meaning that they actually use more filters in the model.
So let's take a look and see how this performs, and we can try different residents as well as not one
on one might do quite well.
Let's get the summary out of it, and let's go to evaluation mode and let's run our predictions.
I don't expect it to be miraculously better, but let's see.
I got this wrong.
Got this wrong?
Got this right?
Just right.
This right, this wrong.
And this right.
This right destroyed says that's OK.
Similar to Vejjajiva in the original resonant let's try amnesty.
So Amnesty is one of the predecessors to efficient net, which was it's also quite good and quite similar
to the that.
So let's see how Amnesty performs.
So let's admit it to evaluation mode, and let's run our predictions and you can see something from
the bottom is very dumb here.
It got this.
Right, that's right.
This right.
It's not a nipple, it's a bath, we've got this right.
Got this right?
My first thought, it's a basketball, it's not a wall, it's a pillow mat.
It got some rooms on, right?
So honestly, in all of this here seems like Vejjajiva probably was the best, wasn't it?
Let's scroll up to the top.
Sorry for the quick scrolling.
I know it could be a bit annoying.
So we can see that Big Hill got, but you seem to have forgotten some of the best performance, all
of the models got this bit wrong and all of the models got issues with the basketball, which I think
was a fairly easy class and a match as well.
So the models generally are performing fairly well.
He can try them on your own images feel free to.
You can either upload your images to those images directly here if you want.
Actually, it is class directly here.
Class one and that's where it's looking for the images, so you can just get the images there and place
them yourself if you want to see how it performs on it.
Also, I would strongly advise you try some new models.
In fact, look at resonate and look at wide, resonant and resonant next and efficient that there was
an experiment with different resonance as well as dance steps as well, and see if any combination of
models that give us all of these images correctly with the correct classes.
And if you do find a model that doesn't share it on the Udemy forum so everyone else can see what the
best model is, so I'll stop there for now and what you do will do the same thing with cameras.
So we're going to see how to load pretreated models and cameras and how to inference them on our test
images.
So I'll see you in the next section.
Thank you.