27#ifndef MLPACK_METHODS_ANN_LAYER_GLIMPSE_HPP
28#define MLPACK_METHODS_ANN_LAYER_GLIMPSE_HPP
51 template<
typename MatType>
54 return arma::mean(arma::mean(input));
64 template<
typename MatType>
65 void Unpooling(
const MatType& input,
const double value, MatType& output)
67 output = arma::zeros<MatType>(input.n_rows, input.n_cols);
68 const double mean = arma::mean(arma::mean(input));
70 output.elem(arma::find(mean == input, 1)).fill(value);
85 typename InputDataType = arma::mat,
86 typename OutputDataType = arma::mat
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);
116 template<
typename eT>
117 void Forward(
const arma::Mat<eT>& input, arma::Mat<eT>& output);
126 template<
typename eT>
128 const arma::Mat<eT>& gy,
137 OutputDataType&
Delta()
const {
return delta; }
139 OutputDataType&
Delta() {
return delta; }
145 this->location = location;
174 size_t const&
Depth()
const {
return depth; }
177 size_t const&
Scale()
const {
return scale; }
188 template<
typename Archive>
197 void Transform(arma::mat& w)
201 for (
size_t i = 0, k = 0; i < w.n_elem; ++k)
203 for (
size_t j = 0; j < w.n_cols; ++j, ++i)
215 void Transform(arma::cube& w)
217 for (
size_t i = 0; i < w.n_slices; ++i)
219 arma::mat t = w.slice(i);
232 template<
typename eT>
233 void Pooling(
const size_t kSize,
234 const arma::Mat<eT>& input,
235 arma::Mat<eT>& output)
237 const size_t rStep = kSize;
238 const size_t cStep = kSize;
240 for (
size_t j = 0; j < input.n_cols; j += cStep)
242 for (
size_t i = 0; i < input.n_rows; i += rStep)
244 output(i / rStep, j / cStep) += pooling.
Pooling(
245 input(arma::span(i, i + rStep - 1), arma::span(j, j + cStep - 1)));
257 template<
typename eT>
258 void Unpooling(
const arma::Mat<eT>& input,
259 const arma::Mat<eT>& error,
260 arma::Mat<eT>& output)
262 const size_t rStep = input.n_rows / error.n_rows;
263 const size_t cStep = input.n_cols / error.n_cols;
265 arma::Mat<eT> unpooledError;
266 for (
size_t j = 0; j < input.n_cols; j += cStep)
268 for (
size_t i = 0; i < input.n_rows; i += rStep)
270 const arma::Mat<eT>& inputArea = input(arma::span(i, i + rStep - 1),
271 arma::span(j, j + cStep - 1));
273 pooling.
Unpooling(inputArea, error(i / rStep, j / cStep),
276 output(arma::span(i, i + rStep - 1),
277 arma::span(j, j + cStep - 1)) += unpooledError;
289 template<
typename eT>
290 void ReSampling(
const arma::Mat<eT>& input, arma::Mat<eT>& output)
292 double wRatio = (double) (input.n_rows - 1) / (size - 1);
293 double hRatio = (double) (input.n_cols - 1) / (size - 1);
295 double iWidth = input.n_rows - 1;
296 double iHeight = input.n_cols - 1;
298 for (
size_t y = 0; y < size; y++)
300 for (
size_t x = 0; x < size; x++)
302 double ix = wRatio * x;
303 double iy = hRatio * y;
306 double ixNw = std::floor(ix);
307 double iyNw = std::floor(iy);
308 double ixNe = ixNw + 1;
309 double iySw = iyNw + 1;
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);
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;
334 template<
typename eT>
335 void DownwardReSampling(
const arma::Mat<eT>& input,
336 const arma::Mat<eT>& error,
337 arma::Mat<eT>& output)
339 double iWidth = input.n_rows - 1;
340 double iHeight = input.n_cols - 1;
342 double wRatio = iWidth / (size - 1);
343 double hRatio = iHeight / (size - 1);
345 for (
size_t y = 0; y < size; y++)
347 for (
size_t x = 0; x < size; x++)
349 double ix = wRatio * x;
350 double iy = hRatio * y;
353 double ixNw = std::floor(ix);
354 double iyNw = std::floor(iy);
355 double ixNe = ixNw + 1;
356 double iySw = iyNw + 1;
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);
364 double ograd = error(y, x);
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),
371 output(std::min(iySw, iHeight), std::min(ixNe, iWidth)) = output(
372 std::min(iySw, iHeight), std::min(ixNe, iWidth)) + se * ograd;
402 OutputDataType delta;
405 OutputDataType outputParameter;
411 arma::cube inputTemp;
414 arma::cube outputTemp;
420 MeanPoolingRule pooling;
423 std::vector<arma::mat> locationParameter;
436#include "glimpse_impl.hpp"
The glimpse layer returns a retina-like representation (down-scaled cropped images) of increasing sca...
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.
size_t const & OutputHeight() const
Get the output height.
size_t & InputHeight()
Modify the input height.
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).
OutputDataType & OutputParameter() const
Get the output parameter.
OutputDataType & Delta() const
Get the detla.
size_t & InputWidth()
Modify input the width.
size_t & OutputHeight()
Modify the output height.
void Location(const arma::mat &location)
Set the locationthe x and y coordinate of the center of the output glimpse.
bool & Deterministic()
Modify the value of the deterministic parameter.
bool Deterministic() const
Get the value of the deterministic parameter.
size_t const & InputWidth() const
Get the input width.
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.
size_t & OutputWidth()
Modify the output width.
size_t InSize() const
Get the size of the input units.
size_t const & InputHeight() const
Get the input height.
size_t const & OutputWidth() const
Get the output width.
size_t const & Scale() const
Get the scale fraction.
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
OutputDataType & Delta()
Modify the delta.
void Unpooling(const MatType &input, const double value, MatType &output)
double Pooling(const MatType &input)
Linear algebra utility functions, generally performed on matrices or vectors.
The core includes that mlpack expects; standard C++ includes and Armadillo.