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 
22 
23 #include "layer_types.hpp"
24 #include "padding.hpp"
25 
26 namespace mlpack {
27 namespace ann {
28 
41 template <
42  typename ForwardConvolutionRule = NaiveConvolution<ValidConvolution>,
43  typename BackwardConvolutionRule = NaiveConvolution<ValidConvolution>,
44  typename GradientConvolutionRule = NaiveConvolution<ValidConvolution>,
45  typename InputDataType = arma::mat,
46  typename OutputDataType = arma::mat
47 >
48 class TransposedConvolution
49 {
50  public:
53 
78  TransposedConvolution(const size_t inSize,
79  const size_t outSize,
80  const size_t kernelWidth,
81  const size_t kernelHeight,
82  const size_t strideWidth = 1,
83  const size_t strideHeight = 1,
84  const size_t padW = 0,
85  const size_t padH = 0,
86  const size_t inputWidth = 0,
87  const size_t inputHeight = 0,
88  const size_t outputWidth = 0,
89  const size_t outputHeight = 0,
90  const std::string& paddingType = "None");
91 
120  TransposedConvolution(const size_t inSize,
121  const size_t outSize,
122  const size_t kernelWidth,
123  const size_t kernelHeight,
124  const size_t strideWidth,
125  const size_t strideHeight,
126  const std::tuple<size_t, size_t>& padW,
127  const std::tuple<size_t, size_t>& padH,
128  const size_t inputWidth = 0,
129  const size_t inputHeight = 0,
130  const size_t outputWidth = 0,
131  const size_t outputHeight = 0,
132  const std::string& paddingType = "None");
133 
134  /*
135  * Set the weight and bias term.
136  */
137  void Reset();
138 
146  template<typename eT>
147  void Forward(const arma::Mat<eT>&& input, arma::Mat<eT>&& output);
148 
158  template<typename eT>
159  void Backward(const arma::Mat<eT>&& /* input */,
160  arma::Mat<eT>&& gy,
161  arma::Mat<eT>&& g);
162 
163  /*
164  * Calculate the gradient using the output delta and the input activation.
165  *
166  * @param input The input parameter used for calculating the gradient.
167  * @param error The calculated error.
168  * @param gradient The calculated gradient.
169  */
170  template<typename eT>
171  void Gradient(const arma::Mat<eT>&& /* input */,
172  arma::Mat<eT>&& error,
173  arma::Mat<eT>&& gradient);
174 
176  OutputDataType const& Parameters() const { return weights; }
178  OutputDataType& Parameters() { return weights; }
179 
181  InputDataType const& InputParameter() const { return inputParameter; }
183  InputDataType& InputParameter() { return inputParameter; }
184 
186  OutputDataType const& OutputParameter() const { return outputParameter; }
188  OutputDataType& OutputParameter() { return outputParameter; }
189 
191  OutputDataType const& Delta() const { return delta; }
193  OutputDataType& Delta() { return delta; }
194 
196  OutputDataType const& Gradient() const { return gradient; }
198  OutputDataType& Gradient() { return gradient; }
199 
201  size_t const& InputWidth() const { return inputWidth; }
203  size_t& InputWidth() { return inputWidth; }
204 
206  size_t const& InputHeight() const { return inputHeight; }
208  size_t& InputHeight() { return inputHeight; }
209 
211  size_t const& OutputWidth() const { return outputWidth; }
213  size_t& OutputWidth() { return outputWidth; }
214 
216  size_t const& OutputHeight() const { return outputHeight; }
218  size_t& OutputHeight() { return outputHeight; }
219 
221  size_t InputSize() const { return inSize; }
222 
224  size_t OutputSize() const { return outSize; }
225 
227  size_t KernelWidth() const { return kernelWidth; }
229  size_t& KernelWidth() { return kernelWidth; }
230 
232  size_t KernelHeight() const { return kernelHeight; }
234  size_t& KernelHeight() { return kernelHeight; }
235 
237  size_t StrideWidth() const { return strideWidth; }
239  size_t& StrideWidth() { return strideWidth; }
240 
242  size_t StrideHeight() const { return strideHeight; }
244  size_t& StrideHeight() { return strideHeight; }
245 
247  size_t PadHTop() const { return padHTop; }
249  size_t& PadHTop() { return padHTop; }
250 
252  size_t PadHBottom() const { return padHBottom; }
254  size_t& PadHBottom() { return padHBottom; }
255 
257  size_t PadWLeft() const { return padWLeft; }
259  size_t& PadWLeft() { return padWLeft; }
260 
262  size_t PadWRight() const { return padWRight; }
264  size_t& PadWRight() { return padWRight; }
265 
267  arma::mat& Bias() { return bias; }
268 
272  template<typename Archive>
273  void serialize(Archive& ar, const unsigned int /* version */);
274 
275  private:
276  /*
277  * Rotates a 3rd-order tensor counterclockwise by 180 degrees.
278  *
279  * @param input The input data to be rotated.
280  * @param output The rotated output.
281  */
282  template<typename eT>
283  void Rotate180(const arma::Cube<eT>& input, arma::Cube<eT>& output)
284  {
285  output = arma::Cube<eT>(input.n_rows, input.n_cols, input.n_slices);
286 
287  // * left-right flip, up-down flip */
288  for (size_t s = 0; s < output.n_slices; s++)
289  output.slice(s) = arma::fliplr(arma::flipud(input.slice(s)));
290  }
291 
292  /*
293  * Function to assign padding such that output size is same as input size.
294  */
295  void InitializeSamePadding();
296 
297  /*
298  * Rotates a dense matrix counterclockwise by 180 degrees.
299  *
300  * @param input The input data to be rotated.
301  * @param output The rotated output.
302  */
303  template<typename eT>
304  void Rotate180(const arma::Mat<eT>& input, arma::Mat<eT>& output)
305  {
306  // * left-right flip, up-down flip */
307  output = arma::fliplr(arma::flipud(input));
308  }
309 
310 
311  /*
312  * Insert zeros between the units of the given input data.
313  * Note: This function should be used before using padding layer.
314  *
315  * @param input The input to be padded.
316  * @param strideWidth Stride of filter application in the x direction.
317  * @param strideHeight Stride of filter application in the y direction.
318  * @param output The padded output data.
319  */
320  template<typename eT>
321  void InsertZeros(const arma::Mat<eT>& input,
322  const size_t strideWidth,
323  const size_t strideHeight,
324  arma::Mat<eT>& output)
325  {
326  if (output.n_rows != input.n_rows * strideWidth - strideWidth + 1 ||
327  output.n_cols != input.n_cols * strideHeight - strideHeight + 1)
328  {
329  output = arma::zeros(input.n_rows * strideWidth - strideWidth + 1,
330  input.n_cols * strideHeight - strideHeight + 1);
331  }
332 
333  for (size_t i = 0; i < output.n_rows; i += strideHeight)
334  {
335  for (size_t j = 0; j < output.n_cols; j += strideWidth)
336  {
337  // TODO: Use [] instead of () for speedup after this is completely
338  // debugged and approved.
339  output(i, j) = input(i / strideHeight, j / strideWidth);
340  }
341  }
342  }
343 
344  /*
345  * Insert zeros between the units of the given input data.
346  * Note: This function should be used before using padding layer.
347  *
348  * @param input The input to be padded.
349  * @param strideWidth Stride of filter application in the x direction.
350  * @param strideHeight Stride of filter application in the y direction.
351  * @param output The padded output data.
352  */
353  template<typename eT>
354  void InsertZeros(const arma::Cube<eT>& input,
355  const size_t strideWidth,
356  const size_t strideHeight,
357  arma::Cube<eT>& output)
358  {
359  output = arma::zeros(input.n_rows * strideWidth - strideWidth + 1,
360  input.n_cols * strideHeight - strideHeight + 1, input.n_slices);
361 
362  for (size_t i = 0; i < input.n_slices; ++i)
363  {
364  InsertZeros<eT>(input.slice(i), strideWidth, strideHeight,
365  output.slice(i));
366  }
367  }
368 
370  size_t inSize;
371 
373  size_t outSize;
374 
376  size_t batchSize;
377 
379  size_t kernelWidth;
380 
382  size_t kernelHeight;
383 
385  size_t strideWidth;
386 
388  size_t strideHeight;
389 
391  size_t padWLeft;
392 
394  size_t padWRight;
395 
397  size_t padHBottom;
398 
400  size_t padHTop;
401 
403  size_t aW;
404 
406  size_t aH;
407 
409  OutputDataType weights;
410 
412  arma::cube weight;
413 
415  arma::mat bias;
416 
418  size_t inputWidth;
419 
421  size_t inputHeight;
422 
424  size_t outputWidth;
425 
427  size_t outputHeight;
428 
430  arma::cube outputTemp;
431 
433  arma::cube inputTemp;
434 
436  arma::cube inputPaddedTemp;
437 
439  arma::cube inputExpandedTemp;
440 
442  arma::cube gTemp;
443 
445  arma::cube gradientTemp;
446 
448  ann::Padding<> paddingForward;
449 
451  ann::Padding<> paddingBackward;
452 
454  OutputDataType delta;
455 
457  OutputDataType gradient;
458 
460  InputDataType inputParameter;
461 
463  OutputDataType outputParameter;
464 }; // class TransposedConvolution
465 
466 } // namespace ann
467 } // namespace mlpack
468 
470 namespace boost {
471 namespace serialization {
472 
473 template<
474  typename ForwardConvolutionRule,
475  typename BackwardConvolutionRule,
476  typename GradientConvolutionRule,
477  typename InputDataType,
478  typename OutputDataType
479 >
480 struct version<
481  mlpack::ann::TransposedConvolution<ForwardConvolutionRule,
482  BackwardConvolutionRule, GradientConvolutionRule, InputDataType,
483  OutputDataType> >
484 {
485  BOOST_STATIC_CONSTANT(int, value = 1);
486 };
487 
488 } // namespace serialization
489 } // namespace boost
490 
491 // Include implementation.
492 #include "transposed_convolution_impl.hpp"
493 
494 #endif
arma::mat & Bias()
Modify the bias weights of the layer.
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 & PadWLeft()
Modify the left padding width.
OutputDataType const & Gradient() const
Get the gradient.
OutputDataType & Parameters()
Modify the parameters.
Set the serialization version of the adaboost class.
Definition: adaboost.hpp:194
size_t const & OutputHeight() const
Get the output height.
size_t const & InputHeight() const
Get the input height.
strip_type.hpp
Definition: add_to_po.hpp:21
Implementation of the Padding module class.
Definition: layer_types.hpp:68
size_t & StrideHeight()
Modify the stride height.
OutputDataType & Delta()
Modify the delta.
The core includes that mlpack expects; standard C++ includes and Armadillo.
size_t & InputHeight()
Modify the input height.
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.
void Backward(const arma::Mat< eT > &&, 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 const & Parameters() const
Get the parameters.
size_t const & InputWidth() const
Get the input width.
size_t const & OutputWidth() const
Get the output width.
size_t & StrideWidth()
Modify the stride width.
TransposedConvolution()
Create the Transposed Convolution object.
size_t KernelHeight() const
Get the kernel height.
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
OutputDataType & Gradient()
Modify the gradient.
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 PadWRight() const
Get the right padding width.
size_t StrideHeight() const
Get the stride height.
size_t & OutputHeight()
Modify the output height.