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;
176 template<
typename Archive>
177 void serialize(Archive& ar,
const unsigned int );
185 void Transform(arma::mat& w)
189 for (
size_t i = 0, k = 0; i < w.n_elem; k++)
191 for (
size_t j = 0; j < w.n_cols; j++, i++)
203 void Transform(arma::cube& w)
205 for (
size_t i = 0; i < w.n_slices; i++)
207 arma::mat t = w.slice(i);
220 template<
typename eT>
221 void Pooling(
const size_t kSize,
222 const arma::Mat<eT>& input,
223 arma::Mat<eT>& output)
225 const size_t rStep = kSize;
226 const size_t cStep = kSize;
228 for (
size_t j = 0; j < input.n_cols; j += cStep)
230 for (
size_t i = 0; i < input.n_rows; i += rStep)
232 output(i / rStep, j / cStep) += pooling.
Pooling(
233 input(arma::span(i, i + rStep - 1), arma::span(j, j + cStep - 1)));
245 template<
typename eT>
246 void Unpooling(
const arma::Mat<eT>& input,
247 const arma::Mat<eT>& error,
248 arma::Mat<eT>& output)
250 const size_t rStep = input.n_rows / error.n_rows;
251 const size_t cStep = input.n_cols / error.n_cols;
253 arma::Mat<eT> unpooledError;
254 for (
size_t j = 0; j < input.n_cols; j += cStep)
256 for (
size_t i = 0; i < input.n_rows; i += rStep)
258 const arma::Mat<eT>& inputArea = input(arma::span(i, i + rStep - 1),
259 arma::span(j, j + cStep - 1));
261 pooling.
Unpooling(inputArea, error(i / rStep, j / cStep),
264 output(arma::span(i, i + rStep - 1),
265 arma::span(j, j + cStep - 1)) += unpooledError;
277 template<
typename eT>
278 void ReSampling(
const arma::Mat<eT>& input, arma::Mat<eT>& output)
280 double wRatio = (double) (input.n_rows - 1) / (size - 1);
281 double hRatio = (double) (input.n_cols - 1) / (size - 1);
283 double iWidth = input.n_rows - 1;
284 double iHeight = input.n_cols - 1;
286 for (
size_t y = 0; y < size; y++)
288 for (
size_t x = 0; x < size; x++)
290 double ix = wRatio * x;
291 double iy = hRatio * y;
294 double ixNw = std::floor(ix);
295 double iyNw = std::floor(iy);
296 double ixNe = ixNw + 1;
297 double iySw = iyNw + 1;
300 double se = (ix - ixNw) * (iy - iyNw);
301 double sw = (ixNe - ix) * (iy - iyNw);
302 double ne = (ix - ixNw) * (iySw - iy);
303 double nw = (ixNe - ix) * (iySw - iy);
306 output(y, x) = input(iyNw, ixNw) * nw +
307 input(iyNw, std::min(ixNe, iWidth)) * ne +
308 input(std::min(iySw, iHeight), ixNw) * sw +
309 input(std::min(iySw, iHeight), std::min(ixNe, iWidth)) * se;
322 template<
typename eT>
323 void DownwardReSampling(
const arma::Mat<eT>& input,
324 const arma::Mat<eT>& error,
325 arma::Mat<eT>& output)
327 double iWidth = input.n_rows - 1;
328 double iHeight = input.n_cols - 1;
330 double wRatio = iWidth / (size - 1);
331 double hRatio = iHeight / (size - 1);
333 for (
size_t y = 0; y < size; y++)
335 for (
size_t x = 0; x < size; x++)
337 double ix = wRatio * x;
338 double iy = hRatio * y;
341 double ixNw = std::floor(ix);
342 double iyNw = std::floor(iy);
343 double ixNe = ixNw + 1;
344 double iySw = iyNw + 1;
347 double se = (ix - ixNw) * (iy - iyNw);
348 double sw = (ixNe - ix) * (iy - iyNw);
349 double ne = (ix - ixNw) * (iySw - iy);
350 double nw = (ixNe - ix) * (iySw - iy);
352 double ograd = error(y, x);
354 output(iyNw, ixNw) = output(iyNw, ixNw) + nw * ograd;
355 output(iyNw, std::min(ixNe, iWidth)) = output(iyNw,
356 std::min(ixNe, iWidth)) + ne * ograd;
357 output(std::min(iySw, iHeight), ixNw) = output(std::min(iySw, iHeight),
359 output(std::min(iySw, iHeight), std::min(ixNe, iWidth)) = output(
360 std::min(iySw, iHeight), std::min(ixNe, iWidth)) + se * ograd;
390 OutputDataType delta;
393 OutputDataType outputParameter;
399 arma::cube inputTemp;
402 arma::cube outputTemp;
408 MeanPoolingRule pooling;
411 std::vector<arma::mat> locationParameter;
424 #include "glimpse_impl.hpp"
size_t & InputHeight()
Modify the input height.
void Backward(const arma::Mat< eT > &, const arma::Mat< eT > &gy, arma::Mat< eT > &g)
Ordinary feed backward pass of the glimpse layer.
double Pooling(const MatType &input)
The core includes that mlpack expects; standard C++ includes and Armadillo.
void Forward(const arma::Mat< eT > &input, arma::Mat< eT > &output)
Ordinary feed forward pass of the glimpse layer.
size_t const & InputWidth() const
Get the input width.
size_t & InputWidth()
Modify input the width.
void Unpooling(const MatType &input, const double value, MatType &output)
size_t & OutputWidth()
Modify the output width.
bool Deterministic() const
Get the value of the deterministic parameter.
OutputDataType & Delta()
Modify the delta.
OutputDataType & OutputParameter()
Modify the output parameter.
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.
OutputDataType & Delta() const
Get the detla.
OutputDataType & OutputParameter() const
Get the output parameter.
The glimpse layer returns a retina-like representation (down-scaled cropped images) of increasing sca...
size_t const & OutputWidth() const
Get the output width.
size_t & OutputHeight()
Modify the output height.
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 & OutputHeight() const
Get the output height.
size_t const & InputHeight() const
Get the input height.
void serialize(Archive &ar, const unsigned int)
Serialize the layer.