22.02.2024 Views

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

If we assume the filter is a square matrix of size f, we can simplify the expression

above to:

Equation 5.2 - Shape after a convolution (square filter)

Makes sense, right? The filter has its dimensions reduced from (f, f) to (1, 1), so the

operation reduces the original size by (f-1).

"But I’d like to keep the image size, is it possible?"

Sure it is! Padding comes to our rescue in this case. We’ll get to that in a couple of

sections.

Convolving in PyTorch

Now that we know how a convolution works, let’s try it out using PyTorch. First, we

need to convert our image and filter to tensors:

image = torch.as_tensor(single).float()

kernel_identity = torch.as_tensor(identity).float()

Since kernel and filter are used interchangeably, especially when it comes to

arguments of different methods, I am calling the variable kernel_identity, even

though it is exactly the same identity filter we have used so far.

Just like the activation functions we saw in Chapter 4, convolutions come in two

flavors: functional and module. There is a fundamental difference between the

two, though: The functional convolution takes the kernel / filter as an argument

while the module has (learnable) weights to represent the kernel / filter.

Let’s use the functional convolution, F.conv2d(), to apply the identity filter to our

input image (notice we’re using stride=1 since we moved the region around one

pixel at a time):

convolved = F.conv2d(image, kernel_identity, stride=1)

convolved

352 | Chapter 5: Convolutions

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

Saved successfully!

Ooh no, something went wrong!