mlpack  master
glimpse.hpp
Go to the documentation of this file.
1 
21 #ifndef MLPACK_METHODS_ANN_LAYER_GLIMPSE_HPP
22 #define MLPACK_METHODS_ANN_LAYER_GLIMPSE_HPP
23 
24 #include <mlpack/prereqs.hpp>
25 
26 #include "layer_types.hpp"
27 #include <algorithm>
28 
29 namespace mlpack {
30 namespace ann {
31 
32 
33 /*
34  * The mean pooling rule for convolution neural networks. Average all values
35  * within the receptive block.
36  */
38 {
39  public:
40  /*
41  * Return the average value within the receptive block.
42  *
43  * @param input Input used to perform the pooling operation.
44  */
45  template<typename MatType>
46  double Pooling(const MatType& input)
47  {
48  return arma::mean(arma::mean(input));
49  }
50 
51  /*
52  * Set the average value within the receptive block.
53  *
54  * @param input Input used to perform the pooling operation.
55  * @param value The unpooled value.
56  * @param output The unpooled output data.
57  */
58  template<typename MatType>
59  void Unpooling(const MatType& input, const double value, MatType& output)
60  {
61  output = arma::zeros<MatType>(input.n_rows, input.n_cols);
62  const double mean = arma::mean(arma::mean(input));
63 
64  output.elem(arma::find(mean == input, 1)).fill(value);
65  }
66 };
67 
78 template <
79  typename InputDataType = arma::mat,
80  typename OutputDataType = arma::mat
81 >
82 class Glimpse
83 {
84  public:
85 
98  Glimpse(const size_t inSize,
99  const size_t size,
100  const size_t depth = 3,
101  const size_t scale = 2,
102  const size_t inputWidth = 0,
103  const size_t inputHeight = 0);
104 
111  template<typename eT>
112  void Forward(const arma::Mat<eT>&& input, arma::Mat<eT>&& output);
113 
121  template<typename eT>
122  void Backward(const arma::Mat<eT>&& /* input */,
123  arma::Mat<eT>&& gy,
124  arma::Mat<eT>&& g);
125 
127  InputDataType& InputParameter() const {return inputParameter; }
129  InputDataType& InputParameter() { return inputParameter; }
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 
226  const size_t rStep = kSize;
227  const size_t cStep = kSize;
228 
229  for (size_t j = 0; j < input.n_cols; j += cStep)
230  {
231  for (size_t i = 0; i < input.n_rows; i += rStep)
232  {
233  output(i / rStep, j / cStep) += pooling.Pooling(
234  input(arma::span(i, i + rStep - 1), arma::span(j, j + cStep - 1)));
235  }
236  }
237  }
238 
246  template<typename eT>
247  void Unpooling(const arma::Mat<eT>& input,
248  const arma::Mat<eT>& error,
249  arma::Mat<eT>& output)
250  {
251  const size_t rStep = input.n_rows / error.n_rows;
252  const size_t cStep = input.n_cols / error.n_cols;
253 
254  arma::Mat<eT> unpooledError;
255  for (size_t j = 0; j < input.n_cols; j += cStep)
256  {
257  for (size_t i = 0; i < input.n_rows; i += rStep)
258  {
259  const arma::Mat<eT>& inputArea = input(arma::span(i, i + rStep - 1),
260  arma::span(j, j + cStep - 1));
261 
262  pooling.Unpooling(inputArea, error(i / rStep, j / cStep),
263  unpooledError);
264 
265  output(arma::span(i, i + rStep - 1),
266  arma::span(j, j + cStep - 1)) += unpooledError;
267  }
268  }
269  }
270 
278  template<typename eT>
279  void ReSampling(const arma::Mat<eT>& input, arma::Mat<eT>& output)
280  {
281  double wRatio = (double) (input.n_rows - 1) / (size - 1);
282  double hRatio = (double) (input.n_cols - 1) / (size - 1);
283 
284  double iWidth = input.n_rows - 1;
285  double iHeight = input.n_cols - 1;
286 
287  for (size_t y = 0; y < size; y++)
288  {
289  for (size_t x = 0; x < size; x++)
290  {
291  double ix = wRatio * x;
292  double iy = hRatio * y;
293 
294  // Get the 4 nearest neighbors.
295  double ixNw = std::floor(ix);
296  double iyNw = std::floor(iy);
297  double ixNe = ixNw + 1;
298  double iySw = iyNw + 1;
299 
300  // Get surfaces to each neighbor.
301  double se = (ix - ixNw) * (iy - iyNw);
302  double sw = (ixNe - ix) * (iy - iyNw);
303  double ne = (ix - ixNw) * (iySw - iy);
304  double nw = (ixNe - ix) * (iySw - iy);
305 
306  // Calculate the weighted sum.
307  output(y, x) = input(iyNw, ixNw) * nw +
308  input(iyNw, std::min(ixNe, iWidth)) * ne +
309  input(std::min(iySw, iHeight), ixNw) * sw +
310  input(std::min(iySw, iHeight), std::min(ixNe, iWidth)) * se;
311  }
312  }
313  }
314 
323  template<typename eT>
324  void DownwardReSampling(const arma::Mat<eT>& input,
325  const arma::Mat<eT>& error,
326  arma::Mat<eT>& output)
327  {
328  double iWidth = input.n_rows - 1;
329  double iHeight = input.n_cols - 1;
330 
331  double wRatio = iWidth / (size - 1);
332  double hRatio = iHeight / (size - 1);
333 
334  for (size_t y = 0; y < size; y++)
335  {
336  for (size_t x = 0; x < size; x++)
337  {
338  double ix = wRatio * x;
339  double iy = hRatio * y;
340 
341  // Get the 4 nearest neighbors.
342  double ixNw = std::floor(ix);
343  double iyNw = std::floor(iy);
344  double ixNe = ixNw + 1;
345  double iySw = iyNw + 1;
346 
347  // Get surfaces to each neighbor.
348  double se = (ix - ixNw) * (iy - iyNw);
349  double sw = (ixNe - ix) * (iy - iyNw);
350  double ne = (ix - ixNw) * (iySw - iy);
351  double nw = (ixNe - ix) * (iySw - iy);
352 
353  double ograd = error(y, x);
354 
355  output(iyNw, ixNw) = output(iyNw, ixNw) + nw * ograd;
356  output(iyNw, std::min(ixNe, iWidth)) = output(iyNw,
357  std::min(ixNe, iWidth)) + ne * ograd;
358  output(std::min(iySw, iHeight), ixNw) = output(std::min(iySw, iHeight),
359  ixNw) + sw * ograd;
360  output(std::min(iySw, iHeight), std::min(ixNe, iWidth)) = output(
361  std::min(iySw, iHeight), std::min(ixNe, iWidth)) + se * ograd;
362  }
363  }
364  }
365 
367  size_t inSize;
368 
370  size_t size;
371 
373  size_t depth;
374 
376  size_t scale;
377 
379  size_t inputWidth;
380 
382  size_t inputHeight;
383 
385  size_t outputWidth;
386 
388  size_t outputHeight;
389 
391  OutputDataType delta;
392 
394  InputDataType inputParameter;
395 
397  OutputDataType outputParameter;
398 
400  size_t inputDepth;
401 
403  arma::cube inputTemp;
404 
406  arma::cube outputTemp;
407 
409  arma::mat location;
410 
413 
415  std::vector<arma::mat> locationParameter;
416 
418  arma::cube gTemp;
419 
422 }; // class GlimpseLayer
423 
424 } // namespace ann
425 } // namespace mlpack
426 
427 // Include implementation.
428 #include "glimpse_impl.hpp"
429 
430 #endif
OutputDataType & Delta() const
Get the detla.
Definition: glimpse.hpp:137
InputDataType inputParameter
Locally-stored input parameter object.
Definition: glimpse.hpp:394
size_t const & OutputWidth() const
Get the output width.
Definition: glimpse.hpp:159
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: binarize.hpp:18
size_t scale
The scale fraction.
Definition: glimpse.hpp:376
void Unpooling(const arma::Mat< eT > &input, const arma::Mat< eT > &error, arma::Mat< eT > &output)
Apply unpooling to the input and store the results.
Definition: glimpse.hpp:247
std::vector< arma::mat > locationParameter
Location-stored module location parameter.
Definition: glimpse.hpp:415
double Pooling(const MatType &input)
Definition: glimpse.hpp:46
The core includes that mlpack expects; standard C++ includes and Armadillo.
size_t depth
The number of patches to crop per glimpse.
Definition: glimpse.hpp:373
arma::cube gTemp
Location-stored transformed gradient paramter.
Definition: glimpse.hpp:418
bool Deterministic() const
Get the value of the deterministic parameter.
Definition: glimpse.hpp:169
arma::mat location
The x and y coordinate of the center of the output glimpse.
Definition: glimpse.hpp:409
OutputDataType & OutputParameter() const
Get the output parameter.
Definition: glimpse.hpp:132
void Pooling(const size_t kSize, const arma::Mat< eT > &input, arma::Mat< eT > &output)
Apply pooling to the input and store the results to the output parameter.
Definition: glimpse.hpp:221
void DownwardReSampling(const arma::Mat< eT > &input, const arma::Mat< eT > &error, arma::Mat< eT > &output)
Apply DownwardReSampling to the input and store the results into the output parameter.
Definition: glimpse.hpp:324
size_t inputHeight
Locally-stored input height.
Definition: glimpse.hpp:382
size_t const & OutputHeight() const
Get the output height.
Definition: glimpse.hpp:164
size_t inputDepth
Locally-stored depth of the input.
Definition: glimpse.hpp:400
size_t & OutputHeight()
Modify the output height.
Definition: glimpse.hpp:166
size_t outputWidth
Locally-stored output width.
Definition: glimpse.hpp:385
InputDataType & InputParameter()
Modify the input parameter.
Definition: glimpse.hpp:129
size_t & InputWidth()
Modify input the width.
Definition: glimpse.hpp:151
void Transform(arma::cube &w)
Definition: glimpse.hpp:203
bool & Deterministic()
Modify the value of the deterministic parameter.
Definition: glimpse.hpp:171
void Transform(arma::mat &w)
Definition: glimpse.hpp:185
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()
Modify the delta.
Definition: glimpse.hpp:139
size_t const & InputHeight() const
Get the input height.
Definition: glimpse.hpp:154
size_t inSize
The size of the input units.
Definition: glimpse.hpp:367
size_t & OutputWidth()
Modify the output width.
Definition: glimpse.hpp:161
arma::cube outputTemp
Locally-stored transformed output parameter.
Definition: glimpse.hpp:406
The glimpse layer returns a retina-like representation (down-scaled cropped images) of increasing sca...
Definition: glimpse.hpp:82
size_t size
The used glimpse size (height = width).
Definition: glimpse.hpp:370
MeanPoolingRule pooling
Locally-stored object to perform the mean pooling operation.
Definition: glimpse.hpp:412
bool deterministic
If true use maximum a posteriori during the forward pass.
Definition: glimpse.hpp:421
arma::cube inputTemp
Locally-stored transformed input parameter.
Definition: glimpse.hpp:403
void Unpooling(const MatType &input, const double value, MatType &output)
Definition: glimpse.hpp:59
size_t & InputHeight()
Modify the input height.
Definition: glimpse.hpp:156
void ReSampling(const arma::Mat< eT > &input, arma::Mat< eT > &output)
Apply ReSampling to the input and store the results in the output parameter.
Definition: glimpse.hpp:279
OutputDataType & OutputParameter()
Modify the output parameter.
Definition: glimpse.hpp:134
InputDataType & InputParameter() const
Get the input parameter.
Definition: glimpse.hpp:127
size_t inputWidth
Locally-stored input width.
Definition: glimpse.hpp:379
size_t outputHeight
Locally-stored output height.
Definition: glimpse.hpp:388
OutputDataType delta
Locally-stored delta object.
Definition: glimpse.hpp:391
OutputDataType outputParameter
Locally-stored output parameter object.
Definition: glimpse.hpp:397
size_t const & InputWidth() const
Get the input width.
Definition: glimpse.hpp:149