Beatmup
pooling2d.h
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2020, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #include "operation.h"
21 
22 namespace Beatmup {
23  namespace NNets {
24 
25  /**
26  2D pooling operation computed on GPU.
27  Has a single input and a single output.
28  Constraints:
29  - Input and output are 3D tensors with values in [0, 1] range sampled over 8 bits.
30  - Number of feature maps is a multiple of 4.
31  - Pooling area is of square shape.
32  - Strides are equal along X and Y.
33  - Average pooling only accepts valid zero padding,
34 
35  Raspberry Pi-related constraints:
36  - Pi cannot sample more than 256 channels to compute a single output value. Actual practical limit is
37  yet lower: pooling size may be limited by 10. When the limit is reached, Pi OpenGL driver reports an
38  out of memory error (0x505).
39  */
41  public:
42  /**
43  Pooling operator specification
44  */
45  enum class Operator {
46  MAX, // max pooling
47  AVERAGE // average pooling
48  };
49 
50  private:
51  const Size size, stride;
52  const Operator op;
55  bool ready;
57 
58  void prepare(GraphicPipeline& gpu, ChunkCollection& data, GL::ProgramBank& bank);
59  void execute(TaskThread& thread, GraphicPipeline& gpu);
60  int getInputPadding(int index = 0) const;
61  void getSampledChannels(int index, int& min, int& max) const;
62 
63  public:
64  /**
65  2D pooling layer.
66  \param[in] name Layer name
67  \param[in] op Pooling operator
68  \param[in] size Spatial pooling operational size
69  \param[in] stride Pooling stride; if 0, the size is used
70  \param[in] padding Zero padding applied to the input
71  */
72  Pooling2D(
73  const std::string& name,
74  const Operator op,
75  const int size,
76  const int stride = 1,
78  );
79 
80  inline int getInputCount() const { return 1; }
81  inline int getOutputCount() const { return 1; }
82 
83  inline bool acceptsStorageInput(int index = 0) const { return index == 0; }
84  inline bool acceptsStorageOutput(int index = 0) const { return index == 0; }
85 
86  Size getOutputSize(int outputIndex = 0) const;
87 
88  inline Storage::View getOutput(int index = 0) { return output; }
89 
90  void setInput(Storage::View&& storage, int inputIndex = 0);
91  void setOutput(Storage::View&& storage, int outputIndex = 0);
92 
93  std::map<std::string, std::string> serialize() const;
94 
95  void disconnect();
96 
97  unsigned long countTexelFetches() const;
98 
99  /**
100  Returns a pooling operator from string.
101  The conversion is case-insensitive. Raises an exception if cannot interpret the string.
102  \param[in] str The input string
103  */
104  static Operator operatorFromString(const std::string& str);
105 
106  /**
107  Sets up deserialization of the operation.
108  */
109  static bool initDeserializer();
110  };
111 
112  /**
113  \internal
114  Being declared here, this variaable ensures Pooling2D::initDeserializer() is called with inclusion of this header file.
115  */
117  }
118 }
119 
120 namespace std {
122 }
A key-value pair set storing pieces of arbitrary data (chunks) under string keys.
Definition: chunkfile.h:36
Stores linked GLSL programs and their associated fragment shader codes.
Definition: program_bank.h:31
GLSL program to render images Makes use of default vertex attributes to pass the texture coordinates ...
Definition: program.h:240
Internal low-level GPU control API.
Definition: pipeline.h:33
Abstract neural net operation (layer).
Definition: operation.h:46
2D pooling operation computed on GPU.
Definition: pooling2d.h:40
Operator
Pooling operator specification.
Definition: pooling2d.h:45
Storage::View input
Definition: pooling2d.h:54
bool acceptsStorageOutput(int index=0) const
Returns true if the operation can take a Storage::View at a specific output.
Definition: pooling2d.h:84
void setOutput(Storage::View &&storage, int outputIndex=0)
Definition: pooling2d.cpp:171
void getSampledChannels(int index, int &min, int &max) const
Retrieves range of input features channels sampled at the same time for a specific input.
Definition: pooling2d.cpp:148
Size getOutputSize(int outputIndex=0) const
Returns full size of a specific operation output.
Definition: pooling2d.cpp:153
std::map< std::string, std::string > serialize() const
Returns a serialized representation of th operation;.
Definition: pooling2d.cpp:177
Storage::View output
Definition: pooling2d.h:54
void setInput(Storage::View &&storage, int inputIndex=0)
Definition: pooling2d.cpp:164
Pooling2D(const std::string &name, const Operator op, const int size, const int stride=1, const Size::Padding padding=Size::Padding::VALID)
2D pooling layer.
Definition: pooling2d.cpp:29
Storage::View getOutput(int index=0)
Returns a storage view bound to a specific operation output.
Definition: pooling2d.h:88
void disconnect()
Assigns empty inputs and outputs.
Definition: pooling2d.cpp:219
int getInputPadding(int index=0) const
Retrieves minimum required size of zero padding for a given input.
Definition: pooling2d.cpp:143
const Size::Padding padding
Definition: pooling2d.h:53
int getInputCount() const
Returns number of operation inputs.
Definition: pooling2d.h:80
const Operator op
Definition: pooling2d.h:52
static Operator operatorFromString(const std::string &str)
Returns a pooling operator from string.
Definition: pooling2d.cpp:230
bool acceptsStorageInput(int index=0) const
Returns true if the operation can take a Storage::View at a specific input.
Definition: pooling2d.h:83
void execute(TaskThread &thread, GraphicPipeline &gpu)
Executes the operation.
Definition: pooling2d.cpp:107
GL::RenderingProgram * program
Definition: pooling2d.h:56
static bool initDeserializer()
Sets up deserialization of the operation.
int getOutputCount() const
Returns number of operation outputs.
Definition: pooling2d.h:81
void prepare(GraphicPipeline &gpu, ChunkCollection &data, GL::ProgramBank &bank)
Compiles GLSL shaders.
Definition: pooling2d.cpp:48
unsigned long countTexelFetches() const
Counts (approximate) number of texels fetches.
Definition: pooling2d.cpp:225
Operation 3D input/output size.
Definition: storage.h:37
Padding
Zero padding specification.
Definition: storage.h:45
Generates GLSL fragment shader code sampling a local neighborhood around the current texture coordina...
Definition: operation.h:272
Maps a 3D tensor onto a storage.
Definition: storage.h:308
Thread executing tasks.
Definition: parallelism.h:154
static const bool POOLING2D_OP_DESERIALIZABLE
Definition: pooling2d.h:116
Definition: geometry.h:721
std::string to_string(Beatmup::NNets::ActivationFunction function)
CustomPoint< numeric > min(const CustomPoint< numeric > &a, const CustomPoint< numeric > &b)
Definition: geometry.h:724
CustomPoint< numeric > max(const CustomPoint< numeric > &a, const CustomPoint< numeric > &b)
Definition: geometry.h:728
jlong jint index
jlong jint op
JNIEnv jobject jstring str