12 #ifndef MLPACK_METHODS_ANN_LAYER_NEAREST_INTERPOLATION_IMPL_HPP 13 #define MLPACK_METHODS_ANN_LAYER_NEAREST_INTERPOLATION_IMPL_HPP 22 template<
typename InputDataType,
typename OutputDataType>
35 template<
typename InputDataType,
typename OutputDataType>
38 const size_t inColSize,
39 const size_t outRowSize,
40 const size_t outColSize,
44 outRowSize(outRowSize),
45 outColSize(outColSize),
52 template<
typename InputDataType,
typename OutputDataType>
55 const arma::Mat<eT>& input, arma::Mat<eT>& output)
57 batchSize = input.n_cols;
58 if (output.is_empty())
59 output.set_size(outRowSize * outColSize * depth, batchSize);
62 assert(output.n_rows == outRowSize * outColSize * depth);
63 assert(output.n_cols == batchSize);
66 assert(inRowSize >= 2);
67 assert(inColSize >= 2);
69 arma::cube inputAsCube(
const_cast<arma::Mat<eT>&
>(input).memptr(),
70 inRowSize, inColSize, depth * batchSize,
false,
false);
71 arma::cube outputAsCube(output.memptr(), outRowSize, outColSize,
72 depth * batchSize,
false,
true);
74 double scaleRow = (double) inRowSize / (
double) outRowSize;
75 double scaleCol = (double) inColSize / (
double) outColSize;
77 for (
size_t i = 0; i < outRowSize; ++i)
79 const size_t rOrigin = std::floor(i * scaleRow);
81 for (
size_t j = 0; j < outColSize; ++j)
83 const size_t cOrigin = std::floor(j * scaleCol);
85 for (
size_t k = 0; k < depth * batchSize; ++k)
87 outputAsCube(i, j, k) = inputAsCube.slice(k)(
94 template<
typename InputDataType,
typename OutputDataType>
97 const arma::Mat<eT>& ,
98 const arma::Mat<eT>& gradient,
99 arma::Mat<eT>& output)
101 if (output.is_empty())
103 output.zeros(inRowSize * inColSize * depth, batchSize);
107 assert(output.n_rows == inRowSize * inColSize * depth);
108 assert(output.n_cols == batchSize);
111 assert(outRowSize >= 2);
112 assert(outColSize >= 2);
114 arma::cube outputAsCube(output.memptr(), inRowSize, inColSize,
115 depth * batchSize,
false,
true);
116 arma::cube gradientAsCube(((arma::Mat<eT>&) gradient).memptr(), outRowSize,
117 outColSize, depth * batchSize,
false,
false);
119 double scaleRow = (double)(inRowSize) / outRowSize;
120 double scaleCol = (double)(inColSize) / outColSize;
122 if (gradient.n_elem == output.n_elem)
124 outputAsCube = gradientAsCube;
128 for (
size_t i = 0; i < outRowSize; ++i)
130 const size_t rOrigin = std::floor(i * scaleRow);
132 for (
size_t j = 0; j < outColSize; ++j)
134 const size_t cOrigin = std::floor(j * scaleCol);
136 for (
size_t k = 0; k < depth * batchSize; ++k)
138 outputAsCube(rOrigin, cOrigin, k) +=
139 gradientAsCube(i, j, k);
146 template<
typename InputDataType,
typename OutputDataType>
147 template<
typename Archive>
149 Archive& ar,
const uint32_t )
151 ar(CEREAL_NVP(inRowSize));
152 ar(CEREAL_NVP(inColSize));
153 ar(CEREAL_NVP(outRowSize));
154 ar(CEREAL_NVP(outColSize));
155 ar(CEREAL_NVP(depth));
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
void Forward(const arma::Mat< eT > &input, arma::Mat< eT > &output)
Forward pass through the layer.
Definition: nearest_interpolation_impl.hpp:54
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
Definition: nearest_interpolation_impl.hpp:148
void Backward(const arma::Mat< eT > &, const arma::Mat< eT > &gradient, arma::Mat< eT > &output)
Ordinary feed backward pass of a neural network, calculating the function f(x) by propagating x backw...
Definition: nearest_interpolation_impl.hpp:96
NearestInterpolation()
Create the NearestInterpolation object.
Definition: nearest_interpolation_impl.hpp:24