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 
41 template <
42  typename ForwardConvolutionRule = NaiveConvolution<ValidConvolution>,
43  typename BackwardConvolutionRule = NaiveConvolution<FullConvolution>,
44  typename GradientConvolutionRule = NaiveConvolution<ValidConvolution>,
45  typename InputDataType = arma::mat,
46  typename OutputDataType = arma::mat
47 >
49 {
50  public:
52  Convolution();
53 
70  Convolution(const size_t inSize,
71  const size_t outSize,
72  const size_t kernelWidth,
73  const size_t kernelHeight,
74  const size_t strideWidth = 1,
75  const size_t strideHeight = 1,
76  const size_t padW = 0,
77  const size_t padH = 0,
78  const size_t inputWidth = 0,
79  const size_t inputHeight = 0,
80  const std::string& paddingType = "None");
81 
102  Convolution(const size_t inSize,
103  const size_t outSize,
104  const size_t kernelWidth,
105  const size_t kernelHeight,
106  const size_t strideWidth,
107  const size_t strideHeight,
108  const std::tuple<size_t, size_t>& padW,
109  const std::tuple<size_t, size_t>& padH,
110  const size_t inputWidth = 0,
111  const size_t inputHeight = 0,
112  const std::string& paddingType = "None");
113 
114  /*
115  * Set the weight and bias term.
116  */
117  void Reset();
118 
126  template<typename eT>
127  void Forward(const arma::Mat<eT>& input, arma::Mat<eT>& output);
128 
138  template<typename eT>
139  void Backward(const arma::Mat<eT>& /* input */,
140  const arma::Mat<eT>& gy,
141  arma::Mat<eT>& g);
142 
143  /*
144  * Calculate the gradient using the output delta and the input activation.
145  *
146  * @param input The input parameter used for calculating the gradient.
147  * @param error The calculated error.
148  * @param gradient The calculated gradient.
149  */
150  template<typename eT>
151  void Gradient(const arma::Mat<eT>& /* input */,
152  const arma::Mat<eT>& error,
153  arma::Mat<eT>& gradient);
154 
156  OutputDataType const& Parameters() const { return weights; }
158  OutputDataType& Parameters() { return weights; }
159 
161  arma::cube const& Weight() const { return weight; }
163  arma::cube& Weight() { return weight; }
164 
166  arma::mat const& Bias() const { return bias; }
168  arma::mat& Bias() { return bias; }
169 
171  InputDataType const& InputParameter() const { return inputParameter; }
173  InputDataType& InputParameter() { return inputParameter; }
174 
176  OutputDataType const& OutputParameter() const { return outputParameter; }
178  OutputDataType& OutputParameter() { return outputParameter; }
179 
181  OutputDataType const& Delta() const { return delta; }
183  OutputDataType& Delta() { return delta; }
184 
186  OutputDataType const& Gradient() const { return gradient; }
188  OutputDataType& Gradient() { return gradient; }
189 
191  size_t InputWidth() const { return inputWidth; }
193  size_t& InputWidth() { return inputWidth; }
194 
196  size_t InputHeight() const { return inputHeight; }
198  size_t& InputHeight() { return inputHeight; }
199 
201  size_t OutputWidth() const { return outputWidth; }
203  size_t& OutputWidth() { return outputWidth; }
204 
206  size_t OutputHeight() const { return outputHeight; }
208  size_t& OutputHeight() { return outputHeight; }
209 
211  size_t InputSize() const { return inSize; }
212 
214  size_t OutputSize() const { return outSize; }
215 
217  size_t KernelWidth() const { return kernelWidth; }
219  size_t& KernelWidth() { return kernelWidth; }
220 
222  size_t KernelHeight() const { return kernelHeight; }
224  size_t& KernelHeight() { return kernelHeight; }
225 
227  size_t StrideWidth() const { return strideWidth; }
229  size_t& StrideWidth() { return strideWidth; }
230 
232  size_t StrideHeight() const { return strideHeight; }
234  size_t& StrideHeight() { return strideHeight; }
235 
237  size_t PadHTop() const { return padHTop; }
239  size_t& PadHTop() { return padHTop; }
240 
242  size_t PadHBottom() const { return padHBottom; }
244  size_t& PadHBottom() { return padHBottom; }
245 
247  size_t PadWLeft() const { return padWLeft; }
249  size_t& PadWLeft() { return padWLeft; }
250 
252  size_t PadWRight() const { return padWRight; }
254  size_t& PadWRight() { return padWRight; }
255 
259  template<typename Archive>
260  void serialize(Archive& ar, const unsigned int /* version */);
261 
262  private:
263  /*
264  * Return the convolution output size.
265  *
266  * @param size The size of the input (row or column).
267  * @param k The size of the filter (width or height).
268  * @param s The stride size (x or y direction).
269  * @param pSideOne The size of the padding (width or height) on one side.
270  * @param pSideTwo The size of the padding (width or height) on another side.
271  * @return The convolution output size.
272  */
273  size_t ConvOutSize(const size_t size,
274  const size_t k,
275  const size_t s,
276  const size_t pSideOne,
277  const size_t pSideTwo)
278  {
279  return std::floor(size + pSideOne + pSideTwo - k) / s + 1;
280  }
281 
282  /*
283  * Function to assign padding such that output size is same as input size.
284  */
285  void InitializeSamePadding();
286 
287  /*
288  * Rotates a 3rd-order tensor counterclockwise by 180 degrees.
289  *
290  * @param input The input data to be rotated.
291  * @param output The rotated output.
292  */
293  template<typename eT>
294  void Rotate180(const arma::Cube<eT>& input, arma::Cube<eT>& output)
295  {
296  output = arma::Cube<eT>(input.n_rows, input.n_cols, input.n_slices);
297 
298  // * left-right flip, up-down flip */
299  for (size_t s = 0; s < output.n_slices; s++)
300  output.slice(s) = arma::fliplr(arma::flipud(input.slice(s)));
301  }
302 
303  /*
304  * Rotates a dense matrix counterclockwise by 180 degrees.
305  *
306  * @param input The input data to be rotated.
307  * @param output The rotated output.
308  */
309  template<typename eT>
310  void Rotate180(const arma::Mat<eT>& input, arma::Mat<eT>& output)
311  {
312  // * left-right flip, up-down flip */
313  output = arma::fliplr(arma::flipud(input));
314  }
315 
317  size_t inSize;
318 
320  size_t outSize;
321 
323  size_t batchSize;
324 
326  size_t kernelWidth;
327 
329  size_t kernelHeight;
330 
332  size_t strideWidth;
333 
335  size_t strideHeight;
336 
338  size_t padWLeft;
339 
341  size_t padWRight;
342 
344  size_t padHBottom;
345 
347  size_t padHTop;
348 
350  OutputDataType weights;
351 
353  arma::cube weight;
354 
356  arma::mat bias;
357 
359  size_t inputWidth;
360 
362  size_t inputHeight;
363 
365  size_t outputWidth;
366 
368  size_t outputHeight;
369 
371  arma::cube outputTemp;
372 
374  arma::cube inputPaddedTemp;
375 
377  arma::cube gTemp;
378 
380  arma::cube gradientTemp;
381 
383  ann::Padding<> padding;
384 
386  OutputDataType delta;
387 
389  OutputDataType gradient;
390 
392  InputDataType inputParameter;
393 
395  OutputDataType outputParameter;
396 }; // class Convolution
397 
398 } // namespace ann
399 } // namespace mlpack
400 
402 namespace boost {
403 namespace serialization {
404 
405 template<
406  typename ForwardConvolutionRule,
407  typename BackwardConvolutionRule,
408  typename GradientConvolutionRule,
409  typename InputDataType,
410  typename OutputDataType
411 >
412 struct version<
413  mlpack::ann::Convolution<ForwardConvolutionRule, BackwardConvolutionRule,
414  GradientConvolutionRule, InputDataType, OutputDataType> >
415 {
416  BOOST_STATIC_CONSTANT(int, value = 1);
417 };
418 
419 } // namespace serialization
420 } // namespace boost
421 
422 // Include implementation.
423 #include "convolution_impl.hpp"
424 
425 #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 & PadWRight()
Modify the right padding width.
OutputDataType const & Parameters() const
Get the parameters.
Set the serialization version of the adaboost class.
Definition: adaboost.hpp:198
Linear algebra utility functions, generally performed on matrices or vectors.
Implementation of the Padding module class.
Definition: layer_types.hpp:81
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:48
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 input size.
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.
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...
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.
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.
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
OutputDataType const & Gradient() const
Get the gradient.
size_t OutputSize() const
Get the output size.