PyTorch 計算卷積大小

son John
4 min readAug 14, 2022

--

torch.nn.Conv2dtorch.nn.MaxPool2d

官方的預設
stride=1, padding=0, dilation=1

計算輸出大小的公式:

  • 為了方便快速計算,沒使用到的就等同預設值
  • 有小數點就向下取整數(floor)
  • torch.nn.Conv2dtorch.nn.MaxPool2d公式是一樣的

1. 使用kernel_size

  • H - kernel_size + 1
X = torch.randn(5, 3, 20, 20)
out = torch.nn.Conv2d(3, 6, kernel_size=5)(X)
'''
in: [5, 3, 20, 20] => [N, C, H, W]
w_out = (H - kernel_size + 1)
= 20 - 5 + 1
= 16

out: [5, 6, 16, 16]
'''

2. 使用kernel_size stride

  • (H - kernel_size) / stride + 1
X = torch.randn(5, 3, 20, 20)
out = torch.nn.Conv2d(3, 6, kernel_size=5, stride=2)(X)
'''
in: [5, 3, 20, 20] => [N, C, H, W]
w_out = (H - (kernel_size - 1) - 1) / stride + 1
= (H - kernel_size) / stride + 1
= (20 - 5) / 2 + 1
= 7.5 + 1
= floor(8.5)
= 8

out: [5, 6, 8, 8]
'''

3. 使用kernel_size stride padding

  • (H + (2 * padding) - kernel_size) / stride + 1
X = torch.randn(5, 3, 20, 20)
out = torch.nn.Conv2d(3, 6, kernel_size=5, stride=2, padding=3)(X)
'''
in: [5, 3, 20, 20] => [N, C, H, W]
w_out = (H + (2 * padding) - (kernel_size - 1) - 1) / stride + 1
= (H + (2 * padding) - kernel_size) / stride + 1
= (20 + 2 * 3 - 5) / 2 + 1
= 10.5 + 1
= floor(11.5)
= 11

out: [5, 6, 11, 11]
'''

4. 使用kernel_size stride padding dilation

  • (H + (2 * padding) - dilation * (kernel_size - 1) - 1) / stride + 1
X = torch.randn(5, 3, 20, 20)
out = torch.nn.Conv2d(3, 6, kernel_size=5, stride=2, padding=3, dilation=2)(X)
'''
in: [5, 3, 20, 20] => [N, C, H, W]
w_out = (H + (2 * padding) - dilation * (kernel_size - 1) - 1) / stride + 1
= (20 + 2 * 3 - 2 * (5 - 1) - 1) / 2 + 1
= (20 + 6 - 8 - 1) / 2 + 1
= 17 / 2 + 1
= floor(9.5)
= 9

out: [5, 6, 9, 9]
'''

全連接層 torch.nn.Linear

a = torch.randn(3, 5, 10, 15)
b = torch.nn.Linear(15, 20)(a)
'''
in: [3, 5, 10, 15]
out: [3, 5, 10, 20]
'''
a = torch.randn(3, 15)
b = torch.nn.Linear(15, 20)(a)
'''
in: [3, 15]
out: [3, 20]
'''

--

--