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  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  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 inputTemp;
370 
372  arma::cube inputPaddedTemp;
373 
375  arma::cube gTemp;
376 
378  arma::cube gradientTemp;
379 
381  ann::Padding<> padding;
382 
384  OutputDataType delta;
385 
387  OutputDataType gradient;
388 
390  OutputDataType outputParameter;
391 }; // class AtrousConvolution
392 
393 } // namespace ann
394 } // namespace mlpack
395 
397 namespace boost {
398 namespace serialization {
399 
400 template<
401  typename ForwardConvolutionRule,
402  typename BackwardConvolutionRule,
403  typename GradientConvolutionRule,
404  typename InputDataType,
405  typename OutputDataType
406 >
407 struct version<
408  mlpack::ann::AtrousConvolution<ForwardConvolutionRule,
409  BackwardConvolutionRule, GradientConvolutionRule, InputDataType,
410  OutputDataType> >
411 {
412  BOOST_STATIC_CONSTANT(int, value = 2);
413 };
414 
415 } // namespace serialization
416 } // namespace boost
417 
418 // Include implementation.
419 #include "atrous_convolution_impl.hpp"
420 
421 #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.
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...
Set the serialization version of the adaboost class.
Definition: adaboost.hpp:194
arma::mat & Bias()
Modify the bias weights of the layer.
strip_type.hpp
Definition: add_to_po.hpp:21
Implementation of the Padding module class.
Definition: layer_types.hpp:68
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
OutputDataType & Delta()
Modify the delta.
size_t & InputHeight()
Modify the input height.
const OutputDataType & Delta() const
Get the delta.
The core includes that mlpack expects; standard C++ includes and Armadillo.
size_t KernelWidth() const
Get the kernel width.
OutputDataType & OutputParameter()
Modify the output parameter.
OutputDataType & Parameters()
Modify the parameters.
size_t DilationWidth() const
Get the dilation rate on the X axis.
AtrousConvolution()
Create the AtrousConvolution object.
const size_t & InputHeight() const
Get the input height.
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 & OutputParameter() const
Get the output parameter.
size_t KernelHeight() const
Get the kernel height.
const size_t & OutputSize() const
Get the output size.
size_t & InputWidth()
Modify input the width.
const OutputDataType & Parameters() const
Get the parameters.
size_t & StrideHeight()
Modify the stride height.
const ann::Padding & Padding() const
Get the internal Padding layer.
size_t & KernelWidth()
Modify the kernel width.
size_t StrideHeight() const
Get the stride height.
const size_t & OutputWidth() const
Get the output width.
size_t & DilationWidth()
Modify the dilation rate on the X axis.
const size_t & InputSize() const
Get the input size.
size_t StrideWidth() const
Get the stride width.
size_t & KernelHeight()
Modify the kernel height.
size_t DilationHeight() const
Get the dilation rate on the Y axis.
OutputDataType & Gradient()
Modify the gradient.
const OutputDataType & Gradient() const
Get the gradient.
const size_t & InputWidth() const
Get the input width.
size_t & OutputWidth()
Modify the output width.
const size_t & OutputHeight() const
Get the output height.
size_t & OutputHeight()
Modify the output height.
Implementation of the Atrous Convolution class.