Max pooling layer
A max pooling layer performs down-sampling by dividing the input into rectangular pooling regions, and computing the maximum of each region.
creates a max pooling layer and sets the layer
= maxPooling2dLayer(poolSize
)PoolSize
property.
sets the optional layer
= maxPooling2dLayer(poolSize
,Name,Value
)Stride
, Name
,
and HasUnpoolingOutputs
properties using name-value pairs. To
specify input padding, use the 'Padding'
name-value pair
argument. For example, maxPooling2dLayer(2,'Stride',3)
creates a max pooling layer with pool size [2 2]
and stride
[3 3]
. You can specify multiple name-value pairs. Enclose
each property name in single quotes.
Use comma-separated name-value pair arguments to specify the size of the
padding to add along the edges of the layer input and to set the Stride
, Name
, and HasUnpoolingOutputs
properties. Enclose names in single
quotes.
maxPooling2dLayer(2,'Stride',3)
creates a max
pooling layer with pool size [2 2]
and stride [3
3]
.'Padding'
— Input edge padding[0 0 0 0]
(default) | vector of nonnegative integers | 'same'
Input edge padding, specified as the comma-separated pair consisting of
'Padding'
and 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 is
ceil(inputSize/stride)
, where inputSize
is the height
or width of the input and stride
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 size
p
to all the edges of the input.
Vector [a b]
of nonnegative integers — Add padding of size
a
to the top and bottom of the input and padding of size
b
to the left and right.
Vector [t b l r]
of nonnegative integers — Add padding of
size t
to the top, b
to the bottom,
l
to the left, and r
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).
PoolSize
— Dimensions of pooling regionsDimensions 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 integersStep 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 integersSize 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 sets PaddingMode
to
'manual'
.
If you set the 'Padding'
option to
'same'
, then the software automatically sets
PaddingMode
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 is
ceil(inputSize/stride)
, where inputSize
is the height
or width of the input and stride
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 integersNote
Padding
property will be removed in a future release. Use
PaddingSize
instead. When creating a layer, use the
'Padding'
name-value pair 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 layerfalse
(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
Layer name, specified as a character vector or a string scalar.
To include a layer in a layer graph, you must specify a nonempty unique layer name. If you train
a series network with the layer and Name
is set to ''
,
then the software automatically assigns a name to the layer at training time.
Data Types: char
| string
NumInputs
— Number of inputsNumber of inputs of the layer. This layer accepts a single input only.
Data Types: double
InputNames
— Input names{'in'}
(default)Input names of the layer. This layer accepts a single input only.
Data Types: cell
NumOutputs
— Number of outputsNumber 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
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 classificationLayer]
layers = 7x1 Layer array with layers: 1 '' Image Input 28x28x1 images with 'zerocenter' normalization 2 '' Convolution 20 5x5 convolutions with stride [1 1] and padding [0 0 0 0] 3 '' ReLU ReLU 4 '' 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 7 '' Classification Output crossentropyex
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 classificationLayer]
layers = 7x1 Layer array with layers: 1 '' Image Input 28x28x1 images with 'zerocenter' normalization 2 '' Convolution 20 5x5 convolutions with stride [1 1] and padding [0 0 0 0] 3 '' ReLU ReLU 4 '' 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 7 '' Classification Output crossentropyex
A max pooling layer performs down-sampling by dividing the input into rectangular pooling regions, and computing the maximum of each region.
Pooling layers follow the convolutional layers for down-sampling, hence, reducing the number of connections to the following layers. They do not perform any learning themselves, but reduce the number of parameters to be learned in the following layers. They also help reduce overfitting.
A max pooling layer returns the maximum values of rectangular regions of its input. The size of the rectangular regions is determined by the poolSize
argument of maxPoolingLayer
. For example, if poolSize
equals [2,3]
, then the layer returns the maximum value in regions of height 2 and width 3.
Pooling layers scan through the input horizontally and vertically in step sizes you can specify using the 'Stride'
name-value pair argument. If the pool size is smaller than or equal to the stride, then the pooling regions do not overlap.
For nonoverlapping regions (Pool Size and Stride are equal), if the input to the pooling layer is n-by-n, and the pooling region size is h-by-h, then the pooling layer down-samples the regions by h [1]. That is, the output of a max or average pooling layer for one channel of a convolutional layer is n/h-by-n/h. For overlapping regions, the output of a pooling layer is (Input Size – Pool Size + 2*Padding)/Stride + 1.
[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.
averagePooling2dLayer
| convolution2dLayer
| globalAveragePooling2dLayer
| maxUnpooling2dLayer
You have a modified version of this example. Do you want to open this example with your edits?