OpenNN
Open-source neural networks library
Loading...
Searching...
No Matches
pooling_layer.h
Go to the documentation of this file.
1// OpenNN: Open Neural Networks Library
2// www.opennn.net
3//
4// P O O L I N G 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
23
24inline const string& pooling_method_to_string(PoolingMethod method)
25{
26 static const string max_str = "MaxPooling";
27 static const string avg_str = "AveragePooling";
28 return method == PoolingMethod::MaxPooling ? max_str : avg_str;
29}
30
31inline PoolingMethod string_to_pooling_method(const string& name)
32{
33 if (name == "MaxPooling") return PoolingMethod::MaxPooling;
34 if (name == "AveragePooling") return PoolingMethod::AveragePooling;
35 throw runtime_error(format("Unknown pooling method: {}", name));
36}
37
39class Pooling final : public Layer
40{
41public:
42
50 Pooling(const Shape& = {2, 2, 1},
51 const Shape& = { 2, 2 },
52 const Shape& = { 2, 2 },
53 const Shape& = { 0, 0 },
54 const string& = "MaxPooling",
55 const string& = "pooling_layer");
56
58 Shape get_input_shape() const override { return {input_height, input_width, input_channels}; }
59
61 Shape get_output_shape() const override;
62
64 Index get_output_height() const;
65
67 Index get_output_width() const;
68
69 Index get_input_height() const { return input_height; }
70 Index get_input_width() const { return input_width; }
71 Index get_input_channels() const { return input_channels; }
72
73 Index get_pool_height() const { return pool_height; }
74 Index get_pool_width() const { return pool_width; }
75
76 Index get_row_stride() const { return row_stride; }
77 Index get_column_stride() const { return column_stride; }
78
79 Index get_padding_height() const { return padding_height; }
80 Index get_padding_width() const { return padding_width; }
81
82 PoolingMethod get_pooling_method() const { return pooling_method; }
83
85 vector<TensorSpec> get_forward_specs(Index batch_size) const override;
86
88 void set(const Shape& = { 0, 0, 0 },
89 const Shape& = { 1, 1 },
90 const Shape& = { 1, 1 },
91 const Shape& = { 0, 0 },
92 const string & = "MaxPooling",
93 const string & = "pooling_layer");
94
96 void set_input_shape(const Shape&) override;
97
99 void set_pool_size(Index, Index);
100
101 void set_row_stride(Index);
102 void set_column_stride(Index);
104 void set_padding_width(Index);
105
107 void set_pooling_method(const string&);
108
110 void read_JSON_body(const Json*) override;
111
113 void write_JSON_body(JsonWriter&) const override;
114
115private:
116
117 Index input_height = 0;
118 Index input_width = 0;
119 Index input_channels = 0;
120
121 Index pool_height = 1;
122 Index pool_width = 1;
123
124 Index padding_height = 0;
125 Index padding_width = 0;
126
127 Index row_stride = 1;
128 Index column_stride = 1;
129
131
132 PoolOp pool;
133
134 enum Forward {Input, MaximalIndices, Output};
135
136 void update_pool_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.
Definition json.h:85
Definition json.h:23
Layer()=default
Index get_input_channels() const
Definition pooling_layer.h:71
void set_column_stride(Index)
Shape get_input_shape() const override
Returns the input tensor shape (height, width, channels).
Definition pooling_layer.h:58
void read_JSON_body(const Json *) override
Reads the layer configuration from a JSON node.
void set_padding_width(Index)
Index get_column_stride() const
Definition pooling_layer.h:77
Index get_pool_width() const
Definition pooling_layer.h:74
void set_pooling_method(const string &)
Sets the pooling method by name ("MaxPooling" or "AveragePooling").
Index get_output_height() const
Returns the output feature map height.
Pooling(const Shape &={2, 2, 1}, const Shape &={ 2, 2 }, const Shape &={ 2, 2 }, const Shape &={ 0, 0 }, const string &="MaxPooling", const string &="pooling_layer")
Constructs a pooling layer with given input, pool, stride and padding shapes.
void set(const Shape &={ 0, 0, 0 }, const Shape &={ 1, 1 }, const Shape &={ 1, 1 }, const Shape &={ 0, 0 }, const string &="MaxPooling", const string &="pooling_layer")
Reconfigures the layer with new shapes and pooling method.
void set_input_shape(const Shape &) override
Updates the layer for a new input shape.
void set_pool_size(Index, Index)
Sets the pooling window height and width.
PoolingMethod get_pooling_method() const
Definition pooling_layer.h:82
void write_JSON_body(JsonWriter &) const override
Writes the layer configuration to a JSON writer.
Index get_padding_width() const
Definition pooling_layer.h:80
void set_row_stride(Index)
Index get_padding_height() const
Definition pooling_layer.h:79
Index get_input_width() const
Definition pooling_layer.h:70
vector< TensorSpec > get_forward_specs(Index batch_size) const override
Returns the tensor specifications used during forward propagation.
Shape get_output_shape() const override
Returns the output tensor shape after pooling.
Index get_pool_height() const
Definition pooling_layer.h:73
Index get_output_width() const
Returns the output feature map width.
Index get_row_stride() const
Definition pooling_layer.h:76
void set_padding_height(Index)
Index get_input_height() const
Definition pooling_layer.h:69
Definition adaptive_moment_estimation.h:14
const string & pooling_method_to_string(PoolingMethod method)
Definition pooling_layer.h:24
PoolingMethod
Pooling reduction method used by Pooling and Pooling3d layers.
Definition pooling_layer.h:19
@ MaxPooling
Definition pooling_layer.h:20
@ AveragePooling
Definition pooling_layer.h:21
PoolingMethod string_to_pooling_method(const string &name)
Definition pooling_layer.h:31
2D pooling operator supporting max and average reductions.
Definition operators.h:860
Fixed-capacity small-vector describing tensor dimensions (rank up to MaxRank).
Definition tensor_utilities.h:42