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
9#pragma once
10
11#include "layer.h"
12#include "operators.h"
13
14namespace opennn
15{
16
17// Convolutional + ReLU fused into a single forward op on GPU
18// (cudnnConvolutionBiasActivationForward with CUDNN_ACTIVATION_RELU). On CPU
19// the activation runs as a separate step. No batch-norm, activation hard-wired
20// to ReLU — keeps forward_propagate branch-free for CUDA Graph capture.
21
23class ConvolutionalRelu final : public Layer
24{
25public:
26
33 ConvolutionalRelu(const Shape& = {3, 3, 1},
34 const Shape& = {3, 3, 1, 1},
35 const Shape& = {1, 1},
36 const string& = "Valid",
37 const string& = "convolutional_relu_layer");
38
40 Shape get_input_shape() const override { return {input_height, input_width, input_channels}; }
41
43 Shape get_output_shape() const override;
44
46 Index get_output_height() const;
47
49 Index get_output_width() const;
50
51 Index get_input_height() const { return input_height; }
52 Index get_input_width() const { return input_width; }
53 Index get_input_channels() const { return input_channels; }
54
55 Index get_kernel_height() const { return kernel_height; }
56 Index get_kernel_width() const { return kernel_width; }
57 Index get_kernel_channels() const { return kernel_channels; }
58 Index get_kernels_number() const { return kernels_number; }
59
60 Index get_row_stride() const { return row_stride; }
61 Index get_column_stride() const { return column_stride; }
62
63 pair<Index, Index> get_padding() const { return {get_padding_height(), get_padding_width()}; }
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
74
76 void set(const Shape& = {0, 0, 0},
77 const Shape& = {3, 3, 1, 1},
78 const Shape& = {1, 1},
79 const string& = "Valid",
80 const string& = "convolutional_relu_layer");
81
83 void set_input_shape(const Shape&) override;
84
86 void on_compute_dtype_changed() override { update_convolution_operator(); }
87
88 void set_row_stride(const Index);
89 void set_column_stride(const Index);
90
92 void set_convolution_type(const string&);
93
95 void read_JSON_body(const Json*) override;
96
98 void write_JSON_body(JsonWriter&) const override;
99
100private:
101
102 Index input_height = 0;
103 Index input_width = 0;
104 Index input_channels = 0;
105
106 Index kernels_number = 0;
107 Index kernel_height = 0;
108 Index kernel_width = 0;
109 Index kernel_channels = 0;
110
111 Index row_stride = 1;
112 Index column_stride = 1;
113
114 bool use_padding = false;
115
116 ConvolutionReluOp convolution_relu;
117
118 void update_convolution_operator();
119};
120
121}
122
123// OpenNN: Open Neural Networks Library.
124// Copyright(C) 2005-2026 Artificial Intelligence Techniques, SL.
125// Licensed under the GNU Lesser General Public License v2.1 or later.
Index get_padding_height() const
Returns the padding applied along the height axis.
Shape get_input_shape() const override
Returns the input tensor shape (height, width, channels).
Definition convolutional_relu_layer.h:40
Shape get_output_shape() const override
Returns the output tensor shape after the fused conv+ReLU.
pair< Index, Index > get_padding() const
Definition convolutional_relu_layer.h:63
void set(const Shape &={0, 0, 0}, const Shape &={3, 3, 1, 1}, const Shape &={1, 1}, const string &="Valid", const string &="convolutional_relu_layer")
Reconfigures the layer with new shapes and convolution settings.
void set_input_shape(const Shape &) override
Updates the layer for a new input shape and reinitializes derived sizes.
Index get_kernel_height() const
Definition convolutional_relu_layer.h:55
ActivationOp::Function get_output_activation() const override
Returns the layer's output activation (Identity for most layers; overridden by Dense/Bounding).
Definition convolutional_relu_layer.h:73
Index get_row_stride() const
Definition convolutional_relu_layer.h:60
void set_row_stride(const Index)
Index get_input_height() const
Definition convolutional_relu_layer.h:51
Index get_output_width() const
Returns the output feature map width.
Index get_input_width() const
Definition convolutional_relu_layer.h:52
void set_column_stride(const Index)
Index get_kernel_width() const
Definition convolutional_relu_layer.h:56
void write_JSON_body(JsonWriter &) const override
Writes the layer configuration to a JSON writer.
void read_JSON_body(const Json *) override
Reads the layer configuration from a JSON node.
Index get_kernels_number() const
Definition convolutional_relu_layer.h:58
Index get_padding_width() const
Returns the padding applied along the width axis.
void on_compute_dtype_changed() override
Rebuilds the fused convolution operator when the compute dtype changes.
Definition convolutional_relu_layer.h:86
ConvolutionalRelu(const Shape &={3, 3, 1}, const Shape &={3, 3, 1, 1}, const Shape &={1, 1}, const string &="Valid", const string &="convolutional_relu_layer")
Constructs a fused convolution+ReLU layer.
Index get_column_stride() const
Definition convolutional_relu_layer.h:61
void set_convolution_type(const string &)
Sets convolution type ("Valid" or "Same") and updates padding.
Index get_output_height() const
Returns the output feature map height.
Index get_kernel_channels() const
Definition convolutional_relu_layer.h:57
Index get_input_channels() const
Definition convolutional_relu_layer.h:53
bool get_use_padding() const
Definition convolutional_relu_layer.h:71
Definition json.h:85
Definition json.h:23
Layer()=default
Definition adaptive_moment_estimation.h:14
Function
Supported activation functions.
Definition operators.h:171
@ ReLU
Definition operators.h:171
Fused 2D convolution + ReLU activation (uses cuDNN fused epilogue on GPU).
Definition operators.h:497
Fixed-capacity small-vector describing tensor dimensions (rank up to MaxRank).
Definition tensor_utilities.h:42