CodeBlanks Logo Icon CodeBlanks

Conv2d

Conv2d uses a kernel to scan for patterns in images.

nn.Conv2d

nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0) wants input of shape (N, C, H, W). Each output channel is a separate filter that looks at all input channels:

conv = nn.Conv2d(3, 16, kernel_size=3, padding=1)
x = torch.zeros(8, 3, 32, 32)
print(conv(x).shape)          # torch.Size([8, 16, 32, 32])
print(conv.weight.shape)      # torch.Size([16, 3, 3, 3])

The output size is ⌊(H + 2·padding - kernel) / stride⌋ + 1. With padding=1 a 3×3 kernel keeps the size.

Parameter count and stride

A Conv2d layer's parameter count is out · in · k · k + out; it doesn't depend on the image size. stride=2 moves the filter two pixels at a time and halves the output:

conv = nn.Conv2d(3, 16, 3)
print(sum(p.numel() for p in conv.parameters()))   # 16·3·3·3 + 16 = 448
s2 = nn.Conv2d(16, 32, 3, stride=2, padding=1)
print(s2(torch.zeros(1, 16, 32, 32)).shape)        # torch.Size([1, 32, 16, 16])

The same layer uses the same 448 parameters on a 32×32 or a 1024×1024 image.

Practice Convolution shapes

Apply the layers nn.Conv2d(1, 8, 5), nn.Conv2d(1, 8, 3, padding=1) and nn.Conv2d(1, 8, 3, stride=2, padding=1) to a (1, 1, 28, 28) input; print the three output shapes on separate lines. Then print the first layer's parameter count.

Starter code

model.py
import torch
from torch import nn

x = torch.zeros(1, 1, 28, 28)

# buraya yaz

Sample solution

model.py
import torch
from torch import nn

x = torch.zeros(1, 1, 28, 28)

katmanlar = [nn.Conv2d(1, 8, 5), nn.Conv2d(1, 8, 3, padding=1), nn.Conv2d(1, 8, 3, stride=2, padding=1)]
for k in katmanlar:
    print(k(x).shape)
print(sum(p.numel() for p in katmanlar[0].parameters()))

Expected output

torch.Size([1, 8, 24, 24])
torch.Size([1, 8, 28, 28])
torch.Size([1, 8, 14, 14])
208

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 With a 32×32 input, kernel 5, stride 1 and padding 0, how many pixels is the output?

  • 28×28 Correct answer
  • 32×32
  • 27×27
  • 30×30

Explanation: (32 + 0 - 5) / 1 + 1 = 28.

2 Build a 3×3 convolution that keeps the size.

Question
conv = nn.Conv2d(3, 16, kernel_size=3, ____=1)
Answer
conv = nn.Conv2d(3, 16, kernel_size=3, padding=1)

Explanation: padding=1 adds one pixel of zeros at the edges.

3 Match the settings with their effects.

Term Meaning
kernel_size Filter size
stride=2 Halves the output
padding=1 Keeps the size (3×3)
out_channels Number of filters

All lessons in Advanced