mlpack  master
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:
144 
152  template<typename eT>
153  void PoolingOperation(const arma::Mat<eT>& input,
154  arma::Mat<eT>& output,
155  arma::Mat<eT>& poolingIndices)
156  {
157  for (size_t j = 0, colidx = 0; j < output.n_cols; ++j, colidx += dW)
158  {
159  for (size_t i = 0, rowidx = 0; i < output.n_rows; ++i, rowidx += dH)
160  {
161  arma::mat subInput = input(arma::span(rowidx, rowidx + kW - 1 - offset),
162  arma::span(colidx, colidx + kH - 1 - offset));
163 
164  const size_t idx = pooling.Pooling(subInput);
165  output(i, j) = subInput(idx);
166 
167  if (!deterministic)
168  {
169  arma::Mat<size_t> subIndices = indices(arma::span(rowidx,
170  rowidx + kW - 1 - offset),
171  arma::span(colidx, colidx + kH - 1 - offset));
172 
173  poolingIndices(i, j) = subIndices(idx);
174  }
175  }
176  }
177  }
178 
186  template<typename eT>
187  void Unpooling(const arma::Mat<eT>& error,
188  arma::Mat<eT>& output,
189  arma::Mat<eT>& poolingIndices)
190  {
191  for (size_t i = 0; i < poolingIndices.n_elem; ++i)
192  {
193  output(poolingIndices(i)) += error(i);
194  }
195  }
196 
198  size_t inSize;
199 
201  size_t outSize;
202 
204  size_t kW;
205 
207  size_t kH;
208 
210  size_t dW;
211 
213  size_t dH;
214 
216  bool reset;
217 
219  bool floor;
220 
222  size_t offset;
223 
225  size_t inputWidth;
226 
228  size_t inputHeight;
229 
231  size_t outputWidth;
232 
234  size_t outputHeight;
235 
238 
240  arma::cube outputTemp;
241 
243  arma::cube inputTemp;
244 
246  arma::cube gTemp;
247 
250 
252  OutputDataType delta;
253 
255  OutputDataType gradient;
256 
258  InputDataType inputParameter;
259 
261  OutputDataType outputParameter;
262 
264  arma::Mat<size_t> indices;
265 
267  arma::Col<size_t> indicesCol;
268 
270  std::vector<arma::cube> poolingIndices;
271 }; // class MaxPooling
272 
273 } // namespace ann
274 } // namespace mlpack
275 
276 // Include implementation.
277 #include "max_pooling_impl.hpp"
278 
279 #endif
size_t dW
Locally-stored width of the stride operation.
InputDataType const & InputParameter() const
Get the input parameter.
Definition: max_pooling.hpp:98
size_t kH
Locally-stored height of the pooling window.
size_t const & InputHeight() const
Get the height.
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: binarize.hpp:18
size_t & InputHeight()
Modify the height.
size_t & OutputHeight()
Modify the height.
The core includes that mlpack expects; standard C++ includes and Armadillo.
bool deterministic
If true use maximum a posteriori during the forward pass.
size_t outputWidth
Locally-stored output width.
size_t inputHeight
Locally-stored input height.
size_t const & OutputWidth() const
Get the width.
MaxPoolingRule pooling
Locally-stored pooling strategy.
std::vector< arma::cube > poolingIndices
Locally-stored pooling indicies.
void PoolingOperation(const arma::Mat< eT > &input, arma::Mat< eT > &output, arma::Mat< eT > &poolingIndices)
Apply pooling to the input and store the results.
InputDataType & InputParameter()
Modify the input parameter.
size_t offset
Locally-stored stored rounding offset.
OutputDataType outputParameter
Locally-stored output parameter object.
OutputDataType gradient
Locally-stored gradient object.
size_t Pooling(const MatType &input)
Definition: max_pooling.hpp:34
InputDataType inputParameter
Locally-stored input parameter object.
size_t dH
Locally-stored height of the stride operation.
bool floor
Rounding operation used.
OutputDataType & OutputParameter()
Modify the output parameter.
size_t inSize
Locally-stored number of input units.
arma::Col< size_t > indicesCol
Locally-stored indices column parameter.
size_t & InputWidth()
Modify the width.
arma::Mat< size_t > indices
Locally-stored indices matrix parameter.
bool reset
Locally-stored reset parameter used to initialize the module once.
OutputDataType & Delta()
Modify the delta.
size_t & OutputWidth()
Modify the width.
OutputDataType delta
Locally-stored delta object.
arma::cube gTemp
Locally-stored transformed output parameter.
arma::cube outputTemp
Locally-stored output parameter.
size_t const & OutputHeight() const
Get the height.
arma::cube inputTemp
Locally-stored transformed input parameter.
Implementation of the MaxPooling layer.
Definition: max_pooling.hpp:52
size_t inputWidth
Locally-stored input width.
void Unpooling(const arma::Mat< eT > &error, arma::Mat< eT > &output, arma::Mat< eT > &poolingIndices)
Apply unpooling to the input and store the results.
bool & Deterministic()
Modify the value of the deterministic parameter.
size_t outSize
Locally-stored number of output units.
size_t outputHeight
Locally-stored output height.
size_t kW
Locally-stored width of the pooling window.
size_t const & InputWidth() const
Get the width.
bool Deterministic() const
Get the value of the deterministic parameter.
OutputDataType const & OutputParameter() const
Get the output parameter.
OutputDataType const & Delta() const
Get the delta.