Beatmup
|
2D pooling operation computed on GPU. More...
#include <pooling2d.h>
Public Types | |
enum class | Operator { MAX , AVERAGE } |
Pooling operator specification. More... | |
Public Member Functions | |
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. More... | |
int | getInputCount () const |
Returns number of operation inputs. More... | |
int | getOutputCount () const |
Returns number of operation outputs. More... | |
bool | acceptsStorageInput (int index=0) const |
Returns true if the operation can take a Storage::View at a specific input. More... | |
bool | acceptsStorageOutput (int index=0) const |
Returns true if the operation can take a Storage::View at a specific output. More... | |
Size | getOutputSize (int outputIndex=0) const |
Returns full size of a specific operation output. More... | |
Storage::View | getOutput (int index=0) |
Returns a storage view bound to a specific operation output. More... | |
void | setInput (Storage::View &&storage, int inputIndex=0) |
void | setOutput (Storage::View &&storage, int outputIndex=0) |
std::map< std::string, std::string > | serialize () const |
Returns a serialized representation of th operation;. More... | |
void | disconnect () |
Assigns empty inputs and outputs. More... | |
unsigned long | countTexelFetches () const |
Counts (approximate) number of texels fetches. More... | |
Public Member Functions inherited from Beatmup::NNets::AbstractOperation | |
virtual | ~AbstractOperation () |
virtual bool | usesGpu () const |
Returns true if the operation is run on GPU. More... | |
virtual bool | acceptsVectorInput (int index=0) const |
Returns true if the operation can take a GL::Vector at a specific input. More... | |
virtual bool | acceptsTextureInput (int index=0) const |
Returns true if the operation can take a GL::TextureHandler at a specific input. More... | |
virtual bool | acceptsVectorOutput (int index=0) const |
Returns true if the operation can take a GL::Vector at a specific output. More... | |
virtual bool | acceptsTextureOutput (int index=0) const |
Returns true if the operation can take a GL::TextureHandler at a specific output. More... | |
virtual void | getOutput (GL::Vector *&vector, int index=0) |
Returns a GL::Vector bound to a specific operation output. More... | |
virtual void | getOutput (GL::TextureHandler *&vector, int index=0) |
Returns a GL::TextureHandler bound to a specific operation output. More... | |
virtual void | setInput (GL::Vector &vector, int index=0) |
virtual void | setOutput (GL::Vector &vector, int index=0) |
virtual void | setInput (GL::TextureHandler &image, int index=0) |
virtual void | setOutput (GL::TextureHandler &image, int index=0) |
virtual unsigned long | countMultiplyAdds () const |
Counts (approximate) number of multiply-adds used by this operation. More... | |
std::string | getName () const |
Static Public Member Functions | |
static Operator | operatorFromString (const std::string &str) |
Returns a pooling operator from string. More... | |
static bool | initDeserializer () |
Sets up deserialization of the operation. More... | |
Private Member Functions | |
void | prepare (GraphicPipeline &gpu, ChunkCollection &data, GL::ProgramBank &bank) |
Compiles GLSL shaders. More... | |
void | execute (TaskThread &thread, GraphicPipeline &gpu) |
Executes the operation. More... | |
int | getInputPadding (int index=0) const |
Retrieves minimum required size of zero padding for a given input. More... | |
void | getSampledChannels (int index, int &min, int &max) const |
Retrieves range of input features channels sampled at the same time for a specific input. More... | |
Private Attributes | |
const Size | size |
const Size | stride |
const Operator | op |
const Size::Padding | padding |
Storage::View | input |
Storage::View | output |
bool | ready |
GL::RenderingProgram * | program |
Additional Inherited Members | |
Protected Member Functions inherited from Beatmup::NNets::AbstractOperation | |
AbstractOperation (const std::string &name) | |
virtual void | execute (TaskThread &thread) |
Executes the operation within a specific CPU thread. More... | |
Protected Member Functions inherited from Beatmup::NNets::SpatialFilteringMixin | |
SpatialFilteringMixin (const int nbSizeX, const int nbSizeY) | |
Initializes spatial filtering mixin. More... | |
~SpatialFilteringMixin () | |
void | writeHeader (StringBuilder &code, bool useUniformShift) |
Writes out the very GLSL fragment shader header required for spatial neighborhood sampling. More... | |
void | declare (StringBuilder &code, const char *datatype, bool inlineSampling=false) |
Declares GLSL fragment shader main(..) code part required for spatial neighborhood sampling. More... | |
void | sample (StringBuilder &code, const char *inputName, const int inputIndex, const Point &shift, const bool isFirstSample=true, const char *suffix="") |
Samples a neighborhood of a given texture. More... | |
void | sampleInline (StringBuilder &code, const char *inputName, const int inputIndex, const IntPoint &position, const Point &shift, const char *suffix="") |
void | setup (const int width, const int height) |
Prepares the spatial filtering operation execution. More... | |
void | setUniformShift (GL::Program &program, const IntPoint &shift, const IntPoint &inputSize) |
Applies an offset to the sampling position at runtime. More... | |
void | setupProgram (GL::Program &program) |
Prepares a given program for spatial filtering. More... | |
IntRectangle | getSamplingArea (const IntPoint &size, const IntPoint &stride, const Size::Padding padding) const |
Implements common padding policies by computing a rectangular area of positions the sampling kernel takes in order to get the result with the required padding. More... | |
IntRectangle | getSamplingArea (const Storage::View &storage, const int channel, const IntPoint &stride, const Size::Padding padding) const |
Computes area in pixels to sample a given storage according to specific stride and padding. More... | |
Rectangle | getTextureCoordinates (const Storage::View &storage, const int channel, const IntPoint &stride, const Size::Padding padding, const IntPoint &outputSize) const |
Computes texture coordinates sampling a specific storage channel for given stride, padding and output size. More... | |
std::string | getInputSamplingPos () const |
Retrieves input sampling point position for the current fragment. More... | |
bool | isUniformShiftUsed () const |
Static Protected Attributes inherited from Beatmup::NNets::SpatialFilteringMixin | |
static const char * | SAMPLE_ID_PREFIX = "i" |
prefix of variables declaring a neighbor sample More... | |
2D pooling operation computed on GPU.
Has a single input and a single output. Constraints:
Raspberry Pi-related constraints:
Definition at line 40 of file pooling2d.h.
|
strong |
Pooling2D::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.
[in] | name | Layer name |
[in] | op | Pooling operator |
[in] | size | Spatial pooling operational size |
[in] | stride | Pooling stride; if 0, the size is used |
[in] | padding | Zero padding applied to the input |
Definition at line 29 of file pooling2d.cpp.
|
privatevirtual |
Compiles GLSL shaders.
[in,out] | gpu | A graphic pipeline instance |
[in,out] | data | Chunkfile containing operation data (e.g. weights and biases) |
[in,out] | bank | A program bank with existing GLSL programs to be reused when possible. If a new program is built, it is added to the bank. |
Implements Beatmup::NNets::AbstractOperation.
Definition at line 48 of file pooling2d.cpp.
|
privatevirtual |
Executes the operation.
The operation should be prepared.
[in,out] | thread | Calling CPU thread descriptor |
[in,out] | gpu | A graphic pipeline instance |
Implements Beatmup::NNets::AbstractOperation.
Definition at line 107 of file pooling2d.cpp.
|
privatevirtual |
Retrieves minimum required size of zero padding for a given input.
Operations that sample a neighborhood of a pixel may need the input to be padded with zeros, if some of the neighboring samples fall out of the are containing data. In Beatmup the zero padding is handled by allocating a bigger input and putting zeros around the area that is actually filled with data.
Reimplemented from Beatmup::NNets::AbstractOperation.
Definition at line 143 of file pooling2d.cpp.
|
privatevirtual |
Retrieves range of input features channels sampled at the same time for a specific input.
The operation would typically take the entire storage and sample it at once, if needed. If the number of textures in a storage exceeds the number of texture samplers that the GPU may use simultaneously, an exception occurs. This function provides the necessary information to limit the number of textures in the storage when allocating it. When the limit is reached, multiple channels are packed into a single texture in the storage.
[in] | index | The input index. Expected to fall in the valid range, i.e. from zero to getInputCount() - 1 inclusive. |
[out] | min | The minimum number of channels that can be sampled at once |
[out] | max | The maximum number of channels that can be sampled at once |
Implements Beatmup::NNets::AbstractOperation.
Definition at line 148 of file pooling2d.cpp.
|
inlinevirtual |
Returns number of operation inputs.
Inputs are then indexed from zero to the returned value minus one inclusive.
Reimplemented from Beatmup::NNets::AbstractOperation.
Definition at line 80 of file pooling2d.h.
|
inlinevirtual |
Returns number of operation outputs.
Outputs are then indexed from zero to the returned value minus one inclusive.
Reimplemented from Beatmup::NNets::AbstractOperation.
Definition at line 81 of file pooling2d.h.
|
inlinevirtual |
Returns true
if the operation can take a Storage::View at a specific input.
Neural network operations may accept different kinds of data containers on inputs and outputs, namely Storage::View, GL::Vector and textures. This function is used to check whether a given operation accepts a storage view on input.
[in] | index | The input index. Expected to fall in the valid range, i.e. from zero to getInputCount() - 1 inclusive. |
Reimplemented from Beatmup::NNets::AbstractOperation.
Definition at line 83 of file pooling2d.h.
|
inlinevirtual |
Returns true
if the operation can take a Storage::View at a specific output.
Neural network operations may accept different kinds of data containers on outputs and outputs, namely Storage::View, GL::Vector and textures. This function is used to check whether a given operation accepts a storage view on output.
[in] | index | The output index. Expected to fall in the valid range, i.e. from zero to getOutputCount() - 1 inclusive. |
Reimplemented from Beatmup::NNets::AbstractOperation.
Definition at line 84 of file pooling2d.h.
|
virtual |
Returns full size of a specific operation output.
Reimplemented from Beatmup::NNets::AbstractOperation.
Definition at line 153 of file pooling2d.cpp.
|
inlinevirtual |
Returns a storage view bound to a specific operation output.
If no view is bound, returns empty view.
[in] | index | The output index. Expected to fall in the valid range, i.e. from zero to getOutputCount() - 1 inclusive. |
Reimplemented from Beatmup::NNets::AbstractOperation.
Definition at line 88 of file pooling2d.h.
|
virtual |
Reimplemented from Beatmup::NNets::AbstractOperation.
Definition at line 164 of file pooling2d.cpp.
|
virtual |
|
virtual |
Returns a serialized representation of th operation;.
Implements Beatmup::NNets::AbstractOperation.
Definition at line 177 of file pooling2d.cpp.
|
virtual |
Assigns empty inputs and outputs.
Implements Beatmup::NNets::AbstractOperation.
Definition at line 219 of file pooling2d.cpp.
|
virtual |
Counts (approximate) number of texels fetches.
Reimplemented from Beatmup::NNets::AbstractOperation.
Definition at line 225 of file pooling2d.cpp.
|
static |
Returns a pooling operator from string.
The conversion is case-insensitive. Raises an exception if cannot interpret the string.
[in] | str | The input string |
Definition at line 230 of file pooling2d.cpp.
|
static |
Sets up deserialization of the operation.
|
private |
Definition at line 51 of file pooling2d.h.
|
private |
Definition at line 51 of file pooling2d.h.
|
private |
Definition at line 52 of file pooling2d.h.
|
private |
Definition at line 53 of file pooling2d.h.
|
private |
Definition at line 54 of file pooling2d.h.
|
private |
Definition at line 54 of file pooling2d.h.
|
private |
Definition at line 55 of file pooling2d.h.
|
private |
Definition at line 56 of file pooling2d.h.