26 #ifndef MLPACK_METHODS_ANN_LAYER_GLIMPSE_HPP 27 #define MLPACK_METHODS_ANN_LAYER_GLIMPSE_HPP 50 template<
typename MatType>
53 return arma::mean(arma::mean(input));
63 template<
typename MatType>
64 void Unpooling(
const MatType& input,
const double value, MatType& output)
66 output = arma::zeros<MatType>(input.n_rows, input.n_cols);
67 const double mean = arma::mean(arma::mean(input));
69 output.elem(arma::find(mean == input, 1)).fill(value);
84 typename InputDataType = arma::mat,
85 typename OutputDataType = arma::mat
102 Glimpse(
const size_t inSize = 0,
103 const size_t size = 0,
104 const size_t depth = 3,
105 const size_t scale = 2,
106 const size_t inputWidth = 0,
107 const size_t inputHeight = 0);
115 template<
typename eT>
116 void Forward(
const arma::Mat<eT>&& input, arma::Mat<eT>&& output);
125 template<
typename eT>
126 void Backward(
const arma::Mat<eT>&& ,
141 OutputDataType&
Delta()
const {
return delta; }
143 OutputDataType&
Delta() {
return delta; }
149 this->location = location;
180 template<
typename Archive>
181 void serialize(Archive& ar,
const unsigned int );
189 void Transform(arma::mat& w)
193 for (
size_t i = 0, k = 0; i < w.n_elem; k++)
195 for (
size_t j = 0; j < w.n_cols; j++, i++)
207 void Transform(arma::cube& w)
209 for (
size_t i = 0; i < w.n_slices; i++)
211 arma::mat t = w.slice(i);
224 template<
typename eT>
225 void Pooling(
const size_t kSize,
226 const arma::Mat<eT>& input,
227 arma::Mat<eT>& output)
229 const size_t rStep = kSize;
230 const size_t cStep = kSize;
232 for (
size_t j = 0; j < input.n_cols; j += cStep)
234 for (
size_t i = 0; i < input.n_rows; i += rStep)
236 output(i / rStep, j / cStep) += pooling.Pooling(
237 input(arma::span(i, i + rStep - 1), arma::span(j, j + cStep - 1)));
249 template<
typename eT>
250 void Unpooling(
const arma::Mat<eT>& input,
251 const arma::Mat<eT>& error,
252 arma::Mat<eT>& output)
254 const size_t rStep = input.n_rows / error.n_rows;
255 const size_t cStep = input.n_cols / error.n_cols;
257 arma::Mat<eT> unpooledError;
258 for (
size_t j = 0; j < input.n_cols; j += cStep)
260 for (
size_t i = 0; i < input.n_rows; i += rStep)
262 const arma::Mat<eT>& inputArea = input(arma::span(i, i + rStep - 1),
263 arma::span(j, j + cStep - 1));
265 pooling.Unpooling(inputArea, error(i / rStep, j / cStep),
268 output(arma::span(i, i + rStep - 1),
269 arma::span(j, j + cStep - 1)) += unpooledError;
281 template<
typename eT>
282 void ReSampling(
const arma::Mat<eT>& input, arma::Mat<eT>& output)
284 double wRatio = (double) (input.n_rows - 1) / (size - 1);
285 double hRatio = (double) (input.n_cols - 1) / (size - 1);
287 double iWidth = input.n_rows - 1;
288 double iHeight = input.n_cols - 1;
290 for (
size_t y = 0; y < size; y++)
292 for (
size_t x = 0; x < size; x++)
294 double ix = wRatio * x;
295 double iy = hRatio * y;
298 double ixNw = std::floor(ix);
299 double iyNw = std::floor(iy);
300 double ixNe = ixNw + 1;
301 double iySw = iyNw + 1;
304 double se = (ix - ixNw) * (iy - iyNw);
305 double sw = (ixNe - ix) * (iy - iyNw);
306 double ne = (ix - ixNw) * (iySw - iy);
307 double nw = (ixNe - ix) * (iySw - iy);
310 output(y, x) = input(iyNw, ixNw) * nw +
311 input(iyNw, std::min(ixNe, iWidth)) * ne +
312 input(std::min(iySw, iHeight), ixNw) * sw +
313 input(std::min(iySw, iHeight), std::min(ixNe, iWidth)) * se;
326 template<
typename eT>
327 void DownwardReSampling(
const arma::Mat<eT>& input,
328 const arma::Mat<eT>& error,
329 arma::Mat<eT>& output)
331 double iWidth = input.n_rows - 1;
332 double iHeight = input.n_cols - 1;
334 double wRatio = iWidth / (size - 1);
335 double hRatio = iHeight / (size - 1);
337 for (
size_t y = 0; y < size; y++)
339 for (
size_t x = 0; x < size; x++)
341 double ix = wRatio * x;
342 double iy = hRatio * y;
345 double ixNw = std::floor(ix);
346 double iyNw = std::floor(iy);
347 double ixNe = ixNw + 1;
348 double iySw = iyNw + 1;
351 double se = (ix - ixNw) * (iy - iyNw);
352 double sw = (ixNe - ix) * (iy - iyNw);
353 double ne = (ix - ixNw) * (iySw - iy);
354 double nw = (ixNe - ix) * (iySw - iy);
356 double ograd = error(y, x);
358 output(iyNw, ixNw) = output(iyNw, ixNw) + nw * ograd;
359 output(iyNw, std::min(ixNe, iWidth)) = output(iyNw,
360 std::min(ixNe, iWidth)) + ne * ograd;
361 output(std::min(iySw, iHeight), ixNw) = output(std::min(iySw, iHeight),
363 output(std::min(iySw, iHeight), std::min(ixNe, iWidth)) = output(
364 std::min(iySw, iHeight), std::min(ixNe, iWidth)) + se * ograd;
394 OutputDataType delta;
397 InputDataType inputParameter;
400 OutputDataType outputParameter;
406 arma::cube inputTemp;
409 arma::cube outputTemp;
418 std::vector<arma::mat> locationParameter;
431 #include "glimpse_impl.hpp" InputDataType & InputParameter() const
Get the input parameter.
size_t & InputHeight()
Modify the input height.
size_t const & OutputHeight() const
Get the output height.
double Pooling(const MatType &input)
The core includes that mlpack expects; standard C++ includes and Armadillo.
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)
InputDataType & InputParameter()
Modify the input parameter.
size_t & OutputWidth()
Modify the output width.
size_t const & InputWidth() const
Get the input width.
OutputDataType & Delta()
Modify the delta.
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.