mlpack  3.1.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
glimpse.hpp
Go to the documentation of this file.
1 
27 #ifndef MLPACK_METHODS_ANN_LAYER_GLIMPSE_HPP
28 #define MLPACK_METHODS_ANN_LAYER_GLIMPSE_HPP
29 
30 #include <mlpack/prereqs.hpp>
31 
32 #include "layer_types.hpp"
33 #include <algorithm>
34 
35 namespace mlpack {
36 namespace ann {
37 
38 
39 /*
40  * The mean pooling rule for convolution neural networks. Average all values
41  * within the receptive block.
42  */
44 {
45  public:
46  /*
47  * Return the average value within the receptive block.
48  *
49  * @param input Input used to perform the pooling operation.
50  */
51  template<typename MatType>
52  double Pooling(const MatType& input)
53  {
54  return arma::mean(arma::mean(input));
55  }
56 
57  /*
58  * Set the average value within the receptive block.
59  *
60  * @param input Input used to perform the pooling operation.
61  * @param value The unpooled value.
62  * @param output The unpooled output data.
63  */
64  template<typename MatType>
65  void Unpooling(const MatType& input, const double value, MatType& output)
66  {
67  output = arma::zeros<MatType>(input.n_rows, input.n_cols);
68  const double mean = arma::mean(arma::mean(input));
69 
70  output.elem(arma::find(mean == input, 1)).fill(value);
71  }
72 };
73 
84 template <
85  typename InputDataType = arma::mat,
86  typename OutputDataType = arma::mat
87 >
88 class Glimpse
89 {
90  public:
103  Glimpse(const size_t inSize = 0,
104  const size_t size = 0,
105  const size_t depth = 3,
106  const size_t scale = 2,
107  const size_t inputWidth = 0,
108  const size_t inputHeight = 0);
109 
116  template<typename eT>
117  void Forward(const arma::Mat<eT>& input, arma::Mat<eT>& output);
118 
126  template<typename eT>
127  void Backward(const arma::Mat<eT>& /* input */,
128  const arma::Mat<eT>& gy,
129  arma::Mat<eT>& g);
130 
132  OutputDataType& OutputParameter() const {return outputParameter; }
134  OutputDataType& OutputParameter() { return outputParameter; }
135 
137  OutputDataType& Delta() const { return delta; }
139  OutputDataType& Delta() { return delta; }
140 
143  void Location(const arma::mat& location)
144  {
145  this->location = location;
146  }
147 
149  size_t const& InputWidth() const { return inputWidth; }
151  size_t& InputWidth() { return inputWidth; }
152 
154  size_t const& InputHeight() const { return inputHeight; }
156  size_t& InputHeight() { return inputHeight; }
157 
159  size_t const& OutputWidth() const { return outputWidth; }
161  size_t& OutputWidth() { return outputWidth; }
162 
164  size_t const& OutputHeight() const { return outputHeight; }
166  size_t& OutputHeight() { return outputHeight; }
167 
169  bool Deterministic() const { return deterministic; }
171  bool& Deterministic() { return deterministic; }
172 
176  template<typename Archive>
177  void serialize(Archive& ar, const unsigned int /* version */);
178 
179  private:
180  /*
181  * Transform the given input by changing rows to columns.
182  *
183  * @param w The input matrix used to perform the transformation.
184  */
185  void Transform(arma::mat& w)
186  {
187  arma::mat t = w;
188 
189  for (size_t i = 0, k = 0; i < w.n_elem; k++)
190  {
191  for (size_t j = 0; j < w.n_cols; j++, i++)
192  {
193  w(k, j) = t(i);
194  }
195  }
196  }
197 
198  /*
199  * Transform the given input by changing rows to columns.
200  *
201  * @param w The input matrix used to perform the transformation.
202  */
203  void Transform(arma::cube& w)
204  {
205  for (size_t i = 0; i < w.n_slices; i++)
206  {
207  arma::mat t = w.slice(i);
208  Transform(t);
209  w.slice(i) = t;
210  }
211  }
212 
220  template<typename eT>
221  void Pooling(const size_t kSize,
222  const arma::Mat<eT>& input,
223  arma::Mat<eT>& output)
224  {
225  const size_t rStep = kSize;
226  const size_t cStep = kSize;
227 
228  for (size_t j = 0; j < input.n_cols; j += cStep)
229  {
230  for (size_t i = 0; i < input.n_rows; i += rStep)
231  {
232  output(i / rStep, j / cStep) += pooling.Pooling(
233  input(arma::span(i, i + rStep - 1), arma::span(j, j + cStep - 1)));
234  }
235  }
236  }
237 
245  template<typename eT>
246  void Unpooling(const arma::Mat<eT>& input,
247  const arma::Mat<eT>& error,
248  arma::Mat<eT>& output)
249  {
250  const size_t rStep = input.n_rows / error.n_rows;
251  const size_t cStep = input.n_cols / error.n_cols;
252 
253  arma::Mat<eT> unpooledError;
254  for (size_t j = 0; j < input.n_cols; j += cStep)
255  {
256  for (size_t i = 0; i < input.n_rows; i += rStep)
257  {
258  const arma::Mat<eT>& inputArea = input(arma::span(i, i + rStep - 1),
259  arma::span(j, j + cStep - 1));
260 
261  pooling.Unpooling(inputArea, error(i / rStep, j / cStep),
262  unpooledError);
263 
264  output(arma::span(i, i + rStep - 1),
265  arma::span(j, j + cStep - 1)) += unpooledError;
266  }
267  }
268  }
269 
277  template<typename eT>
278  void ReSampling(const arma::Mat<eT>& input, arma::Mat<eT>& output)
279  {
280  double wRatio = (double) (input.n_rows - 1) / (size - 1);
281  double hRatio = (double) (input.n_cols - 1) / (size - 1);
282 
283  double iWidth = input.n_rows - 1;
284  double iHeight = input.n_cols - 1;
285 
286  for (size_t y = 0; y < size; y++)
287  {
288  for (size_t x = 0; x < size; x++)
289  {
290  double ix = wRatio * x;
291  double iy = hRatio * y;
292 
293  // Get the 4 nearest neighbors.
294  double ixNw = std::floor(ix);
295  double iyNw = std::floor(iy);
296  double ixNe = ixNw + 1;
297  double iySw = iyNw + 1;
298 
299  // Get surfaces to each neighbor.
300  double se = (ix - ixNw) * (iy - iyNw);
301  double sw = (ixNe - ix) * (iy - iyNw);
302  double ne = (ix - ixNw) * (iySw - iy);
303  double nw = (ixNe - ix) * (iySw - iy);
304 
305  // Calculate the weighted sum.
306  output(y, x) = input(iyNw, ixNw) * nw +
307  input(iyNw, std::min(ixNe, iWidth)) * ne +
308  input(std::min(iySw, iHeight), ixNw) * sw +
309  input(std::min(iySw, iHeight), std::min(ixNe, iWidth)) * se;
310  }
311  }
312  }
313 
322  template<typename eT>
323  void DownwardReSampling(const arma::Mat<eT>& input,
324  const arma::Mat<eT>& error,
325  arma::Mat<eT>& output)
326  {
327  double iWidth = input.n_rows - 1;
328  double iHeight = input.n_cols - 1;
329 
330  double wRatio = iWidth / (size - 1);
331  double hRatio = iHeight / (size - 1);
332 
333  for (size_t y = 0; y < size; y++)
334  {
335  for (size_t x = 0; x < size; x++)
336  {
337  double ix = wRatio * x;
338  double iy = hRatio * y;
339 
340  // Get the 4 nearest neighbors.
341  double ixNw = std::floor(ix);
342  double iyNw = std::floor(iy);
343  double ixNe = ixNw + 1;
344  double iySw = iyNw + 1;
345 
346  // Get surfaces to each neighbor.
347  double se = (ix - ixNw) * (iy - iyNw);
348  double sw = (ixNe - ix) * (iy - iyNw);
349  double ne = (ix - ixNw) * (iySw - iy);
350  double nw = (ixNe - ix) * (iySw - iy);
351 
352  double ograd = error(y, x);
353 
354  output(iyNw, ixNw) = output(iyNw, ixNw) + nw * ograd;
355  output(iyNw, std::min(ixNe, iWidth)) = output(iyNw,
356  std::min(ixNe, iWidth)) + ne * ograd;
357  output(std::min(iySw, iHeight), ixNw) = output(std::min(iySw, iHeight),
358  ixNw) + sw * ograd;
359  output(std::min(iySw, iHeight), std::min(ixNe, iWidth)) = output(
360  std::min(iySw, iHeight), std::min(ixNe, iWidth)) + se * ograd;
361  }
362  }
363  }
364 
366  size_t inSize;
367 
369  size_t size;
370 
372  size_t depth;
373 
375  size_t scale;
376 
378  size_t inputWidth;
379 
381  size_t inputHeight;
382 
384  size_t outputWidth;
385 
387  size_t outputHeight;
388 
390  OutputDataType delta;
391 
393  OutputDataType outputParameter;
394 
396  size_t inputDepth;
397 
399  arma::cube inputTemp;
400 
402  arma::cube outputTemp;
403 
405  arma::mat location;
406 
408  MeanPoolingRule pooling;
409 
411  std::vector<arma::mat> locationParameter;
412 
414  arma::cube gTemp;
415 
417  bool deterministic;
418 }; // class GlimpseLayer
419 
420 } // namespace ann
421 } // namespace mlpack
422 
423 // Include implementation.
424 #include "glimpse_impl.hpp"
425 
426 #endif
size_t & InputHeight()
Modify the input height.
Definition: glimpse.hpp:156
void Backward(const arma::Mat< eT > &, const arma::Mat< eT > &gy, arma::Mat< eT > &g)
Ordinary feed backward pass of the glimpse layer.
double Pooling(const MatType &input)
Definition: glimpse.hpp:52
The core includes that mlpack expects; standard C++ includes and Armadillo.
void Forward(const arma::Mat< eT > &input, arma::Mat< eT > &output)
Ordinary feed forward pass of the glimpse layer.
size_t const & InputWidth() const
Get the input width.
Definition: glimpse.hpp:149
size_t & InputWidth()
Modify input the width.
Definition: glimpse.hpp:151
void Unpooling(const MatType &input, const double value, MatType &output)
Definition: glimpse.hpp:65
size_t & OutputWidth()
Modify the output width.
Definition: glimpse.hpp:161
bool Deterministic() const
Get the value of the deterministic parameter.
Definition: glimpse.hpp:169
OutputDataType & Delta()
Modify the delta.
Definition: glimpse.hpp:139
OutputDataType & OutputParameter()
Modify the output parameter.
Definition: glimpse.hpp:134
bool & Deterministic()
Modify the value of the deterministic parameter.
Definition: glimpse.hpp:171
void Location(const arma::mat &location)
Set the locationthe x and y coordinate of the center of the output glimpse.
Definition: glimpse.hpp:143
OutputDataType & Delta() const
Get the detla.
Definition: glimpse.hpp:137
OutputDataType & OutputParameter() const
Get the output parameter.
Definition: glimpse.hpp:132
The glimpse layer returns a retina-like representation (down-scaled cropped images) of increasing sca...
Definition: glimpse.hpp:88
size_t const & OutputWidth() const
Get the output width.
Definition: glimpse.hpp:159
size_t & OutputHeight()
Modify the output height.
Definition: glimpse.hpp:166
Glimpse(const size_t inSize=0, const size_t size=0, const size_t depth=3, const size_t scale=2, const size_t inputWidth=0, const size_t inputHeight=0)
Create the GlimpseLayer object using the specified ratio and rescale parameter.
size_t const & OutputHeight() const
Get the output height.
Definition: glimpse.hpp:164
size_t const & InputHeight() const
Get the input height.
Definition: glimpse.hpp:154
void serialize(Archive &ar, const unsigned int)
Serialize the layer.