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 kW,
72  const size_t kH,
73  const size_t dW = 1,
74  const size_t dH = 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 kW,
104  const size_t kH,
105  const size_t dW,
106  const size_t dH,
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  OutputDataType const& Parameters() const { return weights; }
157  OutputDataType& Parameters() { return weights; }
158 
160  InputDataType const& InputParameter() const { return inputParameter; }
162  InputDataType& InputParameter() { return inputParameter; }
163 
165  OutputDataType const& OutputParameter() const { return outputParameter; }
167  OutputDataType& OutputParameter() { return outputParameter; }
168 
170  OutputDataType const& Delta() const { return delta; }
172  OutputDataType& Delta() { return delta; }
173 
175  OutputDataType const& Gradient() const { return gradient; }
177  OutputDataType& Gradient() { return gradient; }
178 
180  size_t const& InputWidth() const { return inputWidth; }
182  size_t& InputWidth() { return inputWidth; }
183 
185  size_t const& InputHeight() const { return inputHeight; }
187  size_t& InputHeight() { return inputHeight; }
188 
190  size_t const& OutputWidth() const { return outputWidth; }
192  size_t& OutputWidth() { return outputWidth; }
193 
195  size_t const& OutputHeight() const { return outputHeight; }
197  size_t& OutputHeight() { return outputHeight; }
198 
200  arma::mat& Bias() { return bias; }
201 
205  template<typename Archive>
206  void serialize(Archive& ar, const unsigned int /* version */);
207 
208  private:
209  /*
210  * Return the convolution output size.
211  *
212  * @param size The size of the input (row or column).
213  * @param k The size of the filter (width or height).
214  * @param s The stride size (x or y direction).
215  * @param pSideOne The size of the padding (width or height) on one side.
216  * @param pSideTwo The size of the padding (width or height) on another side.
217  * @return The convolution output size.
218  */
219  size_t ConvOutSize(const size_t size,
220  const size_t k,
221  const size_t s,
222  const size_t pSideOne,
223  const size_t pSideTwo)
224  {
225  return std::floor(size + pSideOne + pSideTwo - k) / s + 1;
226  }
227 
228  /*
229  * Function to assign padding such that output size is same as input size.
230  */
231  void InitializeSamePadding();
232 
233  /*
234  * Rotates a 3rd-order tensor counterclockwise by 180 degrees.
235  *
236  * @param input The input data to be rotated.
237  * @param output The rotated output.
238  */
239  template<typename eT>
240  void Rotate180(const arma::Cube<eT>& input, arma::Cube<eT>& output)
241  {
242  output = arma::Cube<eT>(input.n_rows, input.n_cols, input.n_slices);
243 
244  // * left-right flip, up-down flip */
245  for (size_t s = 0; s < output.n_slices; s++)
246  output.slice(s) = arma::fliplr(arma::flipud(input.slice(s)));
247  }
248 
249  /*
250  * Rotates a dense matrix counterclockwise by 180 degrees.
251  *
252  * @param input The input data to be rotated.
253  * @param output The rotated output.
254  */
255  template<typename eT>
256  void Rotate180(const arma::Mat<eT>& input, arma::Mat<eT>& output)
257  {
258  // * left-right flip, up-down flip */
259  output = arma::fliplr(arma::flipud(input));
260  }
261 
263  size_t inSize;
264 
266  size_t outSize;
267 
269  size_t batchSize;
270 
272  size_t kW;
273 
275  size_t kH;
276 
278  size_t dW;
279 
281  size_t dH;
282 
284  size_t padWLeft;
285 
287  size_t padWRight;
288 
290  size_t padHBottom;
291 
293  size_t padHTop;
294 
296  OutputDataType weights;
297 
299  arma::cube weight;
300 
302  arma::mat bias;
303 
305  size_t inputWidth;
306 
308  size_t inputHeight;
309 
311  size_t outputWidth;
312 
314  size_t outputHeight;
315 
317  arma::cube outputTemp;
318 
320  arma::cube inputTemp;
321 
323  arma::cube inputPaddedTemp;
324 
326  arma::cube gTemp;
327 
329  arma::cube gradientTemp;
330 
332  Padding<>* padding;
333 
335  OutputDataType delta;
336 
338  OutputDataType gradient;
339 
341  InputDataType inputParameter;
342 
344  OutputDataType outputParameter;
345 }; // class Convolution
346 
347 } // namespace ann
348 } // namespace mlpack
349 
351 namespace boost {
352 namespace serialization {
353 
354 template<
355  typename ForwardConvolutionRule,
356  typename BackwardConvolutionRule,
357  typename GradientConvolutionRule,
358  typename InputDataType,
359  typename OutputDataType
360 >
361 struct version<
362  mlpack::ann::Convolution<ForwardConvolutionRule, BackwardConvolutionRule,
363  GradientConvolutionRule, InputDataType, OutputDataType> >
364 {
365  BOOST_STATIC_CONSTANT(int, value = 1);
366 };
367 
368 } // namespace serialization
369 } // namespace boost
370 
371 // Include implementation.
372 #include "convolution_impl.hpp"
373 
374 #endif
InputDataType const & InputParameter() const
Get the input parameter.
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...
OutputDataType const & Parameters() const
Get the parameters.
Set the serialization version of the adaboost class.
Definition: adaboost.hpp:180
.hpp
Definition: add_to_po.hpp:21
Implementation of the Padding module class.
Definition: layer_types.hpp:68
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:47
size_t const & InputWidth() const
Get the input width.
OutputDataType & Gradient()
Modify the gradient.
InputDataType & InputParameter()
Modify the input parameter.
size_t const & InputHeight() const
Get the input height.
size_t & InputHeight()
Modify the input height.
arma::mat & Bias()
Modify the bias weights of the layer.
size_t const & 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...
OutputDataType const & Delta() const
Get the delta.
size_t & InputWidth()
Modify input the width.
OutputDataType & Parameters()
Modify the parameters.
size_t const & OutputHeight() const
Get the output height.
size_t & OutputWidth()
Modify the output width.
OutputDataType & Delta()
Modify the delta.
OutputDataType & OutputParameter()
Modify the output parameter.
Convolution()
Create the Convolution object.
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
OutputDataType const & Gradient() const
Get the gradient.