2-D grouped convolutional layer
A 2-D grouped convolutional layer separates the input channels into groups and applies sliding convolutional filters. Use grouped convolutional layers for channel-wise separable (also known as depth-wise separable) convolution.
For each group, the layer convolves the input by moving the filters along the input vertically and horizontally and computing the dot product of the weights and the input, and then adding a bias term. The layer combines the convolutions for each group independently. If the number of groups is equal to the number of channels, then this layer performs channel-wise convolution.
creates a 2-D grouped convolutional layer and sets the layer
= groupedConvolution2dLayer(filterSize
,numFiltersPerGroup
,numGroups
)FilterSize
, NumFiltersPerGroup
, and NumGroups
properties.
creates a layer for channel-wise convolution (also known as depth-wise convolution). In
this case, the software determines the layer
= groupedConvolution2dLayer(filterSize
,numFiltersPerGroup
,'channel-wise')NumGroups
property at training time. This syntax is equivalent to setting
NumGroups
to the number of input channels.
sets the optional layer
= groupedConvolution2dLayer(___,Name,Value
)Stride
, DilationFactor
, Parameters and Initialization,
Learn Rate and Regularization, and Name
properties using name-value pairs. To specify input
padding, use the 'Padding'
name-value pair argument. For example,
groupedConvolution2dLayer(5,128,2,'Padding','same')
creates a 2-D
grouped convolutional layer with 2 groups of 128 filters of size [5 5]
and pads the input to so that the output has the same size. 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 zero padding
to add along the edges of the layer input or to set the Stride
, DilationFactor
, Parameters and Initialization,
Learn Rate and Regularization, and Name
properties. Enclose names in single quotes.
groupedConvolution2dLayer(5,128,2,'Padding','same')
creates
a 2-D grouped convolutional layer with 2 groups of 128 filters of size [5
5]
and pads the input to so that the output has the same size.'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).
FilterSize
— Height and width of filtersHeight and width of the filters, specified as a vector [h w]
of two positive integers, where h
is the height and w
is the width. FilterSize
defines the size of the local regions to which the neurons connect in the input.
When creating the layer, you can specify FilterSize
as a scalar to use the same value for the height and width.
Example: [5 5]
specifies filters with a height of 5 and a width of 5.
NumFiltersPerGroup
— Number of filters per groupNumber of filters per group, specified as a positive integer. This property
determines the number of channels in the output of the layer. The number of output
channels is FiltersPerGroup * NumGroups
.
Example:
10
NumGroups
— Number of groups'channel-wise'
Number of groups, specified as a positive integer or
'channel-wise'
.
If NumGroups
is 'channel-wise'
, then the
software creates a layer for channel-wise convolution (also known as depth-wise
convolution). In this case, the layer determines the NumGroups
property at training time. This value is equivalent to setting
NumGroups
to the number of input channels.
The number of groups must evenly divide the number of channels of the layer input.
Example:
2
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
[a b]
of two positive integers, 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 step sizes.
Example:
[2 3]
specifies a vertical step size of 2 and a horizontal step size
of 3.
DilationFactor
— Factor for dilated convolution[1 1]
(default) | vector of two positive integersFactor for dilated convolution (also known as atrous convolution), specified as a vector [h w]
of two positive integers, where h
is the vertical dilation and w
is the horizontal dilation. When creating the layer, you can specify DilationFactor
as a scalar to use the same value for both horizontal and vertical dilations.
Use dilated convolutions to increase the receptive field (the area of the input which the layer can see) of the layer without increasing the number of parameters or computation.
The layer expands the filters by inserting zeros between each filter element. The dilation
factor determines the step size for sampling the input or equivalently the upsampling factor
of the filter. It corresponds to an effective filter size of (Filter Size
– 1) .* Dilation Factor + 1. For example, a 3-by-3 filter with the
dilation factor [2 2]
is equivalent to a 5-by-5 filter with zeros between
the elements.
Example: [2 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.
NumChannelsPerGroup
— Number of channels per group'auto'
(default) | positive integerNumber of channels per group, specified as 'auto'
or a positive
integer. The number of channels per group is equal to the number of input channels
divided by the number of groups.
The software automatically sets this property at training time.
Example:
256
WeightsInitializer
— Function to initialize weights'glorot'
(default) | 'he'
| 'narrow-normal'
| 'zeros'
| 'ones'
| function handleFunction to initialize the weights, specified as one of the following:
'glorot'
– Initialize the weights with the Glorot
initializer [1] (also known as Xavier
initializer). The Glorot initializer independently samples from a uniform
distribution with zero mean and variance 2/(numIn + numOut)
,
where numIn = FilterSize(1)*FilterSize(2)*NumChannelsPerGroup
and numOut =
FilterSize(1)*FilterSize(2)*NumFiltersPerGroup
.
'he'
– Initialize the weights with the He initializer
[2]. The He initializer
samples from a normal distribution with zero mean and variance
2/numIn
, where numIn =
FilterSize(1)*FilterSize(2)*NumChannelsPerGroup
.
'narrow-normal'
– Initialize the weights by independently
sampling from a normal distribution with zero mean and standard deviation
0.01.
'zeros'
– Initialize the weights with zeros.
'ones'
– Initialize the weights with ones.
Function handle – Initialize the weights with a custom function. If you
specify a function handle, then the function must be of the form
weights = func(sz)
, where sz
is the size
of the weights. For an example, see Specify Custom Weight Initialization Function.
The layer only initializes the weights when the Weights
property is empty.
Data Types: char
| string
| function_handle
BiasInitializer
— Function to initialize bias'zeros'
(default) | 'narrow-normal'
| 'ones'
| function handleFunction to initialize the bias, specified as one of the following:
'zeros'
– Initialize the bias with zeros.
'ones'
– Initialize the bias with ones.
'narrow-normal'
– Initialize the bias by independently
sampling from a normal distribution with zero mean and standard deviation
0.01.
Function handle – Initialize the bias with a custom function. If you specify a function handle, then the function must be of the form bias = func(sz)
, where sz
is the size of the bias.
The layer only initializes the bias when the Bias
property is
empty.
Data Types: char
| string
| function_handle
Weights
— Layer weights[]
(default) | numeric arrayLayer weights for the layer, specified as a numeric array.
The layer weights are learnable parameters. You can specify the
initial value for the weights directly using the Weights
property of the layer. When training a network, if the Weights
property of the layer is nonempty, then trainNetwork
uses the Weights
property as the
initial value. If the Weights
property is empty, then
trainNetwork
uses the initializer specified by the WeightsInitializer
property of the layer.
At training time, Weights
is a
FilterSize(1)
-by-FilterSize(2)
-by-NumChannelsPerGroup
-by-NumFiltersPerGroup
-by-NumGroups
array, where NumInputChannels
is the number of channels of the
layer input.
Data Types: single
| double
Bias
— Layer biases[]
(default) | numeric arrayLayer biases for the layer, specified as a numeric array.
The layer biases are learnable parameters. When training a network, if Bias
is nonempty, then trainNetwork
uses the Bias
property as the initial value. If Bias
is empty, then trainNetwork
uses the initializer specified by BiasInitializer
.
At training time, Bias
is a
1-by-1-by-NumFiltersPerGroup
-by-NumGroups
array.
Data Types: single
| double
WeightLearnRateFactor
— Learning rate factor for weightsLearning rate factor for the weights, specified as a nonnegative scalar.
The software multiplies this factor by the global learning rate to determine the
learning rate for the weights in this layer. For example, if
WeightLearnRateFactor
is 2, then the learning rate for the
weights in this layer is twice the current global learning rate. The software determines
the global learning rate based on the settings specified with the trainingOptions
function.
Example:
2
BiasLearnRateFactor
— Learning rate factor for biasesLearning rate factor for the biases, specified as a nonnegative scalar.
The software multiplies this factor by the global learning rate
to determine the learning rate for the biases in this layer. For example, if
BiasLearnRateFactor
is 2, then the learning rate for the biases in the
layer is twice the current global learning rate. The software determines the global learning
rate based on the settings specified with the trainingOptions
function.
Example:
2
WeightL2Factor
— L2 regularization factor for weightsL2 regularization factor for the weights, specified as a nonnegative scalar.
The software multiplies this factor by the global L2 regularization factor to determine the L2
regularization for the weights in this layer. For example, if
WeightL2Factor
is 2, then the L2 regularization for the weights
in this layer is twice the global L2 regularization factor. You can specify the global
L2 regularization factor using the trainingOptions
function.
Example:
2
BiasL2Factor
— L2 regularization factor for biasesL2 regularization factor for the biases, specified as a nonnegative scalar.
The software multiplies this factor by the global L2
regularization factor to determine the L2 regularization for the biases in this layer. For
example, if BiasL2Factor
is 2, then the L2 regularization for the biases in
this layer is twice the global L2 regularization factor. You can specify the global L2
regularization factor using the trainingOptions
function.
Example:
2
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. This layer has a single output only.
Data Types: double
OutputNames
— Output names{'out'}
(default)Output names of the layer. This layer has a single output only.
Data Types: cell
Create a grouped convolutional layer with 3 groups of 10 filters, each with a height and width of 11, and the name 'gconv1'
.
layer = groupedConvolution2dLayer(11,10,3,'Name','gconv1')
layer = GroupedConvolution2DLayer with properties: Name: 'gconv1' Hyperparameters FilterSize: [11 11] NumGroups: 3 NumChannelsPerGroup: 'auto' NumFiltersPerGroup: 10 Stride: [1 1] DilationFactor: [1 1] PaddingMode: 'manual' PaddingSize: [0 0 0 0] Learnable Parameters Weights: [] Bias: [] Show all properties
Create a channel-wise convolutional (also known as depth-wise convolutional) layer with groups of 10 filters, each with a height and width of 11, and the name 'cwconv1'
.
layer = groupedConvolution2dLayer(11,10,'channel-wise','Name','cwconv1')
layer = GroupedConvolution2DLayer with properties: Name: 'cwconv1' Hyperparameters FilterSize: [11 11] NumGroups: 'channel-wise' NumChannelsPerGroup: 'auto' NumFiltersPerGroup: 10 Stride: [1 1] DilationFactor: [1 1] PaddingMode: 'manual' PaddingSize: [0 0 0 0] Learnable Parameters Weights: [] Bias: [] Show all properties
A typical convolutional neural network contains blocks of convolution, batch normalization, and ReLU layers. For example,
filterSize = 3; numFilters = 16; convLayers = [ convolution2dLayer(filterSize,numFilters,'Stride',2,'Padding','same') batchNormalizationLayer reluLayer];
For channel-wise separable convolution (also known as depth-wise separable convolution), replace the convolution block with channel-wise convolution and point-wise convolution blocks.
Specify the filter size and the stride in the channel-wise convolution and the number of filters in the point-wise convolution. For the channel-wise convolution, specify one filter per group. For point-wise convolution, specify filters of size 1 in convolution2dLayer
.
cwsConvLayers = [ groupedConvolution2dLayer(filterSize,1,'channel-wise','Stride',2,'Padding','same') batchNormalizationLayer reluLayer convolution2dLayer(1,numFilters,'Padding','same') batchNormalizationLayer reluLayer];
Create a network containing layers for channel-wise separable convolution.
layers = [ imageInputLayer([227 227 3]) convolution2dLayer(3,32,'Padding','same') batchNormalizationLayer reluLayer groupedConvolution2dLayer(3,1,'channel-wise','Stride',2,'Padding','same') batchNormalizationLayer reluLayer convolution2dLayer(1,16,'Padding','same') batchNormalizationLayer reluLayer maxPooling2dLayer(2,'Stride',2) fullyConnectedLayer(5) softmaxLayer classificationLayer];
[1] Glorot, Xavier, and Yoshua Bengio. "Understanding the difficulty of training deep feedforward neural networks." In Proceedings of the thirteenth international conference on artificial intelligence and statistics, pp. 249-256. 2010.
[2] He, Kaiming, Xiangyu Zhang, Shaoqing Ren, and Jian Sun. "Delving deep into rectifiers: Surpassing human-level performance on imagenet classification." In Proceedings of the IEEE international conference on computer vision, pp. 1026-1034. 2015.
Usage notes and limitations:
Code generation for the ARM Compute Library is not supported for a 2-D grouped
convolution layer that has the NumGroups
property set to an integer
value greater than two.
Usage notes and limitations:
Code generation for the ARM Mali GPU is not supported for a 2-D grouped
convolution layer that has the NumGroups
property set as
'channel-wise'
or a value greater than two.
batchNormalizationLayer
| convolution2dLayer
| fullyConnectedLayer
| maxPooling2dLayer
| reluLayer
| trainNetwork
You have a modified version of this example. Do you want to open this example with your edits?