maxPooling2dLayer
Max pooling layer
Description
A 2-D max pooling layer performs downsampling by dividing the input into rectangular pooling regions, then computing the maximum of each region.
Creation
Description
sets optional properties using one or more name-value arguments.layer
= maxPooling2dLayer(poolSize
,Name=Value
)
Input Arguments
poolSize
— Dimensions of pooling regions
vector of two positive integers | positive integer
Dimensions of the pooling regions, specified as a vector of two
positive integers [h w]
, where h
is the height and w
is the width. When creating the
layer, you can specify poolSize
as a scalar to use
the same value for both dimensions.
If the stride dimensions Stride
are less than the
respective pooling dimensions, then the pooling regions overlap.
The padding dimensions PaddingSize
must be less
than the pooling region dimensions poolSize
.
Example:
[2 1]
specifies pooling regions of height 2 and width
1.
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Before R2021a, use commas to separate each name and value,
and enclose
Name
in quotes.
Example: maxPooling2dLayer(2,Stride=3)
creates a max pooling
layer with pool size [2 2]
and stride [3
3]
.
Stride
— Step size for traversing input
[1 1]
(default) | vector of two positive integers | positive integer
Step size for traversing the input vertically and horizontally,
specified as a vector of two positive integers [a
b]
, where a
is the vertical step
size and b
is the horizontal step size. When
creating the layer, you can specify Stride
as a
scalar to use the same value for both dimensions.
If the stride dimensions Stride
are less than
the respective pooling dimensions, then the pooling regions
overlap.
The padding dimensions PaddingSize
must be
less than the pooling region dimensions
PoolSize
.
Example:
[2 3]
specifies a vertical step size of 2 and a
horizontal step size of 3.
Padding
— Input edge padding
[0 0 0 0]
(default) | vector of nonnegative integers | "same"
Input edge padding, specified as one of these values:
"same"
— Add padding of size calculated by the software at training or prediction time so that the output has the same size as the input when the stride equals 1. If the stride is larger than 1, then the output size isceil(inputSize/stride)
, whereinputSize
is the height or width of the input andstride
is the stride in the corresponding dimension. The software adds the same amount of padding to the top and bottom, and to the left and right, if possible. If the padding that must be added vertically has an odd value, then the software adds extra padding to the bottom. If the padding that must be added horizontally has an odd value, then the software adds extra padding to the right.Nonnegative integer
p
— Add padding of sizep
to all the edges of the input.Vector
[a b]
of nonnegative integers — Add padding of sizea
to the top and bottom of the input and padding of sizeb
to the left and right.Vector
[t b l r]
of nonnegative integers — Add padding of sizet
to the top,b
to the bottom,l
to the left, andr
to the right of the input.
Example:
Padding=1
adds one row of padding to the top and bottom, and one
column of padding to the left and right of the input.
Example:
Padding="same"
adds padding so that the output has the same size as
the input (if the stride equals 1).
HasUnpoolingOutputs
— Flag for outputs to unpooling layer
false
(default) | true
Flag for outputs to unpooling layer, specified as
true
or false
.
If the HasUnpoolingOutputs
value equals false
, then the max pooling layer has a single output with the name 'out'
.
To use the output of a max pooling layer as the input to a max unpooling layer, set the
HasUnpoolingOutputs
value to true
. In this case,
the max pooling layer has two additional outputs that you can connect to a max unpooling
layer:
"indices"
— Indices of the maximum value in each pooled region."size"
— Size of the input feature map.
To enable outputs to a max unpooling layer, the pooling regions of the max pooling layer must be nonoverlapping.
For more information on how to unpool the output of a max pooling
layer, see maxUnpooling2dLayer
.
Name
— Layer name
""
(default) | character vector | string scalar
Properties
Max Pooling
PoolSize
— Dimensions of pooling regions
vector of two positive integers
Dimensions of the pooling regions, specified as a vector of two positive integers
[h w]
, where h
is the height and
w
is the width. When creating the layer, you can specify
PoolSize
as a scalar to use the same value for both
dimensions.
If the stride dimensions Stride
are less than the respective
pooling dimensions, then the pooling regions overlap.
The padding dimensions PaddingSize
must be less than the pooling
region dimensions PoolSize
.
Example:
[2 1]
specifies pooling regions of height 2 and width
1.
Stride
— Step size for traversing input
[1 1]
(default) | vector of two positive integers
Step size for traversing the input vertically and horizontally, specified as a vector
of two positive integers [a b]
, where a
is the
vertical step size and b
is the horizontal step size. When creating
the layer, you can specify Stride
as a scalar to use the same value
for both dimensions.
If the stride dimensions Stride
are less than the respective
pooling dimensions, then the pooling regions overlap.
The padding dimensions PaddingSize
must be less than the pooling
region dimensions PoolSize
.
Example:
[2 3]
specifies a vertical step size of 2 and a horizontal step size
of 3.
PaddingSize
— Size of padding
[0 0 0 0]
(default) | vector of four nonnegative integers
Size of padding to apply to input borders, specified as a vector
[t b l r]
of four nonnegative
integers, where t
is the padding applied to
the top, b
is the padding applied to the
bottom, l
is the padding applied to the left,
and r
is the padding applied to the right.
When you create a layer, use the 'Padding'
name-value pair argument to specify the padding size.
Example:
[1 1 2 2]
adds one row of padding to the top
and bottom, and two columns of padding to the left and right of
the input.
PaddingMode
— Method to determine padding size
"manual"
(default) | "same"
Method to determine padding size, specified as "manual"
or
"same"
.
The software automatically sets the value of PaddingMode
based on the Padding
value you specify
when creating a layer.
If you set the
Padding
option to a scalar or a vector of nonnegative integers, then the software automatically setsPaddingMode
to"manual"
.If you set the
Padding
option to"same"
, then the software automatically setsPaddingMode
to'same'
and calculates the size of the padding at training time so that the output has the same size as the input when the stride equals 1. If the stride is larger than 1, then the output size isceil(inputSize/stride)
, whereinputSize
is the height or width of the input andstride
is the stride in the corresponding dimension. The software adds the same amount of padding to the top and bottom, and to the left and right, if possible. If the padding that must be added vertically has an odd value, then the software adds extra padding to the bottom. If the padding that must be added horizontally has an odd value, then the software adds extra padding to the right.
Padding
— Size of padding
[0 0]
(default) | vector of two nonnegative integers
Note
Padding
property will be removed in a future release. Use
PaddingSize
instead. When creating a layer, use the
Padding
name-value argument to specify the padding
size.
Size of padding to apply to input borders vertically and horizontally, specified as a
vector [a b]
of two nonnegative integers, where a
is the padding applied to the top and bottom of the input data and b
is the padding applied to the left and right.
Example:
[1 1]
adds one row of padding to the top and bottom, and one column
of padding to the left and right of the input.
HasUnpoolingOutputs
— Flag for outputs to unpooling layer
false
(default) | true
Flag for outputs to unpooling layer, specified as
true
or false
.
If the HasUnpoolingOutputs
value equals false
, then the max pooling layer has a single output with the name 'out'
.
To use the output of a max pooling layer as the input to a max unpooling layer, set the
HasUnpoolingOutputs
value to true
. In this case,
the max pooling layer has two additional outputs that you can connect to a max unpooling
layer:
"indices"
— Indices of the maximum value in each pooled region."size"
— Size of the input feature map.
To enable outputs to a max unpooling layer, the pooling regions of the max pooling layer must be nonoverlapping.
For more information on how to unpool the output of a max pooling
layer, see maxUnpooling2dLayer
.
Layer
Name
— Layer name
""
(default) | character vector | string scalar
NumInputs
— Number of inputs
1
(default)
This property is read-only.
Number of inputs to the layer, returned as 1
. This layer accepts a
single input only.
Data Types: double
InputNames
— Input names
{'in'}
(default)
This property is read-only.
Input names, returned as {'in'}
. This layer accepts a single input
only.
Data Types: cell
NumOutputs
— Number of outputs
1 (default) | 3
Number of outputs of the layer.
If the HasUnpoolingOutputs
value equals false
, then the max pooling layer has a single output with the name 'out'
.
To use the output of a max pooling layer as the input to a max unpooling layer, set the
HasUnpoolingOutputs
value to true
. In this case,
the max pooling layer has two additional outputs that you can connect to a max unpooling
layer:
"indices"
— Indices of the maximum value in each pooled region."size"
— Size of the input feature map.
To enable outputs to a max unpooling layer, the pooling regions of the max pooling layer must be nonoverlapping.
For more information on how to unpool the output of a max pooling
layer, see maxUnpooling2dLayer
.
Data Types: double
OutputNames
— Output names
{'out'}
(default) | {'out','indices','size'}
Output names of the layer.
If the HasUnpoolingOutputs
value equals false
, then the max pooling layer has a single output with the name 'out'
.
To use the output of a max pooling layer as the input to a max unpooling layer, set the
HasUnpoolingOutputs
value to true
. In this case,
the max pooling layer has two additional outputs that you can connect to a max unpooling
layer:
"indices"
— Indices of the maximum value in each pooled region."size"
— Size of the input feature map.
To enable outputs to a max unpooling layer, the pooling regions of the max pooling layer must be nonoverlapping.
For more information on how to unpool the output of a max pooling
layer, see maxUnpooling2dLayer
.
Data Types: cell
Examples
Create Max Pooling Layer with Nonoverlapping Pooling Regions
Create a max pooling layer with nonoverlapping pooling regions.
layer = maxPooling2dLayer(2,'Stride',2)
layer = MaxPooling2DLayer with properties: Name: '' HasUnpoolingOutputs: 0 NumOutputs: 1 OutputNames: {'out'} Hyperparameters PoolSize: [2 2] Stride: [2 2] PaddingMode: 'manual' PaddingSize: [0 0 0 0]
The height and the width of the rectangular regions (pool size) are both 2. The pooling regions do not overlap because the step size for traversing the images vertically and horizontally (stride) is also [2 2]
.
Include a max pooling layer with nonoverlapping regions in a Layer
array.
layers = [ ... imageInputLayer([28 28 1]) convolution2dLayer(5,20) reluLayer maxPooling2dLayer(2,'Stride',2) fullyConnectedLayer(10) softmaxLayer]
layers = 6x1 Layer array with layers: 1 '' Image Input 28x28x1 images with 'zerocenter' normalization 2 '' 2-D Convolution 20 5x5 convolutions with stride [1 1] and padding [0 0 0 0] 3 '' ReLU ReLU 4 '' 2-D Max Pooling 2x2 max pooling with stride [2 2] and padding [0 0 0 0] 5 '' Fully Connected 10 fully connected layer 6 '' Softmax softmax
Create Max Pooling Layer with Overlapping Pooling Regions
Create a max pooling layer with overlapping pooling regions.
layer = maxPooling2dLayer([3 2],Stride=2)
layer = MaxPooling2DLayer with properties: Name: '' HasUnpoolingOutputs: 0 NumOutputs: 1 OutputNames: {'out'} Hyperparameters PoolSize: [3 2] Stride: [2 2] PaddingMode: 'manual' PaddingSize: [0 0 0 0]
This layer creates pooling regions of size [3 2] and takes the maximum of the six elements in each region. The pooling regions overlap because there are stride dimensions Stride
that are less than the respective pooling dimensions PoolSize
.
Include a max pooling layer with overlapping pooling regions in a Layer
array.
layers = [ ...
imageInputLayer([28 28 1])
convolution2dLayer(5,20)
reluLayer
maxPooling2dLayer([3 2],Stride=2)
fullyConnectedLayer(10)
softmaxLayer]
layers = 6x1 Layer array with layers: 1 '' Image Input 28x28x1 images with 'zerocenter' normalization 2 '' 2-D Convolution 20 5x5 convolutions with stride [1 1] and padding [0 0 0 0] 3 '' ReLU ReLU 4 '' 2-D Max Pooling 3x2 max pooling with stride [2 2] and padding [0 0 0 0] 5 '' Fully Connected 10 fully connected layer 6 '' Softmax softmax
Algorithms
2-D Max Pooling Layer
A 2-D max pooling layer performs downsampling by dividing the input into rectangular pooling regions, then computing the maximum of each region.
The dimensions that the layer pools over depends on the layer input:
For 2-D image input (data with four dimensions corresponding to pixels in two spatial dimensions, the channels, and the observations), the layer pools over the spatial dimensions.
For 2-D image sequence input (data with five dimensions corresponding to the pixels in two spatial dimensions, the channels, the observations, and the time steps), the layer pools over the spatial dimensions.
For 1-D image sequence input (data with four dimensions corresponding to the pixels in one spatial dimension, the channels, the observations, and the time steps), the layer pools over the spatial and time dimensions.
Layer Input and Output Formats
Layers in a layer array or layer graph pass data to subsequent layers as formatted dlarray
objects.
The format of a dlarray
object is a string of characters in which each
character describes the corresponding dimension of the data. The formats consist of one or
more of these characters:
"S"
— Spatial"C"
— Channel"B"
— Batch"T"
— Time"U"
— Unspecified
For example, you can describe 2-D image data that is represented as a 4-D array, where the
first two dimensions correspond to the spatial dimensions of the images, the third
dimension corresponds to the channels of the images, and the fourth dimension
corresponds to the batch dimension, as having the format "SSCB"
(spatial, spatial, channel, batch).
You can interact with these dlarray
objects in automatic differentiation
workflows, such as those for developing a custom layer, using a functionLayer
object, or using the forward
and predict
functions with
dlnetwork
objects.
This table shows the supported input formats of MaxPooling2DLayer
objects and the
corresponding output format. If the software passes the output of the layer to a custom
layer that does not inherit from the nnet.layer.Formattable
class, or a
FunctionLayer
object with the Formattable
property
set to 0
(false
), then the layer receives an
unformatted dlarray
object with dimensions ordered according to the formats
in this table. The formats listed here are only a subset. The layer may support additional
formats such as formats with additional "S"
(spatial) or
"U"
(unspecified) dimensions.
Input Format | Output Format |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
When the
HasUnpoolingOutputs
property is 1
(true
), the "size"
output of the layer
outputs unformatted dlarray
objects. (since R2024b)
Before R2024b: The "size"
output of the
layer outputs a numeric array.
References
[1] Nagi, J., F. Ducatelle, G. A. Di Caro, D. Ciresan, U. Meier, A. Giusti, F. Nagi, J. Schmidhuber, L. M. Gambardella. ''Max-Pooling Convolutional Neural Networks for Vision-based Hand Gesture Recognition''. IEEE International Conference on Signal and Image Processing Applications (ICSIPA2011), 2011.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
Code generation does not support passing
dlarray
objects with unspecified (U) dimensions to this layer.If equal max values exists along the off-diagonal in a kernel window, implementation differences for the
maxPooling2dLayer
might cause minor numerical mismatch between MATLAB® and the generated code. This issue also causes mismatch in the indices of the maximum value in each pooled region.For example, consider the following input to
maxPooling2dLayer
. The output from MATLAB is shown.For the same input, the output from the generated code is shown.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
Refer to the usage notes and limitations in the C/C++ Code Generation section. The same limitations apply to GPU code generation.
Version History
Introduced in R2016aR2024b: Layer outputs pooling sizes as dlarray
objects
When the HasUnpoolingOutputs
property is 1
(true
), the "size"
output of the layer
outputs unformatted dlarray
objects. In previous versions, the
"size"
output of the layer outputs a numeric array.
In most cases, there is no change in behavior and you do not need to update your
code. If you have code that relies on the output being a numeric array, then to
extract the numeric data from the dlarray
object, use the extractdata
function.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)