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
   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);
   116   template<
typename eT>
   117   void Forward(
const arma::Mat<eT>& input, arma::Mat<eT>& output);
   126   template<
typename eT>
   127   void Backward(
const arma::Mat<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; }
   194   template<
typename Archive>
   195   void serialize(Archive& ar, 
const uint32_t );
   203   void Transform(arma::mat& w)
   207     for (
size_t i = 0, k = 0; i < w.n_elem; ++k)
   209       for (
size_t j = 0; j < w.n_cols; ++j, ++i)
   221   void Transform(arma::cube& w)
   223     for (
size_t i = 0; i < w.n_slices; ++i)
   225       arma::mat t = w.slice(i);
   238   template<
typename eT>
   239   void Pooling(
const size_t kSize,
   240                const arma::Mat<eT>& input,
   241                arma::Mat<eT>& output)
   243     const size_t rStep = kSize;
   244     const size_t cStep = kSize;
   246     for (
size_t j = 0; j < input.n_cols; j += cStep)
   248       for (
size_t i = 0; i < input.n_rows; i += rStep)
   250         output(i / rStep, j / cStep) += pooling.Pooling(
   251             input(arma::span(i, i + rStep - 1), arma::span(j, j + cStep - 1)));
   263   template<
typename eT>
   264   void Unpooling(
const arma::Mat<eT>& input,
   265                  const arma::Mat<eT>& error,
   266                  arma::Mat<eT>& output)
   268     const size_t rStep = input.n_rows / error.n_rows;
   269     const size_t cStep = input.n_cols / error.n_cols;
   271     arma::Mat<eT> unpooledError;
   272     for (
size_t j = 0; j < input.n_cols; j += cStep)
   274       for (
size_t i = 0; i < input.n_rows; i += rStep)
   276         const arma::Mat<eT>& inputArea = input(arma::span(i, i + rStep - 1),
   277                                                arma::span(j, j + cStep - 1));
   279         pooling.Unpooling(inputArea, error(i / rStep, j / cStep),
   282         output(arma::span(i, i + rStep - 1),
   283             arma::span(j, j + cStep - 1)) += unpooledError;
   295   template<
typename eT>
   296   void ReSampling(
const arma::Mat<eT>& input, arma::Mat<eT>& output)
   298     double wRatio = (double) (input.n_rows - 1) / (size - 1);
   299     double hRatio = (double) (input.n_cols - 1) / (size - 1);
   301     double iWidth = input.n_rows - 1;
   302     double iHeight = input.n_cols - 1;
   304     for (
size_t y = 0; y < size; y++)
   306       for (
size_t x = 0; x < size; x++)
   308         double ix = wRatio * x;
   309         double iy = hRatio * y;
   312         double ixNw = std::floor(ix);
   313         double iyNw = std::floor(iy);
   314         double ixNe = ixNw + 1;
   315         double iySw = iyNw + 1;
   318         double se = (ix - ixNw) * (iy - iyNw);
   319         double sw = (ixNe - ix) * (iy - iyNw);
   320         double ne = (ix - ixNw) * (iySw - iy);
   321         double nw = (ixNe - ix) * (iySw - iy);
   324         output(y, x) = input(iyNw, ixNw) * nw +
   325             input(iyNw, std::min(ixNe,  iWidth)) * ne +
   326             input(std::min(iySw, iHeight), ixNw) * sw +
   327             input(std::min(iySw, iHeight), std::min(ixNe, iWidth)) * se;
   340   template<
typename eT>
   341   void DownwardReSampling(
const arma::Mat<eT>& input,
   342                           const arma::Mat<eT>& error,
   343                           arma::Mat<eT>& output)
   345     double iWidth = input.n_rows - 1;
   346     double iHeight = input.n_cols - 1;
   348     double wRatio = iWidth / (size - 1);
   349     double hRatio = iHeight / (size - 1);
   351     for (
size_t y = 0; y < size; y++)
   353       for (
size_t x = 0; x < size; x++)
   355         double ix = wRatio * x;
   356         double iy = hRatio * y;
   359         double ixNw = std::floor(ix);
   360         double iyNw = std::floor(iy);
   361         double ixNe = ixNw + 1;
   362         double iySw = iyNw + 1;
   365         double se = (ix - ixNw) * (iy - iyNw);
   366         double sw = (ixNe - ix) * (iy - iyNw);
   367         double ne = (ix - ixNw) * (iySw - iy);
   368         double nw = (ixNe - ix) * (iySw - iy);
   370         double ograd = error(y, x);
   372         output(iyNw, ixNw) = output(iyNw, ixNw) + nw * ograd;
   373         output(iyNw, std::min(ixNe, iWidth)) = output(iyNw,
   374             std::min(ixNe, iWidth)) + ne * ograd;
   375         output(std::min(iySw, iHeight), ixNw) = output(std::min(iySw, iHeight),
   377         output(std::min(iySw, iHeight), std::min(ixNe, iWidth)) = output(
   378             std::min(iySw, iHeight), std::min(ixNe, iWidth)) + se * ograd;
   408   OutputDataType delta;
   411   OutputDataType outputParameter;
   417   arma::cube inputTemp;
   420   arma::cube outputTemp;
   429   std::vector<arma::mat> locationParameter;
   442 #include "glimpse_impl.hpp" size_t & InputHeight()
Modify the input height. 
 
size_t const  & OutputHeight() const
Get the output height. 
 
Linear algebra utility functions, generally performed on matrices or vectors. 
 
double Pooling(const MatType &input)
 
The core includes that mlpack expects; standard C++ includes and Armadillo. 
 
size_t GlimpseSize() const
Get the used glimpse size (height = width). 
 
size_t InputShape() const
Get the shape of the input. 
 
OutputDataType & OutputParameter() const
Get the output parameter. 
 
size_t & InputWidth()
Modify input the width. 
 
OutputDataType & Delta() const
Get the detla. 
 
void Unpooling(const MatType &input, const double value, MatType &output)
 
size_t & OutputWidth()
Modify the output width. 
 
size_t const  & InputWidth() const
Get the input width. 
 
OutputDataType & Delta()
Modify the delta. 
 
size_t InSize() const
Get the size of the input units. 
 
size_t const  & Scale() const
Get the scale fraction. 
 
OutputDataType & OutputParameter()
Modify the output parameter. 
 
size_t const  & InputHeight() const
Get the input height. 
 
bool & Deterministic()
Modify the value of the deterministic parameter. 
 
void Location(const arma::mat &location)
Set the locationthe x and y coordinate of the center of the output glimpse. 
 
The glimpse layer returns a retina-like representation (down-scaled cropped images) of increasing sca...
 
size_t const  & OutputWidth() const
Get the output width. 
 
bool Deterministic() const
Get the value of the deterministic parameter. 
 
size_t & OutputHeight()
Modify the output height. 
 
size_t const  & Depth() const
Get the number of patches to crop per glimpse.