20.03.2021 Views

Deep-Learning-with-PyTorch

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Convolutions in action

199

one-half the convolution kernel’s width (in our case, 3//2 = 1) smaller on each side.

This explains why we’re missing two pixels in each dimension.

However, PyTorch gives us the possibility of padding the image by creating ghost pixels

around the border that have value zero as far as the convolution is concerned. Figure

8.3 shows padding in action.

In our case, specifying padding=1 when kernel_size=3 means i00 has an extra set

of neighbors above it and to its left, so that an output of the convolution can be computed

even in the corner of our original image. 3 The net result is that the output has

now the exact same size as the input:

# In[16]:

conv = nn.Conv2d(3, 1, kernel_size=3, padding=1)

output = conv(img.unsqueeze(0))

img.unsqueeze(0).shape, output.shape

Now with padding

# Out[16]:

(torch.Size([1, 3, 32, 32]), torch.Size([1, 1, 32, 32]))

zeros

outside

0 1 0 0 1 0

1 1 1 0 0 1 1 1 0

0 1 0 1 0 0 1 0 0

0 1 0 0 0 1 0 0

0 0 0 0 0 0 0 0

0 1 0

0 1 1 1

1 0 1 0

0 1 0 0

0 0 0 0

0

1

0

0

1

1

1

0

0 1 0

1 1 1

0 1 0

0 0

0 0

0 1 0 0 0 0 1 0

1 1 1 1 0 1 1 1

0 1 0 0 0 0 1 0

0 0 0 0 0 0 0

0 1

0 1 0

1 1 1

0 1 0

0

1

0

0

0

0

0

0

0

1

0

0 1 0

1 1 1

0 1 0

0

0

0

0

0

0

0

0

OUTPUT

2 2 2 0

2 5 2 1

2 2 2 0

0 1 0 0

0

1

0

0

0 1 0

1 1 1

0 1 0

0 0 0

0 1 0 0

1 0 1 0

0 1 1 1

0 0 1 0

0 1 0 1 0

1 1 1 1 1

0 1 0 1 0

0 0 0 0

0 1 0 0

1 1 0 1 0

0 1 1 1 1

0 0 0 1 0

0

1

1

1

0 1 0

1 1 1

0 1 0

0

1

0

0

0

0

0

0

0 1 0 0

1 1 1 0

0 1 0 0

1 1 1 0

0 1 0

0 1 0 0

1 1 1 0

0 0 1 0

0 1 1 1

0 1 0

0 1 0 0

1 1 1 0

0 1 0 1 0

0 0 1 1 1

0 1 0

Figure 8.3

Zero padding to preserve the image size in the output

3

For even-sized kernels, we would need to pad by a different number on the left and right (and top and bottom).

PyTorch doesn’t offer to do this in the convolution itself, but the function torch.nn.functional

.pad can take care of it. But it’s best to stay with odd kernel sizes; even-sized kernels are just odd.

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

Saved successfully!

Ooh no, something went wrong!