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 
21 
22 #include "layer_types.hpp"
23 #include "padding.hpp"
24 
25 namespace mlpack {
26 namespace ann {
27 
40 template <
41  typename ForwardConvolutionRule = NaiveConvolution<ValidConvolution>,
42  typename BackwardConvolutionRule = NaiveConvolution<FullConvolution>,
43  typename GradientConvolutionRule = NaiveConvolution<ValidConvolution>,
44  typename InputDataType = arma::mat,
45  typename OutputDataType = arma::mat
46 >
48 {
49  public:
51  Convolution();
52 
69  Convolution(const size_t inSize,
70  const size_t outSize,
71  const size_t kernelWidth,
72  const size_t kernelHeight,
73  const size_t strideWidth = 1,
74  const size_t strideHeight = 1,
75  const size_t padW = 0,
76  const size_t padH = 0,
77  const size_t inputWidth = 0,
78  const size_t inputHeight = 0,
79  const std::string& paddingType = "None");
80 
101  Convolution(const size_t inSize,
102  const size_t outSize,
103  const size_t kernelWidth,
104  const size_t kernelHeight,
105  const size_t strideWidth,
106  const size_t strideHeight,
107  const std::tuple<size_t, size_t>& padW,
108  const std::tuple<size_t, size_t>& padH,
109  const size_t inputWidth = 0,
110  const size_t inputHeight = 0,
111  const std::string& paddingType = "None");
112 
113  /*
114  * Set the weight and bias term.
115  */
116  void Reset();
117 
125  template<typename eT>
126  void Forward(const arma::Mat<eT>&& input, arma::Mat<eT>&& output);
127 
137  template<typename eT>
138  void Backward(const arma::Mat<eT>&& /* input */,
139  arma::Mat<eT>&& gy,
140  arma::Mat<eT>&& g);
141 
142  /*
143  * Calculate the gradient using the output delta and the input activation.
144  *
145  * @param input The input parameter used for calculating the gradient.
146  * @param error The calculated error.
147  * @param gradient The calculated gradient.
148  */
149  template<typename eT>
150  void Gradient(const arma::Mat<eT>&& /* input */,
151  arma::Mat<eT>&& error,
152  arma::Mat<eT>&& gradient);
153 
155  const OutputDataType& Parameters() const { return weights; }
157  OutputDataType& Parameters() { return weights; }
158 
160  const InputDataType& InputParameter() const { return inputParameter; }
162  InputDataType& InputParameter() { return inputParameter; }
163 
165  const OutputDataType& OutputParameter() const { return outputParameter; }
167  OutputDataType& OutputParameter() { return outputParameter; }
168 
170  const OutputDataType& Delta() const { return delta; }
172  OutputDataType& Delta() { return delta; }
173 
175  const OutputDataType& Gradient() const { return gradient; }
177  OutputDataType& Gradient() { return gradient; }
178 
180  const size_t& InputWidth() const { return inputWidth; }
182  size_t& InputWidth() { return inputWidth; }
183 
185  const size_t& InputHeight() const { return inputHeight; }
187  size_t& InputHeight() { return inputHeight; }
188 
190  const size_t& OutputWidth() const { return outputWidth; }
192  size_t& OutputWidth() { return outputWidth; }
193 
195  const size_t& OutputHeight() const { return outputHeight; }
197  size_t& OutputHeight() { return outputHeight; }
198 
200  size_t InputSize() const { return inSize; }
201 
203  size_t OutputSize() const { return outSize; }
204 
206  size_t KernelWidth() const { return kernelWidth; }
208  size_t& KernelWidth() { return kernelWidth; }
209 
211  size_t KernelHeight() const { return kernelHeight; }
213  size_t& KernelHeight() { return kernelHeight; }
214 
216  size_t StrideWidth() const { return strideWidth; }
218  size_t& StrideWidth() { return strideWidth; }
219 
221  size_t StrideHeight() const { return strideHeight; }
223  size_t& StrideHeight() { return strideHeight; }
224 
226  size_t PadHTop() const { return padHTop; }
228  size_t& PadHTop() { return padHTop; }
229 
231  size_t PadHBottom() const { return padHBottom; }
233  size_t& PadHBottom() { return padHBottom; }
234 
236  size_t PadWLeft() const { return padWLeft; }
238  size_t& PadWLeft() { return padWLeft; }
239 
241  size_t PadWRight() const { return padWRight; }
243  size_t& PadWRight() { return padWRight; }
244 
246  arma::mat& Bias() { return bias; }
247 
251  template<typename Archive>
252  void serialize(Archive& ar, const unsigned int /* version */);
253 
254  private:
255  /*
256  * Return the convolution output size.
257  *
258  * @param size The size of the input (row or column).
259  * @param k The size of the filter (width or height).
260  * @param s The stride size (x or y direction).
261  * @param pSideOne The size of the padding (width or height) on one side.
262  * @param pSideTwo The size of the padding (width or height) on another side.
263  * @return The convolution output size.
264  */
265  size_t ConvOutSize(const size_t size,
266  const size_t k,
267  const size_t s,
268  const size_t pSideOne,
269  const size_t pSideTwo)
270  {
271  return std::floor(size + pSideOne + pSideTwo - k) / s + 1;
272  }
273 
274  /*
275  * Function to assign padding such that output size is same as input size.
276  */
277  void InitializeSamePadding();
278 
279  /*
280  * Rotates a 3rd-order tensor counterclockwise by 180 degrees.
281  *
282  * @param input The input data to be rotated.
283  * @param output The rotated output.
284  */
285  template<typename eT>
286  void Rotate180(const arma::Cube<eT>& input, arma::Cube<eT>& output)
287  {
288  output = arma::Cube<eT>(input.n_rows, input.n_cols, input.n_slices);
289 
290  // * left-right flip, up-down flip */
291  for (size_t s = 0; s < output.n_slices; s++)
292  output.slice(s) = arma::fliplr(arma::flipud(input.slice(s)));
293  }
294 
295  /*
296  * Rotates a dense matrix 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::Mat<eT>& input, arma::Mat<eT>& output)
303  {
304  // * left-right flip, up-down flip */
305  output = arma::fliplr(arma::flipud(input));
306  }
307 
309  size_t inSize;
310 
312  size_t outSize;
313 
315  size_t batchSize;
316 
318  size_t kernelWidth;
319 
321  size_t kernelHeight;
322 
324  size_t strideWidth;
325 
327  size_t strideHeight;
328 
330  size_t padWLeft;
331 
333  size_t padWRight;
334 
336  size_t padHBottom;
337 
339  size_t padHTop;
340 
342  OutputDataType weights;
343 
345  arma::cube weight;
346 
348  arma::mat bias;
349 
351  size_t inputWidth;
352 
354  size_t inputHeight;
355 
357  size_t outputWidth;
358 
360  size_t outputHeight;
361 
363  arma::cube outputTemp;
364 
366  arma::cube inputTemp;
367 
369  arma::cube inputPaddedTemp;
370 
372  arma::cube gTemp;
373 
375  arma::cube gradientTemp;
376 
378  ann::Padding<> padding;
379 
381  OutputDataType delta;
382 
384  OutputDataType gradient;
385 
387  InputDataType inputParameter;
388 
390  OutputDataType outputParameter;
391 }; // class Convolution
392 
393 } // namespace ann
394 } // namespace mlpack
395 
397 namespace boost {
398 namespace serialization {
399 
400 template<
401  typename ForwardConvolutionRule,
402  typename BackwardConvolutionRule,
403  typename GradientConvolutionRule,
404  typename InputDataType,
405  typename OutputDataType
406 >
407 struct version<
408  mlpack::ann::Convolution<ForwardConvolutionRule, BackwardConvolutionRule,
409  GradientConvolutionRule, InputDataType, OutputDataType> >
410 {
411  BOOST_STATIC_CONSTANT(int, value = 1);
412 };
413 
414 } // namespace serialization
415 } // namespace boost
416 
417 // Include implementation.
418 #include "convolution_impl.hpp"
419 
420 #endif
size_t & PadWLeft()
Modify the left padding width.
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...
size_t & PadWRight()
Modify the right padding width.
Set the serialization version of the adaboost class.
Definition: adaboost.hpp:194
const OutputDataType & Parameters() const
Get the parameters.
strip_type.hpp
Definition: add_to_po.hpp:21
Implementation of the Padding module class.
Definition: layer_types.hpp:68
const InputDataType & InputParameter() const
Get the input parameter.
size_t & OutputHeight()
Modify the output height.
The core includes that mlpack expects; standard C++ includes and Armadillo.
const size_t & InputHeight() const
Get the input height.
Implementation of the Convolution class.
Definition: convolution.hpp:47
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 weights of the layer.
size_t & PadHTop()
Modify the top padding height.
const size_t & OutputHeight() const
Get the output height.
const OutputDataType & Delta() const
Get the delta.
size_t StrideWidth() const
Get the stride width.
const 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...
const OutputDataType & Gradient() const
Get the gradient.
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.
const size_t & InputWidth() const
Get the input width.
size_t & StrideHeight()
Modify the stride height.
size_t & OutputWidth()
Modify the output width.
const OutputDataType & OutputParameter() const
Get the output parameter.
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.
size_t OutputSize() const
Get the output size.