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 
267  size_t InputShape() const
268  {
269  return inputHeight * inputWidth * inSize;
270  }
271 
275  template<typename Archive>
276  void serialize(Archive& ar, const uint32_t /* version */);
277 
278  private:
279  /*
280  * Return the convolution output size.
281  *
282  * @param size The size of the input (row or column).
283  * @param k The size of the filter (width or height).
284  * @param s The stride size (x or y direction).
285  * @param pSideOne The size of the padding (width or height) on one side.
286  * @param pSideTwo The size of the padding (width or height) on another side.
287  * @param d The dilation size.
288  * @return The convolution output size.
289  */
290  size_t ConvOutSize(const size_t size,
291  const size_t k,
292  const size_t s,
293  const size_t pSideOne,
294  const size_t pSideTwo,
295  const size_t d)
296  {
297  return std::floor(size + pSideOne + pSideTwo - d * (k - 1) - 1) / s + 1;
298  }
299 
300  /*
301  * Function to assign padding such that output size is same as input size.
302  */
303  void InitializeSamePadding(size_t& padWLeft,
304  size_t& padWRight,
305  size_t& padHBottom,
306  size_t& padHTop) const;
307 
308  /*
309  * Rotates a 3rd-order tensor counterclockwise by 180 degrees.
310  *
311  * @param input The input data to be rotated.
312  * @param output The rotated output.
313  */
314  template<typename eT>
315  void Rotate180(const arma::Cube<eT>& input, arma::Cube<eT>& output)
316  {
317  output = arma::Cube<eT>(input.n_rows, input.n_cols, input.n_slices);
318 
319  // * left-right flip, up-down flip */
320  for (size_t s = 0; s < output.n_slices; s++)
321  output.slice(s) = arma::fliplr(arma::flipud(input.slice(s)));
322  }
323 
324  /*
325  * Rotates a dense matrix counterclockwise by 180 degrees.
326  *
327  * @param input The input data to be rotated.
328  * @param output The rotated output.
329  */
330  template<typename eT>
331  void Rotate180(const arma::Mat<eT>& input, arma::Mat<eT>& output)
332  {
333  // * left-right flip, up-down flip */
334  output = arma::fliplr(arma::flipud(input));
335  }
336 
338  size_t inSize;
339 
341  size_t outSize;
342 
344  size_t batchSize;
345 
347  size_t kernelWidth;
348 
350  size_t kernelHeight;
351 
353  size_t strideWidth;
354 
356  size_t strideHeight;
357 
359  OutputDataType weights;
360 
362  arma::cube weight;
363 
365  arma::mat bias;
366 
368  size_t inputWidth;
369 
371  size_t inputHeight;
372 
374  size_t outputWidth;
375 
377  size_t outputHeight;
378 
380  size_t dilationWidth;
381 
383  size_t dilationHeight;
384 
386  arma::cube outputTemp;
387 
389  arma::cube inputPaddedTemp;
390 
392  arma::cube gTemp;
393 
395  arma::cube gradientTemp;
396 
398  ann::Padding<> padding;
399 
401  OutputDataType delta;
402 
404  OutputDataType gradient;
405 
407  OutputDataType outputParameter;
408 }; // class AtrousConvolution
409 
410 } // namespace ann
411 } // namespace mlpack
412 
413 // Include implementation.
414 #include "atrous_convolution_impl.hpp"
415 
416 #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 InputShape() const
Get the shape of the input.
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.
Linear algebra utility functions, generally performed on matrices or vectors.
Implementation of the Padding module class.
Definition: layer_types.hpp:84
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.
size_t WeightSize() const
Get size of the weight matrix.
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.
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
Implementation of the Atrous Convolution class.