mlpack 3.4.2
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
35namespace mlpack {
36namespace 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
84template <
85 typename InputDataType = arma::mat,
86 typename OutputDataType = arma::mat
87>
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
174 size_t const& Depth() const { return depth; }
175
177 size_t const& Scale() const { return scale; }
178
180 size_t InSize() const { return inSize; }
181
183 size_t GlimpseSize() const { return size;}
184
188 template<typename Archive>
189 void serialize(Archive& ar, const unsigned int /* version */);
190
191 private:
192 /*
193 * Transform the given input by changing rows to columns.
194 *
195 * @param w The input matrix used to perform the transformation.
196 */
197 void Transform(arma::mat& w)
198 {
199 arma::mat t = w;
200
201 for (size_t i = 0, k = 0; i < w.n_elem; ++k)
202 {
203 for (size_t j = 0; j < w.n_cols; ++j, ++i)
204 {
205 w(k, j) = t(i);
206 }
207 }
208 }
209
210 /*
211 * Transform the given input by changing rows to columns.
212 *
213 * @param w The input matrix used to perform the transformation.
214 */
215 void Transform(arma::cube& w)
216 {
217 for (size_t i = 0; i < w.n_slices; ++i)
218 {
219 arma::mat t = w.slice(i);
220 Transform(t);
221 w.slice(i) = t;
222 }
223 }
224
232 template<typename eT>
233 void Pooling(const size_t kSize,
234 const arma::Mat<eT>& input,
235 arma::Mat<eT>& output)
236 {
237 const size_t rStep = kSize;
238 const size_t cStep = kSize;
239
240 for (size_t j = 0; j < input.n_cols; j += cStep)
241 {
242 for (size_t i = 0; i < input.n_rows; i += rStep)
243 {
244 output(i / rStep, j / cStep) += pooling.Pooling(
245 input(arma::span(i, i + rStep - 1), arma::span(j, j + cStep - 1)));
246 }
247 }
248 }
249
257 template<typename eT>
258 void Unpooling(const arma::Mat<eT>& input,
259 const arma::Mat<eT>& error,
260 arma::Mat<eT>& output)
261 {
262 const size_t rStep = input.n_rows / error.n_rows;
263 const size_t cStep = input.n_cols / error.n_cols;
264
265 arma::Mat<eT> unpooledError;
266 for (size_t j = 0; j < input.n_cols; j += cStep)
267 {
268 for (size_t i = 0; i < input.n_rows; i += rStep)
269 {
270 const arma::Mat<eT>& inputArea = input(arma::span(i, i + rStep - 1),
271 arma::span(j, j + cStep - 1));
272
273 pooling.Unpooling(inputArea, error(i / rStep, j / cStep),
274 unpooledError);
275
276 output(arma::span(i, i + rStep - 1),
277 arma::span(j, j + cStep - 1)) += unpooledError;
278 }
279 }
280 }
281
289 template<typename eT>
290 void ReSampling(const arma::Mat<eT>& input, arma::Mat<eT>& output)
291 {
292 double wRatio = (double) (input.n_rows - 1) / (size - 1);
293 double hRatio = (double) (input.n_cols - 1) / (size - 1);
294
295 double iWidth = input.n_rows - 1;
296 double iHeight = input.n_cols - 1;
297
298 for (size_t y = 0; y < size; y++)
299 {
300 for (size_t x = 0; x < size; x++)
301 {
302 double ix = wRatio * x;
303 double iy = hRatio * y;
304
305 // Get the 4 nearest neighbors.
306 double ixNw = std::floor(ix);
307 double iyNw = std::floor(iy);
308 double ixNe = ixNw + 1;
309 double iySw = iyNw + 1;
310
311 // Get surfaces to each neighbor.
312 double se = (ix - ixNw) * (iy - iyNw);
313 double sw = (ixNe - ix) * (iy - iyNw);
314 double ne = (ix - ixNw) * (iySw - iy);
315 double nw = (ixNe - ix) * (iySw - iy);
316
317 // Calculate the weighted sum.
318 output(y, x) = input(iyNw, ixNw) * nw +
319 input(iyNw, std::min(ixNe, iWidth)) * ne +
320 input(std::min(iySw, iHeight), ixNw) * sw +
321 input(std::min(iySw, iHeight), std::min(ixNe, iWidth)) * se;
322 }
323 }
324 }
325
334 template<typename eT>
335 void DownwardReSampling(const arma::Mat<eT>& input,
336 const arma::Mat<eT>& error,
337 arma::Mat<eT>& output)
338 {
339 double iWidth = input.n_rows - 1;
340 double iHeight = input.n_cols - 1;
341
342 double wRatio = iWidth / (size - 1);
343 double hRatio = iHeight / (size - 1);
344
345 for (size_t y = 0; y < size; y++)
346 {
347 for (size_t x = 0; x < size; x++)
348 {
349 double ix = wRatio * x;
350 double iy = hRatio * y;
351
352 // Get the 4 nearest neighbors.
353 double ixNw = std::floor(ix);
354 double iyNw = std::floor(iy);
355 double ixNe = ixNw + 1;
356 double iySw = iyNw + 1;
357
358 // Get surfaces to each neighbor.
359 double se = (ix - ixNw) * (iy - iyNw);
360 double sw = (ixNe - ix) * (iy - iyNw);
361 double ne = (ix - ixNw) * (iySw - iy);
362 double nw = (ixNe - ix) * (iySw - iy);
363
364 double ograd = error(y, x);
365
366 output(iyNw, ixNw) = output(iyNw, ixNw) + nw * ograd;
367 output(iyNw, std::min(ixNe, iWidth)) = output(iyNw,
368 std::min(ixNe, iWidth)) + ne * ograd;
369 output(std::min(iySw, iHeight), ixNw) = output(std::min(iySw, iHeight),
370 ixNw) + sw * ograd;
371 output(std::min(iySw, iHeight), std::min(ixNe, iWidth)) = output(
372 std::min(iySw, iHeight), std::min(ixNe, iWidth)) + se * ograd;
373 }
374 }
375 }
376
378 size_t inSize;
379
381 size_t size;
382
384 size_t depth;
385
387 size_t scale;
388
390 size_t inputWidth;
391
393 size_t inputHeight;
394
396 size_t outputWidth;
397
399 size_t outputHeight;
400
402 OutputDataType delta;
403
405 OutputDataType outputParameter;
406
408 size_t inputDepth;
409
411 arma::cube inputTemp;
412
414 arma::cube outputTemp;
415
417 arma::mat location;
418
420 MeanPoolingRule pooling;
421
423 std::vector<arma::mat> locationParameter;
424
426 arma::cube gTemp;
427
429 bool deterministic;
430}; // class GlimpseLayer
431
432} // namespace ann
433} // namespace mlpack
434
435// Include implementation.
436#include "glimpse_impl.hpp"
437
438#endif
The glimpse layer returns a retina-like representation (down-scaled cropped images) of increasing sca...
Definition: glimpse.hpp:89
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 & Depth() const
Get the number of patches to crop per glimpse.
Definition: glimpse.hpp:174
size_t const & OutputHeight() const
Get the output height.
Definition: glimpse.hpp:164
size_t & InputHeight()
Modify the input height.
Definition: glimpse.hpp:156
void Forward(const arma::Mat< eT > &input, arma::Mat< eT > &output)
Ordinary feed forward pass of the glimpse layer.
size_t GlimpseSize() const
Get the used glimpse size (height = width).
Definition: glimpse.hpp:183
OutputDataType & OutputParameter() const
Get the output parameter.
Definition: glimpse.hpp:132
OutputDataType & Delta() const
Get the detla.
Definition: glimpse.hpp:137
size_t & InputWidth()
Modify input the width.
Definition: glimpse.hpp:151
size_t & OutputHeight()
Modify the output height.
Definition: glimpse.hpp:166
void Location(const arma::mat &location)
Set the locationthe x and y coordinate of the center of the output glimpse.
Definition: glimpse.hpp:143
bool & Deterministic()
Modify the value of the deterministic parameter.
Definition: glimpse.hpp:171
bool Deterministic() const
Get the value of the deterministic parameter.
Definition: glimpse.hpp:169
size_t const & InputWidth() const
Get the input width.
Definition: glimpse.hpp:149
void Backward(const arma::Mat< eT > &, const arma::Mat< eT > &gy, arma::Mat< eT > &g)
Ordinary feed backward pass of the glimpse layer.
OutputDataType & OutputParameter()
Modify the output parameter.
Definition: glimpse.hpp:134
size_t & OutputWidth()
Modify the output width.
Definition: glimpse.hpp:161
size_t InSize() const
Get the size of the input units.
Definition: glimpse.hpp:180
size_t const & InputHeight() const
Get the input height.
Definition: glimpse.hpp:154
size_t const & OutputWidth() const
Get the output width.
Definition: glimpse.hpp:159
size_t const & Scale() const
Get the scale fraction.
Definition: glimpse.hpp:177
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
OutputDataType & Delta()
Modify the delta.
Definition: glimpse.hpp:139
void Unpooling(const MatType &input, const double value, MatType &output)
Definition: glimpse.hpp:65
double Pooling(const MatType &input)
Definition: glimpse.hpp:52
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
The core includes that mlpack expects; standard C++ includes and Armadillo.