
Andrew Ng explains why convolutional neural networks use padding: it stops repeated convolutions from shrinking images and makes sure edge pixels get used. With padding P, output size becomes N + 2P - F + 1. For odd-sized filters, padding of (F - 1)/2 keeps dimensions the same.
Questions this video answers
N - F + 1 by N - F + 1.
N + 2P - F + 1 by N + 2P - F + 1.
P = (F - 1) / 2.
Zero (zero-padding).
"Valid" means no padding (P = 0). "Same" means pad so output spatial size equals input spatial size (P chosen so N + 2P - F + 1 = N).
Chapters
In order to build deep neural networks. One modification to the basic convolutional operation that you need to be able to use is padding. Let's see how it works. What we saw in earlier videos is that if you take a 6x6 image and convolve it with a 3x3 filter, you end up with a 4x4 output with a 4x4 matrix. And that's because the number of possible positions for your 3x3 filter there are only so the 4 by 4 possible positions for the 3x3 filter to fit in your 6 by 6 matrix. And the math for this turns out to be that if you have a n by n image and convolve that with an F by F filter, then the dimension of the output will be N minus F plus 1 by N minus F plus 1. And in this example, 6 minus 3 plus 1 is equal to 4, which is why you wound up with a 4 by 4 output. So there are two downsides to this. One is that if every time you apply a convolutional operator, your image shrinks, so we've gone from 6 by 6 down to 4 by 4, then you know, you can only do this a few times before your image starts getting really small. Maybe it shrinks down to one by one or something. So. So maybe you don't want your image to shrink every time you detect edges or detect other features on it. So that's one downside. And the second downside is that if you look at a pixel at a corner of an edge, this particular pixel is touchless, used only in one of the outputs because it touches that three by three region. Whereas if you take a pixel in the middle, say this pixel, then there are a lot of three by three regions that overlap that pixel. And so it's as if pixels on the corners around the edges are used much less in the output. So you're throwing away a lot of the information near the edge of the image. So to solve both of these problems, you know, both the shrinking output and when we build really deep neural networks, you see why you don't want the image to shrink on every step. Because if you have maybe, maybe 100 layer deep net, then if it shrinks a bit on every layer, then after 100 layers, you end up with a very small image. Um, so that was one problem. The other is, you know, throwing away a lot of information from the edges of the image. So in order to fix both of these problems, what you can do is before applying the convolutional operation, you can pad the image. So in this case, let's say you pad the image with an additional one border with an additional border of one pixel around the edges. So if you do that, then instead of a six by six image, you've now padded this to an eight by eight image. And if you convolve an eight by eight image with a three by three image, you now get back out not a four by four but a six by six image. And so you've, you've managed to preserve the original input size of six by six. So by convention, when you pad, you pad it with zeros. And if P is the padding amount, so in this case P is equal to one because we're padding all around with an extra border of 1 pixels, then the output becomes n minus n plus 2, p minus f plus 1 by n plus 2, p minus f by 1. So this becomes 6 plus 2 times 1 minus 3 plus 1 by the same thing and that so 6 plus 2 minus 3 plus 1, that's equal to 6. So you end up with a 6 by 6 image that's preserving the size of the R circumference. So this green pixel actually influences all of these cells of the output. And so this effect of, you know, maybe not quite throwing away, but counting less the information from the edge or from the corner of the edge of the image is reduced. And I've shown here the effect of padding the border with just one pixel. If you want, you can also pad the border with two pixels, in which case I guess you add on another border here and then you can pad it with even more pixels if you choose. So I guess what I'm drawing here, this would be a pad of equal to or p equals 2 in terms of how much to pad. It turns out there are two common choices, the that are called valid convolutions and same convolutions. I'm not sure these are great names, but in a valid convolution this basically means no padding. And so in this case you might have an n by n image convolved with an F by F filter. And this would give you an N minus F plus 1 by N minus F plus 1 dimensional output. So this is a like the example we had previously from the previous videos where we had an n by n image convolved with a 3x3 filter and that gave you a 4 by 4 output. The other most common choice of padding is called the same convolution. And that means when you pad so the output size is the same as the input size. So if you actually look at this formula, when you pad by P pixels, then it's as if n goes to n +2p. And then you have from the rest of this right, f plus one. So when you have an n by n image and a padding of a border of P pixels all around, then the output size ends up being this dimension n +2p +f +1. And so if you want n +2p +f+1 to be equal to 1, so that the output size is the same as the input size, if you take this and solve for, I guess, N cancels out on both sides. And if you solve for P, this implies that P is equal to F minus 1 over 2. So when F is odd, by choosing the padding size to be as follows, you can make sure that the output size is the same as the input size. And that's why, for example, when the filter was three by three, as in the example on the previous slide, the padding that would make the output size the same as the input size was V minus 1 over 2, which is 1. And as another example, if your filter was 5 by 5, so if F is equal to 5, then if you plug it into that equation, you find that a padding of two is required to keep the output size the same as the input size when the filter is five by five. And by convention, in computer vision, F is usually odd. It's actually almost always odd. And you rarely see even numbered filters, filter widths using computer vision. And I think there are two reasons for that. One is that if F was even, you know, then you need some asymmetric padding, right? It's only if F is odd that this type of same convolution gives a natural padding where you can pad the same dimension all around rather than, you know, pad more on the left and pad less on the right or something asymmetric. And then second, when you have an odd dimension filter such as 3 by 3 or 5 by 5, then it has a central position. And sometimes in computer vision, it's nice to have a distinguish or it's nice to have a pixel you can call the central pixel, so you can talk about, you know, the position of the filter, right? Maybe none of these is a great reason for using F to be pretty much always odd. But if you look at the computer vision literature, you see 3 by 3 filters are very common. You see some 5 by 5, 7 by 7s, you know, and actually sometimes later we'll also talk about one by one filters and when that makes sense. But just by convention I recommend you just use odd numbered filters as well. I think that you can probably get just fine performance even if you were to use an even numbered value for f. But if you stick to the common computer vision convention, you know I usually just use odd numbered F. So you've now seen how to use padded convolutions to specify the padding. For a convolution operation, you can either specify the value for p, or you can just say that this is a Valid, which means P0. Or you can say this is a same convolution, which means pad as much as you need to make sure the output has the same dimension as the input. So that's it for padding. In the next video, let's talk about how you can implement strided convolutions.










