What Does Conv2d Output with 256×256 Input, Kernel 3?
Conv2d with 256×256 input, kernel_size=3, stride=1, padding=1 outputs 256×256. This is a “same” convolution — the output has the same spatial dimensions as the input. The formula gives: floor((256 + 2×1 - 3) / 1) + 1 = 256.
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 - 3 + 2*1) / 1) + 1
output = floor((256 - 3 + 2) / 1) + 1
output = floor(255 / 1) + 1
output = floor(255) + 1
output = 256
So the spatial dimensions go from 256×256 to 256×256.
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=3, stride=1, padding=1)
# Create input tensor: (batch, channels, height, width)
x = torch.randn(1, 3, 256, 256)
output = conv(x)
print(output.shape) # torch.Size([1, 64, 256, 256])
# Verify with formula
expected = (256 + 2 * 1 - 3) // 1 + 1
print(f"Expected output size: {expected}x{expected}") # 256x256
Architecture Context
This is the standard “same” convolution preserving spatial dimensions. Used extensively in VGG, ResNet, and DenseNet architectures.
Parameter Count
A Conv2d(3, 64, 3) layer has:
parameters = in_channels * out_channels * kernel_size^2 + out_channels (bias)
parameters = 3 * 64 * 3 * 3 + 64
parameters = 1,792
This layer has 1,792 trainable parameters (1728 weights + 64 bias terms).
Practical Tips
- Memory usage: The output feature map for a single image is 64 × 256 × 256 = 4,194,304 float values (16.00 MB in float32).
- Batch dimension: Multiply memory by batch size. A batch of 32 uses 512.0 MB for this layer's output alone.
- Same padding rule: For any kernel, setting padding = (kernel_size - 1) / 2 with stride=1 preserves spatial dimensions.