What Does Conv2d Output with 256×256 Input, Kernel 7, Stride 2?

Conv2d with 256×256 input, kernel_size=7, stride=2, padding=3 outputs 128×128. The formula gives: floor((256 + 2×3 - 7) / 2) + 1 = 128.

Formula Breakdown

The Conv2d output size formula is:

output_size = floor((input_size - kernel_size + 2 * padding) / stride) + 1

Plugging in the values for 256×256 input:

output = floor((256 - 7 + 2*3) / 2) + 1
output = floor((256 - 7 + 6) / 2) + 1
output = floor(255 / 2) + 1
output = floor(127.5) + 1
output = 128

So the spatial dimensions go from 256×256 to 128×128.

PyTorch Code Example

import torch
import torch.nn as nn

# Define the Conv2d layer
conv = nn.Conv2d(in_channels=3, out_channels=64, kernel_size=7, stride=2, padding=3)

# Create input tensor: (batch, channels, height, width)
x = torch.randn(1, 3, 256, 256)
output = conv(x)
print(output.shape)  # torch.Size([1, 64, 128, 128])

# Verify with formula
expected = (256 + 2 * 3 - 7) // 2 + 1
print(f"Expected output size: {expected}x{expected}")  # 128x128

Architecture Context

A 7×7 strided convolution that halves spatial dimensions. This is the classic ResNet conv1 configuration for processing large input images.

Parameter Count

A Conv2d(3, 64, 7) layer has:

parameters = in_channels * out_channels * kernel_size^2 + out_channels (bias)
parameters = 3 * 64 * 7 * 7 + 64
parameters = 9,472

This layer has 9,472 trainable parameters (9408 weights + 64 bias terms).

Practical Tips

Related Questions

Try the Conv2d Calculator