Package ai.djl.nn.convolutional
Class Conv2d
java.lang.Object
ai.djl.nn.AbstractBaseBlock
ai.djl.nn.AbstractBlock
ai.djl.nn.convolutional.Convolution
ai.djl.nn.convolutional.Conv2d
- All Implemented Interfaces:
Block
Being the pioneer of convolution layers,
Conv2d
layer works on two dimensions of input,
LayoutType.WIDTH
and LayoutType.HEIGHT
as usually a Conv2d
layer is used
to process data with two spatial dimensions, namely image. The concept itself works just as how
Convolution
does, and each filter slides through an input data by two directions, first
traversing the LayoutType.WIDTH
then traverses each row of the data.
First proposed by LeCun et al.'s paper, 2-dimensional convolution
layer gained its rising interest with the publication of
paper about AlexNet for image classification task. It is still commonly used in image-related
tasks and adapted in other tasks, including but not limited to 1-dimensional data which may be
transformed to 2-dimensional data, though Conv1d
is now available for use.
The input to a Conv2d
is an NDList
with a single 4-D NDArray
. The layout of the NDArray
must be "NCHW". The
shapes are
data: (batch_size, channel, height, width)
weight: (num_filter, channel, kernel[0], kernel[1])
bias: (num_filter,)
out: (batch_size, num_filter, out_height, out_width)
out_height = f(height, kernel[0], pad[0], stride[0], dilate[0])
out_width = f(width, kernel[1], pad[1], stride[1], dilate[1])
where f(x, k, p, s, d) = floor((x + 2 * p - d * (k - 1) - 1)/s) + 1
Both weight
and bias
are learn-able parameters.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Nested classes/interfaces inherited from class ai.djl.nn.convolutional.Convolution
Convolution.ConvolutionBuilder<T extends Convolution.ConvolutionBuilder>
-
Field Summary
Fields inherited from class ai.djl.nn.convolutional.Convolution
bias, dilation, filters, groups, includeBias, kernelShape, padding, stride, weight
Fields inherited from class ai.djl.nn.AbstractBlock
children, parameters
Fields inherited from class ai.djl.nn.AbstractBaseBlock
inputNames, inputShapes, outputDataTypes, version
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Conv2d.Builder
builder()
Creates a builder to build aConv2d
.static NDList
Applies 2D convolution over an input signal composed of several input planes.static NDList
Applies 2D convolution over an input signal composed of several input planes.static NDList
Applies 2D convolution over an input signal composed of several input planes.static NDList
Applies 2D convolution over an input signal composed of several input planes.static NDList
Applies 2D convolution over an input signal composed of several input planes.static NDList
conv2d
(NDArray input, NDArray weight, NDArray bias, Shape stride, Shape padding, Shape dilation, int groups) Applies 2D convolution over an input signal composed of several input planes.protected LayoutType[]
Returns the expected layout of the input.protected String
Returns the string representing the layout of the input.protected int
Returns the number of dimensions of the input.Methods inherited from class ai.djl.nn.convolutional.Convolution
beforeInitialize, forwardInternal, getDilation, getFilters, getGroups, getKernelShape, getOutputShapes, getPadding, getStride, isIncludeBias, loadMetadata, prepare
Methods inherited from class ai.djl.nn.AbstractBlock
addChildBlock, addChildBlock, addChildBlockSingleton, addParameter, getChildren, getDirectParameters
Methods inherited from class ai.djl.nn.AbstractBaseBlock
cast, clear, describeInput, forward, forward, forwardInternal, getInputShapes, getOutputDataTypes, getParameters, initialize, initializeChildBlocks, isInitialized, loadParameters, readInputShapes, saveInputShapes, saveMetadata, saveParameters, setInitializer, setInitializer, setInitializer, toString
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface ai.djl.nn.Block
forward, freezeParameters, freezeParameters, getOutputShapes
-
Constructor Details
-
Conv2d
-
-
Method Details
-
getExpectedLayout
Returns the expected layout of the input.- Specified by:
getExpectedLayout
in classConvolution
- Returns:
- the expected layout of the input
-
getStringLayout
Returns the string representing the layout of the input.- Specified by:
getStringLayout
in classConvolution
- Returns:
- the string representing the layout of the input
-
numDimensions
protected int numDimensions()Returns the number of dimensions of the input.- Specified by:
numDimensions
in classConvolution
- Returns:
- the number of dimensions of the input
-
conv2d
Applies 2D convolution over an input signal composed of several input planes.- Parameters:
input
- the inputNDArray
of shape (batchSize, inputChannel, height, width)weight
- filtersNDArray
of shape (outChannel, inputChannel/groups, height, width)- Returns:
- the output of the conv2d operation
-
conv2d
Applies 2D convolution over an input signal composed of several input planes.- Parameters:
input
- the inputNDArray
of shape (batchSize, inputChannel, height, width)weight
- filtersNDArray
of shape (outChannel, inputChannel/groups, height, width)bias
- biasNDArray
of shape (outChannel)- Returns:
- the output of the conv2d operation
-
conv2d
Applies 2D convolution over an input signal composed of several input planes.- Parameters:
input
- the inputNDArray
of shape (batchSize, inputChannel, height, width)weight
- filtersNDArray
of shape (outChannel, inputChannel/groups, height, width)bias
- biasNDArray
of shape (outChannel)stride
- the stride of the convolving kernel: Shape(height, width)- Returns:
- the output of the conv2d operation
-
conv2d
public static NDList conv2d(NDArray input, NDArray weight, NDArray bias, Shape stride, Shape padding) Applies 2D convolution over an input signal composed of several input planes.- Parameters:
input
- the inputNDArray
of shape (batchSize, inputChannel, height, width)weight
- filtersNDArray
of shape (outChannel, inputChannel/groups, height, width)bias
- biasNDArray
of shape (outChannel)stride
- the stride of the convolving kernel: Shape(height, width)padding
- implicit paddings on both sides of the input: Shape(height, width)- Returns:
- the output of the conv2d operation
-
conv2d
public static NDList conv2d(NDArray input, NDArray weight, NDArray bias, Shape stride, Shape padding, Shape dilation) Applies 2D convolution over an input signal composed of several input planes.- Parameters:
input
- the inputNDArray
of shape (batchSize, inputChannel, height, width)weight
- filtersNDArray
of shape (outChannel, inputChannel/groups, height, width)bias
- biasNDArray
of shape (outChannel)stride
- the stride of the convolving kernel: Shape(height, width)padding
- implicit paddings on both sides of the input: Shape(height, width)dilation
- the spacing between kernel elements: Shape(height, width)- Returns:
- the output of the conv2d operation
-
conv2d
public static NDList conv2d(NDArray input, NDArray weight, NDArray bias, Shape stride, Shape padding, Shape dilation, int groups) Applies 2D convolution over an input signal composed of several input planes.- Parameters:
input
- the inputNDArray
of shape (batchSize, inputChannel, height, width)weight
- filtersNDArray
of shape (outChannel, inputChannel/groups, height, width)bias
- biasNDArray
of shape (outChannel)stride
- the stride of the convolving kernel: Shape(height, width)padding
- implicit paddings on both sides of the input: Shape(height, width)dilation
- the spacing between kernel elements: Shape(height, width)groups
- split input into groups: input channel(input.size(1)) should be divisible by the number of groups- Returns:
- the output of the conv2d operation
-
builder
Creates a builder to build aConv2d
.- Returns:
- a new builder
-