groupedConvolution2dLayer
2-D grouped convolutional layer
Description
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.
Creation
Syntax
Description
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, Learning 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.
Input Arguments
Use comma-separated name-value pair arguments to specify the size of the
padding to add along the edges of the layer input or to set the Stride
, DilationFactor
, Parameters and Initialization, Learning Rate and Regularization, and
Name
properties. Enclose names in single
quotes.
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.
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).
Properties
Grouped Convolution
FilterSize
— Height and width of filters
vector of two positive integers
Height 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 you create 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 group
positive integer
Number 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
positive integer |
'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 integers
Step 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 integers
Factor 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 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.
PaddingValue
— Value to pad data
0 (default) | scalar | "symmetric-include-edge"
| "symmetric-exclude-edge"
| "replicate"
Value to pad data, specified as one of these values:
PaddingValue | Description | Example |
---|---|---|
Scalar | Pad with the specified scalar value. |
|
"symmetric-include-edge" | Pad using mirrored values of the input, including the edge values. |
|
"symmetric-exclude-edge" | Pad using mirrored values of the input, excluding the edge values. |
|
"replicate" | Pad using repeated border elements of the input |
|
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| char
| string
NumChannelsPerGroup
— Number of channels per group
'auto'
(default) | positive integer
Number 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
Parameters and Initialization
WeightsInitializer
— Function to initialize weights
'glorot'
(default) |
'he'
|
'narrow-normal'
|
'zeros'
|
'ones'
| function handle
Function 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 variance2/(numIn + numOut)
, wherenumIn = FilterSize(1)*FilterSize(2)*NumChannelsPerGroup
andnumOut = 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 variance2/numIn
, wherenumIn = 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)
, wheresz
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 biases
"zeros"
(default) | "narrow-normal"
| "ones"
| function handle
Function to initialize the biases, specified as one of these values:
"zeros"
— Initialize the biases with zeros."ones"
— Initialize the biases with ones."narrow-normal"
— Initialize the biases by independently sampling from a normal distribution with a mean of zero and a standard deviation of 0.01.Function handle — Initialize the biases with a custom function. If you specify a function handle, then the function must have the form
bias = func(sz)
, wheresz
is the size of the biases.
The layer initializes the biases only when the Bias
property is
empty.
The GroupedConvolution2DLayer
object stores this property as a character vector or a
function handle.
Data Types: char
| string
| function_handle
Weights
— Layer weights
[]
(default) | numeric array
Layer weights for the layer, specified as a numeric array.
The layer weights are learnable parameters. You can specify the initial value of the weights
directly using the Weights
property of the layer. When
you train a network, if the Weights
property of the layer
is nonempty, then the trainnet
function uses the Weights
property as the initial value.
If the Weights
property is empty, then the software 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 array
Layer biases for the layer, specified as a numeric array.
The layer biases are learnable parameters. When you train a neural network, if Bias
is nonempty, then the trainnet
function uses the Bias
property as the initial value. If
Bias
is empty, then software uses the initializer
specified by BiasInitializer
.
At training time, Bias
is a
1-by-1-by-NumFiltersPerGroup
-by-NumGroups
array.
Data Types: single
| double
Learning Rate and Regularization
WeightLearnRateFactor
— Learning rate factor for weights
1
(default) | nonnegative scalar
Learning 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 you specify using the trainingOptions
function.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
BiasLearnRateFactor
— Learning rate factor for biases
1
(default) | nonnegative scalar
Learning 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 you specify using the trainingOptions
function.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
WeightL2Factor
— L2 regularization factor for
weights
1 (default) | nonnegative scalar
L2 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.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
BiasL2Factor
— L2 regularization factor for biases
0
(default) | nonnegative scalar
L2 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. The software determines the global L2 regularization factor based on the settings you specify using the trainingOptions
function.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
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)
This property is read-only.
Number of outputs from the layer, returned as 1
. This layer has a
single output only.
Data Types: double
OutputNames
— Output names
{'out'}
(default)
This property is read-only.
Output names, returned as {'out'}
. This layer has a single output
only.
Data Types: cell
Examples
Create Grouped Convolution Layer
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] PaddingValue: 0 Learnable Parameters Weights: [] Bias: [] Use properties method to see a list of all properties.
Create Channel-Wise Convolution Layer
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] PaddingValue: 0 Learnable Parameters Weights: [] Bias: [] Use properties method to see a list of all properties.
Create Layers for Channel-Wise Separable Convolution
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];
Algorithms
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 GroupedConvolution2DLayer
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 |
---|---|
|
|
|
|
|
|
|
|
References
[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, 249–356. Sardinia, Italy: AISTATS, 2010. https://proceedings.mlr.press/v9/glorot10a/glorot10a.pdf
[2] He, Kaiming, Xiangyu Zhang, Shaoqing Ren, and Jian Sun. "Delving Deep into Rectifiers: Surpassing Human-Level Performance on ImageNet Classification." In 2015 IEEE International Conference on Computer Vision (ICCV), 1026–34. Santiago, Chile: IEEE, 2015. https://doi.org/10.1109/ICCV.2015.123
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
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.For code generation, the
PaddingValue
parameter must be equal to0
, which is the default value.Code generation does not support passing
dlarray
objects with unspecified (U) dimensions to this layer.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
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.For code generation, the
PaddingValue
parameter must be equal to0
, which is the default value.Code generation does not support passing
dlarray
objects with unspecified (U) dimensions to this layer.
Version History
Introduced in R2019a
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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)