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