mlpack 3.4.2
transposed_convolution.hpp
Go to the documentation of this file.
1
13#ifndef MLPACK_METHODS_ANN_LAYER_TRANSPOSED_CONVOLUTION_HPP
14#define MLPACK_METHODS_ANN_LAYER_TRANSPOSED_CONVOLUTION_HPP
15
16#include <mlpack/prereqs.hpp>
17
23
24#include "layer_types.hpp"
25#include "padding.hpp"
26
27namespace mlpack {
28namespace ann {
29
42template <
43 typename ForwardConvolutionRule = NaiveConvolution<ValidConvolution>,
44 typename BackwardConvolutionRule = NaiveConvolution<ValidConvolution>,
45 typename GradientConvolutionRule = NaiveConvolution<ValidConvolution>,
46 typename InputDataType = arma::mat,
47 typename OutputDataType = arma::mat
48>
50{
51 public:
54
79 TransposedConvolution(const size_t inSize,
80 const size_t outSize,
81 const size_t kernelWidth,
82 const size_t kernelHeight,
83 const size_t strideWidth = 1,
84 const size_t strideHeight = 1,
85 const size_t padW = 0,
86 const size_t padH = 0,
87 const size_t inputWidth = 0,
88 const size_t inputHeight = 0,
89 const size_t outputWidth = 0,
90 const size_t outputHeight = 0,
91 const std::string& paddingType = "None");
92
121 TransposedConvolution(const size_t inSize,
122 const size_t outSize,
123 const size_t kernelWidth,
124 const size_t kernelHeight,
125 const size_t strideWidth,
126 const size_t strideHeight,
127 const std::tuple<size_t, size_t>& padW,
128 const std::tuple<size_t, size_t>& padH,
129 const size_t inputWidth = 0,
130 const size_t inputHeight = 0,
131 const size_t outputWidth = 0,
132 const size_t outputHeight = 0,
133 const std::string& paddingType = "None");
134
135 /*
136 * Set the weight and bias term.
137 */
138 void Reset();
139
147 template<typename eT>
148 void Forward(const arma::Mat<eT>& input, arma::Mat<eT>& output);
149
159 template<typename eT>
160 void Backward(const arma::Mat<eT>& /* input */,
161 const arma::Mat<eT>& gy,
162 arma::Mat<eT>& g);
163
164 /*
165 * Calculate the gradient using the output delta and the input activation.
166 *
167 * @param * (input) The input parameter used for calculating the gradient.
168 * @param error The calculated error.
169 * @param gradient The calculated gradient.
170 */
171 template<typename eT>
172 void Gradient(const arma::Mat<eT>& /* input */,
173 const arma::Mat<eT>& error,
174 arma::Mat<eT>& gradient);
175
177 OutputDataType const& Parameters() const { return weights; }
179 OutputDataType& Parameters() { return weights; }
180
182 arma::cube const& Weight() const { return weight; }
184 arma::cube& Weight() { return weight; }
185
187 arma::mat const& Bias() const { return bias; }
189 arma::mat& Bias() { return bias; }
190
192 InputDataType const& InputParameter() const { return inputParameter; }
194 InputDataType& InputParameter() { return inputParameter; }
195
197 OutputDataType const& OutputParameter() const { return outputParameter; }
199 OutputDataType& OutputParameter() { return outputParameter; }
200
202 OutputDataType const& Delta() const { return delta; }
204 OutputDataType& Delta() { return delta; }
205
207 OutputDataType const& Gradient() const { return gradient; }
209 OutputDataType& Gradient() { return gradient; }
210
212 size_t InputWidth() const { return inputWidth; }
214 size_t& InputWidth() { return inputWidth; }
215
217 size_t InputHeight() const { return inputHeight; }
219 size_t& InputHeight() { return inputHeight; }
220
222 size_t OutputWidth() const { return outputWidth; }
224 size_t& OutputWidth() { return outputWidth; }
225
227 size_t OutputHeight() const { return outputHeight; }
229 size_t& OutputHeight() { return outputHeight; }
230
232 size_t InputSize() const { return inSize; }
233
235 size_t OutputSize() const { return outSize; }
236
238 size_t KernelWidth() const { return kernelWidth; }
240 size_t& KernelWidth() { return kernelWidth; }
241
243 size_t KernelHeight() const { return kernelHeight; }
245 size_t& KernelHeight() { return kernelHeight; }
246
248 size_t StrideWidth() const { return strideWidth; }
250 size_t& StrideWidth() { return strideWidth; }
251
253 size_t StrideHeight() const { return strideHeight; }
255 size_t& StrideHeight() { return strideHeight; }
256
258 size_t PadHTop() const { return padHTop; }
260 size_t& PadHTop() { return padHTop; }
261
263 size_t PadHBottom() const { return padHBottom; }
265 size_t& PadHBottom() { return padHBottom; }
266
268 size_t PadWLeft() const { return padWLeft; }
270 size_t& PadWLeft() { return padWLeft; }
271
273 size_t PadWRight() const { return padWRight; }
275 size_t& PadWRight() { return padWRight; }
276
280 template<typename Archive>
281 void serialize(Archive& ar, const unsigned int /* version */);
282
283 private:
284 /*
285 * Rotates a 3rd-order tensor counterclockwise by 180 degrees.
286 *
287 * @param input The input data to be rotated.
288 * @param output The rotated output.
289 */
290 template<typename eT>
291 void Rotate180(const arma::Cube<eT>& input, arma::Cube<eT>& output)
292 {
293 output = arma::Cube<eT>(input.n_rows, input.n_cols, input.n_slices);
294
295 // * left-right flip, up-down flip */
296 for (size_t s = 0; s < output.n_slices; s++)
297 output.slice(s) = arma::fliplr(arma::flipud(input.slice(s)));
298 }
299
300 /*
301 * Function to assign padding such that output size is same as input size.
302 */
303 void InitializeSamePadding();
304
305 /*
306 * Rotates a dense matrix counterclockwise by 180 degrees.
307 *
308 * @param input The input data to be rotated.
309 * @param output The rotated output.
310 */
311 template<typename eT>
312 void Rotate180(const arma::Mat<eT>& input, arma::Mat<eT>& output)
313 {
314 // * left-right flip, up-down flip */
315 output = arma::fliplr(arma::flipud(input));
316 }
317
318
319 /*
320 * Insert zeros between the units of the given input data.
321 * Note: This function should be used before using padding layer.
322 *
323 * @param input The input to be padded.
324 * @param strideWidth Stride of filter application in the x direction.
325 * @param strideHeight Stride of filter application in the y direction.
326 * @param output The padded output data.
327 */
328 template<typename eT>
329 void InsertZeros(const arma::Mat<eT>& input,
330 const size_t strideWidth,
331 const size_t strideHeight,
332 arma::Mat<eT>& output)
333 {
334 if (output.n_rows != input.n_rows * strideWidth - strideWidth + 1 ||
335 output.n_cols != input.n_cols * strideHeight - strideHeight + 1)
336 {
337 output = arma::zeros(input.n_rows * strideWidth - strideWidth + 1,
338 input.n_cols * strideHeight - strideHeight + 1);
339 }
340
341 for (size_t i = 0; i < output.n_rows; i += strideHeight)
342 {
343 for (size_t j = 0; j < output.n_cols; j += strideWidth)
344 {
345 // TODO: Use [] instead of () for speedup after this is completely
346 // debugged and approved.
347 output(i, j) = input(i / strideHeight, j / strideWidth);
348 }
349 }
350 }
351
352 /*
353 * Insert zeros between the units of the given input data.
354 * Note: This function should be used before using padding layer.
355 *
356 * @param input The input to be padded.
357 * @param strideWidth Stride of filter application in the x direction.
358 * @param strideHeight Stride of filter application in the y direction.
359 * @param output The padded output data.
360 */
361 template<typename eT>
362 void InsertZeros(const arma::Cube<eT>& input,
363 const size_t strideWidth,
364 const size_t strideHeight,
365 arma::Cube<eT>& output)
366 {
367 output = arma::zeros(input.n_rows * strideWidth - strideWidth + 1,
368 input.n_cols * strideHeight - strideHeight + 1, input.n_slices);
369
370 for (size_t i = 0; i < input.n_slices; ++i)
371 {
372 InsertZeros<eT>(input.slice(i), strideWidth, strideHeight,
373 output.slice(i));
374 }
375 }
376
378 size_t inSize;
379
381 size_t outSize;
382
384 size_t batchSize;
385
387 size_t kernelWidth;
388
390 size_t kernelHeight;
391
393 size_t strideWidth;
394
396 size_t strideHeight;
397
399 size_t padWLeft;
400
402 size_t padWRight;
403
405 size_t padHBottom;
406
408 size_t padHTop;
409
411 size_t aW;
412
414 size_t aH;
415
417 OutputDataType weights;
418
420 arma::cube weight;
421
423 arma::mat bias;
424
426 size_t inputWidth;
427
429 size_t inputHeight;
430
432 size_t outputWidth;
433
435 size_t outputHeight;
436
438 arma::cube outputTemp;
439
441 arma::cube inputPaddedTemp;
442
444 arma::cube inputExpandedTemp;
445
447 arma::cube gTemp;
448
450 arma::cube gradientTemp;
451
453 ann::Padding<> paddingForward;
454
456 ann::Padding<> paddingBackward;
457
459 OutputDataType delta;
460
462 OutputDataType gradient;
463
465 InputDataType inputParameter;
466
468 OutputDataType outputParameter;
469}; // class TransposedConvolution
470
471} // namespace ann
472} // namespace mlpack
473
475namespace boost {
476namespace serialization {
477
478template<
479 typename ForwardConvolutionRule,
480 typename BackwardConvolutionRule,
481 typename GradientConvolutionRule,
482 typename InputDataType,
483 typename OutputDataType
484>
485struct version<
486 mlpack::ann::TransposedConvolution<ForwardConvolutionRule,
487 BackwardConvolutionRule, GradientConvolutionRule, InputDataType,
488 OutputDataType> >
489{
490 BOOST_STATIC_CONSTANT(int, value = 1);
491};
492
493} // namespace serialization
494} // namespace boost
495
496// Include implementation.
497#include "transposed_convolution_impl.hpp"
498
499#endif
Implementation of the Transposed Convolution class.
arma::cube const & Weight() const
Get the weight of the layer.
OutputDataType const & Delta() const
Get the delta.
size_t StrideHeight() const
Get the stride height.
OutputDataType const & Parameters() const
Get the parameters.
size_t & PadHTop()
Modify the top padding height.
size_t & InputHeight()
Modify the input height.
TransposedConvolution(const size_t inSize, const size_t outSize, const size_t kernelWidth, const size_t kernelHeight, const size_t strideWidth, const size_t strideHeight, const std::tuple< size_t, size_t > &padW, const std::tuple< size_t, size_t > &padH, const size_t inputWidth=0, const size_t inputHeight=0, const size_t outputWidth=0, const size_t outputHeight=0, const std::string &paddingType="None")
Create the Transposed Convolution object using the specified number of input maps,...
size_t PadWLeft() const
Get the left padding width.
TransposedConvolution(const size_t inSize, const size_t outSize, const size_t kernelWidth, const size_t kernelHeight, const size_t strideWidth=1, const size_t strideHeight=1, const size_t padW=0, const size_t padH=0, const size_t inputWidth=0, const size_t inputHeight=0, const size_t outputWidth=0, const size_t outputHeight=0, const std::string &paddingType="None")
Create the Transposed Convolution object using the specified number of input maps,...
size_t InputWidth() const
Get the input width.
void Forward(const arma::Mat< eT > &input, arma::Mat< eT > &output)
Ordinary feed forward pass of a neural network, evaluating the function f(x) by propagating the activ...
arma::cube & Weight()
Modify the weight of the layer.
InputDataType & InputParameter()
Modify the input parameter.
OutputDataType const & OutputParameter() const
Get the output parameter.
size_t & PadHBottom()
Modify the bottom padding height.
size_t InputSize() const
Get the input size.
size_t & InputWidth()
Modify input the width.
size_t KernelWidth() const
Get the kernel width.
TransposedConvolution()
Create the Transposed Convolution object.
size_t KernelHeight() const
Get the kernel height.
size_t & StrideWidth()
Modify the stride width.
size_t PadHBottom() const
Get the bottom padding height.
arma::mat const & Bias() const
Get the bias of the layer.
size_t & KernelWidth()
Modify the kernel width.
size_t PadWRight() const
Get the right padding width.
size_t & OutputHeight()
Modify the output height.
size_t & PadWRight()
Modify the right padding width.
size_t OutputHeight() const
Get the output height.
size_t OutputSize() const
Get the output size.
void Gradient(const arma::Mat< eT > &, const arma::Mat< eT > &error, arma::Mat< eT > &gradient)
size_t OutputWidth() const
Get the output width.
OutputDataType const & Gradient() const
Get the gradient.
InputDataType const & InputParameter() const
Get the input parameter.
size_t InputHeight() const
Get the input height.
OutputDataType & Gradient()
Modify the gradient.
size_t & KernelHeight()
Modify the kernel height.
size_t & PadWLeft()
Modify the left padding width.
size_t StrideWidth() const
Get the stride width.
size_t & StrideHeight()
Modify the stride height.
void Backward(const arma::Mat< eT > &, const arma::Mat< eT > &gy, arma::Mat< eT > &g)
Ordinary feed backward pass of a neural network, calculating the function f(x) by propagating x backw...
OutputDataType & OutputParameter()
Modify the output parameter.
size_t & OutputWidth()
Modify the output width.
size_t PadHTop() const
Get the top padding height.
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
OutputDataType & Parameters()
Modify the parameters.
arma::mat & Bias()
Modify the bias of the layer.
OutputDataType & Delta()
Modify the delta.
Set the serialization version of the adaboost class.
Definition: adaboost.hpp:198
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
The core includes that mlpack expects; standard C++ includes and Armadillo.