Main Content

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

Description

example

layer = groupedConvolution2dLayer(filterSize,numFiltersPerGroup,numGroups) creates a 2-D grouped convolutional layer and sets the FilterSize, NumFiltersPerGroup, and NumGroups properties.

example

layer = groupedConvolution2dLayer(filterSize,numFiltersPerGroup,'channel-wise') creates a layer for channel-wise convolution (also known as depth-wise convolution). In this case, the software determines the NumGroups property at training time. This syntax is equivalent to setting NumGroups to the number of input channels.

example

layer = groupedConvolution2dLayer(___,Name,Value) sets the optional 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

expand all

Name-Value 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.

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).

Properties

expand all

Grouped Convolution

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.

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

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

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.

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]

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.

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.

Value to pad data, specified as one of the following:

PaddingValueDescriptionExample
ScalarPad with the specified scalar value.

[314159265][0000000000000000314000015900002650000000000000000]

'symmetric-include-edge'Pad using mirrored values of the input, including the edge values.

[314159265][5115995133144113314415115995622655662265565115995]

'symmetric-exclude-edge'Pad using mirrored values of the input, excluding the edge values.

[314159265][5626562951595141314139515951562656295159514131413]

'replicate'Pad using repeated border elements of the input

[314159265][3331444333144433314441115999222655522265552226555]

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | char | string

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

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 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

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), where sz is the size of the biases.

The layer initializes the biases only when the Bias property is empty.

Data Types: char | string | function_handle

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 and trainNetwork functions use 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

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 and trainNetwork functions use 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

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

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

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

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

Layer name, specified as a character vector or a string scalar. For Layer array input, the trainnet and dlnetwork functions automatically assign names to layers with the name "".

The GroupedConvolution2DLayer object stores this property as a character vector.

Data Types: char | string

This property is read-only.

Number of inputs to the layer, returned as 1. This layer accepts a single input only.

Data Types: double

This property is read-only.

Input names, returned as {'in'}. This layer accepts a single input only.

Data Types: cell

This property is read-only.

Number of outputs from the layer, returned as 1. This layer has a single output only.

Data Types: double

This property is read-only.

Output names, returned as {'out'}. This layer has a single output only.

Data Types: cell

Examples

collapse all

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 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.

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

expand all

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

Version History

Introduced in R2019a