OpenNN
Open-source neural networks library
Loading...
Searching...
No Matches
convolutional_layer.h
Go to the documentation of this file.
1// OpenNN: Open Neural Networks Library
2// www.opennn.net
3//
4// C O N V O L U T I O N A L L A Y E R C L A S S H E A D E R
5//
6// Artificial Intelligence Techniques SL
7// artelnics@artelnics.com
8
9#pragma once
10
11#include "layer.h"
12#include "operators.h"
13
14namespace opennn
15{
16
18class Convolutional final : public Layer
19{
20public:
21
30 Convolutional(const Shape& = {3, 3, 1},
31 const Shape& = {3, 3, 1, 1},
32 const string& = "Identity",
33 const Shape& = {1, 1},
34 const string& = "Valid",
35 bool = false,
36 const string& = "convolutional_layer");
37
39 Shape get_input_shape() const override { return {input_height, input_width, input_channels}; }
40
42 Shape get_output_shape() const override;
43
45 Index get_output_height() const;
46
48 Index get_output_width() const;
49
50 Index get_input_height() const;
51 Index get_input_width() const;
52 Index get_input_channels() const;
53
54 Index get_kernel_height() const { return kernel_height; }
55 Index get_kernel_width() const { return kernel_width; }
56 Index get_kernel_channels() const { return kernel_channels; }
57 Index get_kernels_number() const { return kernels_number; }
58
59 Index get_row_stride() const { return row_stride; }
60 Index get_column_stride() const { return column_stride; }
61
63 pair<Index, Index> get_padding() const;
64
66 Index get_padding_height() const;
67
69 Index get_padding_width() const;
70
71 bool get_use_padding() const { return use_padding; }
72
73 ActivationOp::Function get_activation_function() const { return activation.function; }
74 ActivationOp::Function get_output_activation() const override { return activation.function; }
75
76 bool get_batch_normalization() const { return batch_norm.active(); }
77
79 vector<TensorSpec> get_forward_specs(Index batch_size) const override;
80
82 void set(const Shape& = {0, 0, 0},
83 const Shape& = {3, 3, 1, 1},
84 const string& = "Identity",
85 const Shape& = {1, 1},
86 const string& = "Valid",
87 bool = false,
88 const string& = "convolutional_layer");
89
91 void set_input_shape(const Shape&) override;
92
94 void on_compute_dtype_changed() override { update_convolution_operator(); }
95
96 void set_row_stride(const Index);
97 void set_column_stride(const Index);
98
100 void set_convolution_type(const string&);
101
103 void set_activation_function(const string&);
104
107
109 void read_JSON_body(const Json*) override;
110
112 void write_JSON_body(JsonWriter&) const override;
113
114private:
115
116 Index input_height = 0;
117 Index input_width = 0;
118 Index input_channels = 0;
119
120 Index kernels_number = 0;
121 Index kernel_height = 0;
122 Index kernel_width = 0;
123 Index kernel_channels = 0;
124
125 Index row_stride = 1;
126 Index column_stride = 1;
127
128 bool use_padding = false;
129
130 ConvolutionOp convolution;
131 ActivationOp activation;
132 BatchNormOp batch_norm;
133
134 enum Forward {Input, ConvolutionView, BatchNormMean, BatchNormInverseVariance, Output};
135
136 void update_convolution_operator();
137};
138
139}
140
141// OpenNN: Open Neural Networks Library.
142// Copyright(C) 2005-2026 Artificial Intelligence Techniques, SL.
143// Licensed under the GNU Lesser General Public License v2.1 or later.
ActivationOp::Function get_activation_function() const
Definition convolutional_layer.h:73
Shape get_input_shape() const override
Returns the input tensor shape (height, width, channels).
Definition convolutional_layer.h:39
void set_row_stride(const Index)
void on_compute_dtype_changed() override
Rebuilds the convolution operator when the compute dtype changes.
Definition convolutional_layer.h:94
Shape get_output_shape() const override
Returns the output tensor shape after convolution.
ActivationOp::Function get_output_activation() const override
Returns the layer's output activation (Identity for most layers; overridden by Dense/Bounding).
Definition convolutional_layer.h:74
Index get_kernel_height() const
Definition convolutional_layer.h:54
void set_convolution_type(const string &)
Sets convolution type ("Valid" or "Same") and updates padding.
void read_JSON_body(const Json *) override
Reads the layer configuration from a JSON node.
Index get_output_height() const
Returns the output feature map height.
void set_activation_function(const string &)
Sets the activation function by name.
void set_batch_normalization(bool)
Enables or disables batch normalization after the convolution.
Index get_input_channels() const
bool get_batch_normalization() const
Definition convolutional_layer.h:76
void write_JSON_body(JsonWriter &) const override
Writes the layer configuration to a JSON writer.
Index get_padding_width() const
Returns the padding applied along the width axis.
vector< TensorSpec > get_forward_specs(Index batch_size) const override
Returns the tensor specifications used during forward propagation.
Index get_column_stride() const
Definition convolutional_layer.h:60
void set_column_stride(const Index)
Index get_kernel_channels() const
Definition convolutional_layer.h:56
void set_input_shape(const Shape &) override
Updates the layer for a new input shape and reinitializes derived sizes.
Convolutional(const Shape &={3, 3, 1}, const Shape &={3, 3, 1, 1}, const string &="Identity", const Shape &={1, 1}, const string &="Valid", bool=false, const string &="convolutional_layer")
Constructs a convolutional layer with the given shapes and activation.
Index get_kernels_number() const
Definition convolutional_layer.h:57
Index get_row_stride() const
Definition convolutional_layer.h:59
Index get_input_height() const
Index get_output_width() const
Returns the output feature map width.
void set(const Shape &={0, 0, 0}, const Shape &={3, 3, 1, 1}, const string &="Identity", const Shape &={1, 1}, const string &="Valid", bool=false, const string &="convolutional_layer")
Reconfigures the layer with new shapes and parameters.
bool get_use_padding() const
Definition convolutional_layer.h:71
Index get_padding_height() const
Returns the padding applied along the height axis.
pair< Index, Index > get_padding() const
Returns padding along height and width (in that order).
Index get_input_width() const
Index get_kernel_width() const
Definition convolutional_layer.h:55
Definition json.h:85
Definition json.h:23
Layer()=default
Definition adaptive_moment_estimation.h:14
Element-wise non-linear activation (Identity, Sigmoid, Tanh, ReLU, Softmax).
Definition operators.h:169
Function
Supported activation functions.
Definition operators.h:171
Batch normalization with learnable scale/shift and running statistics for inference.
Definition operators.h:308
2D convolution operator (NHWC layout) backed by Eigen on CPU and cuDNN on GPU.
Definition operators.h:397
Fixed-capacity small-vector describing tensor dimensions (rank up to MaxRank).
Definition tensor_utilities.h:42