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  const OutputDataType& Parameters() const { return weights; }
172  OutputDataType& Parameters() { return weights; }
173 
175  const OutputDataType& OutputParameter() const { return outputParameter; }
177  OutputDataType& OutputParameter() { return outputParameter; }
178 
180  const OutputDataType& Delta() const { return delta; }
182  OutputDataType& Delta() { return delta; }
183 
185  const OutputDataType& Gradient() const { return gradient; }
187  OutputDataType& Gradient() { return gradient; }
188 
190  const size_t& InputWidth() const { return inputWidth; }
192  size_t& InputWidth() { return inputWidth; }
193 
195  const size_t& InputHeight() const { return inputHeight; }
197  size_t& InputHeight() { return inputHeight; }
198 
200  const size_t& OutputWidth() const { return outputWidth; }
202  size_t& OutputWidth() { return outputWidth; }
203 
205  const size_t& OutputHeight() const { return outputHeight; }
207  size_t& OutputHeight() { return outputHeight; }
208 
210  const size_t& InputSize() const { return inSize; }
211 
213  const size_t& OutputSize() const { return outSize; }
214 
216  size_t KernelWidth() const { return kernelWidth; }
218  size_t& KernelWidth() { return kernelWidth; }
219 
221  size_t KernelHeight() const { return kernelHeight; }
223  size_t& KernelHeight() { return kernelHeight; }
224 
226  size_t StrideWidth() const { return strideWidth; }
228  size_t& StrideWidth() { return strideWidth; }
229 
231  size_t StrideHeight() const { return strideHeight; }
233  size_t& StrideHeight() { return strideHeight; }
234 
236  size_t DilationWidth() const { return dilationWidth; }
238  size_t& DilationWidth() { return dilationWidth; }
239 
241  size_t DilationHeight() const { return dilationHeight; }
243  size_t& DilationHeight() { return dilationHeight; }
244 
246  const ann::Padding<>& Padding() const { return padding; }
248  ann::Padding<>& Padding() { return padding; }
249 
251  arma::mat& Bias() { return bias; }
252 
256  template<typename Archive>
257  void serialize(Archive& ar, const unsigned int /* version */);
258 
259  private:
260  /*
261  * Return the convolution output size.
262  *
263  * @param size The size of the input (row or column).
264  * @param k The size of the filter (width or height).
265  * @param s The stride size (x or y direction).
266  * @param pSideOne The size of the padding (width or height) on one side.
267  * @param pSideTwo The size of the padding (width or height) on another side.
268  * @param d The dilation size.
269  * @return The convolution output size.
270  */
271  size_t ConvOutSize(const size_t size,
272  const size_t k,
273  const size_t s,
274  const size_t pSideOne,
275  const size_t pSideTwo,
276  const size_t d)
277  {
278  return std::floor(size + pSideOne + pSideTwo - d * (k - 1) - 1) / s + 1;
279  }
280 
281  /*
282  * Function to assign padding such that output size is same as input size.
283  */
284  void InitializeSamePadding(size_t& padWLeft,
285  size_t& padWRight,
286  size_t& padHBottom,
287  size_t& padHTop) const;
288 
289  /*
290  * Rotates a 3rd-order tensor counterclockwise by 180 degrees.
291  *
292  * @param input The input data to be rotated.
293  * @param output The rotated output.
294  */
295  template<typename eT>
296  void Rotate180(const arma::Cube<eT>& input, arma::Cube<eT>& output)
297  {
298  output = arma::Cube<eT>(input.n_rows, input.n_cols, input.n_slices);
299 
300  // * left-right flip, up-down flip */
301  for (size_t s = 0; s < output.n_slices; s++)
302  output.slice(s) = arma::fliplr(arma::flipud(input.slice(s)));
303  }
304 
305  /*
306  * Rotates a dense matrix counterclockwise by 180 degrees.
307  *
308  * @param input The input data to be rotated.
309  * @param output The rotated output.
310  */
311  template<typename eT>
312  void Rotate180(const arma::Mat<eT>& input, arma::Mat<eT>& output)
313  {
314  // * left-right flip, up-down flip */
315  output = arma::fliplr(arma::flipud(input));
316  }
317 
319  size_t inSize;
320 
322  size_t outSize;
323 
325  size_t batchSize;
326 
328  size_t kernelWidth;
329 
331  size_t kernelHeight;
332 
334  size_t strideWidth;
335 
337  size_t strideHeight;
338 
340  OutputDataType weights;
341 
343  arma::cube weight;
344 
346  arma::mat bias;
347 
349  size_t inputWidth;
350 
352  size_t inputHeight;
353 
355  size_t outputWidth;
356 
358  size_t outputHeight;
359 
361  size_t dilationWidth;
362 
364  size_t dilationHeight;
365 
367  arma::cube outputTemp;
368 
370  arma::cube inputPaddedTemp;
371 
373  arma::cube gTemp;
374 
376  arma::cube gradientTemp;
377 
379  ann::Padding<> padding;
380 
382  OutputDataType delta;
383 
385  OutputDataType gradient;
386 
388  OutputDataType outputParameter;
389 }; // class AtrousConvolution
390 
391 } // namespace ann
392 } // namespace mlpack
393 
395 namespace boost {
396 namespace serialization {
397 
398 template<
399  typename ForwardConvolutionRule,
400  typename BackwardConvolutionRule,
401  typename GradientConvolutionRule,
402  typename InputDataType,
403  typename OutputDataType
404 >
405 struct version<
406  mlpack::ann::AtrousConvolution<ForwardConvolutionRule,
407  BackwardConvolutionRule, GradientConvolutionRule, InputDataType,
408  OutputDataType> >
409 {
410  BOOST_STATIC_CONSTANT(int, value = 2);
411 };
412 
413 } // namespace serialization
414 } // namespace boost
415 
416 // Include implementation.
417 #include "atrous_convolution_impl.hpp"
418 
419 #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.
Set the serialization version of the adaboost class.
Definition: adaboost.hpp:198
arma::mat & Bias()
Modify the bias weights of the layer.
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: add_to_po.hpp:21
Implementation of the Padding module class.
Definition: layer_types.hpp:77
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.
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...
AtrousConvolution()
Create the AtrousConvolution object.
const size_t & InputHeight() const
Get the input height.
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.
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 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.