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 
263  template<typename Archive>
264  void serialize(Archive& ar, const unsigned int /* version */);
265 
266  private:
267  /*
268  * Return the convolution output size.
269  *
270  * @param size The size of the input (row or column).
271  * @param k The size of the filter (width or height).
272  * @param s The stride size (x or y direction).
273  * @param pSideOne The size of the padding (width or height) on one side.
274  * @param pSideTwo The size of the padding (width or height) on another side.
275  * @param d The dilation size.
276  * @return The convolution output size.
277  */
278  size_t ConvOutSize(const size_t size,
279  const size_t k,
280  const size_t s,
281  const size_t pSideOne,
282  const size_t pSideTwo,
283  const size_t d)
284  {
285  return std::floor(size + pSideOne + pSideTwo - d * (k - 1) - 1) / s + 1;
286  }
287 
288  /*
289  * Function to assign padding such that output size is same as input size.
290  */
291  void InitializeSamePadding(size_t& padWLeft,
292  size_t& padWRight,
293  size_t& padHBottom,
294  size_t& padHTop) const;
295 
296  /*
297  * Rotates a 3rd-order tensor counterclockwise by 180 degrees.
298  *
299  * @param input The input data to be rotated.
300  * @param output The rotated output.
301  */
302  template<typename eT>
303  void Rotate180(const arma::Cube<eT>& input, arma::Cube<eT>& output)
304  {
305  output = arma::Cube<eT>(input.n_rows, input.n_cols, input.n_slices);
306 
307  // * left-right flip, up-down flip */
308  for (size_t s = 0; s < output.n_slices; s++)
309  output.slice(s) = arma::fliplr(arma::flipud(input.slice(s)));
310  }
311 
312  /*
313  * Rotates a dense matrix counterclockwise by 180 degrees.
314  *
315  * @param input The input data to be rotated.
316  * @param output The rotated output.
317  */
318  template<typename eT>
319  void Rotate180(const arma::Mat<eT>& input, arma::Mat<eT>& output)
320  {
321  // * left-right flip, up-down flip */
322  output = arma::fliplr(arma::flipud(input));
323  }
324 
326  size_t inSize;
327 
329  size_t outSize;
330 
332  size_t batchSize;
333 
335  size_t kernelWidth;
336 
338  size_t kernelHeight;
339 
341  size_t strideWidth;
342 
344  size_t strideHeight;
345 
347  OutputDataType weights;
348 
350  arma::cube weight;
351 
353  arma::mat bias;
354 
356  size_t inputWidth;
357 
359  size_t inputHeight;
360 
362  size_t outputWidth;
363 
365  size_t outputHeight;
366 
368  size_t dilationWidth;
369 
371  size_t dilationHeight;
372 
374  arma::cube outputTemp;
375 
377  arma::cube inputPaddedTemp;
378 
380  arma::cube gTemp;
381 
383  arma::cube gradientTemp;
384 
386  ann::Padding<> padding;
387 
389  OutputDataType delta;
390 
392  OutputDataType gradient;
393 
395  OutputDataType outputParameter;
396 }; // class AtrousConvolution
397 
398 } // namespace ann
399 } // namespace mlpack
400 
402 namespace boost {
403 namespace serialization {
404 
405 template<
406  typename ForwardConvolutionRule,
407  typename BackwardConvolutionRule,
408  typename GradientConvolutionRule,
409  typename InputDataType,
410  typename OutputDataType
411 >
412 struct version<
413  mlpack::ann::AtrousConvolution<ForwardConvolutionRule,
414  BackwardConvolutionRule, GradientConvolutionRule, InputDataType,
415  OutputDataType> >
416 {
417  BOOST_STATIC_CONSTANT(int, value = 2);
418 };
419 
420 } // namespace serialization
421 } // namespace boost
422 
423 // Include implementation.
424 #include "atrous_convolution_impl.hpp"
425 
426 #endif
OutputDataType const & Parameters() const
Get the parameters.
ann::Padding & Padding()
Modify the internal Padding layer.
OutputDataType const & OutputParameter() const
Get the output parameter.
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 of the layer.
Linear algebra utility functions, generally performed on matrices or vectors.
Implementation of the Padding module class.
Definition: layer_types.hpp:81
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
OutputDataType & Delta()
Modify the delta.
size_t OutputHeight() const
Get the output height.
size_t & InputHeight()
Modify the input height.
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.
ann::Padding const & Padding() const
Get the internal Padding layer.
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...
arma::cube const & Weight() const
Get the weight of the layer.
AtrousConvolution()
Create the AtrousConvolution object.
size_t OutputWidth() const
Get the output width.
OutputDataType const & Gradient() const
Get the gradient.
arma::cube & Weight()
Modify the weight of the layer.
size_t KernelHeight() const
Get the kernel height.
size_t InputSize() const
Get the input size.
size_t & InputWidth()
Modify input the width.
size_t & StrideHeight()
Modify the stride height.
size_t & KernelWidth()
Modify the kernel width.
size_t StrideHeight() const
Get the stride height.
size_t InputHeight() const
Get the input height.
arma::mat const & Bias() const
Get the bias of the layer.
size_t & DilationWidth()
Modify the dilation rate on the X axis.
size_t StrideWidth() const
Get the stride width.
size_t InputWidth() const
Get the input 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.
size_t & OutputWidth()
Modify the output width.
size_t & OutputHeight()
Modify the output height.
OutputDataType const & Delta() const
Get the delta.
size_t OutputSize() const
Get the output size.
Implementation of the Atrous Convolution class.