13 #ifndef MLPACK_METHODS_ANN_LAYER_BILINEAR_INTERPOLATION_IMPL_HPP 14 #define MLPACK_METHODS_ANN_LAYER_BILINEAR_INTERPOLATION_IMPL_HPP 23 template<
typename InputDataType,
typename OutputDataType>
36 template<
typename InputDataType,
typename OutputDataType>
39 const size_t inRowSize,
40 const size_t inColSize,
41 const size_t outRowSize,
42 const size_t outColSize,
46 outRowSize(outRowSize),
47 outColSize(outColSize),
54 template<
typename InputDataType,
typename OutputDataType>
57 const arma::Mat<eT>& input, arma::Mat<eT>& output)
59 batchSize = input.n_cols;
60 if (output.is_empty())
61 output.set_size(outRowSize * outColSize * depth, batchSize);
64 assert(output.n_rows == outRowSize * outColSize * depth);
65 assert(output.n_cols == batchSize);
68 assert(inRowSize >= 2);
69 assert(inColSize >= 2);
71 arma::cube inputAsCube(
const_cast<arma::Mat<eT>&
>(input).memptr(),
72 inRowSize, inColSize, depth * batchSize,
false,
false);
73 arma::cube outputAsCube(output.memptr(), outRowSize, outColSize,
74 depth * batchSize,
false,
true);
76 double scaleRow = (double) inRowSize / (
double) outRowSize;
77 double scaleCol = (double) inColSize / (
double) outColSize;
80 for (
size_t i = 0; i < outRowSize; ++i)
82 size_t rOrigin = (size_t) std::floor(i * scaleRow);
83 if (rOrigin > inRowSize - 2)
84 rOrigin = inRowSize - 2;
87 double deltaR = i * scaleRow - rOrigin;
90 for (
size_t j = 0; j < outColSize; ++j)
93 size_t cOrigin = (size_t) std::floor(j * scaleCol);
94 if (cOrigin > inColSize - 2)
95 cOrigin = inColSize - 2;
97 double deltaC = j * scaleCol - cOrigin;
100 coeffs[0] = (1 - deltaR) * (1 - deltaC);
101 coeffs[1] = deltaR * (1 - deltaC);
102 coeffs[2] = (1 - deltaR) * deltaC;
103 coeffs[3] = deltaR * deltaC;
105 for (
size_t k = 0; k < depth * batchSize; ++k)
107 outputAsCube(i, j, k) = arma::accu(inputAsCube.slice(k).submat(
108 rOrigin, cOrigin, rOrigin + 1, cOrigin + 1) % coeffs);
114 template<
typename InputDataType,
typename OutputDataType>
115 template<
typename eT>
117 const arma::Mat<eT>& ,
118 const arma::Mat<eT>& gradient,
119 arma::Mat<eT>& output)
121 if (output.is_empty())
122 output.set_size(inRowSize * inColSize * depth, batchSize);
125 assert(output.n_rows == inRowSize * inColSize * depth);
126 assert(output.n_cols == batchSize);
129 assert(outRowSize >= 2);
130 assert(outColSize >= 2);
132 arma::cube gradientAsCube(((arma::Mat<eT>&) gradient).memptr(), outRowSize,
133 outColSize, depth * batchSize,
false,
false);
134 arma::cube outputAsCube(output.memptr(), inRowSize, inColSize,
135 depth * batchSize,
false,
true);
137 if (gradient.n_elem == output.n_elem)
139 outputAsCube = gradientAsCube;
143 double scaleRow = (double)(outRowSize) / inRowSize;
144 double scaleCol = (double)(outColSize) / inColSize;
147 for (
size_t i = 0; i < inRowSize; ++i)
149 size_t rOrigin = (size_t) std::floor(i * scaleRow);
150 if (rOrigin > outRowSize - 2)
151 rOrigin = outRowSize - 2;
152 double deltaR = i * scaleRow - rOrigin;
153 for (
size_t j = 0; j < inColSize; ++j)
155 size_t cOrigin = (size_t) std::floor(j * scaleCol);
157 if (cOrigin > outColSize - 2)
158 cOrigin = outColSize - 2;
160 double deltaC = j * scaleCol - cOrigin;
161 coeffs[0] = (1 - deltaR) * (1 - deltaC);
162 coeffs[1] = deltaR * (1 - deltaC);
163 coeffs[2] = (1 - deltaR) * deltaC;
164 coeffs[3] = deltaR * deltaC;
166 for (
size_t k = 0; k < depth * batchSize; ++k)
168 outputAsCube(i, j, k) = arma::accu(gradientAsCube.slice(k).submat(
169 rOrigin, cOrigin, rOrigin + 1, cOrigin + 1) % coeffs);
176 template<
typename InputDataType,
typename OutputDataType>
177 template<
typename Archive>
179 Archive& ar,
const uint32_t )
181 ar(CEREAL_NVP(inRowSize));
182 ar(CEREAL_NVP(inColSize));
183 ar(CEREAL_NVP(outRowSize));
184 ar(CEREAL_NVP(outColSize));
185 ar(CEREAL_NVP(depth));
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
Definition: bilinear_interpolation_impl.hpp:178
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: bilinear_interpolation_impl.hpp:56
BilinearInterpolation()
Create the Bilinear Interpolation object.
Definition: bilinear_interpolation_impl.hpp:25
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: bilinear_interpolation_impl.hpp:116