Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub

peiying410632
from peiying410632 More from this publisher
22.02.2024 Views

Moreover, notice that if we were to take another step of two pixels the gray regionwould be placed partially outside the underlying image. This is a big no-no, so thereare only two valid operations while moving horizontally. The same thing willeventually happen when we move vertically. The first stride of two pixels down isfine, but the second will be, once again, a failed operation.The resulting image, after the only four valid operations, looks like this.Figure 5.10 - Shrinking even more!The identity kernel may be boring, but it is definitely useful for highlighting theinner workings of the convolutions. It is crystal clear in the figure above where thepixel values in the resulting image come from.Also, notice that using a larger stride made the shape of the resulting image evensmaller.The larger the stride, the smaller the resulting image.Once again, it makes sense: If we are skipping pixels in the input image, there arefewer regions of interest to apply the filter to. We can extend our previous formulato include the stride size (s):Equation 5.3 - Shape after a convolution with strideAs we’ve seen before, the stride is only an argument of the convolution, so let’s usePyTorch’s functional convolution to double-check the results:Convolutions | 357

convolved_stride2 = F.conv2d(image, kernel_identity, stride=2)convolved_stride2Outputtensor([[[[9., 0.],[7., 6.]]]])Cool, it works!So far, the operations we have performed have been shrinking the images. Whatabout restoring them to their original glory, I mean, size?PaddingPadding means stuffing. We need to stuff the original image so it can sustain the"attack" on its size."How do I stuff an image?"Glad you asked! Simply add zeros around it. An image is worth a thousand words inthis case.Figure 5.11 - Zero-padded imageSee what I mean? By adding columns and rows of zeros around it, we expand theinput image such that the gray region starts centered in the actual top left cornerof the input image. This simple trick can be used to preserve the original size of theimage.358 | Chapter 5: Convolutions

convolved_stride2 = F.conv2d(image, kernel_identity, stride=2)

convolved_stride2

Output

tensor([[[[9., 0.],

[7., 6.]]]])

Cool, it works!

So far, the operations we have performed have been shrinking the images. What

about restoring them to their original glory, I mean, size?

Padding

Padding means stuffing. We need to stuff the original image so it can sustain the

"attack" on its size.

"How do I stuff an image?"

Glad you asked! Simply add zeros around it. An image is worth a thousand words in

this case.

Figure 5.11 - Zero-padded image

See what I mean? By adding columns and rows of zeros around it, we expand the

input image such that the gray region starts centered in the actual top left corner

of the input image. This simple trick can be used to preserve the original size of the

image.

358 | Chapter 5: Convolutions

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!