Spatial downsampling
A convolutional vision model needs smaller feature maps between convolution blocks.
Keras · Class
MaxPooling2D downsamples 2D spatial data by taking channel-wise maxima over moving windows. Configure the window, stride, padding, and channel layout to control the resulting feature-map size.
MaxPooling2D moves a rectangular window across an image-like tensor and keeps the largest value in each channel for every window position. Its stride controls movement, padding controls boundary handling, and the selected data format determines where the channel dimension appears.
keras.layers.MaxPooling2D(pool_size=(2, 2), strides=None, padding="valid", data_format=None, name=None, **kwargs)| Name | Type | Description |
|---|---|---|
pool_size | int or tuple of 2 integers | Integer or tuple of two integers controlling the downscaling window along the two spatial dimensions. |
strides | int, tuple of 2 integers, or None | Integer, tuple of two integers, or None specifying the window movement; None uses pool_size. |
padding | string | Padding mode: valid for no padding or same for evenly padded boundaries. |
data_format | string or None | Dimension ordering, either channels_last or channels_first. |
name | string or None | Optional layer name. |
**kwargs | keyword arguments | Additional keyword arguments accepted by the base Layer class. |
A 4D tensor whose spatial dimensions are pooled; with channels_last its shape is (batch_size, pooled_height, pooled_width, channels), and with channels_first the channels dimension remains second.
import keras
model = keras.Sequential([
keras.Input(shape=(28, 28, 3)),
keras.layers.MaxPooling2D(pool_size=2),
])
output = model(keras.ops.random.uniform((1, 28, 28, 3)))
print(output.shape)This model applies a 2-by-2 maximum window with the default stride and valid padding, reducing each spatial dimension while preserving channels.
A convolutional vision model needs smaller feature maps between convolution blocks.
A feature extractor should retain the strongest local activation in each channel.
A model uses image tensors in a framework-wide channels-first convention.
The layer performs max pooling over 2D spatial data independently for each channel. It slides a window defined by pool_size using strides and outputs the maximum value in each window.
Follow-up: How does valid output size differ from same? For valid padding, each spatial dimension follows floor((input_size - pool_size) / stride) + 1; same padding keeps the height and width dimensions equal to the input when the stride permits it.
pool_size accepts one integer or two integers, and a single integer is reused for height and width. strides has the same forms; when it is None, it defaults to pool_size.
Follow-up: Why might explicit strides be useful? They let the pooling window overlap or skip farther between positions instead of tying movement to the window dimensions.
The layer expects a rank-4 input. With channels_last, the shape is (batch_size, height, width, channels), while channels_first uses (batch_size, channels, height, width); the output keeps the same channel placement with pooled spatial dimensions.
Follow-up: What should be checked when integrating this layer into a model? Verify that the preceding and following layers use the same data-format convention and calculate the resulting spatial size before connecting shape-sensitive layers.
keras.layers.MaxPooling1Dkeras.layers.GlobalMaxPooling2Dkeras.layers.UpSampling2Dkeras.layers.Conv2D