Hello and welcome to this Python tutorial.
So now the next step is to make that count neurons function which will give us what we want.
That is this number of neurons in this huge vector after the convolutions are applied.
That's the only missing information we need right now and we are going to get it with the function.
So let's make this function we are going to call it counts.
And those core neurons very simply.
And what is this count neurons function going to take as arguments.
Well it is going to take the object self but then it's going to take something else because this number
of output neurons in the flattening layer actually only depends on one thing.
It depends on the dominations of the original input image the one that goes at the very beginning of
the neural network.
And so the only argument we need right now is actually the time mentions the timing of the input images.
Therefore let's give a name to this argument representing the diamond and of the input image.
And we're going to call it image.
All right.
And I can tell you right now that the actual dimensions of the input images coming from Dume are going
to be 80 by 80.
We're going to reduce the size of the original images to 80 by 80 and that's going to be the format
of the images going into the neural network.
So Image them is actually going to be one 80 80 and the one corresponds to the fact that we're working
with black and white images.
That is with only one channel so image them is going to be correlated to the total one eighty and 80.
All right.
So that's the only argument we need.
And now let's scan the neurons.
So how are you going to do that.
Well first of all we actually don't have any input image right now.
We don't have any doom image that we can import.
We're going to do that later.
So the first thing we have to do is create a fake image but that has done introns 80 over 80.
We're going to create that fake image with fake pixels and that will still give us eventually the number
that we want because that number only depends on the dimensions and not on the pixels that are inside
the images.
So this just creates a fake image to start.
And then we will compute the number of neurons that we want.
So the trick to create a fake image is well we are going to call it x.
First of all and then we were going to use the torch that Rand because you know we're going to put some
random pixels in these images who we're using these random functions from torche which is the rand function.
Then inside we're going to input as you can see the damnations of the images.
That is one 80 80.
But since we're going to put this image into the neural network and as you remember the neural network
can only accept batches of input states that is here batches of input images.
We are going to create that fake diamonds in which we can directly do this run function.
We actually just start with the one that will correspond to the batch and then we can just put it up
all 188 the corresponding to the dominations of the input image.
And as you understood these nominations are contained in this image them argument which represents that
table 1 80 80.
So we just need to add image them.
But in order to pass the elements of the table because you know right now Image them is a double as
a list of arguments of a function.
We need to add here before image them there is before that Apple store the store will allow to pass
the elements of the imaged him to Apple as a list of arguments for function.
And as you can see that's exactly what is specified here with the store and the diamond.
All right so that will create an image of fake pixels that will have nothing to do with the images.
But again we'll still be able to get the final number of neurons and now the last thing that we need
to do remember is to convert this input vector into a torch variable because this is going to go into
the neural network.
All right.
So this now represents an input image of random pixels that was just converted into a viable and that
will now go into the neural network.
And more specifically the convolutional layers of the neural network because since we only need the
number of neurons after the convolutions are applied we will just go up to the convolutions 3.
So right up to the third convolutional layer.
And we will not go into the two full connections here.
And that's because the number of neurons that we want is between convolution 3 and f.c 1.
All right so now that we have one input image with the right mentions Well it's time to propagate this
image into the neural network to reach the flattening layer then we're going to get the neurons in the
flattened layer and we'll just get the information that we want that is the number of neurons in this
flattening layer.
So now we have to do is exactly what we do in a forward function.
We need to propagate the signals into the neural network but only in the convolutional layers until
we reach the flooding layer.
So let's do this.
We're going to update x now x is the input image and with the second X here X will become well the first
convolutional layer.
And now what we have to do is the three steps process first step we apply deconvolution to the input
images.
Then the second step we apply mix pruning to the convoluted images and inserts that we activate the
neurons in this puled convoluted images and the x will become this first convolutional they're composed
of all these pools convoluted Energis.
So let's do this first step apply the first convolution convolution one to the input images.
So what we do is take our convolution one self that convo Lucian one that we go we apply it to our input
images which so far are represented by x.
So that's the first that first that done now.
Second step we are going to apply spooling to our convoluted images returned by convolution when X and
two climax pulling Well we're going to take a function from the functional module.
So we take the short cut than that and then we're going to use the function Max pool to D.
That's the one we put self convolution one X in the parenthesis of the max pull to the because we play
next pull into the convoluted images.
But this next function takes additional arguments which are first the kernel size.
So again that's the size of the window sliding through your images and that will take the maximum of
the pixels in each slide.
So that will still detect the features because the features are associated to a high value of the pixel
in the arrays.
As you say intuition lectures.
So this first documented human need to input is this kernel size.
And we're going to take three.
That's a common choice for the kernel size.
And then we need to put the strides you know by how many pixels it's going to slide in the images.
And we are going to take a stride of two.
Again that's a common choice.
So there we go.
Now the second step is done.
And now let's move on to the third step which is to activate all the neurons in this pool and convoluted
images.
And this first convolution layer and to do this again we are going to apply a function to all this.
And so here and taking again because we're going to take another function which as you might have guessed
is going to be an activation function but which one.
As usual it's going to be rectified activation function and maybe you remember the name for that is
really.
There we go.
That's the one.
And so we apply really to our pooled convoluted images that is all this.
All right.
And that's it.
Three steps done.
That was very quick.
So remember the way we have to look at this is first we apply the convolution to our input images then
we apply Max pulling to our convoluted images obtained with a convolution.
And then we activate the neurons in all this pool convolutional layer with the rectifier activation
function so perfect we get our first convolutional layer on which was a climax pulling and in which
the neurons are now activated.
And so basically what it does is that it propagates the signals from the first convolutional layer to
the next one.
And speaking of the next one that's exactly what we're going to take care of right now.
We're going to do the same thing as we just did on the first convolutional there to the second convolutional
layer to again propagate the signals further into the neural network by activating the neurons of the
second convolutional layer.
But before doing this we need to get this convolutional layer.
And so we are going to apply convolution to X that is now the first convolutional layer.
Well we are going to apply convolution to 2 x to obtain the second convolutional layer after which will
be Max pulling it.
And then finally activating Sirat.
So let's do this.
It's actually very easy we just to copy that and pasting that below.
Now of course we need to replace convolution one by convolution too.
And there we go.
That's actually ready see very easy.
And now with this line we propagate the signals from the second convolutional there to the next one
which is going to be the third convolutional there.
And to get this third convolutional will will need to apply that again.
So I'm copying this pasting that below and replacing convolution too by convolution 3 and that's done
isn't it so practical.
We propagate the signals in the three convolutional letters in a flashlight.
Thanks to this awesome structure.
All right so perfect.
Now we have our signals propagated up to the third convolutional layer and after.
And speaking of after that leads us to what we're looking for.
What we're interested in that is the flattening there.
All right so now that we have our third convolutional there that's the last X here.
It's time to get our flattening out.
And so that's exactly what we're going to do.
Now we're going to flatten all the pixels of this third convolutional layer that is we're going to take
all the pixels of all the channels of the third convolutional layer.
We're going to put them one after the other in a huge vector.
And of course this huge vector is going to be nothing else than the flattening layer and at the same
time we will use a trick to get the number of neurons in this flattening there.
That is what we're looking for.
That's the number of neurons we're missing and therefore let's directly return what we want and in this
return we're going to flatten the third convolutional layer and get at the same and the number of neurons
in this flattening layer.
So we're going to take X. which is our third convolutional there.
We're going to take all the channels of the third convolutional there and we're going to use a function
which is the size function to flatten all the pixels of all these channels in one same huge vector.
And so the trick you can find it in the pite watch the toil.
Well first we take the data of X because X is a special structure you know it's a torch Voivode So he
has a pretty complex structure.
But first we need to access it with data here then we need to view what's inside of it.
So we use this view function and now we need to access what we're looking for and that is even with
the arguments 1 and minus 1.
You have to understand what's inside the structure.
But you can just understand that this is how we're going to get this number of neurons and then to finish
we need to add size in parenthesis and inside inputs 1.
So basically what we do here what we do is we take all the pixels of all the channels and we put them
one after the other in this huge vector which will be the input of the fully connected network.
That's basically what the size does.
And with this we can get this number of new ones that we're looking for.
All right so now we get what we want.
And so finally we can replace number neurons here by what is returned by this function when it is applied
to the format of the images.
That is one by 80 by 80.
So what we have to do now is replace number neurons by we take the count neurons function which we apply
to the format of the images which will be the total one 80 and 80.
And there we go.
And of course we don't forget self because Count neuron is actually a method of the CNN test.
So we need to add the.
And now the warning should disappear.
And there we go.
Now everything is good.
We get the architecture of the neural network with nothing missing and we have this countersuits function
in case you know you want to try some other architectures and you don't want to count the number of
neurons manually.
You just use this function you play it to the format of your images.
And this will get you directly what you want.
That is the number of neurons in the flooding layer without having to do anything.
And wherever the architecture is that's critical.
And now we're done with the first big important step of this brain that we're making and we have one
last step.
That is one last function to make which is going to be the main forward function.
So we are going to propagate the signals from the beginning of the brain that is from the eyes of the
eye to the output layer that is after the second connection.
So we'll do that in the next two toile and until then our AI.