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  OutputDataType const& OutputParameter() const { return outputParameter; }
100  OutputDataType& OutputParameter() { return outputParameter; }
101 
103  OutputDataType const& Delta() const { return delta; }
105  OutputDataType& Delta() { return delta; }
106 
108  size_t const& InputWidth() const { return inputWidth; }
110  size_t& InputWidth() { return inputWidth; }
111 
113  size_t const& InputHeight() const { return inputHeight; }
115  size_t& InputHeight() { return inputHeight; }
116 
118  size_t const& OutputWidth() const { return outputWidth; }
120  size_t& OutputWidth() { return outputWidth; }
121 
123  size_t const& OutputHeight() const { return outputHeight; }
125  size_t& OutputHeight() { return outputHeight; }
126 
128  bool Deterministic() const { return deterministic; }
130  bool& Deterministic() { return deterministic; }
131 
135  template<typename Archive>
136  void serialize(Archive& ar, const unsigned int /* version */);
137 
138  private:
146  template<typename eT>
147  void PoolingOperation(const arma::Mat<eT>& input,
148  arma::Mat<eT>& output,
149  arma::Mat<eT>& poolingIndices)
150  {
151  for (size_t j = 0, colidx = 0; j < output.n_cols; ++j, colidx += dW)
152  {
153  for (size_t i = 0, rowidx = 0; i < output.n_rows; ++i, rowidx += dH)
154  {
155  arma::mat subInput = input(arma::span(rowidx, rowidx + kW - 1 - offset),
156  arma::span(colidx, colidx + kH - 1 - offset));
157 
158  const size_t idx = pooling.Pooling(subInput);
159  output(i, j) = subInput(idx);
160 
161  if (!deterministic)
162  {
163  arma::Mat<size_t> subIndices = indices(arma::span(rowidx,
164  rowidx + kW - 1 - offset),
165  arma::span(colidx, colidx + kH - 1 - offset));
166 
167  poolingIndices(i, j) = subIndices(idx);
168  }
169  }
170  }
171  }
172 
180  template<typename eT>
181  void Unpooling(const arma::Mat<eT>& error,
182  arma::Mat<eT>& output,
183  arma::Mat<eT>& poolingIndices)
184  {
185  for (size_t i = 0; i < poolingIndices.n_elem; ++i)
186  {
187  output(poolingIndices(i)) += error(i);
188  }
189  }
190 
192  size_t kW;
193 
195  size_t kH;
196 
198  size_t dW;
199 
201  size_t dH;
202 
204  bool floor;
205 
207  size_t inSize;
208 
210  size_t outSize;
211 
213  bool reset;
214 
216  size_t inputWidth;
217 
219  size_t inputHeight;
220 
222  size_t outputWidth;
223 
225  size_t outputHeight;
226 
228  bool deterministic;
229 
231  size_t offset;
232 
234  size_t batchSize;
235 
237  arma::cube outputTemp;
238 
240  arma::cube inputTemp;
241 
243  arma::cube gTemp;
244 
246  MaxPoolingRule pooling;
247 
249  OutputDataType delta;
250 
252  OutputDataType gradient;
253 
255  OutputDataType outputParameter;
256 
258  arma::Mat<size_t> indices;
259 
261  arma::Col<size_t> indicesCol;
262 
264  std::vector<arma::cube> poolingIndices;
265 }; // class MaxPooling
266 
267 } // namespace ann
268 } // namespace mlpack
269 
270 // Include implementation.
271 #include "max_pooling_impl.hpp"
272 
273 #endif
OutputDataType const & OutputParameter() const
Get the output parameter.
Definition: max_pooling.hpp:98
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
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.
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.