Class Convolution
- java.lang.Object
-
- ai.djl.nn.AbstractBaseBlock
-
- ai.djl.nn.AbstractBlock
-
- ai.djl.nn.convolutional.Convolution
-
- All Implemented Interfaces:
Block
public abstract class Convolution extends AbstractBlock
A convolution layer does a dot product calculation on each channel of \(k\)-channel input data by specified number of filters, each containing \(k\) kernels for calculating each channel in the input data and then summed per filter, hence the number of filters denote the number of output channels of a convolution layer. Some modifications may be set on a convolution layer, namely stride which shows the distance between each convolved input data in a channel, and padding which shows the preservation of input size (width and/or height and/or depth) by adding specified padding to the sides of the output. A convolution layer extracts features of input data with different representations where each representation lies per channel in the output, often known as feature map or feature vector.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
ConvolutionareConv1dwith input dimension ofLayoutType.WIDTH,Conv2dwith input dimension ofLayoutType.WIDTHandLayoutType.HEIGHT, and lastlyConv3dwith input dimension ofLayoutType.WIDTH,LayoutType.HEIGHT, andLayoutType.DEPTH. These implementations share the same core principal as aConvolutionlayer does, with the difference being the number of input dimension each operates on as denoted byConvXDforXdimension(s).- See Also:
- The D2L chapters on convolution
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classConvolution.ConvolutionBuilder<T extends Convolution.ConvolutionBuilder>A builder that can build anyConvolutionblock.
-
Field Summary
Fields Modifier and Type Field Description protected Parameterbiasprotected Shapedilationprotected intfiltersprotected intgroupsprotected booleanincludeBiasprotected ShapekernelShapeprotected Shapepaddingprotected Shapestrideprotected Parameterweight-
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 Constructor Description Convolution(Convolution.ConvolutionBuilder<?> builder)Creates aConvolutionobject.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected voidbeforeInitialize(Shape... inputShapes)Performs any action necessary before initialization.protected NDListforwardInternal(ParameterStore parameterStore, NDList inputs, boolean training, ai.djl.util.PairList<java.lang.String,java.lang.Object> params)A helper forBlock.forward(ParameterStore, NDList, boolean, PairList)after initialization.ShapegetDilation()Returns the dilation along each dimension.protected abstract LayoutType[]getExpectedLayout()Returns the expected layout of the input.intgetFilters()Returns the required number of filters.intgetGroups()Returns the number of group partitions.ShapegetKernelShape()Returns the shape of the kernel.Shape[]getOutputShapes(Shape[] inputs)Returns the expected output shapes of the block for the specified input shapes.ShapegetPadding()Returns the padding along each dimension.ShapegetStride()Returns the stride of the convolution.protected abstract java.lang.StringgetStringLayout()Returns the string representing the layout of the input.booleanisIncludeBias()Returns whether to include a bias vector.voidloadMetadata(byte loadVersion, java.io.DataInputStream is)Overwrite this to load additional metadata with the parameter values.protected abstract intnumDimensions()Returns the number of dimensions of the input.protected voidprepare(Shape[] inputs)Sets the shape ofParameters.-
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
-
-
-
-
Field Detail
-
kernelShape
protected Shape kernelShape
-
stride
protected Shape stride
-
padding
protected Shape padding
-
dilation
protected Shape dilation
-
filters
protected int filters
-
groups
protected int groups
-
includeBias
protected boolean includeBias
-
weight
protected Parameter weight
-
bias
protected Parameter bias
-
-
Constructor Detail
-
Convolution
public Convolution(Convolution.ConvolutionBuilder<?> builder)
Creates aConvolutionobject.- Parameters:
builder- theBuilderthat has the necessary configurations
-
-
Method Detail
-
getExpectedLayout
protected abstract LayoutType[] getExpectedLayout()
Returns the expected layout of the input.- Returns:
- the expected layout of the input
-
getStringLayout
protected abstract java.lang.String 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<java.lang.String,java.lang.Object> params)
A helper forBlock.forward(ParameterStore, NDList, boolean, PairList)after initialization.- Specified by:
forwardInternalin 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
protected void beforeInitialize(Shape... inputShapes)
Performs any action necessary before initialization. For example, keep the input information or verify the layout.- Overrides:
beforeInitializein classAbstractBaseBlock- Parameters:
inputShapes- the expected shapes of the input
-
prepare
protected void prepare(Shape[] inputs)
Sets the shape ofParameters.- Overrides:
preparein classAbstractBaseBlock- Parameters:
inputs- the shapes of inputs
-
getOutputShapes
public Shape[] getOutputShapes(Shape[] inputs)
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, java.io.DataInputStream is) throws java.io.IOException, MalformedModelExceptionOverwrite 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:
loadMetadatain classAbstractBaseBlock- Parameters:
loadVersion- the version used for loading this metadata.is- the input stream we are loading from- Throws:
java.io.IOException- loading failedMalformedModelException- data can be loaded but has wrong format
-
getKernelShape
public Shape getKernelShape()
Returns the shape of the kernel.- Returns:
- the shape of the kernel
-
getStride
public Shape getStride()
Returns the stride of the convolution.- Returns:
- the stride of the convolution
-
getPadding
public Shape getPadding()
Returns the padding along each dimension.- Returns:
- the padding along each dimension
-
getDilation
public Shape 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
-
-