mlpack  3.1.1
 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 
22 
23 #include "layer_types.hpp"
24 #include "padding.hpp"
25 
26 namespace mlpack{
27 namespace ann {
28 
44 template <
45  typename ForwardConvolutionRule = NaiveConvolution<ValidConvolution>,
46  typename BackwardConvolutionRule = NaiveConvolution<FullConvolution>,
47  typename GradientConvolutionRule = NaiveConvolution<ValidConvolution>,
48  typename InputDataType = arma::mat,
49  typename OutputDataType = arma::mat
50 >
52 {
53  public:
56 
76  AtrousConvolution(const size_t inSize,
77  const size_t outSize,
78  const size_t kernelWidth,
79  const size_t kernelHeight,
80  const size_t strideWidth = 1,
81  const size_t strideHeight = 1,
82  const size_t padW = 0,
83  const size_t padH = 0,
84  const size_t inputWidth = 0,
85  const size_t inputHeight = 0,
86  const size_t dilationWidth = 1,
87  const size_t dilationHeight = 1,
88  const std::string& paddingType = "None");
89 
113  AtrousConvolution(const size_t inSize,
114  const size_t outSize,
115  const size_t kernelWidth,
116  const size_t kernelHeight,
117  const size_t strideWidth,
118  const size_t strideHeight,
119  const std::tuple<size_t, size_t>& padW,
120  const std::tuple<size_t, size_t>& padH,
121  const size_t inputWidth = 0,
122  const size_t inputHeight = 0,
123  const size_t dilationWidth = 1,
124  const size_t dilationHeight = 1,
125  const std::string& paddingType = "None");
126 
127  /*
128  * Set the weight and bias term.
129  */
130  void Reset();
131 
139  template<typename eT>
140  void Forward(const arma::Mat<eT>& input, arma::Mat<eT>& output);
141 
151  template<typename eT>
152  void Backward(const arma::Mat<eT>& /* input */,
153  const arma::Mat<eT>& gy,
154  arma::Mat<eT>& g);
155 
156  /*
157  * Calculate the gradient using the output delta and the input activation.
158  *
159  * @param input The input parameter used for calculating the gradient.
160  * @param error The calculated error.
161  * @param gradient The calculated gradient.
162  */
163  template<typename eT>
164  void Gradient(const arma::Mat<eT>& /* input */,
165  const arma::Mat<eT>& error,
166  arma::Mat<eT>& gradient);
167 
169  const OutputDataType& Parameters() const { return weights; }
171  OutputDataType& Parameters() { return weights; }
172 
174  const OutputDataType& OutputParameter() const { return outputParameter; }
176  OutputDataType& OutputParameter() { return outputParameter; }
177 
179  const OutputDataType& Delta() const { return delta; }
181  OutputDataType& Delta() { return delta; }
182 
184  const OutputDataType& Gradient() const { return gradient; }
186  OutputDataType& Gradient() { return gradient; }
187 
189  const size_t& InputWidth() const { return inputWidth; }
191  size_t& InputWidth() { return inputWidth; }
192 
194  const size_t& InputHeight() const { return inputHeight; }
196  size_t& InputHeight() { return inputHeight; }
197 
199  const size_t& OutputWidth() const { return outputWidth; }
201  size_t& OutputWidth() { return outputWidth; }
202 
204  const size_t& OutputHeight() const { return outputHeight; }
206  size_t& OutputHeight() { return outputHeight; }
207 
209  const size_t& InputSize() const { return inSize; }
210 
212  const size_t& OutputSize() const { return outSize; }
213 
215  size_t KernelWidth() const { return kernelWidth; }
217  size_t& KernelWidth() { return kernelWidth; }
218 
220  size_t KernelHeight() const { return kernelHeight; }
222  size_t& KernelHeight() { return kernelHeight; }
223 
225  size_t StrideWidth() const { return strideWidth; }
227  size_t& StrideWidth() { return strideWidth; }
228 
230  size_t StrideHeight() const { return strideHeight; }
232  size_t& StrideHeight() { return strideHeight; }
233 
235  size_t DilationWidth() const { return dilationWidth; }
237  size_t& DilationWidth() { return dilationWidth; }
238 
240  size_t DilationHeight() const { return dilationHeight; }
242  size_t& DilationHeight() { return dilationHeight; }
243 
245  const ann::Padding<>& Padding() const { return padding; }
247  ann::Padding<>& Padding() { return padding; }
248 
250  arma::mat& Bias() { return bias; }
251 
255  template<typename Archive>
256  void serialize(Archive& ar, const unsigned int /* version */);
257 
258  private:
259  /*
260  * Return the convolution output size.
261  *
262  * @param size The size of the input (row or column).
263  * @param k The size of the filter (width or height).
264  * @param s The stride size (x or y direction).
265  * @param pSideOne The size of the padding (width or height) on one side.
266  * @param pSideTwo The size of the padding (width or height) on another side.
267  * @param d The dilation size.
268  * @return The convolution output size.
269  */
270  size_t ConvOutSize(const size_t size,
271  const size_t k,
272  const size_t s,
273  const size_t pSideOne,
274  const size_t pSideTwo,
275  const size_t d)
276  {
277  return std::floor(size + pSideOne + pSideTwo - d * (k - 1) - 1) / s + 1;
278  }
279 
280  /*
281  * Function to assign padding such that output size is same as input size.
282  */
283  void InitializeSamePadding(size_t& padWLeft,
284  size_t& padWRight,
285  size_t& padHBottom,
286  size_t& padHTop) const;
287 
288  /*
289  * Rotates a 3rd-order tensor counterclockwise by 180 degrees.
290  *
291  * @param input The input data to be rotated.
292  * @param output The rotated output.
293  */
294  template<typename eT>
295  void Rotate180(const arma::Cube<eT>& input, arma::Cube<eT>& output)
296  {
297  output = arma::Cube<eT>(input.n_rows, input.n_cols, input.n_slices);
298 
299  // * left-right flip, up-down flip */
300  for (size_t s = 0; s < output.n_slices; s++)
301  output.slice(s) = arma::fliplr(arma::flipud(input.slice(s)));
302  }
303 
304  /*
305  * Rotates a dense matrix counterclockwise by 180 degrees.
306  *
307  * @param input The input data to be rotated.
308  * @param output The rotated output.
309  */
310  template<typename eT>
311  void Rotate180(const arma::Mat<eT>& input, arma::Mat<eT>& output)
312  {
313  // * left-right flip, up-down flip */
314  output = arma::fliplr(arma::flipud(input));
315  }
316 
318  size_t inSize;
319 
321  size_t outSize;
322 
324  size_t batchSize;
325 
327  size_t kernelWidth;
328 
330  size_t kernelHeight;
331 
333  size_t strideWidth;
334 
336  size_t strideHeight;
337 
339  OutputDataType weights;
340 
342  arma::cube weight;
343 
345  arma::mat bias;
346 
348  size_t inputWidth;
349 
351  size_t inputHeight;
352 
354  size_t outputWidth;
355 
357  size_t outputHeight;
358 
360  size_t dilationWidth;
361 
363  size_t dilationHeight;
364 
366  arma::cube outputTemp;
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  OutputDataType outputParameter;
388 }; // class AtrousConvolution
389 
390 } // namespace ann
391 } // namespace mlpack
392 
394 namespace boost {
395 namespace serialization {
396 
397 template<
398  typename ForwardConvolutionRule,
399  typename BackwardConvolutionRule,
400  typename GradientConvolutionRule,
401  typename InputDataType,
402  typename OutputDataType
403 >
404 struct version<
405  mlpack::ann::AtrousConvolution<ForwardConvolutionRule,
406  BackwardConvolutionRule, GradientConvolutionRule, InputDataType,
407  OutputDataType> >
408 {
409  BOOST_STATIC_CONSTANT(int, value = 2);
410 };
411 
412 } // namespace serialization
413 } // namespace boost
414 
415 // Include implementation.
416 #include "atrous_convolution_impl.hpp"
417 
418 #endif
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 weights of the layer.
Implementation of the Padding module class.
Definition: layer_types.hpp:72
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
const size_t & OutputWidth() const
Get the output width.
OutputDataType & Delta()
Modify the delta.
size_t DilationWidth() const
Get the dilation rate on the X axis.
size_t & InputHeight()
Modify the input height.
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...
const OutputDataType & Delta() const
Get the delta.
AtrousConvolution()
Create the AtrousConvolution object.
size_t StrideHeight() const
Get the stride height.
const OutputDataType & OutputParameter() const
Get the output parameter.
size_t & InputWidth()
Modify input the width.
size_t & StrideHeight()
Modify the stride height.
size_t & KernelWidth()
Modify the kernel width.
const ann::Padding & Padding() const
Get the internal Padding layer.
const size_t & InputWidth() const
Get the input width.
const OutputDataType & Parameters() const
Get the parameters.
const size_t & OutputSize() const
Get the output size.
const OutputDataType & Gradient() const
Get the gradient.
size_t KernelWidth() const
Get the kernel width.
size_t & DilationWidth()
Modify the dilation rate on the X axis.
const size_t & OutputHeight() const
Get the output height.
size_t & KernelHeight()
Modify the kernel 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.
const size_t & InputHeight() const
Get the input height.
string(REGEX REPLACE".*#define MLPACK_VERSION_MINOR ([0-9]+).*""\\1"MLPACK_VERSION_MINOR"${VERSION_HPP_CONTENTS}") string(REGEX REPLACE".* "\\1" MLPACK_VERSION_PATCH "$
Definition: CMakeLists.txt:35
size_t & OutputWidth()
Modify the output width.
size_t & OutputHeight()
Modify the output height.
const size_t & InputSize() const
Get the input size.
Implementation of the Atrous Convolution class.