Average Pooling 2D
Computer Vision
Medium
Problem
Average pooling is a downsampling operation that reduces the spatial dimensions of a feature map by computing the mean value within non-overlapping rectangular regions. Unlike max pooling which selects the strongest activation, average pooling captures the overall presence of features in each region.
Given a 2D matrix and a pool size, apply average pooling with non-overlapping windows (stride equal to pool size).
Algorithm
- Compute the output dimensions by dividing the input dimensions by the pool size (integer division):
H_{out} = \left\lfloor \frac{H}{p} \right\rfloor
W_{out} = \left\lfloor \frac{W}{p} \right\rfloor
- For each output position (i, j), compute the mean of all values in the corresponding p × p window:
\text{out}[i][j] = \frac{1}{p^2} \sum_{a=0}^{p-1} \sum_{b=0}^{p-1} X[i \cdot p + a][j \cdot p + b]
Return the pooled two-dimensional list of floats.
Theory
Average pooling is a downsampling operation that reduces spatial dimensions by computing the mean value within each pooling region. Unlike max pooling which selects the strongest activation, average pooling captures the overall presence of features.
The Average Pooling Formula
For pool size p \times p with non-overlapping windows:
\text{output}[i][j] = \frac{1}{p^2} \sum_{a=0}^{p-1} \sum_{b=0}^{p-1} \text{input}[i \cdot p + a][j \cdot p + b]
This computes the arithmetic mean of all values in each window.
Step-by-Step Example
Input (4x4):
4 2 6 8 0 4 2 4 8 6 2 0 2 4 6 8
Pool size: 2x2
Top-left window: (4 + 2 + 0 + 4) / 4 = 10/4 = 2.5 Top-right window: (6 + 8 + 2 + 4) / 4 = 20/4 = 5.0 Bottom-left window: (8 + 6 + 2 + 4) / 4 = 20/4 = 5.0 Bottom-right window: (2 + 0 + 6 + 8) / 4 = 16/4 = 4.0
Output (2x2):
2.5 5.0 5.0 4.0
Output Dimensions
Same as max pooling:
H_{out} = \left\lfloor \frac{H}{p} \right\rfloor
W_{out} = \left\lfloor \frac{W}{p} \right\rfloor
For a 6x6 input with 2x2 pooling: output is 3x3.
Average Pooling vs. Max Pooling
Average pooling:
- Uses all values in the window
- Smooths the feature map
- Sensitive to all activations
- Better for dense, distributed features
Max pooling:
- Uses only the maximum value
- Preserves sharp features
- Invariant to most activations
- Better for sparse, localized features
When to Use Average Pooling
Global average pooling (final layer):
- Replace fully connected layers
- Average entire feature map to single value per channel
- Used in GoogLeNet, ResNet, and modern architectures
- Reduces parameters significantly
Intermediate layers:
- Less common than max pooling
- Used when smoothing is desirable
- Some architectures mix both types
Dense prediction tasks:
- Segmentation, depth estimation
- Where every input region matters
- May preserve more information
The Gradient (Backpropagation)
During backpropagation, the gradient is distributed equally:
Forward pass:
- Compute mean of p^2 values
Backward pass:
- Each input element receives: (incoming gradient) / p^2
- Gradient is uniformly distributed across the window
Compared to max pooling where only the max element receives gradient, average pooling provides gradient to all elements.
Global Average Pooling
A special case where pool size equals the spatial dimensions:
\text{output}_c = \frac{1}{H \times W} \sum_{i=0}^{H-1} \sum_{j=0}^{W-1} \text{input}[i][j][c]
For input H x W x C, output is 1 x 1 x C (or just a vector of length C).
Benefits:
- No parameters (unlike fully connected layer)
- Works with any input size
- Reduces overfitting
- Often followed by single FC layer for classification
Implementation Notes
Division placement:
- Divide by p^2 at the end (after summing)
- Or accumulate running mean
- Both give same result
Floating point output:
- Unlike max pooling, average pooling typically produces floats
- Even if input is integers, output is usually float
Handling remainders:
- If H is not divisible by p, extra rows/columns are ignored
- Same truncation behavior as max pooling
Numerical Example with Stride
For general stride s (not equal to pool size):
\text{output}[i][j] = \frac{1}{p^2} \sum_{a=0}^{p-1} \sum_{b=0}^{p-1} \text{input}[i \cdot s + a][j \cdot s + b]
This allows overlapping windows (s < p) or gaps (s > p), similar to max pooling.
Average Pooling in Modern Architectures
ResNet:
- Uses global average pooling before final FC
- Followed by single 1000-way classifier
EfficientNet:
- Global average pooling at the end
- Followed by dropout and final FC
MobileNet:
- Global average pooling
- Designed for efficiency
The trend is: minimal use of intermediate average pooling, but global average pooling at the end is standard.
Examples
Example 1
- Input
X = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], pool_size = 2- Output
[[3.5, 5.5], [11.5, 13.5]]- Explanation
- Each non-overlapping 2 by 2 block contributes its arithmetic mean.
Example 2
- Input
X = [[10, 20], [30, 40]], pool_size = 2- Output
[[25.0]]
Hints
- Use integer division to count complete pooling windows along each dimension.
- Sum one window at a time and divide by pool_size squared.
Requirements
- Apply non-overlapping average pooling with stride equal to pool_size
- Compute the arithmetic mean of all values in each pooling window
- Handle rectangular inputs where dimensions may not be square
- Discard any remaining rows or columns that don't form a complete pool
- Return the pooled 2D matrix as a list of lists of floats
Constraints
- X is a non-empty 2D matrix of numbers
- pool_size >= 1
- Input dimensions are at least pool_size in both directions
- Return a 2D list of floats
- Time limit: 300 ms
Starter Code
def average_pooling_2d(X: list, pool_size: int) -> list:
"""
Returns non-overlapping average-pooled windows.
"""
# Write code here
passTest Cases
| Case | Matches | |
|---|---|---|
| 4x4 with 2x2 pooling | — | public |
| 2x2 full pooling | — | public |