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 kW,
68  const size_t kH,
69  const size_t dW = 1,
70  const size_t dH = 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  arma::Mat<eT>&& gy,
95  arma::Mat<eT>&& g);
96 
98  InputDataType const& InputParameter() const { return inputParameter; }
100  InputDataType& InputParameter() { return inputParameter; }
101 
103  OutputDataType const& OutputParameter() const { return outputParameter; }
105  OutputDataType& OutputParameter() { return outputParameter; }
106 
108  OutputDataType const& Delta() const { return delta; }
110  OutputDataType& Delta() { return delta; }
111 
113  size_t const& InputWidth() const { return inputWidth; }
115  size_t& InputWidth() { return inputWidth; }
116 
118  size_t const& InputHeight() const { return inputHeight; }
120  size_t& InputHeight() { return inputHeight; }
121 
123  size_t const& OutputWidth() const { return outputWidth; }
125  size_t& OutputWidth() { return outputWidth; }
126 
128  size_t const& OutputHeight() const { return outputHeight; }
130  size_t& OutputHeight() { return outputHeight; }
131 
133  bool Deterministic() const { return deterministic; }
135  bool& Deterministic() { return deterministic; }
136 
140  template<typename Archive>
141  void serialize(Archive& ar, const unsigned int /* version */);
142 
143  private:
151  template<typename eT>
152  void PoolingOperation(const arma::Mat<eT>& input,
153  arma::Mat<eT>& output,
154  arma::Mat<eT>& poolingIndices)
155  {
156  for (size_t j = 0, colidx = 0; j < output.n_cols; ++j, colidx += dW)
157  {
158  for (size_t i = 0, rowidx = 0; i < output.n_rows; ++i, rowidx += dH)
159  {
160  arma::mat subInput = input(arma::span(rowidx, rowidx + kW - 1 - offset),
161  arma::span(colidx, colidx + kH - 1 - offset));
162 
163  const size_t idx = pooling.Pooling(subInput);
164  output(i, j) = subInput(idx);
165 
166  if (!deterministic)
167  {
168  arma::Mat<size_t> subIndices = indices(arma::span(rowidx,
169  rowidx + kW - 1 - offset),
170  arma::span(colidx, colidx + kH - 1 - offset));
171 
172  poolingIndices(i, j) = subIndices(idx);
173  }
174  }
175  }
176  }
177 
185  template<typename eT>
186  void Unpooling(const arma::Mat<eT>& error,
187  arma::Mat<eT>& output,
188  arma::Mat<eT>& poolingIndices)
189  {
190  for (size_t i = 0; i < poolingIndices.n_elem; ++i)
191  {
192  output(poolingIndices(i)) += error(i);
193  }
194  }
195 
197  size_t inSize;
198 
200  size_t outSize;
201 
203  size_t kW;
204 
206  size_t kH;
207 
209  size_t dW;
210 
212  size_t dH;
213 
215  bool reset;
216 
218  bool floor;
219 
221  size_t offset;
222 
224  size_t inputWidth;
225 
227  size_t inputHeight;
228 
230  size_t outputWidth;
231 
233  size_t outputHeight;
234 
236  bool deterministic;
237 
239  arma::cube outputTemp;
240 
242  arma::cube inputTemp;
243 
245  arma::cube gTemp;
246 
248  MaxPoolingRule pooling;
249 
251  OutputDataType delta;
252 
254  OutputDataType gradient;
255 
257  InputDataType inputParameter;
258 
260  OutputDataType outputParameter;
261 
263  arma::Mat<size_t> indices;
264 
266  arma::Col<size_t> indicesCol;
267 
269  std::vector<arma::cube> poolingIndices;
270 }; // class MaxPooling
271 
272 } // namespace ann
273 } // namespace mlpack
274 
275 // Include implementation.
276 #include "max_pooling_impl.hpp"
277 
278 #endif
OutputDataType const & OutputParameter() const
Get the output parameter.
size_t & InputWidth()
Modify the width.
.hpp
Definition: add_to_po.hpp:21
The core includes that mlpack expects; standard C++ includes and Armadillo.
size_t & OutputWidth()
Modify the width.
size_t const & InputWidth() const
Get the width.
bool & Deterministic()
Modify the value of the deterministic parameter.
size_t & InputHeight()
Modify the height.
size_t & OutputHeight()
Modify the height.
size_t const & OutputWidth() const
Get the width.
size_t Pooling(const MatType &input)
Definition: max_pooling.hpp:34
InputDataType const & InputParameter() const
Get the input parameter.
Definition: max_pooling.hpp:98
OutputDataType const & Delta() const
Get the delta.
size_t const & InputHeight() const
Get the height.
bool Deterministic() const
Get the value of the deterministic parameter.
InputDataType & InputParameter()
Modify the input parameter.
Implementation of the MaxPooling layer.
Definition: max_pooling.hpp:52
OutputDataType & Delta()
Modify the delta.
OutputDataType & OutputParameter()
Modify the output parameter.
size_t const & OutputHeight() const
Get the height.