Deep learning convolution
The convolution operation applies sliding filters to the input data. Use thedlconv
function for deep learning convolution, grouped convolution, and channel-wise separable convolution.
Thedlconv
function applies the deep learning convolution operation todlarray
data.Usingdlarray
objects makes working with high dimensional data easier by allowing you to label the dimensions. For example, you can label which dimensions correspond to spatial, time, channel, and batch dimensions using the"S"
,"T"
,"C"
, and"B"
labels, respectively. For unspecified and other dimensions, use the“U"
label. Fordlarray
object functions that operate over particular dimensions, you can specify the dimension labels by formatting thedlarray
object directly, or by using theDataFormat
option.
Note
To apply convolution within alayerGraph
object orLayer
array, use one of the following layers:
applies the deep learning convolution operation to the formatteddlY
= dlconv(dlX
,weights
,bias
)dlarray
objectdlX
. The function uses sliding convolutional filters defined byweights
and adds the constantbias
. The outputdlY
is a formatteddlarray
object with the same format asdlX
.
The function, by default, convolves over up to three dimensions ofdlX
labeled'S'
(spatial). To convolve over dimensions labeled'T'
(time), specifyweights
with a'T'
dimension using a formatteddlarray
object or by using the'WeightsFormat'
option.
For unformatted input data, use the'DataFormat'
option.
applies the deep learning convolution operation to the unformatteddlY
= dlconv(dlX
,weights
,bias
,'DataFormat',FMT)dlarray
objectdlX
with format specified byFMT
using any of the previous syntaxes. The outputdlY
is an unformatteddlarray
object with dimensions in the same order asdlX
. For example,'DataFormat','SSCB'
specifies data for 2-D convolution with format'SSCB'
(spatial, spatial, channel, batch).
specifies options using one or more name-value pair arguments using any of the previous syntaxes. For example,dlY
= dlconv(___,Name,Value
)'WeightsFormat','TCU'
specifies weights for 1-D convolution with format'TCU'
(time, channel, unspecified).
Create a formatteddlarray
object containing a batch of 128 28-by-28 images with 3 channels. Specify the format'SSCB'
(spatial, spatial, channel, batch).
miniBatchSize = 128; inputSize = [28 28]; numChannels = 3; X = rand(inputSize(1),inputSize(2),numChannels,miniBatchSize); dlX = dlarray(X,'SSCB');
View the size and format of the input data.
size(dlX)
ans =1×428 28 3 128
dims(dlX)
ans = 'SSCB'
Initialize the weights and bias for 2-D convolution. For the weights, specify 64 3-by-3 filters. For the bias, specify a vector of zeros.
filterSize = [3 3]; numFilters = 64; weights = rand(filterSize(1),filterSize(2),numChannels,numFilters); bias = zeros(1,numFilters);
Apply 2-D convolution using thedlconv
function.
dlY = dlconv(dlX,weights,bias);
View the size and format of the output.
size(dlY)
ans =1×426 26 64 128
dims(dlY)
ans = 'SSCB'
Convolve the input data in three groups of two channels each. Apply four filters per group.
Create the input data as 10 observations of size 100-by-100 with six channels.
height = 100; width = 100; channels = 6; numObservations = 10; X = rand(height,width,channels,numObservations); dlX = dlarray(X,'SSCB');
Initialize the convolutional filters. Specify three groups of convolutions that each apply four convolution filters to two channels of the input data.
filterHeight = 8; filterWidth = 8; numChannelsPerGroup = 2; numFiltersPerGroup = 4; numGroups = 3; weights = rand(filterHeight,filterWidth,numChannelsPerGroup,numFiltersPerGroup,numGroups);
Initialize the bias term.
bias = rand(numFiltersPerGroup*numGroups,1);
Perform the convolution.
dlY = dlconv(dlX,weights,bias); size(dlY)
ans =1×493 93 12 10
dims(dlY)
ans = 'SSCB'
The 12 channels of the convolution output represent the three groups of convolutions with four filters per group.
Separate the input data into channels and perform convolution on each channel separately.
Create the input data as a single observation with a size of 64-by-64 and 10 channels. Create the data as an unformatteddlarray
.
height = 64; width = 64; channels = 10; X = rand(height,width,channels); dlX = dlarray(X);
Initialize the convolutional filters. Specify an ungrouped convolution that applies a single convolution to all three channels of the input data.
filterHeight = 8; filterWidth = 8; numChannelsPerGroup = 1; numFiltersPerGroup = 1; numGroups = channels; weights = rand(filterHeight,filterWidth,numChannelsPerGroup,numFiltersPerGroup,numGroups);
Initialize the bias term.
bias = rand(numFiltersPerGroup*numGroups,1);
Perform the convolution. Specify the dimension labels of the input data using the'DataFormat'
option.
dlY = dlconv(dlX,weights,bias,'DataFormat','SSC'); size(dlY)
ans =1×357 57 10
Each channel is convolved separately, so there are 10 channels in the output.
Create a formatteddlarray
object containing 128 sequences of length 512 containing 5 features. Specify the format'CBT'
(channel, batch, time).
numChannels = 5; miniBatchSize = 128; sequenceLength = 512; X = rand(numChannels,miniBatchSize,sequenceLength); dlX = dlarray(X,'CBT');
Initialize the weights and bias for 1-D convolution. For the weights, specify 64 filters with a filter size of 3. For the bias, specify a vector of zeros.
filterSize = 3; numFilters = 64; weights = rand(filterSize,numChannels,numFilters); bias = zeros(1,numFilters);
Apply 1-D convolution using thedlconv
function. To convolve over the'T'
(time) dimension of the input data, specify the weights format'TCU'
(time, channel, unspecified) using the'WeightsFormat'
option.
dlY = dlconv(dlX,weights,bias,'WeightsFormat','TCU');
View the size and format of the output.
size(dlY)
ans =1×364 128 510
dims(dlY)
ans = 'CBT'
dlX
—Input datadlarray
|numeric arrayInput data, specified as a formatteddlarray
, an unformatteddlarray
, or a numeric array.
IfdlX
is an unformatteddlarray
or a numeric array, then you must specify the format using the'DataFormat'
option. IfdlX
is a numeric array, then eitherweights
orbias
must be adlarray
object.
The function, by default, convolves over up to three dimensions ofdlX
labeled'S'
(spatial). To convolve over dimensions labeled'T'
(time), specifyweights
with a'T'
dimension using a formatteddlarray
object or by using the'WeightsFormat'
option.
weights
—Convolutional filtersdlarray
|numeric array卷积过滤器,指定为一个格式化的dlarray
, an unformatteddlarray
, or a numeric array.
The size and format of the weights depends on the type of task. Ifweights
is an unformatteddlarray
or a numeric array, then the size and shape ofweights
depends on the'WeightsFormat'
option.
The following table describes the size and format of the weights for various tasks. You can specify an array with the dimensions in any order using formatteddlarray
objects or by using the'WeightsFormat'
option. When the weights has multiple dimensions with the same label (for example, multiple dimensions labeled'S'
), then those dimensions must be in ordered as described in this table.
Task | Required Dimensions | 大小 | Example | |
---|---|---|---|---|
Weights | Format | |||
1-D convolution | 'S' (spatial) or'T' (time) |
Filter size |
|
'SCU' (spatial, channel, unspecified) |
'C' (channel) |
Number of channels | |||
'U' (unspecified) |
Number of filters | |||
1-D grouped convolution | 'S' (spatial) or'T' (time) |
Filter size |
|
'SCUU' (spatial, channel, unspecified, unspecified) |
'C' (channel) |
Number of channels per group | |||
First'U' (unspecified) |
Number of filters per group | |||
Second'U' (unspecified) |
Number of groups | |||
2-D convolution | First'S' (spatial) |
Filter height |
|
'SSCU' (spatial, spatial, channel, unspecified) |
Second'S' (spatial) or'T' (time) |
Filter width | |||
'C' (channel) |
Number of channels | |||
'U' (unspecified) |
Number of filters | |||
2-D grouped convolution | First'S' (spatial) |
Filter height |
|
'SSCUU' (spatial, spatial, channel, unspecified, unspecified) |
Second'S' (spatial) or'T' (time) |
Filter width | |||
'C' (channel) |
Number of channels per group | |||
First'U' (unspecified) |
Number of filters per group | |||
Second'U' (unspecified) |
Number of groups | |||
3-D convolution | First'S' (spatial) |
Filter height |
|
'SSSCU' (spatial, spatial, spatial, channel, unspecified) |
Second'S' (spatial) |
Filter width | |||
Third'S' (spatial) or'T' (time) |
Filter depth | |||
'C' (channel) |
Number of channels | |||
'U' (unspecified) |
Number of filters |
channel-wise分离(也称为depth-wise separable) convolution, use grouped convolution with number of groups equal to the number of channels.
Tip
The function, by default, convolves over up to three dimensions ofdlX
labeled'S'
(spatial). To convolve over dimensions labeled'T'
(time), specifyweights
with a'T'
dimension using a formatteddlarray
object or by using the'WeightsFormat'
option.
bias
—Bias constantdlarray
|numeric vector|numeric scalarBias constant, specified as a formatteddlarray
, an unformatteddlarray
, a numeric vector, or a numeric scalar.
Ifbias
is a scalar, then the same bias is applied to each output.
Ifbias
has a nonsingleton dimension, then each element ofbias
is the bias applied to the corresponding convolutional filter specified byweights
. The number of elements ofbias
must match the number of filters specified byweights
.
Ifbias
is0
, then the bias term is disabled and no bias is added during the convolution operation.
Ifbias
is a formatteddlarray
, then the nonsingleton dimension must be a channel dimension with label'C'
(channel).
Specify optional comma-separated pairs ofName,Value
arguments.Name
is the argument name andValue
is the corresponding value.Name
must appear inside quotes. You can specify several name and value pair arguments in any order asName1,Value1,...,NameN,ValueN
.
'DilationFactor',2
sets the dilation factor for each convolutional filter to2
.
DataFormat
—Dimension order of unformatted dataDimension order of unformatted input data, specified as a character vector or string scalarFMT
that provides a label for each dimension of the data.
When you specify the format of adlarray
object, each character provides a label for each dimension of the data and must be one of the following:
"S"
— Spatial
"C"
— Channel
"B"
— Batch (for example, samples and observations)
"T"
— Time (for example, time steps of sequences)
“U"
— Unspecified
You can specify multiple dimensions labeled"S"
or“U"
. You can use the labels"C"
,"B"
, and"T"
at most once.
You must specifyDataFormat
当输入哒ta is not a formatteddlarray
.
Data Types:char
|string
WeightsFormat
—Dimension order of weightsDimension order of the weights, specified as the comma-separated pair consisting of'WeightsFormat'
and a character vector or string scalar that provides a label for each dimension of the weights.
The default value of'WeightsFormat'
depends on the task:
Task | Default |
---|---|
1-D convolution | 'SCU' (spatial, channel, unspecified) |
1-D grouped convolution | 'SCUU' (spatial, channel, unspecified, unspecified) |
2-D convolution | 'SSCU' (spatial, spatial, channel, unspecified) |
2-D grouped convolution | 'SSCUU' (spatial, spatial, channel, unspecified, unspecified) |
3-D convolution | 'SSSCU' (spatial, spatial, spatial, channel, unspecified) |
The supported combinations of dimension labels depends on the type of convolution, for more information, see theweights
argument.
Tip
The function, by default, convolves over up to three dimensions ofdlX
labeled'S'
(spatial). To convolve over dimensions labeled'T'
(time), specifyweights
with a'T'
dimension using a formatteddlarray
object or by using the'WeightsFormat'
option.
Example:'WeightsFormat','TCU'
Data Types:char
|string
Stride
—Step size for traversing input data1
(default) |numeric scalar|numeric vectorStep size for traversing the input data, specified as the comma-separated pair consisting of'Stride'
and a numeric scalar or numeric vector. If you specify'Stride'
作为一个标量,相同的值是用于所有spatial dimensions. If you specify'Stride'
as a vector of the same size as the number of spatial dimensions of the input data, the vector values are used for the corresponding spatial dimensions.
The default value of'Stride'
is1
.
Example:'Stride',3
Data Types:single
|double
|int8
|int16
|int32
|int64
|uint8
|uint16
|uint32
|uint64
DilationFactor
—Filter dilation factor1
(default) |numeric scalar|numeric vectorFilter dilation factor, specified as the comma-separated pair consisting of'DilationFactor'
and one of the following.
Numeric scalar — The same dilation factor value is applied for all spatial dimensions.
Numeric vector — A different dilation factor value is applied along each spatial dimension. Use a vector of sized
, whered
is the number of spatial dimensions of the input data. Thei
th element of the vector specifies the dilation factor applied to thei
th spatial dimension.
Use the dilation factor to increase the receptive field of the filter (the area of the input that the filter can see) on the input data. Using a dilation factor corresponds to an effective filter size offilterSize + (filterSize-1)*(dilationFactor-1)
.
Example:'DilationFactor',2
Data Types:single
|double
|int8
|int16
|int32
|int64
|uint8
|uint16
|uint32
|uint64
Padding
—大小of padding0
(default) |'same'
|'causal'
|numeric scalar|numeric vector|numeric matrix大小of padding applied to the'S'
and'T'
dimensions given by the format of the weights, specified as the comma-separated pair consisting of'Padding'
and one of the following:
'same'
— Apply padding such that the output dimension sizes areceil(inputSize/stride)
, whereinputSize
is the size of the corresponding input dimension. WhenStride
is1
, the output is the same size as the input.
'causal'
– Apply left padding with size(FilterSize - 1)
.*DilationFactor
. This option supports convolving over a single time or spatial dimension only. WhenStride
is1
, the output is the same size as the input.
Nonnegative integersz
— Add padding of sizesz
to both ends of the'S'
or'T'
dimensions given by the format of the weights.
Vector of integerssz
— Add padding of sizesz(i)
to both ends of thei
th'S'
or'T'
dimensions given by the format of the weights. The number of elements ofsz
must match the number of'S'
or'T'
dimensions of the weights.
Matrix of integerssz
— Add padding of sizesz(1,i)
andsz(2,i)
to the start and end of thei
th'S'
or'T'
dimensions given by the format of the weights. For example, for 2-D input,[t l; b r]
applies padding of sizet
,b
,l
, andr
to the top, bottom, left, and right of the input, respectively.
Example:'Padding','same'
Data Types:single
|double
|int8
|int16
|int32
|int64
|uint8
|uint16
|uint32
|uint64
|char
|string
PaddingValue
—Value to pad data'symmetric-include-edge'
|'symmetric-exclude-edge'
|'replicate'
Value to pad data, specified as one of the following:
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
dlY
— Convolved feature mapdlarray
Convolved feature map, returned as adlarray
with the same underlying data type asdlX
.
If the input datadlX
is a formatteddlarray
, thendlY
has the same format asdlX
. If the input data is not a formatteddlarray
, thendlY
is an unformatteddlarray
with the same dimension order as the input data.
The size of the'C'
(channel) dimension ofdlY
depends on the task.
Task | 大小of'C' Dimension |
---|---|
Convolution | Number of filters |
Grouped convolution | Number of filters per group multiplied by the number of groups |
Thedlconv
function applies sliding convolution filters to the input data. Thedlconv
function supports convolution in one, two, or three spatial dimensions or one time dimension. To learn more about deep learning convolution, see the definition ofconvolutional layeron theconvolution2dLayer
reference page.
Usage notes and limitations:
When at least one of the following input arguments is agpuArray
or adlarray
with underlying data of typegpuArray
, this function runs on the GPU.
dlX
weights
bias
For more information, seeRun MATLAB Functions on a GPU(Parallel Computing Toolbox).
dlarray
|batchnorm
|fullyconnect
|maxpool
|relu
|dlgradient
|dlfeval
You have a modified version of this example. Do you want to open this example with your edits?
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.
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:.
Selectweb siteYou can also select a web site from the following list:
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.