Hello my friends, and welcome.
To part two of. This implementation, where we're going to.
Build together the. Convolutional neural network.
And more specifically, the whole architecture of this new artificial.
Neural network.
All right. So it's actually. Going to start.
The same as with our artificial neural network.
Because a convolutional neural network.
Is still a. Sequence of layers.
Therefore we're going to. Initialize here our.
CNN with the.
Same class which.
Remember. Is sequential class.
And that's.
A first step where. We're going to not.
Only call that sequential class, but mostly create that.
CNN. Variable which.
Will represent exactly.
This. Convolutional neural network.
And the CNN variable. Will be created.
Once again as an instance of that sequential class.
Which allows us to create an.
Artificial neural.
Network as a. Sequence of layers.
So now remember how we can get.
Access to this class.
Well, first.
We need to.
Call TensorFlow which has a shortcut TF.
From which we're going to.
Call the Keras library.
From which we're going to get.
Access to the. Models. Module.
And from which we're going to call.
That sequential class.
So exactly the same as before.
And this initializes. Indeed our.
CNN as. A.
Sequence.
Of layers as opposed to a computational graph.
Great. And now.
Step by step we're going to use this.
You know, add method.
To add to. Different.
Layers, whether they are.
Convolutional. Layers or.
Fully connected layers.
And in the end the output layer.
So we're going to use successively.
Now the add method starting with step one convolution.
And so now and then. You could sell.
Well first we'll take our.
CNN object our convolutional neural network from which we're going to.
Call of course the add method to add
our very first convolutional layer.
All right.
So what we want to add right now is of course a convolutional layer.
And once again this convolutional layer. Will be.
An. Object of a certain class which is the conv 2d class.
In this class, just after.
Dense.
Class which allows us to build a fully connected
layer, belongs to the same module which is a layers module from the Keras library.
From this time. TensorFlow.
It's TensorFlow 2.0.
So first we're going to get TensorFlow, from which we're going to get access
to the Keras library, from which we're going to get access to the layers module.
And there we go from which we're going to call that conv 2d.
There we go. Class comes to the class.
Then we add some parentheses of course because that's a class.
And inside we have to input three.
Important.
Parameters which are the filters which basically.
Is the number.
Of feature detectors you want to apply to.
Your images, you know, to. Detect features.
And these are indeed also called filters or kernels.
Right.
If we scroll. Down here.
Actually you have all the info of these parameters, right.
Arguments filters.
The number of output. Filters. In the convolution.
Well basically that's the feature detectors.
So that's our first argument.
Then we will. Also specify. A kernel size.
And actually.
I've prepared some. Slides that I made to. Show you.
So basically this is the filter.
And this.
Filters parameter will tell us how many feature detectors.
We want, how many filters we want.
And the kernel.
Size is exactly, you know, the size of that feature.
Detector, meaning the number of. Rows, which is also.
The number of columns, because.
It's is usually a square array.
You know.
So for example, if we choose a size of three,
which is actually the size, we're going to choose, well,
that means that the size of our feature detector will be three by three.
Okay. As simple as that.
And then we have. Some other. Arguments.
But no worries.
We'll just keep the default value
for the rest of them because that will be just fine.
However, of course. We're not.
Going to keep the default value of that activation parameter,
which corresponds of course, with the activation.
Function, because indeed, you know, on a general.
Rule, as long as we haven't
reached the output layer, we rather want to get a rectifier.
Activation function.
And therefore for this one.
Activation.
We're going to choose once again the ReLU parameters name
which corresponds to the rectifier activation function.
And finally. We.
Have one less argument we have to input in which is not.
Displayed here because.
That's hidden by this one, but which you.
Can actually see here.
That's the input shape.
When you.
Add your very first layer, whether it is a convolutional layer.
Or dense layer.
Well, you have to specify the input shape of your.
Inputs. And here.
Since we were working with. Colored images.
Therefore in three. Dimensions, you know.
Corresponding to the RGB code of colors, and.
Since we.
Actually resized in parts one, our images
down to 64 by 64, well, the input shape.
Of our images will.
Be 64, 64. And three.
And if we were working with black and white images.
Here, we would have. One instead. Of three.
But we're working with.
Colored images and therefore.
That's what will be our input shape 6464.
And three.
All right.
And that's the essential parameters that we have to enter.
So there we go.
Let's enter them.
Starting with the filters. Parameter.
There we go.
And now the question is well how many.
Feature detectors we want.
Well we're just going to go for a classic architecture.
You can find many. Architectures of.
Convolutional neural networks online.
We're going to choose. A classic. One, which.
I of course tried. On our images and which.
Turns out to work very well.
And that architecture consists of having 32 filters in that first.
Convolutional layer and then another 30.
Two. Filters in that second convolutional layer.
All right.
So let's just choose here filters equals 32.
And feel. Free of course to. Choose another.
Parameter value.
I remind that.
This is an artist job.
You're free to.
Experiment any. Architecture you'd like.
And you. Might probably get some better results.
Than the ones we're about to get in the end.
All right, so filters equal 32.
That's for the first parameter.
And now for the second parameter.
As we said.
It is going to be the kernel underscore.
Size. This one.
And as we said we want a.
Three by three. Dimensions.
And we only have. To specify three.
And then as. We said. We want to make.
Sure that we have a rectifier activation. Function.
So here. For this new.
Activation parameter we're going to choose in quotes.
Well the ReLU activation.
Function exactly the same as.
Before.
And finally for that.
Last parameter.
We have to specify. The input.
Shape of our images, which we.
Have to enter in a. Pair of square brackets.
And there we go.
We resized. Our images to.
Have the 64 by 64.
Dimensions. And since we're.
Working with images, we have to add here three.
Instead of. One. All right.
That's our input shape. And that's. It.
This adds.
The convolutional. Layer to your.
CNN so far initialized as a sequence of layers.
Good.
So now we can move on.
To step two pooling.
So let's create a new code cell.
And this consists of. Course of.
Applying pooling. And more.
Specifically we're going to.
Apply max. Pooling just as you saw in.
The intuition lectures.
All right.
So once. Again we need. To take our. CNN.
Object from.
Which we're going to call now a new method.
But is that really new.
According to you. Do we. Have to call the.
Ad method again?
Well, yes we do actually we're adding the pooling layers to.
Our convolutional layer, you know, as the next step in the sequence.
Of layers.
So there we go.
We call the. Add method once again.
And then inside.
Well that's. Max. Pooling layer.
Will once again.
Be created.
As an object, you know, as an instance of a certain class.
Which. Is called the max.
Pool 2D class.
And this class belongs to the same module.
As before, the layers module.
So there we go. We're actually. Going to.
Take this.
You know I'm going to.
Copy this paste that right here.
And then instead of taking the cons to the.
Class we're going to take this. Time the max.
Pool 2D class. Perfect.
And inside.
We have to specify two essential.
Arguments which are. The pool size.
So let me show you what this corresponds to.
So this is a whole. Convolutional. Process.
You know with the feature detector applied.
To the input image resulting in having that.
Created feature map.
All right.
So that's. All this.
And there we go. Now we move on to step two.
So we have our feature map which is the result of our previous convolution.
And we apply max pooling to obtain this.
Pooled feature map.
You know that same feature map of the convolutional layer.
After we apply. Max pooling.
And when we do.
This, you know, we have this little frame. Here.
That will.
Get the maximum.
Pixel of the four cells.
Inside, you know, the. Four pixels. Inside.
And that.
Pool size parameter, which we're about.
To enter here.
Is exactly the size of that.
Frame, you know, which is once again a square.
So we only have to specify the width.
Or you know, the. Height.
So basically.
Here in this example. Two okay. And well.
You know speaking of two that's exactly the pool size.
Which we're going to choose and which. Is.
The one I recommend to. Work.
With when applying max pooling.
Okay.
Then let's see.
The other parameter that is important is.
The strides okay. So interesting.
Let's have a look at our slides once again.
So I'm going to move to the next slide here.
And let's. See by which.
Number.
Of pixels is this frame shifted to the right.
Let's see.
All right.
So it is actually shifted by two pixels.
You know instead of going.
From this frame to that.
Frame. Which would be a slide of one.
Well we went directly from this frame.
To that one. Okay.
And this makes sense when applying max pooling,
because we only want to get the maximum of.
Each square here of pixels. Right.
So the. Recommended. Slide. Once again.
Is two that indeed we're sliding two by two pixels.
And when we reach, you know that.
Border of the feature.
Map here, you know with extra empty cells. Here.
Well you can actually. Choose two different ways.
And that corresponds to that.
Parameter which is the padding.
The default value is valid, but the other value is same.
And well the difference is, is that you know with a valid padding,
you will just ignore. The other two cells here.
And with the same padding you will. Just.
Add an extra column.
With just fake pixels that are equal to zero.
But don't.
Worry too much. About this padding.
I tried actually with the two values.
This doesn't change anything in the final result, and so I just recommend to.
Keep the default value.
But the slide is important.
And now we're sliding. Two by two okay.
So let's do this.
Let's enter our parameter is. For that. Max.
Pool to the class.
The first one is as we said the pool size.
There we. Go.
And we want.
To have exactly as in this slide a two by two.
Frame which we only have.
To specify with the parameter input two.
And then the second argument.
Is just tried, you know, the. Essential one.
And we want to shift that frame.
Every two pixels.
And therefore we're going to choose a straight of two.
All right.
And that apply successfully max pooling.
As simple as that. And you can just copy paste this.
Line whenever you want to apply max pooling to.
Your. CNN.
And actually speaking of which.
Well, you know, now we want to add a second convolutional layer and admire
how I'm going to do. This so efficiently.
So first I'm going to create a new code cell.
Then I'm going to.
Take that cell, copy it, then paste it right here.
Then for the pooling.
Take that cell copy it, then paste it right below.
And now.
According to you, do we have.
To change something here or can we.
Leave it this way?
Well we can't leave it this way.
Actually, we just need to.
Remove that input shape parameter because this one is enter only when you add.
Your very first layer.
You know, to automatically connect.
That first layer to the.
Input layer which automatically adds.
The input layer. But here we are already with our.
Second convolutional. Layer.
So that's all good. We can just remove it.
Good.
And now.
Perfect.
All this adds a. Second convolutional.
Layer. With max pooling applied.
And we can now move on. To step three.
Flattening which will consist. Of of course.
Flattening.
The result of all these.
Convolutions and pooling into a one dimensional.
Vector which. Will become the input.
Of a future fully.
Connected neural network. Just as.
We built. In the previous section.
All right.
So let's do this.
Let's implement that. Step three. Flatten thing.
And well as usual.
You know we need to.
Take our.
CNN. Object from.
Which we're going to.
Call the ad.
Method once again because.
The way we're going to create that.
Flattening. Layer is.
Once again, by creating an instance of a certain class, and that certain class.
Is the flatten class.
And, you know, Keras will automatically understand that.
It is the result of all.
These convolutions and pooling.
That will be.
Flattened into this one dimensional. Vector.
So the only thing we have. To do is just to specify.
That we want to apply flattening.
And to do this we need to call once again the layers.
Module by the Keras library by. TensorFlow.
From which we're going to call this.
Time that.
Flatten class. And good news.
This class actually doesn't need to.
Take anything as. Parameters.
All right.
So this.
Simply implements step. Three. Flattening.
And we can directly move on to. Step four.
For connection.
All right.
So now it's your turn.
You can actually. Do it yourself.
So I would like you to please press pause on the video.
Because we are in the. Exact same situation as before.
You know, building a fully connected neural network.
So you know exactly how to do this.
I would like you to add a new.
Fully connected layer.
To that flatten layer.
Which is now nothing else than a one dimensional vector.
That. Will become the input.
Of a fully. Connected neural network.
So do this.
First and will implement this solution in a few seconds.
Good.
All right. Let's do this.
So you know exactly.
How to do this. First we create a new code cell.
Then we take once again our CNN neural network
from which we're going to add method, because now we're about to add a.
New layer which is a fully connected layer.
And which belongs.
Still to that Tf.keras layers, which I've already copied.
So I can just paste that here.
And this time. Take that. Dense.
Class. Perfect.
And now into some new parenthesis.
I'm sure you figured out.
What to enter as parameters.
First, remember we have the units, which is the number of hidden neurons
you want to have into.
This new. Fully connected layer.
And since now we're actually dealing with a more complex problem,
you know, computer vision is way more complex than data mining.
Classic data set as before.
Well, we're going to choose a larger. Number. Of.
Hidden. Neurons. We're going to choose.
128 hidden neurons.
However, if you picked the. Same number. As.
Before, you know, in the previous section with our
and well, I'm sure that's totally fine and I'm sure.
You'll get great results afterwards.
But we might. Get a better accuracy in. The end.
With a larger. Number of neurons, and therefore.
Let's choose here units equals.
128. Great.
And now second argument.
That's of course the activation function.
And once again my recommendation is.
That as long as you haven't reached the final output layer I.
Recommend to.
Use a rectifier activation. Function.
And that's exactly.
What we're going to specify here with this activation.
Parameter.
And remember the code name for the rectifier activation function is ReLU.
All right.
And perfect this at our.
Fully connected. Layer.
And finally the step five.
You see how we're being so efficient.
Here in that step five we need to add the final.
Output layer which will still be fully.
Connected. To that. Previous hidden layer.
Therefore we're going to.
Call once again the dense class.
And therefore in the new. Code cell.
I'm going to. Actually.
You know. Take all this. Then.
Based off here.
And inside will only have to.
Replace two things which are the values of these two parameters, because indeed.
The number of units, you know, the number of neurons in the final.
Output layer is.
Definitely not 120. Eight.
But you tell me.
Well, it's actually, of course.
One, it's exactly the same as before.
We're doing binary classification.
Therefore we only need one neuron to encode that binary.
Class zero. Or one or, you know. Cat or dog.
And therefore we only need one neuron and for the activation function remember.
That for the output layer. It is not recommended to.
Have a rectifier.
Activation function.
But rather sigmoid.
Activation function.
And that's because. Of course we're.
Doing binary classification.
Otherwise if we.
Were doing multi-class. Classification.
We would have remember a softmax activation function.
But there we go.
This will add a great output layer which will.
Optimize the result in the end.
And now. I have to say. Once again a.
Huge congratulations to you because there you go.
You just built.
A convolutional neural network.
We're done with part two.
We can already move on to part.
Three. Training the CNN.
And this will consist. Of course.
Making this brain, you know, this artificial brain with some eyes.
Pretty smart to.
Recognize. Cats or. Dogs in images.
So now we just have a good break.
So make sure. To recharge.
In good energy for the next. Tutorial. And as soon as.
You're ready, well, let's smash together.
Part three training the CNN.