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  const OutputDataType& Parameters() const { return weights; }
158  OutputDataType& Parameters() { return weights; }
159 
161  const InputDataType& InputParameter() const { return inputParameter; }
163  InputDataType& InputParameter() { return inputParameter; }
164 
166  const OutputDataType& OutputParameter() const { return outputParameter; }
168  OutputDataType& OutputParameter() { return outputParameter; }
169 
171  const OutputDataType& Delta() const { return delta; }
173  OutputDataType& Delta() { return delta; }
174 
176  const OutputDataType& Gradient() const { return gradient; }
178  OutputDataType& Gradient() { return gradient; }
179 
181  const size_t& InputWidth() const { return inputWidth; }
183  size_t& InputWidth() { return inputWidth; }
184 
186  const size_t& InputHeight() const { return inputHeight; }
188  size_t& InputHeight() { return inputHeight; }
189 
191  const size_t& OutputWidth() const { return outputWidth; }
193  size_t& OutputWidth() { return outputWidth; }
194 
196  const size_t& OutputHeight() const { return outputHeight; }
198  size_t& OutputHeight() { return outputHeight; }
199 
201  size_t InputSize() const { return inSize; }
202 
204  size_t OutputSize() const { return outSize; }
205 
207  size_t KernelWidth() const { return kernelWidth; }
209  size_t& KernelWidth() { return kernelWidth; }
210 
212  size_t KernelHeight() const { return kernelHeight; }
214  size_t& KernelHeight() { return kernelHeight; }
215 
217  size_t StrideWidth() const { return strideWidth; }
219  size_t& StrideWidth() { return strideWidth; }
220 
222  size_t StrideHeight() const { return strideHeight; }
224  size_t& StrideHeight() { return strideHeight; }
225 
227  size_t PadHTop() const { return padHTop; }
229  size_t& PadHTop() { return padHTop; }
230 
232  size_t PadHBottom() const { return padHBottom; }
234  size_t& PadHBottom() { return padHBottom; }
235 
237  size_t PadWLeft() const { return padWLeft; }
239  size_t& PadWLeft() { return padWLeft; }
240 
242  size_t PadWRight() const { return padWRight; }
244  size_t& PadWRight() { return padWRight; }
245 
247  arma::mat& Bias() { return bias; }
248 
252  template<typename Archive>
253  void serialize(Archive& ar, const unsigned int /* version */);
254 
255  private:
256  /*
257  * Return the convolution output size.
258  *
259  * @param size The size of the input (row or column).
260  * @param k The size of the filter (width or height).
261  * @param s The stride size (x or y direction).
262  * @param pSideOne The size of the padding (width or height) on one side.
263  * @param pSideTwo The size of the padding (width or height) on another side.
264  * @return The convolution output size.
265  */
266  size_t ConvOutSize(const size_t size,
267  const size_t k,
268  const size_t s,
269  const size_t pSideOne,
270  const size_t pSideTwo)
271  {
272  return std::floor(size + pSideOne + pSideTwo - k) / s + 1;
273  }
274 
275  /*
276  * Function to assign padding such that output size is same as input size.
277  */
278  void InitializeSamePadding();
279 
280  /*
281  * Rotates a 3rd-order tensor counterclockwise by 180 degrees.
282  *
283  * @param input The input data to be rotated.
284  * @param output The rotated output.
285  */
286  template<typename eT>
287  void Rotate180(const arma::Cube<eT>& input, arma::Cube<eT>& output)
288  {
289  output = arma::Cube<eT>(input.n_rows, input.n_cols, input.n_slices);
290 
291  // * left-right flip, up-down flip */
292  for (size_t s = 0; s < output.n_slices; s++)
293  output.slice(s) = arma::fliplr(arma::flipud(input.slice(s)));
294  }
295 
296  /*
297  * Rotates a dense matrix counterclockwise by 180 degrees.
298  *
299  * @param input The input data to be rotated.
300  * @param output The rotated output.
301  */
302  template<typename eT>
303  void Rotate180(const arma::Mat<eT>& input, arma::Mat<eT>& output)
304  {
305  // * left-right flip, up-down flip */
306  output = arma::fliplr(arma::flipud(input));
307  }
308 
310  size_t inSize;
311 
313  size_t outSize;
314 
316  size_t batchSize;
317 
319  size_t kernelWidth;
320 
322  size_t kernelHeight;
323 
325  size_t strideWidth;
326 
328  size_t strideHeight;
329 
331  size_t padWLeft;
332 
334  size_t padWRight;
335 
337  size_t padHBottom;
338 
340  size_t padHTop;
341 
343  OutputDataType weights;
344 
346  arma::cube weight;
347 
349  arma::mat bias;
350 
352  size_t inputWidth;
353 
355  size_t inputHeight;
356 
358  size_t outputWidth;
359 
361  size_t outputHeight;
362 
364  arma::cube outputTemp;
365 
367  arma::cube inputPaddedTemp;
368 
370  arma::cube gTemp;
371 
373  arma::cube gradientTemp;
374 
376  ann::Padding<> padding;
377 
379  OutputDataType delta;
380 
382  OutputDataType gradient;
383 
385  InputDataType inputParameter;
386 
388  OutputDataType outputParameter;
389 }; // class Convolution
390 
391 } // namespace ann
392 } // namespace mlpack
393 
395 namespace boost {
396 namespace serialization {
397 
398 template<
399  typename ForwardConvolutionRule,
400  typename BackwardConvolutionRule,
401  typename GradientConvolutionRule,
402  typename InputDataType,
403  typename OutputDataType
404 >
405 struct version<
406  mlpack::ann::Convolution<ForwardConvolutionRule, BackwardConvolutionRule,
407  GradientConvolutionRule, InputDataType, OutputDataType> >
408 {
409  BOOST_STATIC_CONSTANT(int, value = 1);
410 };
411 
412 } // namespace serialization
413 } // namespace boost
414 
415 // Include implementation.
416 #include "convolution_impl.hpp"
417 
418 #endif
size_t & PadWLeft()
Modify the left padding width.
size_t & PadWRight()
Modify the right padding width.
Set the serialization version of the adaboost class.
Definition: adaboost.hpp:198
const OutputDataType & Parameters() const
Get the parameters.
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: add_to_po.hpp:21
Implementation of the Padding module class.
Definition: layer_types.hpp:77
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: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 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.
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.
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.