public abstract class Convolution extends AbstractBlock
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).
Modifier and Type | Class and Description |
---|---|
static class |
Convolution.ConvolutionBuilder<T extends Convolution.ConvolutionBuilder>
A builder that can build any
Convolution block. |
Modifier and Type | Field and Description |
---|---|
protected Parameter |
bias |
protected Shape |
dilation |
protected int |
filters |
protected int |
groups |
protected boolean |
includeBias |
protected Shape |
kernelShape |
protected Shape |
padding |
protected Shape |
stride |
protected Parameter |
weight |
children, inputNames, inputShapes, parameters, parameterShapeCallbacks, version
Constructor and Description |
---|
Convolution(Convolution.ConvolutionBuilder<?> builder)
Creates a
Convolution object. |
Modifier and Type | Method and Description |
---|---|
protected void |
beforeInitialize(Shape[] inputs)
Performs any action necessary before initialization.
|
NDList |
forward(ParameterStore parameterStore,
NDList inputs,
boolean training,
ai.djl.util.PairList<java.lang.String,java.lang.Object> params)
Applies the operating function of the block once.
|
protected abstract LayoutType[] |
getExpectedLayout()
Returns the expected layout of the input.
|
Shape[] |
getOutputShapes(NDManager manager,
Shape[] inputs)
Returns the expected output shapes of the block for the specified input shapes.
|
protected abstract java.lang.String |
getStringLayout()
Returns the string representing the layout of the input.
|
void |
loadMetadata(byte version,
java.io.DataInputStream is)
Overwrite this to load additional metadata with the parameter values.
|
protected abstract int |
numDimensions()
Returns the number of dimensions of the input.
|
addChildBlock, addParameter, addParameter, addParameter, cast, clear, describeInput, getChildren, getDirectParameters, getParameters, getParameterShape, initialize, initializeChildBlocks, isInitialized, loadParameters, readInputShapes, saveInputShapes, saveMetadata, saveParameters, setInitializer, setInitializer, toString
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
forward, forward, validateLayout
protected Shape kernelShape
protected Shape stride
protected Shape padding
protected Shape dilation
protected int filters
protected int groups
protected boolean includeBias
protected Parameter weight
protected Parameter bias
public Convolution(Convolution.ConvolutionBuilder<?> builder)
Convolution
object.builder
- the Builder
that has the necessary configurationsprotected abstract LayoutType[] getExpectedLayout()
protected abstract java.lang.String getStringLayout()
protected abstract int numDimensions()
public NDList forward(ParameterStore parameterStore, NDList inputs, boolean training, ai.djl.util.PairList<java.lang.String,java.lang.Object> params)
parameterStore
- the parameter storeinputs
- the input NDListtraining
- true for a training forward passparams
- optional parametersprotected void beforeInitialize(Shape[] inputs)
beforeInitialize
in class AbstractBlock
inputs
- the expected shapes of the inputpublic Shape[] getOutputShapes(NDManager manager, Shape[] inputs)
manager
- an NDManagerinputs
- the shapes of the inputspublic void loadMetadata(byte version, java.io.DataInputStream is) throws java.io.IOException, MalformedModelException
If you overwrite AbstractBlock.saveMetadata(DataOutputStream)
or need to provide
backward compatibility to older binary formats, you prabably need to overwrite this. This
default implementation checks if the version number fits, if not it throws an MalformedModelException
. After that it restores the input shapes.
loadMetadata
in class AbstractBlock
version
- the version used for loading this metadata.is
- the input stream we are loading fromjava.io.IOException
- loading failedMalformedModelException
- data can be loaded but has wrong format