Transcript for:
Understanding Padding in CNNs

Hello all, my name is Krishnag and welcome to my YouTube channel. Today we are going to continue the deep learning tutorial. This will be the continuation of deep learning tutorial and we are going to basically discuss about padding in convolution neural network. In my previous video, I have already shown you how does convolution operation takes place. So let me just revise little bit of convolution, how the convolution operation takes place and then we'll continue with respect to the padding. So I took an example that I had an image which was basically 6 cross 6 pixels. 6x6 pixels and then I was applying a vertical filter which is basically 3x3 of size and over here and this is vertical edge filter sorry and the output that I was basically getting was a 4x4 pixels output. Now you need to notice one thing over here guys. This kind of convolving operation is basically used from strides right. We were basically jumping from one place to the other place. We are doing the multiplication operation plus we are doing the summation of all the values and finally we were getting the output. Now one thing you have to note over here, initially I gave a 6x6 pixels right and when I applied a filter of 3x3 and this filter may change, we may also have a 5x5 filter, we may also have a 7x7 filter. It depends on different different type of filters, what type of operation they basically do. Now when I am applying this 3x3 filter, I am basically getting the output as 4x4. Now you notice one thing, initially I gave a 6x6 image, I am getting the output as 4x4. Now what is happening over here? We are losing some amount of data when it is basically passing through one of the filter. So if we want the same kind of output over here. So I basically want that I will be able to get 6x6 pixels output only. from my 6x6 pixel of images. At that time if you want to get this kind of output we basically have to apply something called as padding. Now if you remember my formula of calculating this output dimension with respect to the N value. So suppose my N I'm saying that it is basically 6 pixels. Here I'm basically saying that my filter size is 3. So I showed you that one formula that I basically apply is N minus F plus 1. Here my N value is 6. Filter size is 5, sorry 3 plus 1. When I'm calculating this I'm getting an output as 4. And that is what this output dimension I'm getting. Now what if I want to get the output dimension of 6 from my 6x6 pixel. So here my output will be 6. I have to calculate this n value. Okay now suppose if I calculate the n value it will be basically this F will go there and it will become plus 3 minus 1. and if I calculate this n value will be 8. But you notice that we have 6 x 6 pixels. How can we make it as 8 x 8 pixels that we need to think and for that we'll use a technique called as padding. Now padding what it does is that if I want to make this 6 x 6 into say 8 x 8 pixels I'll add a padding of 1 suppose. I'm adding a padding of 1 and what this does is that it tries to add 1 column sorry one row over here another one column over here one more row on the top and finally one column on the left. Now this is basically like you have your home over here okay and you are basically creating a compound on the on surrounding the home so that you don't lose anything from here right because you see that if I was just using 6x6 pixels I'm basically getting 4x4 output so I'm basically losing some information. Now what I've done is that I've just taken padding is equal to one and I've basically created I've added one row on the bottom one row on the the top one row one column on the right hand side and one row on the left hand side now this particular row will then I'll just join all these lines it will now become 8 cross 8 pixels this image will now become 8 cross 8 pixels you see this 8 x 8 pixels. Fine. Now when I have this 8 x 8 pixels, right? You can see that I'm having 8 rows and 8 columns now. Now what value I can fill in this is basically of two types. One is that you use zero padding. You have something called as zero padding wherein the first technique is basically called as zero padding where you will just insert zeros everywhere. Like this. The other technique is that you try to find out Which is the nearest value and you try to put the same value suppose this is your neighbor, right? One is your neighbor. You'll try to put one one again one suppose zero is your neighbor. You try to put zero so most Useful technique most commonly used practice is basically that you will try to use zero padding. Okay, everywhere you will try to fill it with zero Okay now When you are using 8 cross 8 and now when you apply a filter is equal to 3 You are basically going to get 6 cross 6 right because now you your image just imagine that it was 6x6 you have added a padding on top of that Then you have applied a filter and finally your image output is 6x6. So now let us see that how this formula changes if I apply padding. So suppose if I'm adding a padding value of p is equal to 1. Okay so now my formula basically changes as n plus 2p minus f minus 1. n plus 2p minus f plus 1. Sorry, it should be plus 1 or minus 1. Let me just think about it, okay? So n plus 2p. So let's, you know, n value is basically 6 because that was my original size. Plus 2 multiplied by 1. minus 3 right now when I am multiplying this it becomes 8 okay it should be plus 1 sorry so when I am multiplying 2 cross 2 multiplied by 1 plus 6 it is basically 8 minus 3 which is my 5 plus 1 which is equal to 6 okay I am sorry because of this little bit confusion so I have just made it as plus 1 okay so when I apply this particular operation now I am able to get 6 cross right now understand guys here based on the type of output right if you try to apply this particular formula you will be able to get the same pixel of the image size so that basically means you are not losing much information in addition you will be able to get more edges how see when I suppose if I am using zero padding over here and when I apply this filter this is one more edge right you can see that this ones are here zeros are there so this becomes one more edge and this is also one more edge right this is also one more edge and suppose here you also putting zeros this will also be become one more horizontal edge but currently I'm just basically seeing which will be the vertical edge so I'll be able to find two vertical edges in this and I'll be able to get 6 cross 6 okay and again here also one additional row will get added everywhere row and column will get added and your output will look something like this which will be 6 cross 6 So you should try to use padding because it will actually give you the same image size after you apply a filter operation. That basically means now you can continue creating number of many number of convolutions based on your image size, based on on the types of filters that you want to extract. I will show you that this particular convolution operation may be repeated many number of times. So this was about padding in convolution neural network. I hope you understood why we basically needs to apply padding. That is basically that we do not need to remove some of the information from the images. I want all the 6 plus 6 pixels over here also and try to extract more and more information from them. So this was a video all about padding. I hope you like this particular video. Please do subscribe the channel if you have not already done. I'll see you all in the next video. Have a great day ahead. Thank you and good luck.