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 
27 namespace mlpack {
28 namespace ann {
29 
42 template <
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 >
49 class TransposedConvolution
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 
278  size_t InputShape() const
279  {
280  return inputHeight * inputWidth * inSize;
281  }
282 
284  size_t WeightSize() const
285  {
286  return (outSize * inSize * kernelWidth * kernelHeight) + outSize;
287  }
291  template<typename Archive>
292  void serialize(Archive& ar, const uint32_t /* version */);
293 
294  private:
295  /*
296  * Rotates a 3rd-order tensor counterclockwise by 180 degrees.
297  *
298  * @param input The input data to be rotated.
299  * @param output The rotated output.
300  */
301  template<typename eT>
302  void Rotate180(const arma::Cube<eT>& input, arma::Cube<eT>& output)
303  {
304  output = arma::Cube<eT>(input.n_rows, input.n_cols, input.n_slices);
305 
306  // * left-right flip, up-down flip */
307  for (size_t s = 0; s < output.n_slices; s++)
308  output.slice(s) = arma::fliplr(arma::flipud(input.slice(s)));
309  }
310 
311  /*
312  * Function to assign padding such that output size is same as input size.
313  */
314  void InitializeSamePadding();
315 
316  /*
317  * Rotates a dense matrix counterclockwise by 180 degrees.
318  *
319  * @param input The input data to be rotated.
320  * @param output The rotated output.
321  */
322  template<typename eT>
323  void Rotate180(const arma::Mat<eT>& input, arma::Mat<eT>& output)
324  {
325  // * left-right flip, up-down flip */
326  output = arma::fliplr(arma::flipud(input));
327  }
328 
329 
330  /*
331  * Insert zeros between the units of the given input data.
332  * Note: This function should be used before using padding layer.
333  *
334  * @param input The input to be padded.
335  * @param strideWidth Stride of filter application in the x direction.
336  * @param strideHeight Stride of filter application in the y direction.
337  * @param output The padded output data.
338  */
339  template<typename eT>
340  void InsertZeros(const arma::Mat<eT>& input,
341  const size_t strideWidth,
342  const size_t strideHeight,
343  arma::Mat<eT>& output)
344  {
345  if (output.n_rows != input.n_rows * strideWidth - strideWidth + 1 ||
346  output.n_cols != input.n_cols * strideHeight - strideHeight + 1)
347  {
348  output = arma::zeros(input.n_rows * strideWidth - strideWidth + 1,
349  input.n_cols * strideHeight - strideHeight + 1);
350  }
351 
352  for (size_t i = 0; i < output.n_rows; i += strideHeight)
353  {
354  for (size_t j = 0; j < output.n_cols; j += strideWidth)
355  {
356  // TODO: Use [] instead of () for speedup after this is completely
357  // debugged and approved.
358  output(i, j) = input(i / strideHeight, j / strideWidth);
359  }
360  }
361  }
362 
363  /*
364  * Insert zeros between the units of the given input data.
365  * Note: This function should be used before using padding layer.
366  *
367  * @param input The input to be padded.
368  * @param strideWidth Stride of filter application in the x direction.
369  * @param strideHeight Stride of filter application in the y direction.
370  * @param output The padded output data.
371  */
372  template<typename eT>
373  void InsertZeros(const arma::Cube<eT>& input,
374  const size_t strideWidth,
375  const size_t strideHeight,
376  arma::Cube<eT>& output)
377  {
378  output = arma::zeros(input.n_rows * strideWidth - strideWidth + 1,
379  input.n_cols * strideHeight - strideHeight + 1, input.n_slices);
380 
381  for (size_t i = 0; i < input.n_slices; ++i)
382  {
383  InsertZeros<eT>(input.slice(i), strideWidth, strideHeight,
384  output.slice(i));
385  }
386  }
387 
389  size_t inSize;
390 
392  size_t outSize;
393 
395  size_t batchSize;
396 
398  size_t kernelWidth;
399 
401  size_t kernelHeight;
402 
404  size_t strideWidth;
405 
407  size_t strideHeight;
408 
410  size_t padWLeft;
411 
413  size_t padWRight;
414 
416  size_t padHBottom;
417 
419  size_t padHTop;
420 
422  size_t aW;
423 
425  size_t aH;
426 
428  OutputDataType weights;
429 
431  arma::cube weight;
432 
434  arma::mat bias;
435 
437  size_t inputWidth;
438 
440  size_t inputHeight;
441 
443  size_t outputWidth;
444 
446  size_t outputHeight;
447 
449  arma::cube outputTemp;
450 
452  arma::cube inputPaddedTemp;
453 
455  arma::cube inputExpandedTemp;
456 
458  arma::cube gTemp;
459 
461  arma::cube gradientTemp;
462 
464  ann::Padding<> paddingForward;
465 
467  ann::Padding<> paddingBackward;
468 
470  OutputDataType delta;
471 
473  OutputDataType gradient;
474 
476  InputDataType inputParameter;
477 
479  OutputDataType outputParameter;
480 }; // class TransposedConvolution
481 
482 } // namespace ann
483 } // namespace mlpack
484 
485 // Include implementation.
486 #include "transposed_convolution_impl.hpp"
487 
488 #endif
arma::mat & Bias()
Modify the bias of the layer.
size_t InputHeight() const
Get the input height.
size_t & PadWLeft()
Modify the left padding width.
OutputDataType const & Gradient() const
Get the gradient.
OutputDataType & Parameters()
Modify the parameters.
Linear algebra utility functions, generally performed on matrices or vectors.
Implementation of the Padding module class.
Definition: layer_types.hpp:89
size_t & StrideHeight()
Modify the stride height.
size_t WeightSize() const
Get the size of the weight matrix.
OutputDataType & Delta()
Modify the delta.
The core includes that mlpack expects; standard C++ includes and Armadillo.
size_t OutputHeight() const
Get the output height.
size_t & InputHeight()
Modify the input height.
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
size_t & KernelWidth()
Modify the kernel width.
size_t & PadHTop()
Modify the top padding height.
size_t & PadWRight()
Modify the right padding width.
size_t PadWLeft() const
Get the left padding width.
OutputDataType const & OutputParameter() const
Get the output parameter.
size_t PadHTop() const
Get the top padding height.
OutputDataType const & Parameters() const
Get the parameters.
arma::cube & Weight()
Modify the weight of the layer.
arma::mat const & Bias() const
Get the bias of the layer.
size_t & StrideWidth()
Modify the stride width.
TransposedConvolution()
Create the Transposed Convolution object.
size_t KernelHeight() const
Get the kernel height.
arma::cube const & Weight() const
Get the weight of the layer.
OutputDataType & Gradient()
Modify the gradient.
size_t InputShape() const
Get the shape of the input.
size_t & KernelHeight()
Modify the kernel height.
InputDataType const & InputParameter() const
Get the input parameter.
OutputDataType const & Delta() const
Get the delta.
size_t & PadHBottom()
Modify the bottom padding height.
size_t & OutputWidth()
Modify the output width.
size_t OutputSize() const
Get the output size.
size_t & InputWidth()
Modify input the width.
size_t KernelWidth() const
Get the kernel width.
size_t InputSize() const
Get the input size.
size_t PadHBottom() const
Get the bottom padding height.
size_t StrideWidth() const
Get the stride width.
OutputDataType & OutputParameter()
Modify the output parameter.
InputDataType & InputParameter()
Modify the input parameter.
size_t OutputWidth() const
Get the output 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...
size_t InputWidth() const
Get the input width.
size_t PadWRight() const
Get the right padding width.
size_t StrideHeight() const
Get the stride height.
size_t & OutputHeight()
Modify the output 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...