mlpack  3.4.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
atrous_convolution.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_METHODS_ANN_LAYER_ATROUS_CONVOLUTION_HPP
14 #define MLPACK_METHODS_ANN_LAYER_ATROUS_CONVOLUTION_HPP
15 
16 #include <mlpack/prereqs.hpp>
17 
23 
24 #include "layer_types.hpp"
25 #include "padding.hpp"
26 
27 namespace mlpack{
28 namespace ann {
29 
45 template <
46  typename ForwardConvolutionRule = NaiveConvolution<ValidConvolution>,
47  typename BackwardConvolutionRule = NaiveConvolution<FullConvolution>,
48  typename GradientConvolutionRule = NaiveConvolution<ValidConvolution>,
49  typename InputDataType = arma::mat,
50  typename OutputDataType = arma::mat
51 >
53 {
54  public:
57 
77  AtrousConvolution(const size_t inSize,
78  const size_t outSize,
79  const size_t kernelWidth,
80  const size_t kernelHeight,
81  const size_t strideWidth = 1,
82  const size_t strideHeight = 1,
83  const size_t padW = 0,
84  const size_t padH = 0,
85  const size_t inputWidth = 0,
86  const size_t inputHeight = 0,
87  const size_t dilationWidth = 1,
88  const size_t dilationHeight = 1,
89  const std::string& paddingType = "None");
90 
114  AtrousConvolution(const size_t inSize,
115  const size_t outSize,
116  const size_t kernelWidth,
117  const size_t kernelHeight,
118  const size_t strideWidth,
119  const size_t strideHeight,
120  const std::tuple<size_t, size_t>& padW,
121  const std::tuple<size_t, size_t>& padH,
122  const size_t inputWidth = 0,
123  const size_t inputHeight = 0,
124  const size_t dilationWidth = 1,
125  const size_t dilationHeight = 1,
126  const std::string& paddingType = "None");
127 
128  /*
129  * Set the weight and bias term.
130  */
131  void Reset();
132 
140  template<typename eT>
141  void Forward(const arma::Mat<eT>& input, arma::Mat<eT>& output);
142 
152  template<typename eT>
153  void Backward(const arma::Mat<eT>& /* input */,
154  const arma::Mat<eT>& gy,
155  arma::Mat<eT>& g);
156 
157  /*
158  * Calculate the gradient using the output delta and the input activation.
159  *
160  * @param input The input parameter used for calculating the gradient.
161  * @param error The calculated error.
162  * @param gradient The calculated gradient.
163  */
164  template<typename eT>
165  void Gradient(const arma::Mat<eT>& /* input */,
166  const arma::Mat<eT>& error,
167  arma::Mat<eT>& gradient);
168 
170  OutputDataType const& Parameters() const { return weights; }
172  OutputDataType& Parameters() { return weights; }
173 
175  arma::cube const& Weight() const { return weight; }
177  arma::cube& Weight() { return weight; }
178 
180  arma::mat const& Bias() const { return bias; }
182  arma::mat& Bias() { return bias; }
183 
185  OutputDataType const& OutputParameter() const { return outputParameter; }
187  OutputDataType& OutputParameter() { return outputParameter; }
188 
190  OutputDataType const& Delta() const { return delta; }
192  OutputDataType& Delta() { return delta; }
193 
195  OutputDataType const& Gradient() const { return gradient; }
197  OutputDataType& Gradient() { return gradient; }
198 
200  size_t InputWidth() const { return inputWidth; }
202  size_t& InputWidth() { return inputWidth; }
203 
205  size_t InputHeight() const { return inputHeight; }
207  size_t& InputHeight() { return inputHeight; }
208 
210  size_t OutputWidth() const { return outputWidth; }
212  size_t& OutputWidth() { return outputWidth; }
213 
215  size_t OutputHeight() const { return outputHeight; }
217  size_t& OutputHeight() { return outputHeight; }
218 
220  size_t InputSize() const { return inSize; }
221 
223  size_t OutputSize() const { return outSize; }
224 
226  size_t KernelWidth() const { return kernelWidth; }
228  size_t& KernelWidth() { return kernelWidth; }
229 
231  size_t KernelHeight() const { return kernelHeight; }
233  size_t& KernelHeight() { return kernelHeight; }
234 
236  size_t StrideWidth() const { return strideWidth; }
238  size_t& StrideWidth() { return strideWidth; }
239 
241  size_t StrideHeight() const { return strideHeight; }
243  size_t& StrideHeight() { return strideHeight; }
244 
246  size_t DilationWidth() const { return dilationWidth; }
248  size_t& DilationWidth() { return dilationWidth; }
249 
251  size_t DilationHeight() const { return dilationHeight; }
253  size_t& DilationHeight() { return dilationHeight; }
254 
256  ann::Padding<> const& Padding() const { return padding; }
258  ann::Padding<>& Padding() { return padding; }
259 
261  size_t WeightSize() const
262  {
263  return (outSize * inSize * kernelWidth * kernelHeight) + outSize;
264  }
265 
269  template<typename Archive>
270  void serialize(Archive& ar, const unsigned int /* version */);
271 
272  private:
273  /*
274  * Return the convolution output size.
275  *
276  * @param size The size of the input (row or column).
277  * @param k The size of the filter (width or height).
278  * @param s The stride size (x or y direction).
279  * @param pSideOne The size of the padding (width or height) on one side.
280  * @param pSideTwo The size of the padding (width or height) on another side.
281  * @param d The dilation size.
282  * @return The convolution output size.
283  */
284  size_t ConvOutSize(const size_t size,
285  const size_t k,
286  const size_t s,
287  const size_t pSideOne,
288  const size_t pSideTwo,
289  const size_t d)
290  {
291  return std::floor(size + pSideOne + pSideTwo - d * (k - 1) - 1) / s + 1;
292  }
293 
294  /*
295  * Function to assign padding such that output size is same as input size.
296  */
297  void InitializeSamePadding(size_t& padWLeft,
298  size_t& padWRight,
299  size_t& padHBottom,
300  size_t& padHTop) const;
301 
302  /*
303  * Rotates a 3rd-order tensor counterclockwise by 180 degrees.
304  *
305  * @param input The input data to be rotated.
306  * @param output The rotated output.
307  */
308  template<typename eT>
309  void Rotate180(const arma::Cube<eT>& input, arma::Cube<eT>& output)
310  {
311  output = arma::Cube<eT>(input.n_rows, input.n_cols, input.n_slices);
312 
313  // * left-right flip, up-down flip */
314  for (size_t s = 0; s < output.n_slices; s++)
315  output.slice(s) = arma::fliplr(arma::flipud(input.slice(s)));
316  }
317 
318  /*
319  * Rotates a dense matrix counterclockwise by 180 degrees.
320  *
321  * @param input The input data to be rotated.
322  * @param output The rotated output.
323  */
324  template<typename eT>
325  void Rotate180(const arma::Mat<eT>& input, arma::Mat<eT>& output)
326  {
327  // * left-right flip, up-down flip */
328  output = arma::fliplr(arma::flipud(input));
329  }
330 
332  size_t inSize;
333 
335  size_t outSize;
336 
338  size_t batchSize;
339 
341  size_t kernelWidth;
342 
344  size_t kernelHeight;
345 
347  size_t strideWidth;
348 
350  size_t strideHeight;
351 
353  OutputDataType weights;
354 
356  arma::cube weight;
357 
359  arma::mat bias;
360 
362  size_t inputWidth;
363 
365  size_t inputHeight;
366 
368  size_t outputWidth;
369 
371  size_t outputHeight;
372 
374  size_t dilationWidth;
375 
377  size_t dilationHeight;
378 
380  arma::cube outputTemp;
381 
383  arma::cube inputPaddedTemp;
384 
386  arma::cube gTemp;
387 
389  arma::cube gradientTemp;
390 
392  ann::Padding<> padding;
393 
395  OutputDataType delta;
396 
398  OutputDataType gradient;
399 
401  OutputDataType outputParameter;
402 }; // class AtrousConvolution
403 
404 } // namespace ann
405 } // namespace mlpack
406 
408 namespace boost {
409 namespace serialization {
410 
411 template<
412  typename ForwardConvolutionRule,
413  typename BackwardConvolutionRule,
414  typename GradientConvolutionRule,
415  typename InputDataType,
416  typename OutputDataType
417 >
418 struct version<
419  mlpack::ann::AtrousConvolution<ForwardConvolutionRule,
420  BackwardConvolutionRule, GradientConvolutionRule, InputDataType,
421  OutputDataType> >
422 {
423  BOOST_STATIC_CONSTANT(int, value = 2);
424 };
425 
426 } // namespace serialization
427 } // namespace boost
428 
429 // Include implementation.
430 #include "atrous_convolution_impl.hpp"
431 
432 #endif
size_t OutputWidth() const
Get the output width.
ann::Padding & Padding()
Modify the internal Padding layer.
size_t & DilationHeight()
Modify the dilation rate on the Y axis.
size_t & StrideWidth()
Modify the stride width.
arma::mat & Bias()
Modify the bias of the layer.
Implementation of the Padding module class.
Definition: layer_types.hpp:81
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
size_t WeightSize() const
Get size of the weight matrix.
OutputDataType & Delta()
Modify the delta.
size_t DilationWidth() const
Get the dilation rate on the X axis.
size_t & InputHeight()
Modify the input height.
arma::cube const & Weight() const
Get the weight of the layer.
The core includes that mlpack expects; standard C++ includes and Armadillo.
size_t DilationHeight() const
Get the dilation rate on the Y axis.
OutputDataType & OutputParameter()
Modify the output parameter.
OutputDataType & Parameters()
Modify the parameters.
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 & Gradient() const
Get the gradient.
AtrousConvolution()
Create the AtrousConvolution object.
size_t StrideHeight() const
Get the stride height.
size_t OutputHeight() const
Get the output height.
arma::mat const & Bias() const
Get the bias of the layer.
OutputDataType const & Delta() const
Get the delta.
size_t OutputSize() const
Get the output size.
size_t InputWidth() const
Get the input width.
arma::cube & Weight()
Modify the weight of the layer.
OutputDataType const & Parameters() const
Get the parameters.
size_t & InputWidth()
Modify input the width.
size_t & StrideHeight()
Modify the stride height.
size_t & KernelWidth()
Modify the kernel width.
ann::Padding const & Padding() const
Get the internal Padding layer.
size_t KernelWidth() const
Get the kernel width.
size_t & DilationWidth()
Modify the dilation rate on the X axis.
size_t & KernelHeight()
Modify the kernel height.
size_t InputHeight() const
Get the input height.
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 KernelHeight() const
Get the kernel height.
size_t StrideWidth() const
Get the stride width.
OutputDataType & Gradient()
Modify the gradient.
OutputDataType const & OutputParameter() const
Get the output parameter.
size_t & OutputWidth()
Modify the output width.
size_t InputSize() const
Get the input size.
size_t & OutputHeight()
Modify the output height.
src mlpack core util version hpp VERSION_HPP_CONTENTS string(REGEX REPLACE".*#define MLPACK_VERSION_MAJOR ([0-9]+).*""\\1"MLPACK_VERSION_MAJOR"${VERSION_HPP_CONTENTS}") string(REGEX REPLACE".* MLPACK_VERSION_MINOR "$
Definition: CMakeLists.txt:79
Implementation of the Atrous Convolution class.