All right, my friends, we're almost at the end of this implementation.
Congratulations again for implementing, you know, part one.
That was quite big technical.
And then of course, part two and part three to.
Build and train our new.
Artificial. Neural. Network, the CNN.
And now we're going to tackle. Together part four.
To make a single prediction.
Which. I remind will consist.
Of. Predicting.
What's. Inside.
These two. Images.
So this one and that one.
So basically we will deploy our.
CNN on each of. These. Single images.
And we'll. Hope.
That our CNN. Predicts a dog here.
And of course. A cat here.
All right.
Let's do this.
So you'll know how to basically deploy.
CNN in production on a.
Single observation.
All right.
So let's create a new code cell.
And we're going to.
Start by.
Importing numpy.
I should have actually left it as our essential libraries.
You know that's what I mean.
We always need numpy you know most of the time.
So there we go.
NumPy as NP.
Yes please please at this shortcut.
NP then we will import a new module.
Which is actually.
Something we almost import before you know, at the beginning
we imported the image data generator from the image submodule.
Of the preprocessing module of the curious library.
And in fact.
What we would.
Like to import now is that image module.
But because we specifically.
Imported something specific from that module, well we need to import it again.
So let's just do it quickly.
So we're going to start from Keras.
From which we're going to get.
Access to the pre-processing module.
Perfect.
From which we're going to import.
That. Image module. Great.
All right.
So now we have everything we need to make that single prediction.
And now the next.
Step will be of course to load that single.
Image on which we want to.
Deploy our model to predict if there is a cat or dog.
Inside.
And therefore I'm going to create here a new variable that will be exactly.
That image and which will be actually the input of the predict method.
And we're going to. Call that variable test.
Underscore image.
That's the image we want to test our model on in production.
And the first thing we'll do with that. Variable. You know.
The way.
We're going to initialize.
It is by. Simply loading it from.
Our folder.
Or same single prediction folder that we have here.
Okay.
So the way we're going to do this is with a.
Specific function of the image.
Submodule, which is the load.
Img function in which we can actually fine.
Right here by scrolling down a bit, you know.
Or scrolling up actually.
Where is it exactly.
It is exactly.
Here. Load image function.
So we're going to use that function to simply load an image into. The.
Pil format.
All right. So that's the first. Format we'll. Get.
But you're going to see that. We will need. To adapt it.
A bit more to make.
It accepted by the predict. Method.
Okay.
So let's go back to our implementation.
And there we. Go.
Let's go first that.
Image submodule from which we're going to.
Call that load.
Underscore img function.
And inside this function will very simply we have to enter.
Two. Arguments.
The first one is the.
Path in quotes that starts. From the root.
Which. Is, you know, our. Data set folder.
And then going to that single prediction folder and then specifying which image.
We want to select. So we'll start with this one.
And therefore here we.
Need to specify the path data set slash single.
Prediction. Slash character one. All right.
And that's exactly.
What is expected as the first argument of this load.
Underscore img function.
So let's do this.
We have to enter it in quotes.
So we start with the data set.
Then slash it.
Then we go. Into our single.
Underscore.
Prediction folder.
And then. We specify.
Which image we want to select.
As this test. Image okay. And that's.
Let's just do a copy. Paste.
Make sure we don't make any mistake.
So pressing enter.
Copy that.
And then.
Let's paste that back.
Inside our implementation. Right here.
All right.
And this.
Will lead us to that image which will be that.
Test image. Variable.
And then let's not forget.
The extension.
Dot jpg okay.
So that will lead us to that image.
So that this. Test image will become exactly that image.
Selected okay. So that's for the first argument.
And then.
Let's add the second.
Argument a very very important one.
And actually. Compulsory.
That's image which you know will become the input of the predict method.
Has to have the. Same. Size.
As the one that. Was used.
During the training. And remember.
We actually resized our.
Images into, you know.
This size target size of 64 by 64.
You know, whether it was for the training set or the test set.
And we specified this.
Again when building the CNN.
Right here with the input shape.
So the size of.
Any image we're going to work. With, whether it is.
To train our CNN or. To call the.
Predict method.
Has to be 64 by 64.
Okay.
And in order to. Specify this. Here, we need to enter.
As this. New argument the compulsory one.
Target.
Underscore. Size equal.
And in some parenthesis.
64 6464 by 64 okay.
Perfect. So now we have a first test image.
But in order to be.
Accepted by the predict method which you know expects.
A certain format.
We have to do some extra work.
Here on that test image.
And that first. Extra work is. To convert.
That. Pil. You know, Pil format.
Which is a.
Format of images into an array.
Remember that the Predict method expects as its input a 2D array.
You know, you remember these.
Double pair of square brackets, which we used to.
Input in the predict methods of.
Our previous. Models.
You know, when predicting the outcome of a single observation.
Well, here we're about to do exactly.
The same by converting this.
Test. Image into an array.
And the. Way we're going to do this is with.
Another function of this image data preprocessing module.
Which is exactly.
This one image two. Array, which.
Converts indeed a image instance into a numpy.
Array, which is exactly the.
Format of.
Array expected by the predict. Method.
So we're going to use that function right away to convert.
Our. Test image.
Which so far has the Pil. Format into.
This. Numpy array format.
All right.
So we're going to update.
Our test image again. So I'm calling it again.
And then updating it by sending it.
Equal to a new value.
And well as you understood we're going to use our image submodule again
from which we're going to call this time the I img underscore. Two.
Underscore. Array.
Function.
And guess what?
It has to take as input.
Well very simply it just needs to take.
That image in the format which we want to convert into the numpy array format.
And therefore well.
The input will.
Simply be that test. Image and nothing else.
You know, we only need.
That.
Image. Of which.
We want to convert the format.
Okay. So that the next step.
But then we still have some.
Extra work to do, which is still related to the fact that.
You know, the predict method has to. Be called.
On. The exact.
Same format that. Was used.
During the training.
And well, remember
that, you know, when preprocessing our training set in our tests.
It right here. In part one, data.
Preprocessing we actually created batches of.
Images.
Therefore, you know our CNN was not trained on single images.
You know, entering the network one.
After the other, but it.
Was trained with. Batches of. Images.
And therefore we.
Have this. Extra dimension corresponding.
To the. Batch.
You know, we have batch number one containing 32 images, then batch
number two containing other 32 images, batch number three, etc..
So we have this.
Extra dimension of the batch and even if now we're.
About to.
Deploy our model on a single image, well, that single image still has to be into a.
Batch. Even if we're going to have one image in the batch.
It needs to be into this batch so that the.
CNN model, and more specifically, the predict method of the CNN model,
can recognize.
The batch as that. Extra dimension.
And so, you guessed it, what we're about.
To do now. Is just to add.
An. Extra.
Dimension which will correspond to the batch
and which will contain that image into a batch.
And the. Way to do this is.
Once again by, you know, I.
Just copied it before, so I can just paste it.
Once again.
Update our test. Image by adding to.
It this.
Extra dimension corresponding to the batch.
And the way.
To do this is. With numpy.
You know.
That's how we can always easily.
Manipulate a numpy array.
Now that you know, test image is indeed a numpy array.
And so I'm going to call numpy first, which has a shortcut
np from which I'm going to call this function, which.
Allows exactly you to add a fake.
Dimension or you know, a dimension corresponding. To the batch.
And which is. Called the.
Expand underscore dims function.
And now. You guess.
Once again, what do we have to input inside this function.
Well, we. Have to input the image to which.
We want to add this.
Extra dimension. Corresponding to the batch.
So first I have to paste once.
Again our test image.
But we have to add.
One extra argument here.
Which is where we want to add that.
Extra dimension.
And that dimension of the batch.
Is actually always the first. Dimension.
You know, that makes sense. You give first the.
Batch of images and then inside.
Each batch you get the. Different images.
So it seems. Natural to have the batch as the.
First dimension.
And to. Specify this, that's exactly what.
We need to enter here. As this extra. Argument.
And that parameter to.
Specify this is axis which we have to set equal to zero okay.
So that the.
Dimension of the batch which we're adding to our image.
Will be the. First dimension. Okay.
And now we're getting very close.
To the final result because.
Now is the time we can.
Call the. Predict method.
Because indeed.
That test.
Image, which is not only in the right numpy.
Array, but also which has that extra.
Dimension corresponding to the.
Batch, has exactly the right format expected by.
The predict method.
And therefore here we can create a new variable
which we're going to call result.
Because this will actually be the.
Prediction. Of our.
CNN called. With. Our test image.
But I'm not calling it prediction. Yet because it will.
Only return 0 or 1.
And then we'll have to do some encoding.
Work to say what is zero. And what is one. You know.
Zero will actually be cat. And one will. Actually be dog.
I'll explain why later, but there we go.
We're just going to call this first result variable,
which will be of course, the output of the predict.
Method call from our CNN.
And so there we go. We need to.
Take our. CNN. First.
From which we're going to. Call.
Of course the predict method which will take.
As input. Of course. Well our test.
Image, which now.
Has the right format expected by that.
Predict method.
So nothing else.
We simply need to input this image here.
And then now is the time to.
Do that. Encoding work.
So the way to figure out what is zero.
And what is one. You know.
If zero is cat or dog and one is cat or dog,
well, the trick is to call the class.
Indices. Attribute.
From our training set.
Object.
But since. Now we can't run this. Code.
I'm just.
Going to write the code that.
Allows us to know which indices.
Correspond to which classes.
And then you can see that on Jupyter Notebook.
But I will just tell you that by using this we.
Actually know that one corresponds.
To dog.
And. Zero corresponds to cat.
So to get.
That information we need to call our training set.
Or actually the test set as. You want.
And then from which we.
Call this attribute called class.
Underscore indices.
Just like that.
And by printing this.
You will get the right class indices.
And with this we.
Get that dog corresponds. To one and cat.
Corresponds to zero.
And therefore our.
Final step here, you know, to have a nice.
Result displayed in the output.
In the end, when we make that single prediction on these two single images,
well we're going to finish this with, you know, an if condition.
Where we're going to specify that if the result.
All right.
Now remember that. Result. Contains.
Also actually the result.
Into a. Batch because it was. Called on a test image.
That was. Into a. Batch.
So results also has this batch dimension.
And therefore we're going to first.
Get access to the batch. And there are only one.
And it has of course index zero because.
Indexes in Python start. From zero.
So here we get access to the batch.
And then inside the batch we're going to get access to.
Well the first.
And only element of the.
Batch which corresponds to the.
Prediction of that same. Cat or dog.
One image.
Right.
We're dealing with a single.
Image and therefore a single prediction.
And to.
Get that single.
Prediction well we need to get inside this batch of index.
Zero.
Well the.
First and only element, you know, the first and only prediction
which has once again index. Zero.
All right.
So that's how we get exactly the prediction.
We want by first accessing the batch.
And then accessing the single element of the batch.
And that's.
Exactly our. Prediction.
And so.
If that prediction.
Is. Equal notice double equal here to. One.
Then since we know.
That one corresponds to duck.
Then we'll create a new.
Variable which we're going to call prediction.
And we'll set that prediction variable equal to dog.
And then I'm going to copy. This because.
Then in an else condition you know meaning if.
The result.
You know the prediction is equal. To zero.
Then well prediction will.
Be equal to cat.
And then we simply finish this.
With a less code cell.
Containing a simple print of.
The prediction. And that's it my friends.
This is how you make a single prediction
with a convolutional neural network.
So now now it is to show time.
It is time for. The exciting step.
Because we're all going to sit comfortably in our chair and we're going to shoot.
This code. On Jupyter Notebook.