OpenNN
Open-source neural networks library
Loading...
Searching...
No Matches
convolutional_relu_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 R E L U 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
15
16#pragma once
17
18#include "layer.h"
19#include "operators.h"
20
21namespace opennn
22{
23
36class ConvolutionalRelu final : public Layer
37{
38public:
39
48 ConvolutionalRelu(const Shape& input_shape = {3, 3, 1},
49 const Shape& kernel_shape = {3, 3, 1, 1},
50 const Shape& strides = {1, 1},
51 const string& convolution_type = "Valid",
52 const string& label = "convolutional_relu_layer");
53
55 Shape get_input_shape() const override { return {input_height, input_width, input_channels}; }
56
61 Shape get_output_shape() const override;
62
64 Index get_output_height() const;
66 Index get_output_width() const;
67
69 Index get_input_height() const { return input_height; }
71 Index get_input_width() const { return input_width; }
73 Index get_input_channels() const { return input_channels; }
74
76 Index get_kernel_height() const { return kernel_height; }
78 Index get_kernel_width() const { return kernel_width; }
80 Index get_kernel_channels() const { return kernel_channels; }
82 Index get_kernels_number() const { return kernels_number; }
83
85 Index get_row_stride() const { return row_stride; }
87 Index get_column_stride() const { return column_stride; }
88
93 pair<Index, Index> get_padding() const { return {get_padding_height(), get_padding_width()}; }
95 Index get_padding_height() const;
97 Index get_padding_width() const;
98
100 bool get_use_padding() const { return use_padding; }
101
104
106 vector<Operator*> get_operators() override { return {&convolution}; }
107
113 vector<pair<Shape, Type>> get_forward_specs(Index batch_size) const override;
114
123 void set(const Shape& input_shape = {0, 0, 0},
124 const Shape& kernel_shape = {3, 3, 1, 1},
125 const Shape& strides = {1, 1},
126 const string& convolution_type = "Valid",
127 const string& label = "convolutional_relu_layer");
128
130 void set_input_shape(const Shape&) override;
132 void on_compute_dtype_changed() override { update_convolution_operator(); }
133
138 void set_row_stride(const Index new_row_stride);
143 void set_column_stride(const Index new_column_stride);
150 void set_convolution_type(const string&);
151
158 void forward_propagate(ForwardPropagation&, size_t, bool) noexcept override;
166 void back_propagate(ForwardPropagation&, BackPropagation&, size_t) const noexcept override;
167
172 void read_JSON_body(const Json*) override;
177 void write_JSON_body(JsonWriter&) const override;
178
179private:
180
182 Index input_height = 0;
184 Index input_width = 0;
186 Index input_channels = 0;
187
189 Index kernels_number = 0;
191 Index kernel_height = 0;
193 Index kernel_width = 0;
195 Index kernel_channels = 0;
196
198 Index row_stride = 1;
200 Index column_stride = 1;
201
203 bool use_padding = false;
204
206 Convolution convolution;
208 Activation activation;
209
211 enum Forward {Input, Output};
213 enum Backward {OutputDelta, InputDelta};
214
216 void update_convolution_operator();
217};
218
219}
220
221// OpenNN: Open Neural Networks Library.
222// Copyright(C) 2005-2026 Artificial Intelligence Techniques, SL.
223// Licensed under the GNU Lesser General Public License v2.1 or later.
Index get_padding_height() const
Padding rows added on each side.
Shape get_input_shape() const override
Returns the per-sample input shape (height, width, channels).
Definition convolutional_relu_layer.h:55
void back_propagate(ForwardPropagation &, BackPropagation &, size_t) const noexcept override
Backward pass through the fused ReLU and Convolution operators.
Activation::Function get_output_activation() const override
Activation function fused at the end of this layer (always ReLU).
Definition convolutional_relu_layer.h:103
Shape get_output_shape() const override
Returns the per-sample output shape.
pair< Index, Index > get_padding() const
Returns padding sizes for the configured convolution type.
Definition convolutional_relu_layer.h:93
void set_input_shape(const Shape &) override
Updates the input shape and re-shapes kernel tensors accordingly.
Index get_kernel_height() const
Kernel height in pixels.
Definition convolutional_relu_layer.h:76
Index get_row_stride() const
Vertical stride in pixels.
Definition convolutional_relu_layer.h:85
Index get_input_height() const
Configured input height.
Definition convolutional_relu_layer.h:69
void set(const Shape &input_shape={0, 0, 0}, const Shape &kernel_shape={3, 3, 1, 1}, const Shape &strides={1, 1}, const string &convolution_type="Valid", const string &label="convolutional_relu_layer")
Re-initializes the layer; same arguments as the constructor.
Index get_output_width() const
Output spatial width after applying stride and padding.
void set_row_stride(const Index new_row_stride)
Sets the vertical (row) stride.
Index get_input_width() const
Configured input width.
Definition convolutional_relu_layer.h:71
Index get_kernel_width() const
Kernel width in pixels.
Definition convolutional_relu_layer.h:78
void write_JSON_body(JsonWriter &) const override
Writes the layer-specific JSON body (kernel shape, strides, padding) to the given JSON writer.
void read_JSON_body(const Json *) override
Reads the layer-specific JSON body (kernel shape, strides, padding) from the given JSON node.
Index get_kernels_number() const
Number of kernels (output channel count).
Definition convolutional_relu_layer.h:82
Index get_padding_width() const
Padding columns added on each side.
void set_column_stride(const Index new_column_stride)
Sets the horizontal (column) stride.
vector< pair< Shape, Type > > get_forward_specs(Index batch_size) const override
Specifications of the forward intermediate buffers.
void on_compute_dtype_changed() override
Recreates the convolution operator descriptor when the dtype changes.
Definition convolutional_relu_layer.h:132
Index get_column_stride() const
Horizontal stride in pixels.
Definition convolutional_relu_layer.h:87
void set_convolution_type(const string &)
Sets the padding mode by name.
Index get_output_height() const
Output spatial height after applying stride and padding.
ConvolutionalRelu(const Shape &input_shape={3, 3, 1}, const Shape &kernel_shape={3, 3, 1, 1}, const Shape &strides={1, 1}, const string &convolution_type="Valid", const string &label="convolutional_relu_layer")
Constructs a ConvolutionalRelu layer.
vector< Operator * > get_operators() override
Returns the single operator (Convolution with fused ReLU on GPU).
Definition convolutional_relu_layer.h:106
Index get_kernel_channels() const
Kernel channel count (must equal input channels).
Definition convolutional_relu_layer.h:80
Index get_input_channels() const
Configured input channel count.
Definition convolutional_relu_layer.h:73
bool get_use_padding() const
True if padding is applied (i.e. convolution type is not "Valid").
Definition convolutional_relu_layer.h:100
void forward_propagate(ForwardPropagation &, size_t, bool) noexcept override
Forward pass: convolution + ReLU fused on GPU, sequential on CPU.
Definition json.h:84
Definition json.h:22
Layer()=default
Default constructor; only invoked by subclasses.
string label
User-visible label for this layer instance (default "my_layer").
Definition layer.h:469
Declares the Layer abstract base class and the LayerType enumeration.
Definition adaptive_moment_estimation.h:19
Definition operators.h:107
Function
Definition operators.h:108
@ ReLU
Definition operators.h:108
Definition back_propagation.h:26
Definition operators.h:275
Definition forward_propagation.h:19
Definition tensor_utilities.h:46