public abstract class Convolution extends ParameterBlock
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 |
dilate |
protected boolean |
includeBias |
protected Shape |
kernel |
protected int |
numFilters |
protected int |
numGroups |
protected Shape |
pad |
protected Shape |
stride |
protected Parameter |
weight |
inputNames, inputShapes
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.
|
java.util.List<Parameter> |
getDirectParameters()
Returns a list of all the direct parameters of the block.
|
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.
|
Shape |
getParameterShape(java.lang.String name,
Shape[] inputShapes)
Returns the shape of the specified direct parameter of this block given the shapes of the
input to the block.
|
protected abstract java.lang.String |
getStringLayout()
Returns the string representing the layout of the input.
|
void |
loadParameters(NDManager manager,
java.io.DataInputStream is)
Loads the parameters from the given input stream.
|
protected abstract int |
numDimensions()
Returns the number of dimensions of the input.
|
void |
saveParameters(java.io.DataOutputStream os)
Writes the parameters of the block to the given outputStream.
|
getChildren, initialize, toString
cast, clear, describeInput, getParameters, isInitialized, readInputShapes, saveInputShapes, setInitializer, setInitializer
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
forward, validateLayout
protected Shape kernel
protected Shape stride
protected Shape pad
protected Shape dilate
protected int numFilters
protected int numGroups
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 Shape getParameterShape(java.lang.String name, Shape[] inputShapes)
name
- the name of the parameterinputShapes
- the shapes of the input to the blockpublic java.util.List<Parameter> getDirectParameters()
Parameter
public void saveParameters(java.io.DataOutputStream os) throws java.io.IOException
os
- the outputstream to save the parameters tojava.io.IOException
- if an I/O error occurspublic void loadParameters(NDManager manager, java.io.DataInputStream is) throws java.io.IOException, MalformedModelException
manager
- an NDManager to create the parameter arraysis
- the inputstream that stream the parameter valuesjava.io.IOException
- if an I/O error occursMalformedModelException
- if the model file is corrupted or unsupported