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.hpp"
24 #include "padding.hpp"
25 
26 namespace mlpack {
27 namespace ann {
28 
68 template <
69  typename ForwardConvolutionRule = NaiveConvolution<ValidConvolution>,
70  typename BackwardConvolutionRule = NaiveConvolution<FullConvolution>,
71  typename GradientConvolutionRule = NaiveConvolution<ValidConvolution>,
72  typename MatType = arma::mat
73 >
74 class ConvolutionType : public Layer<MatType>
75 {
76  public:
79 
95  ConvolutionType(const size_t maps,
96  const size_t kernelWidth,
97  const size_t kernelHeight,
98  const size_t strideWidth = 1,
99  const size_t strideHeight = 1,
100  const size_t padW = 0,
101  const size_t padH = 0,
102  const std::string& paddingType = "none");
103 
123  ConvolutionType(const size_t maps,
124  const size_t kernelWidth,
125  const size_t kernelHeight,
126  const size_t strideWidth,
127  const size_t strideHeight,
128  const std::tuple<size_t, size_t>& padW,
129  const std::tuple<size_t, size_t>& padH,
130  const std::string& paddingType = "none");
131 
133  ConvolutionType* Clone() const { return new ConvolutionType(*this); }
134 
136  ConvolutionType(const ConvolutionType& layer);
137 
140 
143 
146 
147  // Virtual destructor.
148  virtual ~ConvolutionType() { }
149 
150  /*
151  * Set the weight and bias term.
152  */
153  void SetWeights(typename MatType::elem_type* weightsPtr);
154 
162  void Forward(const MatType& input, MatType& output);
163 
173  void Backward(const MatType& /* input */,
174  const MatType& gy,
175  MatType& g);
176 
184  void Gradient(const MatType& /* input */,
185  const MatType& error,
186  MatType& gradient);
187 
189  MatType const& Parameters() const { return weights; }
191  MatType& Parameters() { return weights; }
192 
194  arma::Cube<typename MatType::elem_type> const& Weight() const
195  {
196  return weight;
197  }
199  arma::Cube<typename MatType::elem_type>& Weight() { return weight; }
200 
202  MatType const& Bias() const { return bias; }
204  MatType& Bias() { return bias; }
205 
207  size_t const& Maps() const { return maps; }
208 
210  size_t const& KernelWidth() const { return kernelWidth; }
212  size_t& KernelWidth() { return kernelWidth; }
213 
215  size_t const& KernelHeight() const { return kernelHeight; }
217  size_t& KernelHeight() { return kernelHeight; }
218 
220  size_t const& StrideWidth() const { return strideWidth; }
222  size_t& StrideWidth() { return strideWidth; }
223 
225  size_t const& StrideHeight() const { return strideHeight; }
227  size_t& StrideHeight() { return strideHeight; }
228 
230  size_t const& PadHTop() const { return padHTop; }
232  size_t& PadHTop() { return padHTop; }
233 
235  size_t const& PadHBottom() const { return padHBottom; }
237  size_t& PadHBottom() { return padHBottom; }
238 
240  size_t const& PadWLeft() const { return padWLeft; }
242  size_t& PadWLeft() { return padWLeft; }
243 
245  size_t const& PadWRight() const { return padWRight; }
247  size_t& PadWRight() { return padWRight; }
248 
250  size_t WeightSize() const
251  {
252  return (maps * inMaps * higherInDimensions * kernelWidth * kernelHeight) +
253  maps;
254  }
255 
258 
262  template<typename Archive>
263  void serialize(Archive& ar, const uint32_t /* version */);
264 
265  private:
276  size_t ConvOutSize(const size_t size,
277  const size_t k,
278  const size_t s,
279  const size_t pSideOne,
280  const size_t pSideTwo)
281  {
282  return std::floor(size + pSideOne + pSideTwo - k) / s + 1;
283  }
284 
288  void InitializeSamePadding();
289 
296  template<typename eT>
297  void Rotate180(const arma::Cube<eT>& input, arma::Cube<eT>& output)
298  {
299  output = arma::Cube<eT>(input.n_rows, input.n_cols, input.n_slices);
300 
301  // * left-right flip, up-down flip */
302  for (size_t s = 0; s < output.n_slices; s++)
303  output.slice(s) = arma::fliplr(arma::flipud(input.slice(s)));
304  }
305 
312  template<typename eT>
313  void Rotate180(const arma::Mat<eT>& input, arma::Mat<eT>& output)
314  {
315  // * left-right flip, up-down flip */
316  output = arma::fliplr(arma::flipud(input));
317  }
318 
320  size_t maps;
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  MatType weights;
351 
353  arma::Cube<typename MatType::elem_type> weight;
354 
356  MatType bias;
357 
359  arma::Cube<typename MatType::elem_type> outputTemp;
360 
362  MatType inputPadded;
363 
365  arma::Cube<typename MatType::elem_type> gTemp;
366 
368  arma::Cube<typename MatType::elem_type> gradientTemp;
369 
371  ann::Padding padding;
372 
374  std::string paddingType;
375 
377  size_t inMaps;
379  size_t higherInDimensions;
380 }; // class Convolution
381 
382 // Standard Convolution layer.
383 typedef ConvolutionType<
386  NaiveConvolution<ValidConvolution>,
387  arma::mat
389 
390 } // namespace ann
391 } // namespace mlpack
392 
393 // Include implementation.
394 #include "convolution_impl.hpp"
395 
396 #endif
size_t & PadWRight()
Modify the right padding width.
size_t & StrideWidth()
Modify the stride width.
Implementation of the Convolution class.
Definition: convolution.hpp:74
void SetWeights(typename MatType::elem_type *weightsPtr)
Reset the layer parameter.
constexpr auto size(Container const &container) noexcept -> decltype(container.size())
Definition: iterator.hpp:29
MatType const & Bias() const
Get the bias of the layer.
void Gradient(const MatType &, const MatType &error, MatType &gradient)
Calculate the gradient using the output delta and the input activation.
ConvolutionType()
Create the ConvolutionType object.
Linear algebra utility functions, generally performed on matrices or vectors.
size_t const & PadHTop() const
Get the top padding height.
size_t const & PadWRight() const
Get the right padding width.
The core includes that mlpack expects; standard C++ includes and Armadillo.
size_t & KernelHeight()
Modify the kernel height.
arma::Cube< typename MatType::elem_type > & Weight()
Modify the weight of the layer as a cube.
size_t const & Maps() const
Get the number of output maps.
size_t & PadHBottom()
Modify the bottom padding height.
ConvolutionType & operator=(const ConvolutionType &layer)
Copy the given ConvolutionType (but not weights).
size_t & StrideHeight()
Modify the stride height.
size_t const & StrideHeight() const
Get the stride height.
size_t & PadHTop()
Modify the top padding height.
void Backward(const MatType &, const MatType &gy, MatType &g)
Ordinary feed backward pass of a neural network, calculating the function f(x) by propagating x backw...
size_t const & PadWLeft() const
Get the left padding width.
size_t const & PadHBottom() const
Get the bottom padding height.
ConvolutionType * Clone() const
Clone the ConvolutionType object. This handles polymorphism correctly.
MatType & Parameters()
Modify the parameters.
size_t const & KernelWidth() const
Get the kernel width.
size_t WeightSize() const
Get size of weights for the layer.
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
void ComputeOutputDimensions()
Compute the output dimensions of the layer based on InputDimensions().
size_t const & StrideWidth() const
Get the stride width.
Computes the two-dimensional convolution.
MatType const & Parameters() const
Get the parameters.
arma::Cube< typename MatType::elem_type > const & Weight() const
Get the weight of the layer as a cube.
void Forward(const MatType &input, MatType &output)
Ordinary feed forward pass of a neural network, evaluating the function f(x) by propagating the activ...
size_t & KernelWidth()
Modify the kernel width.
A layer is an abstract class implementing common neural networks operations, such as convolution...
Definition: layer.hpp:52
MatType & Bias()
Modify the bias of the layer.
size_t & PadWLeft()
Modify the left padding width.
size_t const & KernelHeight() const
Get the kernel height.
ConvolutionType< NaiveConvolution< ValidConvolution >, NaiveConvolution< FullConvolution >, NaiveConvolution< ValidConvolution >, arma::mat > Convolution