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>&& ,
136 OutputDataType&
Delta()
const {
return delta; }
138 OutputDataType&
Delta() {
return delta; }
144 this->location = location;
175 template<
typename Archive>
176 void serialize(Archive& ar,
const unsigned int );
184 void Transform(arma::mat& w)
188 for (
size_t i = 0, k = 0; i < w.n_elem; k++)
190 for (
size_t j = 0; j < w.n_cols; j++, i++)
202 void Transform(arma::cube& w)
204 for (
size_t i = 0; i < w.n_slices; i++)
206 arma::mat t = w.slice(i);
219 template<
typename eT>
220 void Pooling(
const size_t kSize,
221 const arma::Mat<eT>& input,
222 arma::Mat<eT>& output)
224 const size_t rStep = kSize;
225 const size_t cStep = kSize;
227 for (
size_t j = 0; j < input.n_cols; j += cStep)
229 for (
size_t i = 0; i < input.n_rows; i += rStep)
231 output(i / rStep, j / cStep) += pooling.Pooling(
232 input(arma::span(i, i + rStep - 1), arma::span(j, j + cStep - 1)));
244 template<
typename eT>
245 void Unpooling(
const arma::Mat<eT>& input,
246 const arma::Mat<eT>& error,
247 arma::Mat<eT>& output)
249 const size_t rStep = input.n_rows / error.n_rows;
250 const size_t cStep = input.n_cols / error.n_cols;
252 arma::Mat<eT> unpooledError;
253 for (
size_t j = 0; j < input.n_cols; j += cStep)
255 for (
size_t i = 0; i < input.n_rows; i += rStep)
257 const arma::Mat<eT>& inputArea = input(arma::span(i, i + rStep - 1),
258 arma::span(j, j + cStep - 1));
260 pooling.Unpooling(inputArea, error(i / rStep, j / cStep),
263 output(arma::span(i, i + rStep - 1),
264 arma::span(j, j + cStep - 1)) += unpooledError;
276 template<
typename eT>
277 void ReSampling(
const arma::Mat<eT>& input, arma::Mat<eT>& output)
279 double wRatio = (double) (input.n_rows - 1) / (size - 1);
280 double hRatio = (double) (input.n_cols - 1) / (size - 1);
282 double iWidth = input.n_rows - 1;
283 double iHeight = input.n_cols - 1;
285 for (
size_t y = 0; y < size; y++)
287 for (
size_t x = 0; x < size; x++)
289 double ix = wRatio * x;
290 double iy = hRatio * y;
293 double ixNw = std::floor(ix);
294 double iyNw = std::floor(iy);
295 double ixNe = ixNw + 1;
296 double iySw = iyNw + 1;
299 double se = (ix - ixNw) * (iy - iyNw);
300 double sw = (ixNe - ix) * (iy - iyNw);
301 double ne = (ix - ixNw) * (iySw - iy);
302 double nw = (ixNe - ix) * (iySw - iy);
305 output(y, x) = input(iyNw, ixNw) * nw +
306 input(iyNw, std::min(ixNe, iWidth)) * ne +
307 input(std::min(iySw, iHeight), ixNw) * sw +
308 input(std::min(iySw, iHeight), std::min(ixNe, iWidth)) * se;
321 template<
typename eT>
322 void DownwardReSampling(
const arma::Mat<eT>& input,
323 const arma::Mat<eT>& error,
324 arma::Mat<eT>& output)
326 double iWidth = input.n_rows - 1;
327 double iHeight = input.n_cols - 1;
329 double wRatio = iWidth / (size - 1);
330 double hRatio = iHeight / (size - 1);
332 for (
size_t y = 0; y < size; y++)
334 for (
size_t x = 0; x < size; x++)
336 double ix = wRatio * x;
337 double iy = hRatio * y;
340 double ixNw = std::floor(ix);
341 double iyNw = std::floor(iy);
342 double ixNe = ixNw + 1;
343 double iySw = iyNw + 1;
346 double se = (ix - ixNw) * (iy - iyNw);
347 double sw = (ixNe - ix) * (iy - iyNw);
348 double ne = (ix - ixNw) * (iySw - iy);
349 double nw = (ixNe - ix) * (iySw - iy);
351 double ograd = error(y, x);
353 output(iyNw, ixNw) = output(iyNw, ixNw) + nw * ograd;
354 output(iyNw, std::min(ixNe, iWidth)) = output(iyNw,
355 std::min(ixNe, iWidth)) + ne * ograd;
356 output(std::min(iySw, iHeight), ixNw) = output(std::min(iySw, iHeight),
358 output(std::min(iySw, iHeight), std::min(ixNe, iWidth)) = output(
359 std::min(iySw, iHeight), std::min(ixNe, iWidth)) + se * ograd;
389 OutputDataType delta;
392 OutputDataType outputParameter;
398 arma::cube inputTemp;
401 arma::cube outputTemp;
410 std::vector<arma::mat> locationParameter;
423 #include "glimpse_impl.hpp" 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)
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.