transposed_convolution.hpp
Go to the documentation of this file.
1 // Temporarily drop.
14 #ifndef MLPACK_METHODS_ANN_LAYER_TRANSPOSED_CONVOLUTION_HPP
15 #define MLPACK_METHODS_ANN_LAYER_TRANSPOSED_CONVOLUTION_HPP
16 
17 #include <mlpack/prereqs.hpp>
18 
24 
25 #include "layer.hpp"
26 #include "padding.hpp"
27 
28 namespace mlpack {
29 namespace ann {
30 
43 template <
44  typename ForwardConvolutionRule = NaiveConvolution<ValidConvolution>,
45  typename BackwardConvolutionRule = NaiveConvolution<ValidConvolution>,
46  typename GradientConvolutionRule = NaiveConvolution<ValidConvolution>,
47  typename InputType = arma::mat,
48  typename OutputType = arma::mat
49 >
50 class TransposedConvolutionType : public Layer<InputType, OutputType>
51 {
52  public:
55 
80  // TODO: remove inputWidth and inputHeight?
81  TransposedConvolutionType(const size_t inSize,
82  const size_t outSize,
83  const size_t kernelWidth,
84  const size_t kernelHeight,
85  const size_t strideWidth = 1,
86  const size_t strideHeight = 1,
87  const size_t padW = 0,
88  const size_t padH = 0,
89  const size_t inputWidth = 0,
90  const size_t inputHeight = 0,
91  const size_t outputWidth = 0,
92  const size_t outputHeight = 0,
93  const std::string& paddingType = "None");
94 
123  TransposedConvolutionType(const size_t inSize,
124  const size_t outSize,
125  const size_t kernelWidth,
126  const size_t kernelHeight,
127  const size_t strideWidth,
128  const size_t strideHeight,
129  const std::tuple<size_t, size_t>& padW,
130  const std::tuple<size_t, size_t>& padH,
131  const size_t inputWidth = 0,
132  const size_t inputHeight = 0,
133  const size_t outputWidth = 0,
134  const size_t outputHeight = 0,
135  const std::string& paddingType = "None");
136 
137  /*
138  * Set the weight and bias term.
139  */
140  void SetWeights(const typename OutputType::elem_type* weightsPtr);
141 
149  void Forward(const InputType& input, OutputType& output);
150 
160  void Backward(const InputType& /* input */,
161  const OutputType& gy,
162  OutputType& g);
163 
171  void Gradient(const InputType& /* input */,
172  const OutputType& error,
173  OutputType& gradient);
174 
176  OutputType const& Parameters() const { return weights; }
178  OutputType& Parameters() { return weights; }
179 
181  arma::Cube<typename OutputType::elem_type> const& Weight() const
182  {
183  return weight;
184  }
186  arma::Cube<typename OutputType::elem_type>& Weight() { return weight; }
187 
189  OutputType const& Bias() const { return bias; }
191  OutputType& Bias() { return bias; }
192 
194  size_t const& InputWidth() const { return inputWidth; }
196  size_t& InputWidth() { return inputWidth; }
197 
199  size_t const& InputHeight() const { return inputHeight; }
201  size_t& InputHeight() { return inputHeight; }
202 
204  size_t const& OutputWidth() const { return outputWidth; }
206  size_t& OutputWidth() { return outputWidth; }
207 
209  size_t const& OutputHeight() const { return outputHeight; }
211  size_t& OutputHeight() { return outputHeight; }
212 
214  size_t const& InputSize() const { return inSize; }
215 
217  size_t const& OutputSize() const { return outSize; }
218 
220  size_t const& KernelWidth() const { return kernelWidth; }
222  size_t& KernelWidth() { return kernelWidth; }
223 
225  size_t const& KernelHeight() const { return kernelHeight; }
227  size_t& KernelHeight() { return kernelHeight; }
228 
230  size_t const& StrideWidth() const { return strideWidth; }
232  size_t& StrideWidth() { return strideWidth; }
233 
235  size_t const& StrideHeight() const { return strideHeight; }
237  size_t& StrideHeight() { return strideHeight; }
238 
240  size_t const& PadHTop() const { return padHTop; }
242  size_t& PadHTop() { return padHTop; }
243 
245  size_t const& PadHBottom() const { return padHBottom; }
247  size_t& PadHBottom() { return padHBottom; }
248 
250  size_t const& PadWLeft() const { return padWLeft; }
252  size_t& PadWLeft() { return padWLeft; }
253 
255  size_t const& PadWRight() const { return padWRight; }
257  size_t& PadWRight() { return padWRight; }
258 
260  size_t WeightSize() const
261  {
262  return (outSize * inSize * kernelWidth * kernelHeight) + outSize;
263  }
264 
265  const std::vector<size_t>& OutputDimensions() const
266  {
267  std::vector<size_t> result(inputDimensions.size(), 0);
268  result[0] = outputWidth;
269  result[1] = outputHeight;
270  // Higher dimensions are unmodified.
271  for (size_t i = 2; i < inputDimensions.size(); ++i)
272  result[i] = inputDimensions[i];
273  return result;
274  }
275 
279  template<typename Archive>
280  void serialize(Archive& ar, const uint32_t /* version */);
281 
282  private:
283  /*
284  * Rotates a 3rd-order tensor counterclockwise by 180 degrees.
285  *
286  * @param input The input data to be rotated.
287  * @param output The rotated output.
288  */
289  template<typename eT>
290  void Rotate180(const arma::Cube<eT>& input, arma::Cube<eT>& output)
291  {
292  output = arma::Cube<eT>(input.n_rows, input.n_cols, input.n_slices);
293 
294  // * left-right flip, up-down flip */
295  for (size_t s = 0; s < output.n_slices; s++)
296  output.slice(s) = arma::fliplr(arma::flipud(input.slice(s)));
297  }
298 
299  /*
300  * Function to assign padding such that output size is same as input size.
301  */
302  void InitializeSamePadding();
303 
304  /*
305  * Rotates a dense matrix counterclockwise by 180 degrees.
306  *
307  * @param input The input data to be rotated.
308  * @param output The rotated output.
309  */
310  template<typename eT>
311  void Rotate180(const arma::Mat<eT>& input, arma::Mat<eT>& output)
312  {
313  // * left-right flip, up-down flip */
314  output = arma::fliplr(arma::flipud(input));
315  }
316 
317 
318  /*
319  * Insert zeros between the units of the given input data.
320  * Note: This function should be used before using padding layer.
321  *
322  * @param input The input to be padded.
323  * @param strideWidth Stride of filter application in the x direction.
324  * @param strideHeight Stride of filter application in the y direction.
325  * @param output The padded output data.
326  */
327  template<typename eT>
328  void InsertZeros(const arma::Mat<eT>& input,
329  const size_t strideWidth,
330  const size_t strideHeight,
331  arma::Mat<eT>& output)
332  {
333  if (output.n_rows != input.n_rows * strideWidth - strideWidth + 1 ||
334  output.n_cols != input.n_cols * strideHeight - strideHeight + 1)
335  {
336  output = arma::zeros(input.n_rows * strideWidth - strideWidth + 1,
337  input.n_cols * strideHeight - strideHeight + 1);
338  }
339 
340  for (size_t i = 0; i < output.n_rows; i += strideHeight)
341  {
342  for (size_t j = 0; j < output.n_cols; j += strideWidth)
343  {
344  // TODO: Use [] instead of () for speedup after this is completely
345  // debugged and approved.
346  output(i, j) = input(i / strideHeight, j / strideWidth);
347  }
348  }
349  }
350 
351  /*
352  * Insert zeros between the units of the given input data.
353  * Note: This function should be used before using padding layer.
354  *
355  * @param input The input to be padded.
356  * @param strideWidth Stride of filter application in the x direction.
357  * @param strideHeight Stride of filter application in the y direction.
358  * @param output The padded output data.
359  */
360  template<typename eT>
361  void InsertZeros(const arma::Cube<eT>& input,
362  const size_t strideWidth,
363  const size_t strideHeight,
364  arma::Cube<eT>& output)
365  {
366  output = arma::zeros(input.n_rows * strideWidth - strideWidth + 1,
367  input.n_cols * strideHeight - strideHeight + 1, input.n_slices);
368 
369  for (size_t i = 0; i < input.n_slices; ++i)
370  {
371  InsertZeros<eT>(input.slice(i), strideWidth, strideHeight,
372  output.slice(i));
373  }
374  }
375 
377  size_t inSize;
378 
380  size_t outSize;
381 
383  size_t batchSize;
384 
386  size_t kernelWidth;
387 
389  size_t kernelHeight;
390 
392  size_t strideWidth;
393 
395  size_t strideHeight;
396 
398  size_t padWLeft;
399 
401  size_t padWRight;
402 
404  size_t padHBottom;
405 
407  size_t padHTop;
408 
410  size_t aW;
411 
413  size_t aH;
414 
416  OutputType weights;
417 
419  arma::Cube<typename OutputType::elem_type> weight;
420 
422  OutputType bias;
423 
425  size_t inputWidth;
426 
428  size_t inputHeight;
429 
431  size_t outputWidth;
432 
434  size_t outputHeight;
435 
437  arma::Cube<typename OutputType::elem_type> outputTemp;
438 
440  arma::Cube<typename InputType::elem_type> inputPaddedTemp;
441 
443  arma::Cube<typename InputType::elem_type> inputExpandedTemp;
444 
446  arma::Cube<typename OutputType::elem_type> gTemp;
447 
449  arma::Cube<typename OutputType::elem_type> gradientTemp;
450 
452  ann::Padding paddingForward;
453 
455  ann::Padding paddingBackward;
456 }; // class TransposedConvolutionType
457 
458 // Standard TransposedConvolution
461  NaiveConvolution<ValidConvolution>,
462  NaiveConvolution<ValidConvolution>,
463  arma::mat,
464  arma::mat
466 
467 } // namespace ann
468 } // namespace mlpack
469 
470 // Include implementation.
471 #include "transposed_convolution_impl.hpp"
472 
473 #endif
size_t & StrideWidth()
Modify the stride width.
size_t const & InputWidth() const
Get the input width.
size_t const & OutputHeight() const
Get the output height.
size_t & OutputWidth()
Modify the output width.
size_t const & InputSize() const
Get the input size.
std::vector< size_t > inputDimensions
Logical input dimensions of each point.
Definition: layer.hpp:302
size_t const & PadWRight() const
Get the right padding width.
size_t const & PadHTop() const
Get the top padding height.
size_t & StrideHeight()
Modify the stride height.
size_t & InputWidth()
Modify input the width.
Linear algebra utility functions, generally performed on matrices or vectors.
size_t & InputHeight()
Modify the input height.
arma::Cube< typename OutputType::elem_type > const & Weight() const
Get the weight of the layer.
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
The core includes that mlpack expects; standard C++ includes and Armadillo.
size_t const & InputHeight() const
Get the input height.
void SetWeights(const typename OutputType::elem_type *weightsPtr)
const std::vector< size_t > & OutputDimensions() const
void Gradient(const InputType &, const OutputType &error, OutputType &gradient)
Calculate the gradient using the output delta and the input activation.
void Backward(const InputType &, const OutputType &gy, OutputType &g)
Ordinary feed backward pass of a neural network, calculating the function f(x) by propagating x backw...
size_t & KernelWidth()
Modify the kernel width.
OutputType & Parameters()
Modify the parameters.
size_t const & PadWLeft() const
Get the left padding width.
size_t const & PadHBottom() const
Get the bottom padding height.
OutputType const & Parameters() const
Get the parameters.
size_t & OutputHeight()
Modify the output height.
size_t & PadHBottom()
Modify the bottom padding height.
size_t const & OutputWidth() const
Get the output width.
TransposedConvolutionType()
Create the Transposed Convolution object.
size_t & PadWLeft()
Modify the left padding width.
Computes the two-dimensional convolution.
size_t & KernelHeight()
Modify the kernel height.
size_t const & OutputSize() const
Get the output size.
Implementation of the Transposed Convolution class.
size_t & PadHTop()
Modify the top padding height.
size_t const & KernelHeight() const
Get the kernel height.
A layer is an abstract class implementing common neural networks operations, such as convolution...
Definition: layer.hpp:52
size_t const & StrideWidth() const
Get the stride width.
OutputType & Bias()
Modify the bias of the layer.
OutputType const & Bias() const
Get the bias of the layer.
size_t & PadWRight()
Modify the right padding width.
size_t const & KernelWidth() const
Get the kernel width.
arma::Cube< typename OutputType::elem_type > & Weight()
Modify the weight of the layer.
void Forward(const InputType &input, OutputType &output)
Ordinary feed forward pass of a neural network, evaluating the function f(x) by propagating the activ...
TransposedConvolutionType< NaiveConvolution< ValidConvolution >, NaiveConvolution< ValidConvolution >, NaiveConvolution< ValidConvolution >, arma::mat, arma::mat > TransposedConvolution
size_t WeightSize() const
Get the size of the weight matrix.
size_t const & StrideHeight() const
Get the stride height.