convolution.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_ANN_LAYER_CONVOLUTION_HPP
13 #define MLPACK_METHODS_ANN_LAYER_CONVOLUTION_HPP
14 
15 #include <mlpack/prereqs.hpp>
16 
22 
23 #include "layer_types.hpp"
24 #include "padding.hpp"
25 
26 namespace mlpack {
27 namespace ann {
28 
70 template <
71  typename ForwardConvolutionRule = NaiveConvolution<ValidConvolution>,
72  typename BackwardConvolutionRule = NaiveConvolution<FullConvolution>,
73  typename GradientConvolutionRule = NaiveConvolution<ValidConvolution>,
74  typename InputDataType = arma::mat,
75  typename OutputDataType = arma::mat
76 >
78 {
79  public:
81  Convolution();
82 
99  Convolution(const size_t inSize,
100  const size_t outSize,
101  const size_t kernelWidth,
102  const size_t kernelHeight,
103  const size_t strideWidth = 1,
104  const size_t strideHeight = 1,
105  const size_t padW = 0,
106  const size_t padH = 0,
107  const size_t inputWidth = 0,
108  const size_t inputHeight = 0,
109  const std::string& paddingType = "None");
110 
131  Convolution(const size_t inSize,
132  const size_t outSize,
133  const size_t kernelWidth,
134  const size_t kernelHeight,
135  const size_t strideWidth,
136  const size_t strideHeight,
137  const std::tuple<size_t, size_t>& padW,
138  const std::tuple<size_t, size_t>& padH,
139  const size_t inputWidth = 0,
140  const size_t inputHeight = 0,
141  const std::string& paddingType = "None");
142 
144  Convolution(const Convolution& layer);
145 
148 
150  Convolution& operator=(const Convolution& layer);
151 
154 
155  /*
156  * Set the weight and bias term.
157  */
158  void Reset();
159 
167  template<typename eT>
168  void Forward(const arma::Mat<eT>& input, arma::Mat<eT>& output);
169 
179  template<typename eT>
180  void Backward(const arma::Mat<eT>& /* input */,
181  const arma::Mat<eT>& gy,
182  arma::Mat<eT>& g);
183 
184  /*
185  * Calculate the gradient using the output delta and the input activation.
186  *
187  * @param input The input parameter used for calculating the gradient.
188  * @param error The calculated error.
189  * @param gradient The calculated gradient.
190  */
191  template<typename eT>
192  void Gradient(const arma::Mat<eT>& /* input */,
193  const arma::Mat<eT>& error,
194  arma::Mat<eT>& gradient);
195 
197  OutputDataType const& Parameters() const { return weights; }
199  OutputDataType& Parameters() { return weights; }
200 
202  arma::cube const& Weight() const { return weight; }
204  arma::cube& Weight() { return weight; }
205 
207  arma::mat const& Bias() const { return bias; }
209  arma::mat& Bias() { return bias; }
210 
212  InputDataType const& InputParameter() const { return inputParameter; }
214  InputDataType& InputParameter() { return inputParameter; }
215 
217  OutputDataType const& OutputParameter() const { return outputParameter; }
219  OutputDataType& OutputParameter() { return outputParameter; }
220 
222  OutputDataType const& Delta() const { return delta; }
224  OutputDataType& Delta() { return delta; }
225 
227  OutputDataType const& Gradient() const { return gradient; }
229  OutputDataType& Gradient() { return gradient; }
230 
232  size_t InputWidth() const { return inputWidth; }
234  size_t& InputWidth() { return inputWidth; }
235 
237  size_t InputHeight() const { return inputHeight; }
239  size_t& InputHeight() { return inputHeight; }
240 
242  size_t OutputWidth() const { return outputWidth; }
244  size_t& OutputWidth() { return outputWidth; }
245 
247  size_t OutputHeight() const { return outputHeight; }
249  size_t& OutputHeight() { return outputHeight; }
250 
252  size_t InputSize() const { return inSize; }
253 
255  size_t OutputSize() const { return outSize; }
256 
258  size_t KernelWidth() const { return kernelWidth; }
260  size_t& KernelWidth() { return kernelWidth; }
261 
263  size_t KernelHeight() const { return kernelHeight; }
265  size_t& KernelHeight() { return kernelHeight; }
266 
268  size_t StrideWidth() const { return strideWidth; }
270  size_t& StrideWidth() { return strideWidth; }
271 
273  size_t StrideHeight() const { return strideHeight; }
275  size_t& StrideHeight() { return strideHeight; }
276 
278  size_t PadHTop() const { return padHTop; }
280  size_t& PadHTop() { return padHTop; }
281 
283  size_t PadHBottom() const { return padHBottom; }
285  size_t& PadHBottom() { return padHBottom; }
286 
288  size_t PadWLeft() const { return padWLeft; }
290  size_t& PadWLeft() { return padWLeft; }
291 
293  size_t PadWRight() const { return padWRight; }
295  size_t& PadWRight() { return padWRight; }
296 
298  size_t WeightSize() const
299  {
300  return (outSize * inSize * kernelWidth * kernelHeight) + outSize;
301  }
302 
304  size_t InputShape() const
305  {
306  return inputHeight * inputWidth * inSize;
307  }
308 
312  template<typename Archive>
313  void serialize(Archive& ar, const uint32_t /* version */);
314 
315  private:
316  /*
317  * Return the convolution output size.
318  *
319  * @param size The size of the input (row or column).
320  * @param k The size of the filter (width or height).
321  * @param s The stride size (x or y direction).
322  * @param pSideOne The size of the padding (width or height) on one side.
323  * @param pSideTwo The size of the padding (width or height) on another side.
324  * @return The convolution output size.
325  */
326  size_t ConvOutSize(const size_t size,
327  const size_t k,
328  const size_t s,
329  const size_t pSideOne,
330  const size_t pSideTwo)
331  {
332  return std::floor(size + pSideOne + pSideTwo - k) / s + 1;
333  }
334 
335  /*
336  * Function to assign padding such that output size is same as input size.
337  */
338  void InitializeSamePadding();
339 
340  /*
341  * Rotates a 3rd-order tensor counterclockwise by 180 degrees.
342  *
343  * @param input The input data to be rotated.
344  * @param output The rotated output.
345  */
346  template<typename eT>
347  void Rotate180(const arma::Cube<eT>& input, arma::Cube<eT>& output)
348  {
349  output = arma::Cube<eT>(input.n_rows, input.n_cols, input.n_slices);
350 
351  // * left-right flip, up-down flip */
352  for (size_t s = 0; s < output.n_slices; s++)
353  output.slice(s) = arma::fliplr(arma::flipud(input.slice(s)));
354  }
355 
356  /*
357  * Rotates a dense matrix counterclockwise by 180 degrees.
358  *
359  * @param input The input data to be rotated.
360  * @param output The rotated output.
361  */
362  template<typename eT>
363  void Rotate180(const arma::Mat<eT>& input, arma::Mat<eT>& output)
364  {
365  // * left-right flip, up-down flip */
366  output = arma::fliplr(arma::flipud(input));
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  OutputDataType weights;
404 
406  arma::cube weight;
407 
409  arma::mat bias;
410 
412  size_t inputWidth;
413 
415  size_t inputHeight;
416 
418  size_t outputWidth;
419 
421  size_t outputHeight;
422 
424  arma::cube outputTemp;
425 
427  arma::cube inputPaddedTemp;
428 
430  arma::cube gTemp;
431 
433  arma::cube gradientTemp;
434 
436  ann::Padding<> padding;
437 
439  OutputDataType delta;
440 
442  OutputDataType gradient;
443 
445  InputDataType inputParameter;
446 
448  OutputDataType outputParameter;
449 }; // class Convolution
450 
451 } // namespace ann
452 } // namespace mlpack
453 
454 // Include implementation.
455 #include "convolution_impl.hpp"
456 
457 #endif
size_t OutputWidth() const
Get the output width.
size_t & PadWLeft()
Modify the left padding width.
InputDataType const & InputParameter() const
Get the input parameter.
size_t InputShape() const
Get the shape of the input.
size_t & PadWRight()
Modify the right padding width.
OutputDataType const & Parameters() const
Get the parameters.
constexpr auto size(Container const &container) noexcept -> decltype(container.size())
Definition: iterator.hpp:29
Linear algebra utility functions, generally performed on matrices or vectors.
Implementation of the Padding module class.
Definition: layer_types.hpp:89
size_t & OutputHeight()
Modify the output height.
The core includes that mlpack expects; standard C++ includes and Armadillo.
OutputDataType const & OutputParameter() const
Get the output parameter.
Implementation of the Convolution class.
Definition: convolution.hpp:77
size_t KernelHeight() const
Get the kernel height.
size_t PadWRight() const
Get the right padding width.
size_t KernelWidth() const
Get the kernel width.
size_t PadHTop() const
Get the top padding height.
OutputDataType & Gradient()
Modify the gradient.
size_t StrideHeight() const
Get the stride height.
InputDataType & InputParameter()
Modify the input parameter.
size_t InputSize() const
Get the number of input maps.
size_t & KernelHeight()
Modify the kernel height.
size_t & InputHeight()
Modify the input height.
arma::mat & Bias()
Modify the bias of the layer.
size_t & PadHTop()
Modify the top padding height.
size_t StrideWidth() const
Get the stride width.
arma::mat const & Bias() const
Get the bias of the layer.
size_t InputWidth() const
Get the input width.
Convolution & operator=(const Convolution &layer)
Copy assignment operator.
size_t InputHeight() const
Get the input height.
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...
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
OutputDataType const & Delta() const
Get the delta.
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...
size_t & InputWidth()
Modify input the width.
OutputDataType & Parameters()
Modify the parameters.
size_t & PadHBottom()
Modify the bottom padding height.
size_t PadWLeft() const
Get the left padding width.
size_t WeightSize() const
Get size of weights for the layer.
arma::cube const & Weight() const
Get the weight of the layer.
size_t & StrideHeight()
Modify the stride height.
size_t & OutputWidth()
Modify the output width.
arma::cube & Weight()
Modify the weight of the layer.
size_t OutputHeight() const
Get the output height.
OutputDataType & Delta()
Modify the delta.
OutputDataType & OutputParameter()
Modify the output parameter.
Convolution()
Create the Convolution object.
size_t & KernelWidth()
Modify the kernel width.
size_t & StrideWidth()
Modify the stride width.
size_t PadHBottom() const
Get the bottom padding height.
OutputDataType const & Gradient() const
Get the gradient.
size_t OutputSize() const
Get the number of output maps.