Line data Source code
1 : // SPDX-License-Identifier: Apache-2.0
2 : /**
3 : * Copyright (C) 2020 Jijoong Moon <jijoong.moon@samsung.com>
4 : *
5 : * @file pooling2d_layer.h
6 : * @date 12 June 2020
7 : * @see https://github.com/nnstreamer/nntrainer
8 : * @author Jijoong Moon <jijoong.moon@samsung.com>
9 : * @bug No known bugs except for NYI items
10 : * @brief This is 2 Dimensional Pooling Layer Class for Neural Network
11 : *
12 : */
13 :
14 : #ifndef __POOLING2D_LAYER_H__
15 : #define __POOLING2D_LAYER_H__
16 : #ifdef __cplusplus
17 :
18 : #include <tuple>
19 : #include <vector>
20 :
21 : #include <base_properties.h>
22 : #include <common_properties.h>
23 : #include <layer_devel.h>
24 :
25 : namespace nntrainer {
26 :
27 : constexpr const unsigned int POOLING2D_DIM = 2;
28 :
29 : /**
30 : * @class Pooling 2D Layer
31 : * @brief Pooling 2D Layer
32 : */
33 : class Pooling2DLayer : public Layer {
34 : public:
35 : /**
36 : * @brief PaddingType Class
37 : * @todo support keras type of padding
38 : */
39 : enum class PaddingType {
40 : full = 0,
41 : same = 1,
42 : valid = 2,
43 : unknown = 3,
44 : };
45 :
46 : /**
47 : * @brief Constructor of Pooling 2D Layer
48 : */
49 : Pooling2DLayer(const std::array<unsigned int, POOLING2D_DIM * 2> &padding_ = {
50 : 0, 0, 0, 0});
51 :
52 : /**
53 : * @brief Destructor of Pooling 2D Layer
54 : */
55 250 : ~Pooling2DLayer() = default;
56 :
57 : /**
58 : * @brief Move constructor of Pooling 2D Layer.
59 : * @param[in] Pooling2D &&
60 : */
61 : Pooling2DLayer(Pooling2DLayer &&rhs) noexcept = default;
62 :
63 : /**
64 : * @brief Move assignment operator.
65 : * @parma[in] rhs Pooling2DLayer to be moved.
66 : */
67 : Pooling2DLayer &operator=(Pooling2DLayer &&rhs) = default;
68 :
69 : /**
70 : * @copydoc Layer::finalize(InitLayerContext &context)
71 : */
72 : void finalize(InitLayerContext &context) override;
73 :
74 : /**
75 : * @copydoc Layer::forwarding(RunLayerContext &context, bool training)
76 : */
77 : void forwarding(RunLayerContext &context, bool training) override;
78 :
79 : /**
80 : * @copydoc Layer::calcDerivative(RunLayerContext &context)
81 : */
82 : void calcDerivative(RunLayerContext &context) override;
83 :
84 : /**
85 : * @copydoc Layer::exportTo(Exporter &exporter, ml::train::ExportMethods
86 : * method)
87 : */
88 : void exportTo(Exporter &exporter,
89 : const ml::train::ExportMethods &method) const override;
90 :
91 : /**
92 : * @copydoc Layer::getType()
93 : */
94 1726 : const std::string getType() const override { return Pooling2DLayer::type; };
95 :
96 : /**
97 : * @copydoc Layer::supportBackwarding()
98 : */
99 42 : bool supportBackwarding() const override { return true; }
100 :
101 : /**
102 : * @copydoc Layer::setProperty(const std::vector<std::string> &values)
103 : */
104 : void setProperty(const std::vector<std::string> &values) override;
105 :
106 : static constexpr const char *type = "pooling2d";
107 :
108 : /**
109 : * @copydoc Layer::setBatch(RunLayerContext &context, unsigned int batch)
110 : */
111 : void setBatch(RunLayerContext &context, unsigned int batch) override;
112 :
113 : private:
114 : std::array<unsigned int, POOLING2D_DIM * 2> padding;
115 : std::tuple<props::PoolingType, std::vector<props::PoolSize>,
116 : std::array<props::Stride, POOLING2D_DIM>, props::Padding2D>
117 : pooling2d_props;
118 :
119 : unsigned int pool_helper_idx; /**< helper tensor idx */
120 : std::vector<unsigned int>
121 : pool_helper_size; /**< helper size for each elements in the case of
122 : global_max pooling */
123 :
124 : /**
125 : * @brief calculation convolution
126 : * @param[in] in input tensor (batch sliced)
127 : * @param[in] training check if training, if training this will memorize index
128 : * @param[in] output output tensor (batch sliced)
129 : * @param[in] pool_helper helper tensor (batch sliced)
130 : * @param[in] batch_idx idx of the batch
131 : */
132 : void pooling2d(Tensor &in, bool training, Tensor &output, Tensor &pool_helper,
133 : int batch_idx);
134 : };
135 :
136 : } // namespace nntrainer
137 :
138 : #endif /* __cplusplus */
139 : #endif /* __POOLING_LAYER_H__ */
|