21 #ifndef MLPACK_METHODS_ANN_LAYER_GLIMPSE_HPP 22 #define MLPACK_METHODS_ANN_LAYER_GLIMPSE_HPP 45 template<
typename MatType>
48 return arma::mean(arma::mean(input));
58 template<
typename MatType>
59 void Unpooling(
const MatType& input,
const double value, MatType& output)
61 output = arma::zeros<MatType>(input.n_rows, input.n_cols);
62 const double mean = arma::mean(arma::mean(input));
64 output.elem(arma::find(mean == input, 1)).fill(value);
79 typename InputDataType = arma::mat,
80 typename OutputDataType = arma::mat
100 const size_t depth = 3,
101 const size_t scale = 2,
102 const size_t inputWidth = 0,
103 const size_t inputHeight = 0);
111 template<
typename eT>
112 void Forward(
const arma::Mat<eT>&& input, arma::Mat<eT>&& output);
121 template<
typename eT>
122 void Backward(
const arma::Mat<eT>&& ,
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 );
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++)
205 for (
size_t i = 0; i < w.n_slices; i++)
207 arma::mat t = w.slice(i);
220 template<
typename eT>
222 const arma::Mat<eT>& input,
223 arma::Mat<eT>& output)
226 const size_t rStep = kSize;
227 const size_t cStep = kSize;
229 for (
size_t j = 0; j < input.n_cols; j += cStep)
231 for (
size_t i = 0; i < input.n_rows; i += rStep)
233 output(i / rStep, j / cStep) += pooling.Pooling(
234 input(arma::span(i, i + rStep - 1), arma::span(j, j + cStep - 1)));
246 template<
typename eT>
248 const arma::Mat<eT>& error,
249 arma::Mat<eT>& output)
251 const size_t rStep = input.n_rows / error.n_rows;
252 const size_t cStep = input.n_cols / error.n_cols;
254 arma::Mat<eT> unpooledError;
255 for (
size_t j = 0; j < input.n_cols; j += cStep)
257 for (
size_t i = 0; i < input.n_rows; i += rStep)
259 const arma::Mat<eT>& inputArea = input(arma::span(i, i + rStep - 1),
260 arma::span(j, j + cStep - 1));
262 pooling.Unpooling(inputArea, error(i / rStep, j / cStep),
265 output(arma::span(i, i + rStep - 1),
266 arma::span(j, j + cStep - 1)) += unpooledError;
278 template<
typename eT>
279 void ReSampling(
const arma::Mat<eT>& input, arma::Mat<eT>& output)
281 double wRatio = (double) (input.n_rows - 1) / (size - 1);
282 double hRatio = (double) (input.n_cols - 1) / (size - 1);
284 double iWidth = input.n_rows - 1;
285 double iHeight = input.n_cols - 1;
287 for (
size_t y = 0; y < size; y++)
289 for (
size_t x = 0; x < size; x++)
291 double ix = wRatio * x;
292 double iy = hRatio * y;
295 double ixNw = std::floor(ix);
296 double iyNw = std::floor(iy);
297 double ixNe = ixNw + 1;
298 double iySw = iyNw + 1;
301 double se = (ix - ixNw) * (iy - iyNw);
302 double sw = (ixNe - ix) * (iy - iyNw);
303 double ne = (ix - ixNw) * (iySw - iy);
304 double nw = (ixNe - ix) * (iySw - iy);
307 output(y, x) = input(iyNw, ixNw) * nw +
308 input(iyNw, std::min(ixNe, iWidth)) * ne +
309 input(std::min(iySw, iHeight), ixNw) * sw +
310 input(std::min(iySw, iHeight), std::min(ixNe, iWidth)) * se;
323 template<
typename eT>
325 const arma::Mat<eT>& error,
326 arma::Mat<eT>& output)
328 double iWidth = input.n_rows - 1;
329 double iHeight = input.n_cols - 1;
331 double wRatio = iWidth / (size - 1);
332 double hRatio = iHeight / (size - 1);
334 for (
size_t y = 0; y < size; y++)
336 for (
size_t x = 0; x < size; x++)
338 double ix = wRatio * x;
339 double iy = hRatio * y;
342 double ixNw = std::floor(ix);
343 double iyNw = std::floor(iy);
344 double ixNe = ixNw + 1;
345 double iySw = iyNw + 1;
348 double se = (ix - ixNw) * (iy - iyNw);
349 double sw = (ixNe - ix) * (iy - iyNw);
350 double ne = (ix - ixNw) * (iySw - iy);
351 double nw = (ixNe - ix) * (iySw - iy);
353 double ograd = error(y, x);
355 output(iyNw, ixNw) = output(iyNw, ixNw) + nw * ograd;
356 output(iyNw, std::min(ixNe, iWidth)) = output(iyNw,
357 std::min(ixNe, iWidth)) + ne * ograd;
358 output(std::min(iySw, iHeight), ixNw) = output(std::min(iySw, iHeight),
360 output(std::min(iySw, iHeight), std::min(ixNe, iWidth)) = output(
361 std::min(iySw, iHeight), std::min(ixNe, iWidth)) + se * ograd;
428 #include "glimpse_impl.hpp" OutputDataType & Delta() const
Get the detla.
InputDataType inputParameter
Locally-stored input parameter object.
size_t const & OutputWidth() const
Get the output width.
Linear algebra utility functions, generally performed on matrices or vectors.
size_t scale
The scale fraction.
void Unpooling(const arma::Mat< eT > &input, const arma::Mat< eT > &error, arma::Mat< eT > &output)
Apply unpooling to the input and store the results.
std::vector< arma::mat > locationParameter
Location-stored module location parameter.
double Pooling(const MatType &input)
The core includes that mlpack expects; standard C++ includes and Armadillo.
size_t depth
The number of patches to crop per glimpse.
arma::cube gTemp
Location-stored transformed gradient paramter.
bool Deterministic() const
Get the value of the deterministic parameter.
arma::mat location
The x and y coordinate of the center of the output glimpse.
OutputDataType & OutputParameter() const
Get the output parameter.
void Pooling(const size_t kSize, const arma::Mat< eT > &input, arma::Mat< eT > &output)
Apply pooling to the input and store the results to the output parameter.
void DownwardReSampling(const arma::Mat< eT > &input, const arma::Mat< eT > &error, arma::Mat< eT > &output)
Apply DownwardReSampling to the input and store the results into the output parameter.
size_t inputHeight
Locally-stored input height.
size_t const & OutputHeight() const
Get the output height.
size_t inputDepth
Locally-stored depth of the input.
size_t & OutputHeight()
Modify the output height.
size_t outputWidth
Locally-stored output width.
InputDataType & InputParameter()
Modify the input parameter.
size_t & InputWidth()
Modify input the width.
void Transform(arma::cube &w)
bool & Deterministic()
Modify the value of the deterministic parameter.
void Transform(arma::mat &w)
void Location(const arma::mat &location)
Set the locationthe x and y coordinate of the center of the output glimpse.
OutputDataType & Delta()
Modify the delta.
size_t const & InputHeight() const
Get the input height.
size_t inSize
The size of the input units.
size_t & OutputWidth()
Modify the output width.
arma::cube outputTemp
Locally-stored transformed output parameter.
The glimpse layer returns a retina-like representation (down-scaled cropped images) of increasing sca...
size_t size
The used glimpse size (height = width).
MeanPoolingRule pooling
Locally-stored object to perform the mean pooling operation.
bool deterministic
If true use maximum a posteriori during the forward pass.
arma::cube inputTemp
Locally-stored transformed input parameter.
void Unpooling(const MatType &input, const double value, MatType &output)
size_t & InputHeight()
Modify the input height.
void ReSampling(const arma::Mat< eT > &input, arma::Mat< eT > &output)
Apply ReSampling to the input and store the results in the output parameter.
OutputDataType & OutputParameter()
Modify the output parameter.
InputDataType & InputParameter() const
Get the input parameter.
size_t inputWidth
Locally-stored input width.
size_t outputHeight
Locally-stored output height.
OutputDataType delta
Locally-stored delta object.
OutputDataType outputParameter
Locally-stored output parameter object.
size_t const & InputWidth() const
Get the input width.