MediumComputer Vision

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

  1. 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

  1. 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:

Max pooling:


When to Use Average Pooling

Global average pooling (final layer):

Intermediate layers:

Dense prediction tasks:


The Gradient (Backpropagation)

During backpropagation, the gradient is distributed equally:

Forward pass:

Backward pass:

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:


Implementation Notes

Division placement:

Floating point output:

Handling remainders:


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:

EfficientNet:

MobileNet:

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

  1. Use integer division to count complete pooling windows along each dimension.
  2. Sum one window at a time and divide by pool_size squared.

Requirements

Constraints

Starter Code

def average_pooling_2d(X: list, pool_size: int) -> list:
    """
    Returns non-overlapping average-pooled windows.
    """
    # Write code here
    pass

Test Cases

CaseMatches
4x4 with 2x2 poolingpublic
2x2 full poolingpublic