- Background Knowledge
Convolutional Neural Networks (CNNs) use 2D convolutions (Conv2D) to slide a small filter (kernel) over an input image (or feature map) to produce an output feature map. At each position, the kernel and the corresponding input patch are multiplied elementwise and summed, giving one output value. Repeating this over all valid positions builds the full output grid. The output’s height and width depend on how far the kernel can move over the input while still staying (possibly after padding) within bounds.
Three hyperparameters control this movement:
- Kernel size K: the spatial size of the filter (e.g., 3×3). Larger kernels “consume” more spatial extent per position, generally reducing output size.
- Padding P: how many pixels are added around the border of the input (typically with zeros). Padding lets the kernel be centered near edges, increasing the effective input size.
- Stride S: how many pixels the kernel shifts each step. Larger stride means fewer positions, thus smaller output.
The given formula for output height
Hout=⌊SHin+2P−K⌋+1
comes from counting how many stride steps fit between the first valid kernel placement and the last one. An analogous formula holds for width, replacing Hin with Win.
- Algorithm / Approach
The general approach to such problems:
- Interpret the formula as:
- Effective input size in that dimension: Hin+2P.
- Remaining space after placing one kernel: (Hin+2P−K).
- Number of stride steps that fit in that remaining space: \left\lfloor\frac{H_{in} + 2P - K}{S}\right\rfloor.
- Add 1 for the first position.
- Apply the same pattern for width:
Wout=⌊SWin+2P−K⌋+1
- Use integer math carefully, respecting the floor operation.
- Step-by-Step Strategy
To compute Conv2D output size:
- Identify input dimensions
- Hin: input height
- Win: input width
- Identify Conv2D parameters
- Kernel size K (assuming square for this problem; otherwise use Kh and Kw)
- Padding P
- Stride S
- Compute effective sizes
- H_{\text{eff}} = H_{in} + 2P
- W_{\text{eff}} = W_{in} + 2P
- Apply the formula for each dimension
- H_{out} = \left\lfloor \dfrac{H_{\text{eff}} - K}{S} \right\rfloor + 1
- W_{out} = \left\lfloor \dfrac{W_{\text{eff}} - K}{S} \right\rfloor + 1
- Return the output shape
- If the problem ignores channels: just (Hout,Wout).
- In practice with channels: (Cout,Hout,Wout), with Cout specified by the layer.
Example skeleton (Python-style):
def conv2d_output_size(H_in, W_in, K, P, S):
H_out = (H_in + 2 * P - K) // S + 1
W_out = (W_in + 2 * P - K) // S + 1
return H_out, W_out
- Common Pitfalls
- Forgetting padding on both sides: Use 2P, not just P.
- Ignoring floor behavior: In code, make sure you use integer division // (or an explicit floor) if you’re working with integers.
- Invalid configurations:
- When Hin+2P<K, the kernel does not fit even once: the formula becomes invalid or gives non-positive output.
- When (Hin+2P−K) is not divisible by S, the floor means the last possible kernel position may not align perfectly with the far edge.
- Mixing up parameters: Confusing stride with padding, or using width where height should be used.
- Time & Space Complexity
If you are only computing the output size using the formula, the complexity is:
- Time complexity: O(1) — a constant number of arithmetic operations.
- Space complexity: O(1) — only a few scalar variables are needed.
(For context, actually performing the convolution has higher cost, but this problem only concerns calculating the output dimensions.)