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 
18 namespace mlpack {
19 namespace 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 
48 template <
49  typename InputDataType = arma::mat,
50  typename OutputDataType = arma::mat
51 >
53 {
54  public:
56  MaxPooling();
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 
164  size_t WeightSize() const { return 0; }
165 
169  template<typename Archive>
170  void serialize(Archive& ar, const uint32_t /* version */);
171 
172  private:
180  template<typename eT>
181  void PoolingOperation(const arma::Mat<eT>& input,
182  arma::Mat<eT>& output,
183  arma::Mat<eT>& poolingIndices)
184  {
185  for (size_t j = 0, colidx = 0; j < output.n_cols;
186  ++j, colidx += strideHeight)
187  {
188  for (size_t i = 0, rowidx = 0; i < output.n_rows;
189  ++i, rowidx += strideWidth)
190  {
191  size_t rowEnd = rowidx + kernelWidth - 1;
192  size_t colEnd = colidx + kernelHeight - 1;
193 
194  if (rowEnd > input.n_rows - 1)
195  rowEnd = input.n_rows - 1;
196  if (colEnd > input.n_cols - 1)
197  colEnd = input.n_cols - 1;
198 
199  arma::mat subInput = input(
200  arma::span(rowidx, rowEnd),
201  arma::span(colidx, colEnd));
202 
203  const size_t idx = pooling.Pooling(subInput);
204  output(i, j) = subInput(idx);
205 
206  if (!deterministic)
207  {
208  arma::Mat<size_t> subIndices = indices(arma::span(rowidx, rowEnd),
209  arma::span(colidx, colEnd));
210 
211  poolingIndices(i, j) = subIndices(idx);
212  }
213  }
214  }
215  }
216 
224  template<typename eT>
225  void Unpooling(const arma::Mat<eT>& error,
226  arma::Mat<eT>& output,
227  arma::Mat<eT>& poolingIndices)
228  {
229  for (size_t i = 0; i < poolingIndices.n_elem; ++i)
230  {
231  output(poolingIndices(i)) += error(i);
232  }
233  }
234 
236  size_t kernelWidth;
237 
239  size_t kernelHeight;
240 
242  size_t strideWidth;
243 
245  size_t strideHeight;
246 
248  bool floor;
249 
251  size_t inSize;
252 
254  size_t outSize;
255 
257  bool reset;
258 
260  size_t inputWidth;
261 
263  size_t inputHeight;
264 
266  size_t outputWidth;
267 
269  size_t outputHeight;
270 
272  bool deterministic;
273 
274 
276  size_t batchSize;
277 
279  arma::cube outputTemp;
280 
282  arma::cube inputTemp;
283 
285  arma::cube gTemp;
286 
288  MaxPoolingRule pooling;
289 
291  OutputDataType delta;
292 
294  OutputDataType gradient;
295 
297  OutputDataType outputParameter;
298 
300  arma::Mat<size_t> indices;
301 
303  arma::Col<size_t> indicesCol;
304 
306  std::vector<arma::cube> poolingIndices;
307 }; // class MaxPooling
308 
309 } // namespace ann
310 } // namespace mlpack
311 
312 // Include implementation.
313 #include "max_pooling_impl.hpp"
314 
315 #endif
bool Floor() const
Get the value of the rounding operation.
size_t InputSize() const
Get the input size.
size_t KernelWidth() const
Get the kernel width.
size_t & InputWidth()
Modify the input width.
Linear algebra utility functions, generally performed on matrices or vectors.
size_t InputHeight() const
Get the input height.
The core includes that mlpack expects; standard C++ includes and Armadillo.
size_t & OutputWidth()
Modify the output width.
size_t OutputSize() const
Get the output size.
bool & Deterministic()
Modify the value of the deterministic parameter.
size_t & StrideWidth()
Modify the stride width.
size_t & StrideHeight()
Modify the stride height.
size_t & KernelHeight()
Modify the kernel height.
size_t OutputWidth() const
Get the output width.
size_t & InputHeight()
Modify the input height.
size_t & OutputHeight()
Modify the output height.
size_t Pooling(const MatType &input)
Definition: max_pooling.hpp:34
size_t StrideHeight() const
Get the stride height.
size_t KernelHeight() const
Get the kernel height.
size_t InputWidth() const
Get the input width.
const OutputDataType & Delta() const
Get the delta.
size_t OutputHeight() const
Get the output height.
size_t WeightSize() const
Get the size of the weights.
size_t StrideWidth() const
Get the stride width.
size_t & KernelWidth()
Modify the kernel width.
bool & Floor()
Modify the value of the rounding operation.
bool Deterministic() const
Get the value of the deterministic parameter.
Implementation of the MaxPooling layer.
Definition: max_pooling.hpp:52
OutputDataType & Delta()
Modify the delta.
const OutputDataType & OutputParameter() const
Get the output parameter.
Definition: max_pooling.hpp:98
OutputDataType & OutputParameter()
Modify the output parameter.