Module containing implementation of a 2D convolutional layer
This module implements 2D convolution for processing image-like data. Applies learnable filters to extract spatial features.
Mathematical operation:
where: - are spatial coordinates in the output - is the output channel (filter) index - are kernel offsets - is the input channel index - are kernel dimensions - is the activation function
Shape:
Interface for setting up the 2D convolutional layer
Set up the 2D convolutional layer
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer, | intent(in), | optional, | dimension(:) | :: | input_shape |
Input shape |
| integer, | intent(in), | optional | :: | num_filters |
Number of filters |
|
| integer, | intent(in), | optional, | dimension(..) | :: | kernel_size |
Kernel size |
| integer, | intent(in), | optional, | dimension(..) | :: | stride |
Stride |
| integer, | intent(in), | optional, | dimension(..) | :: | dilation |
Dilation |
| character(len=*), | intent(in), | optional | :: | padding |
Padding method |
|
| logical, | intent(in), | optional | :: | use_bias |
Use bias |
|
| class(*), | intent(in), | optional | :: | activation |
Activation function, kernel initialiser, bias initialiser |
|
| class(*), | intent(in), | optional | :: | kernel_initialiser |
Activation function, kernel initialiser, bias initialiser |
|
| class(*), | intent(in), | optional | :: | bias_initialiser |
Activation function, kernel initialiser, bias initialiser |
|
| integer, | intent(in), | optional | :: | verbose |
Verbosity level |
Instance of the 2D convolutional layer
Type for 2D convolutional layer with overloaded procedures
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| class(base_actv_type), | public, | allocatable | :: | activation |
Activation function |
||
| real(kind=real32), | public, | pointer | :: | bias(:) | => | null() |
Bias pointer |
| class(base_init_type), | public, | allocatable | :: | bias_init |
Initialisers for kernel and bias |
||
| character(len=14), | public | :: | bias_initialiser | = | '' |
Initialisers for kernel and bias |
|
| integer, | public, | allocatable, dimension(:) | :: | bias_shape |
Shape of biases |
||
| class(array_type), | public, | allocatable | :: | di_padded |
Padded input gradients |
||
| integer, | public, | allocatable, dimension(:) | :: | dil |
Kernel, stride, and dilation sizes |
||
| type(graph_type), | public, | allocatable, dimension(:) | :: | graph |
Graph structure of input data |
||
| integer, | public | :: | id |
Unique identifier |
|||
| logical, | public | :: | inference | = | .false. |
Inference mode |
|
| integer, | public | :: | input_rank | = | 0 |
Rank of input data |
|
| integer, | public, | allocatable, dimension(:) | :: | input_shape |
Input shape |
||
| class(base_init_type), | public, | allocatable | :: | kernel_init |
Initialisers for kernel and bias |
||
| character(len=14), | public | :: | kernel_initialiser | = | '' |
Initialisers for kernel and bias |
|
| integer, | public, | allocatable, dimension(:) | :: | knl |
Kernel, stride, and dilation sizes |
||
| character(len=:), | public, | allocatable | :: | name |
Layer name |
||
| integer, | public | :: | num_channels |
Number of channels |
|||
| integer, | public | :: | num_filters |
Number of filters |
|||
| integer, | public | :: | num_params | = | 0 |
Number of learnable parameters |
|
| class(array_type), | public, | allocatable, dimension(:,:) | :: | output |
Output |
||
| integer, | public | :: | output_rank | = | 0 |
Rank of output data |
|
| integer, | public, | allocatable, dimension(:) | :: | output_shape |
Output shape |
||
| class(pad_layer_type), | public, | allocatable | :: | pad_layer |
Optional preprocess padding layer |
||
| type(array_type), | public, | allocatable, dimension(:) | :: | params |
Learnable parameters |
||
| integer, | public, | allocatable, dimension(:) | :: | stp |
Kernel, stride, and dilation sizes |
||
| character(len=20), | public | :: | subtype | = | repeat(" ", 20) | ||
| character(len=4), | public | :: | type | = | 'base' |
Layer type |
|
| logical, | public | :: | use_bias | = | .false. |
Layer has bias |
|
| logical, | public | :: | use_graph_input | = | .false. |
Use graph input |
|
| logical, | public | :: | use_graph_output | = | .false. |
Use graph output |
|
| integer, | public, | allocatable, dimension(:,:) | :: | weight_shape |
Shape of weights |
||
| type(array_type), | public, | dimension(2) | :: | z |
Temporary arrays for forward propagation |
Interface for setting up the 2D convolutional layer
| private module function layer_setup (input_shape, num_filters, kernel_size, stride, dilation, padding, use_bias, activation, kernel_initialiser, bias_initialiser, verbose) | Set up the 2D convolutional layer |
| final :: finalise_conv2d | Finalise 2D convolutional layer |
| procedure, public :: add_t_t => add_learnable | Add two layers |
| procedure, public, pass(this) :: build_from_onnx => build_from_onnx_conv2d | Build 2D convolutional layer from ONNX node and initialiser |
| procedure, public, pass(this) :: emit_onnx_graph_inputs => emit_onnx_graph_inputs_base | Emit graph input tensor declarations for this layer |
| procedure, public, pass(this) :: emit_onnx_nodes => emit_onnx_nodes_base | Emit ONNX JSON nodes for this layer (format-aware and polymorphic) |
| procedure, public, pass(this) :: extract_output => extract_output_base | Extract the output of the layer as a standard real array |
| procedure, public, pass(this) :: forward => forward_conv2d | Forward propagation derived type handler |
| procedure, public, pass(this) :: forward_eval => forward_eval_base | Forward pass of layer and return output for evaluation |
| procedure, public, pass(this) :: get_attributes => get_attributes_conv | Get the attributes of the layer (for ONNX export) |
| procedure, public, pass(this) :: get_gradients | Get parameter gradients of layer |
| procedure, public, pass(this) :: get_num_params => get_num_params_conv | Get the number of parameters in the layer |
| procedure, public, pass(this) :: get_params | Get learnable parameters of layer |
| procedure, public, pass(this) :: init => init_conv | Initialise the layer |
| procedure, public, pass(this) :: nullify_graph => nullify_graph_base | Nullify the forward pass data of the layer to free memory |
| generic, public :: operator(+) => add_t_t | Operator overloading for addition |
| procedure, public, pass(this) :: print => print_base | Print the layer to a file with additional information |
| procedure, public, pass(this) :: print_to_unit => print_to_unit_conv | Print layer to unit |
| procedure, public, pass(this) :: read => read_conv2d | Read 2D convolutional layer from file |
| procedure, public, pass(this) :: reduce => reduce_learnable | Merge another learnable layer into this one |
| procedure, public, pass(this) :: set_gradients | Set learnable parameters of layer |
| procedure, public, pass(this) :: set_graph => set_graph_base | Set the graph structure of the input data !! this is adjacency and edge weighting |
| procedure, public, pass(this) :: set_hyperparams => set_hyperparams_conv2d | Set hyperparameters for 2D convolutional layer |
| procedure, public, pass(this) :: set_params | Set learnable parameters of layer |
| procedure, public, pass(this) :: set_rank => set_rank_base | Set the input and output ranks of the layer |
| procedure, public, pass(this) :: set_shape => set_shape_base | Set the input shape of the layer |
Read 2D convolutional layer from file and return layer
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer, | intent(in) | :: | unit |
Unit number |
||
| integer, | intent(in), | optional | :: | verbose |
Verbosity level |
Instance of the 2D convolutional layer
Set up the 2D convolutional layer
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer, | intent(in), | optional, | dimension(:) | :: | input_shape |
Input shape |
| integer, | intent(in), | optional | :: | num_filters |
Number of filters |
|
| integer, | intent(in), | optional, | dimension(..) | :: | kernel_size |
Kernel size |
| integer, | intent(in), | optional, | dimension(..) | :: | stride |
Stride |
| integer, | intent(in), | optional, | dimension(..) | :: | dilation |
Dilation |
| character(len=*), | intent(in), | optional | :: | padding |
Padding method |
|
| logical, | intent(in), | optional | :: | use_bias |
Use bias |
|
| class(*), | intent(in), | optional | :: | activation |
Activation function |
|
| class(*), | intent(in), | optional | :: | kernel_initialiser |
Activation function, kernel initialiser, and bias initialiser |
|
| class(*), | intent(in), | optional | :: | bias_initialiser |
Activation function, kernel initialiser, and bias initialiser |
|
| integer, | intent(in), | optional | :: | verbose |
Verbosity level |
Instance of the 2D convolutional layer
Read ONNX attributes for 2D convolutional layer
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(conv2d_layer_type), | intent(inout) | :: | this |
Instance of the 2D convolutional layer |
||
| type(onnx_node_type), | intent(in) | :: | node |
ONNX node information |
||
| type(onnx_initialiser_type), | intent(in), | dimension(:) | :: | initialisers |
ONNX initialiser information |
|
| type(onnx_tensor_type), | intent(in), | dimension(:) | :: | value_info |
ONNX value info information |
|
| integer, | intent(in) | :: | verbose |
Verbosity level |
finalise layer ! finalise layer Finalise 2D convolutional layer
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(conv2d_layer_type), | intent(inout) | :: | this |
Instance of the 2D convolutional layer |
Forward propagation
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(conv2d_layer_type), | intent(inout) | :: | this |
Instance of the 2D convolutional layer |
||
| class(array_type), | intent(in), | dimension(:,:) | :: | input |
Input values |
Read 2D convolutional layer from file
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(conv2d_layer_type), | intent(inout) | :: | this |
Instance of the 2D convolutional layer |
||
| integer, | intent(in) | :: | unit |
Unit number |
||
| integer, | intent(in), | optional | :: | verbose |
Verbosity level |
Set hyperparameters for 2D convolutional layer
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(conv2d_layer_type), | intent(inout) | :: | this |
Instance of the 2D convolutional layer |
||
| integer, | intent(in) | :: | num_filters |
Number of filters |
||
| integer, | intent(in), | dimension(2) | :: | kernel_size |
Kernel size, stride, dilation |
|
| integer, | intent(in), | dimension(2) | :: | stride |
Kernel size, stride, dilation |
|
| integer, | intent(in), | dimension(2) | :: | dilation |
Kernel size, stride, dilation |
|
| character(len=*), | intent(in) | :: | padding |
Padding |
||
| logical, | intent(in) | :: | use_bias |
Use bias |
||
| class(base_actv_type), | intent(in), | allocatable | :: | activation |
Activation function |
|
| class(base_init_type), | intent(in), | allocatable | :: | kernel_initialiser |
Kernel and bias initialisers |
|
| class(base_init_type), | intent(in), | allocatable | :: | bias_initialiser |
Kernel and bias initialisers |
|
| integer, | intent(in), | optional | :: | verbose |
Verbosity level |