Class Convolution
- All Implemented Interfaces:
Block
While convolution process itself has been around for quite some time in mathematics, in 1998 LeCun et al. implemented the very first convolution layers forming a network called LeNet-5 for character recognition task; details of the network's implementation can be find in LeNet-5's paper. When other approaches at that time used handcrafted features with external stage of feature extraction, convolution layer performed feature extraction on its own with no human interference. This marks a new era of machine-extracted features, but it was not until 2012 that the published paper of AlexNet marked the beginning of convolutional neural networks, which by the name itself heavily relies on convolution layer.
Convolution layer is usually used in image-related tasks due to its well-renowned performance as shown by existing works and currently, other non-image-related fields of study are beginning to incorporate convolution layer as an addition or replacement of previous approaches, with one example being time series processing with 1-dimensional convolution layer. Due to the nature of convolution that processes all points in the input data, it is computationally expensive, hence the use of GPU is strongly recommended for faster performance as opposed to using CPU. Note that it is also common to stack convolution layers with different output channels for more representations of the input data.
Current implementations of Convolution
are Conv1d
with input dimension of
LayoutType.WIDTH
, Conv2d
with input dimension of LayoutType.WIDTH
and
LayoutType.HEIGHT
, and lastly Conv3d
with input dimension of LayoutType.WIDTH
, LayoutType.HEIGHT
, and LayoutType.DEPTH
. These implementations
share the same core principal as a Convolution
layer does, with the difference being the
number of input dimension each operates on as denoted by ConvXD
for X
dimension(s).
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
A builder that can build anyConvolution
block. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Parameter
protected Shape
protected int
protected int
protected boolean
protected Shape
protected Shape
protected Shape
protected Parameter
Fields inherited from class ai.djl.nn.AbstractBlock
children, parameters
Fields inherited from class ai.djl.nn.AbstractBaseBlock
inputNames, inputShapes, outputDataTypes, version
-
Constructor Summary
ConstructorsConstructorDescriptionConvolution
(Convolution.ConvolutionBuilder<?> builder) Creates aConvolution
object. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
beforeInitialize
(Shape... inputShapes) Performs any action necessary before initialization.protected NDList
forwardInternal
(ParameterStore parameterStore, NDList inputs, boolean training, ai.djl.util.PairList<String, Object> params) A helper forBlock.forward(ParameterStore, NDList, boolean, PairList)
after initialization.Returns the dilation along each dimension.protected abstract LayoutType[]
Returns the expected layout of the input.int
Returns the required number of filters.int
Returns the number of group partitions.Returns the shape of the kernel.Shape[]
getOutputShapes
(Shape[] inputs) Returns the expected output shapes of the block for the specified input shapes.Returns the padding along each dimension.Returns the stride of the convolution.protected abstract String
Returns the string representing the layout of the input.boolean
Returns whether to include a bias vector.void
loadMetadata
(byte loadVersion, DataInputStream is) Overwrite this to load additional metadata with the parameter values.protected abstract int
Returns the number of dimensions of the input.protected void
Sets the shape ofParameter
s.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, getCustomMetadata, getOutputShapes
-
Field Details
-
kernelShape
-
stride
-
padding
-
dilation
-
filters
protected int filters -
groups
protected int groups -
includeBias
protected boolean includeBias -
weight
-
bias
-
-
Constructor Details
-
Convolution
Creates aConvolution
object.- Parameters:
builder
- theBuilder
that has the necessary configurations
-
-
Method Details
-
getExpectedLayout
Returns the expected layout of the input.- Returns:
- the expected layout of the input
-
getStringLayout
Returns the string representing the layout of the input.- Returns:
- the string representing the layout of the input
-
numDimensions
protected abstract int numDimensions()Returns the number of dimensions of the input.- Returns:
- the number of dimensions of the input
-
forwardInternal
protected NDList forwardInternal(ParameterStore parameterStore, NDList inputs, boolean training, ai.djl.util.PairList<String, Object> params) A helper forBlock.forward(ParameterStore, NDList, boolean, PairList)
after initialization.- Specified by:
forwardInternal
in classAbstractBaseBlock
- Parameters:
parameterStore
- the parameter storeinputs
- the input NDListtraining
- true for a training forward passparams
- optional parameters- Returns:
- the output of the forward pass
-
beforeInitialize
Performs any action necessary before initialization. For example, keep the input information or verify the layout.- Overrides:
beforeInitialize
in classAbstractBaseBlock
- Parameters:
inputShapes
- the expected shapes of the input
-
prepare
Sets the shape ofParameter
s.- Overrides:
prepare
in classAbstractBaseBlock
- Parameters:
inputs
- the shapes of inputs
-
getOutputShapes
Returns the expected output shapes of the block for the specified input shapes.- Parameters:
inputs
- the shapes of the inputs- Returns:
- the expected output shapes of the block
-
loadMetadata
public void loadMetadata(byte loadVersion, DataInputStream is) throws IOException, MalformedModelException Overwrite this to load additional metadata with the parameter values.If you overwrite
AbstractBaseBlock.saveMetadata(DataOutputStream)
or need to provide backward compatibility to older binary formats, you probably need to overwrite this. This default implementation checks if the version number fits, if not it throws anMalformedModelException
. After that it restores the input shapes.- Overrides:
loadMetadata
in classAbstractBaseBlock
- Parameters:
loadVersion
- the version used for loading this metadata.is
- the input stream we are loading from- Throws:
IOException
- loading failedMalformedModelException
- data can be loaded but has wrong format
-
getKernelShape
Returns the shape of the kernel.- Returns:
- the shape of the kernel
-
getStride
Returns the stride of the convolution.- Returns:
- the stride of the convolution
-
getPadding
Returns the padding along each dimension.- Returns:
- the padding along each dimension
-
getDilation
Returns the dilation along each dimension.- Returns:
- the dilation along each dimension
-
getFilters
public int getFilters()Returns the required number of filters.- Returns:
- the required number of filters
-
getGroups
public int getGroups()Returns the number of group partitions.- Returns:
- the number of group partitions
-
isIncludeBias
public boolean isIncludeBias()Returns whether to include a bias vector.- Returns:
- whether to include a bias vector
-