mlpack 3.4.2
max_pooling.hpp
Go to the documentation of this file.
1
13#ifndef MLPACK_METHODS_ANN_LAYER_MAX_POOLING_HPP
14#define MLPACK_METHODS_ANN_LAYER_MAX_POOLING_HPP
15
16#include <mlpack/prereqs.hpp>
17
18namespace mlpack {
19namespace ann {
20
21/*
22 * The max pooling rule for convolution neural networks. Take the maximum value
23 * within the receptive block.
24 */
26{
27 public:
28 /*
29 * Return the maximum value within the receptive block.
30 *
31 * @param input Input used to perform the pooling operation.
32 */
33 template<typename MatType>
34 size_t Pooling(const MatType& input)
35 {
36 return arma::as_scalar(arma::find(input.max() == input, 1));
37 }
38};
39
48template <
49 typename InputDataType = arma::mat,
50 typename OutputDataType = arma::mat
51>
53{
54 public:
57
67 MaxPooling(const size_t kernelWidth,
68 const size_t kernelHeight,
69 const size_t strideWidth = 1,
70 const size_t strideHeight = 1,
71 const bool floor = true);
72
80 template<typename eT>
81 void Forward(const arma::Mat<eT>& input, arma::Mat<eT>& output);
82
92 template<typename eT>
93 void Backward(const arma::Mat<eT>& /* input */,
94 const arma::Mat<eT>& gy,
95 arma::Mat<eT>& g);
96
98 const OutputDataType& OutputParameter() const { return outputParameter; }
100 OutputDataType& OutputParameter() { return outputParameter; }
101
103 const OutputDataType& Delta() const { return delta; }
105 OutputDataType& Delta() { return delta; }
106
108 size_t InputWidth() const { return inputWidth; }
110 size_t& InputWidth() { return inputWidth; }
111
113 size_t InputHeight() const { return inputHeight; }
115 size_t& InputHeight() { return inputHeight; }
116
118 size_t OutputWidth() const { return outputWidth; }
120 size_t& OutputWidth() { return outputWidth; }
121
123 size_t OutputHeight() const { return outputHeight; }
125 size_t& OutputHeight() { return outputHeight; }
126
128 size_t InputSize() const { return inSize; }
129
131 size_t OutputSize() const { return outSize; }
132
134 size_t KernelWidth() const { return kernelWidth; }
136 size_t& KernelWidth() { return kernelWidth; }
137
139 size_t KernelHeight() const { return kernelHeight; }
141 size_t& KernelHeight() { return kernelHeight; }
142
144 size_t StrideWidth() const { return strideWidth; }
146 size_t& StrideWidth() { return strideWidth; }
147
149 size_t StrideHeight() const { return strideHeight; }
151 size_t& StrideHeight() { return strideHeight; }
152
154 bool Floor() const { return floor; }
156 bool& Floor() { return floor; }
157
159 bool Deterministic() const { return deterministic; }
161 bool& Deterministic() { return deterministic; }
162
166 template<typename Archive>
167 void serialize(Archive& ar, const unsigned int /* version */);
168
169 private:
177 template<typename eT>
178 void PoolingOperation(const arma::Mat<eT>& input,
179 arma::Mat<eT>& output,
180 arma::Mat<eT>& poolingIndices)
181 {
182 for (size_t j = 0, colidx = 0; j < output.n_cols;
183 ++j, colidx += strideHeight)
184 {
185 for (size_t i = 0, rowidx = 0; i < output.n_rows;
186 ++i, rowidx += strideWidth)
187 {
188 arma::mat subInput = input(
189 arma::span(rowidx, rowidx + kernelWidth - 1 - offset),
190 arma::span(colidx, colidx + kernelHeight - 1 - offset));
191
192 const size_t idx = pooling.Pooling(subInput);
193 output(i, j) = subInput(idx);
194
195 if (!deterministic)
196 {
197 arma::Mat<size_t> subIndices = indices(arma::span(rowidx,
198 rowidx + kernelWidth - 1 - offset),
199 arma::span(colidx, colidx + kernelHeight - 1 - offset));
200
201 poolingIndices(i, j) = subIndices(idx);
202 }
203 }
204 }
205 }
206
214 template<typename eT>
215 void Unpooling(const arma::Mat<eT>& error,
216 arma::Mat<eT>& output,
217 arma::Mat<eT>& poolingIndices)
218 {
219 for (size_t i = 0; i < poolingIndices.n_elem; ++i)
220 {
221 output(poolingIndices(i)) += error(i);
222 }
223 }
224
226 size_t kernelWidth;
227
229 size_t kernelHeight;
230
232 size_t strideWidth;
233
235 size_t strideHeight;
236
238 bool floor;
239
241 size_t inSize;
242
244 size_t outSize;
245
247 bool reset;
248
250 size_t inputWidth;
251
253 size_t inputHeight;
254
256 size_t outputWidth;
257
259 size_t outputHeight;
260
262 bool deterministic;
263
265 size_t offset;
266
268 size_t batchSize;
269
271 arma::cube outputTemp;
272
274 arma::cube inputTemp;
275
277 arma::cube gTemp;
278
280 MaxPoolingRule pooling;
281
283 OutputDataType delta;
284
286 OutputDataType gradient;
287
289 OutputDataType outputParameter;
290
292 arma::Mat<size_t> indices;
293
295 arma::Col<size_t> indicesCol;
296
298 std::vector<arma::cube> poolingIndices;
299}; // class MaxPooling
300
301} // namespace ann
302} // namespace mlpack
303
304// Include implementation.
305#include "max_pooling_impl.hpp"
306
307#endif
size_t Pooling(const MatType &input)
Definition: max_pooling.hpp:34
Implementation of the MaxPooling layer.
Definition: max_pooling.hpp:53
size_t StrideHeight() const
Get the stride height.
size_t & InputHeight()
Modify the input height.
size_t InputWidth() const
Get the input width.
void Forward(const arma::Mat< eT > &input, arma::Mat< eT > &output)
Ordinary feed forward pass of a neural network, evaluating the function f(x) by propagating the activ...
size_t InputSize() const
Get the input size.
size_t & InputWidth()
Modify the input width.
size_t KernelWidth() const
Get the kernel width.
size_t KernelHeight() const
Get the kernel height.
size_t & StrideWidth()
Modify the stride width.
size_t & KernelWidth()
Modify the kernel width.
size_t & OutputHeight()
Modify the output height.
size_t OutputHeight() const
Get the output height.
size_t OutputSize() const
Get the output size.
bool & Deterministic()
Modify the value of the deterministic parameter.
bool Deterministic() const
Get the value of the deterministic parameter.
size_t OutputWidth() const
Get the output width.
MaxPooling()
Create the MaxPooling object.
bool & Floor()
Modify the value of the rounding operation.
size_t InputHeight() const
Get the input height.
MaxPooling(const size_t kernelWidth, const size_t kernelHeight, const size_t strideWidth=1, const size_t strideHeight=1, const bool floor=true)
Create the MaxPooling object using the specified number of units.
size_t & KernelHeight()
Modify the kernel height.
size_t StrideWidth() const
Get the stride width.
size_t & StrideHeight()
Modify the stride height.
void Backward(const arma::Mat< eT > &, const arma::Mat< eT > &gy, arma::Mat< eT > &g)
Ordinary feed backward pass of a neural network, using 3rd-order tensors as input,...
const OutputDataType & OutputParameter() const
Get the output parameter.
Definition: max_pooling.hpp:98
OutputDataType & OutputParameter()
Modify the output parameter.
size_t & OutputWidth()
Modify the output width.
bool Floor() const
Get the value of the rounding operation.
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
OutputDataType & Delta()
Modify the delta.
const OutputDataType & Delta() const
Get the delta.
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
The core includes that mlpack expects; standard C++ includes and Armadillo.