Hello, guys, welcome back.
So far, we have successfully created a CNN model and we have compiled it and we have seen that we have
got an accuracy of around 89.
Now, we have saved this whole model inside this file.
Now, all of this model with all its weight, its parameters, everything is saved.
And this CNN number is Dautrich five.
Now, if we go to documents CNN, we will see that all the files that we have created are here.
Remember that we created this CNN dot by file inside the documents, inside the folder called CNN.
And as you can see, this is the model that we are going to use in order to start prediction.
This upcoming lectures are about prediction.
How can we make a prediction using a pre trained model?
Now, this one is called pre trained model because we have already trained it in a different code.
OK, now we are going to create a new python file.
Let's save it.
First thing we do is save it as a format, we don't go to documents, CNN and here we're going to type
knottier here at the top.
We're going to type validation that by you can name it whatever you want.
But the important thing is it is a dot by file.
Click on Save.
Now we have a new python file, which we will use in order to predict the data that we have.
OK, so the first thing I want to import is some Kiraz preprocessing for the images.
So I'm going to go from Kiraz DOT preprocessing import eMESH.
OK, another, Kyra, is the load model cell from Kiraz that models.
Import load model, this library here is going to help me to load that five model that we have saved.
And finally, I need some nonpoint cell from.
Or not from, let's say, import.
MP as MP.
All right, so I'm going to go and create a classifier, this classifier is going to equal to load model,
so load underscore model and then open parentheses.
Now, here we are going to put.
Our Beth.
Open to quotations, and we will put the part of the model here.
Now, we can simply do that by just going through this directory, right?
Click copy.
This is the five model.
We just copy XPath and we are going to paste it here.
Now, the classifier is going to have all of this model inside.
OK, what's next?
Well, we want to load some image in order to pass it to our classifier in order to predict, so I'm
going to say test underscore eMESH.
Is equal to image.
Dot, dot, am.
And I'm going to put the part of my image here just to make stuff super simple.
I'm going to go to the database.
We had.
So right now here, my database is on desktop, so I'm just going to go to, let's say, the testing,
I'm going to grab one of the numbers, let's say number two.
And well, I'm going to choose, let's say, this picture, so I'm going to copy it spath the two,
three, six, which if we open it looks like this, this is number two.
This is the one we are copying and we are trying to predict.
So I'm going to add quotations here.
But the path.
But we are not done yet.
Put a comma, go another line so that we don't write long lines.
And here we are going to do specify the target size.
So target underscore size.
Is equal to 28 by 28.
This is the size that we have specified already in the CNN remember in the CNN, we are taking Sivas
of 28 by 28.
So are the size should match as well.
OK, because this is the size that you are trying to pass to that model, which accepts only 28 by 28.
All right.
Finally, we need the color moad color underscore mode equals two double quotation gray scale.
OK, so far, so good.
Now, the next step is in order to pass this to this model, you need to convert this image from JPEG
to an array.
And there is a very easy way to do that is by calling a class.
Our method in this image class and this method is actually called image to right.
So if I type No.
Test, let's say image is equal to eMESH, not HMG, underscore to underscore.
All right.
And I'm going to pass your test image again.
OK, so this variable is called this image.
So the variable test image that we have here, we are updating it and changing it and assigning it again
to an array.
So we are taking this test image, which is here, this image.
We are converting it to an array and so that we can pass it to the classifier because the classifier
cannot take G because it needs an array format.
Now, next, we need to do some expansion of the dimensionality.
This is a very important step.
So I'm going to say test underscore image is equal to MPLX expand.
Underscoring the importance of which image tests underscore image and axis equals zero.
Now, what this will actually do is it's going to insert the one we are waiting for.
So in the end, the input shape is actually 28 by twenty, eight by one.
And in order for this one to exist, we need to add it by ourselves in order to get the right shape.
The way to do that is by expanding dimensionality of this image.
All right.
So the next thing we're going to do is actually the result.
So we say result is equal to classifier, not predict.
This is where the prediction is going to take place.
What do we want to predict?
We want to predict the test.
eMESH, I want to predict what is a.
I have 10 classes between zero and nine.
And this classifier, I predict, is going to tell me which class is highly probable that it is the
image.
OK, no, I'm going to show you how we can do that and what is the return value of this predict.
So to do this, I'm just going to say for now, print results.
OK, now let's see if we have any syntax errors.
We do have some syntax errors, let us fix them all right here.
It's not that.
It's image underscore.
This is our first mistake.
Let's hope we don't have any others.
All right, so we have got our prediction and as you can see, these are probabilities.
This is six point five times ten to the minus six.
This is two point six times ten to the minus four.
And as you can see, it's really hard to tell which one is the highest because of this format.
But as you can see, we have all the classes and they are ordered correctly.
So this is a zero one, two, three, four, five, six, seven, eight, nine.
And each class is giving us a probability that, OK, for this to be number zero, it is giving it a
probability of six point five times to the minus six or let's say number nine, say, OK, this is maybe
nine, but I'm giving it only a probability of five point to seven times to that point, minus four.
OK, so we need only the maximum value and the right prediction.
I don't want to try to read all of this.
I want just the maximum value and the maximum value of this array is actually the prediction that we
are waiting for.
The highest probability is the winner here.
So let us do that here.
We are going to use some very simple function.
So we are going to use this square function in numbers that will help me determine the index and the
value of my maximum probability.
So I'm going to say rizzle.
Is equal to ENPI Dot, where open parenthesis and I'm going to pass here, result equals equals result,
not Max.
Now this will return the index of my value.
OK, so this may be where when you pass the max to it like this, you're returning the index.
Why do you need the index?
Well, let's say it returned only the probability.
How would you know to which class it refers to.
Right.
But if it returns the index and since the indexes represent which class, that index zero is actually
zero.
Index one is actually class one and up to nine.
Right.
So you need to return the index and the probability.
Now, this is the index.
Now, what is the probability?
Well, I'm going to just rename this actually to index and I'm going to create a new variable called
a prop for probability.
And it's going to equal to maybe dot max result.
Remember the max function we have talked about before in Python?
We can use it in the scenarios then I just want to print.
Rob.
And I want to print the index now, let let's build a compile.
Now, if you take a look here, we will see that it returned array with index to meaning number two.
So it predicted that this is a do with the probability of zero point four eight.
Well, it did predict that this is number two in the next lecture.
We're going to try more cases and see if this model is really predicting or not.