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 
78  AtrousConvolution(const size_t inSize,
79  const size_t outSize,
80  const size_t kernelWidth,
81  const size_t kernelHeight,
82  const size_t strideWidth = 1,
83  const size_t strideHeight = 1,
84  const size_t padW = 0,
85  const size_t padH = 0,
86  const size_t inputWidth = 0,
87  const size_t inputHeight = 0,
88  const size_t dilationWidth = 1,
89  const size_t dilationHeight = 1,
90  const std::string& paddingType = "None");
91 
116  AtrousConvolution(const size_t inSize,
117  const size_t outSize,
118  const size_t kernelWidth,
119  const size_t kernelHeight,
120  const size_t strideWidth,
121  const size_t strideHeight,
122  const std::tuple<size_t, size_t>& padW,
123  const std::tuple<size_t, size_t>& padH,
124  const size_t inputWidth = 0,
125  const size_t inputHeight = 0,
126  const size_t dilationWidth = 1,
127  const size_t dilationHeight = 1,
128  const std::string& paddingType = "None");
129 
130  /*
131  * Set the weight and bias term.
132  */
133  void Reset();
134 
142  template<typename eT>
143  void Forward(const arma::Mat<eT>& input, arma::Mat<eT>& output);
144 
154  template<typename eT>
155  void Backward(const arma::Mat<eT>& /* input */,
156  const arma::Mat<eT>& gy,
157  arma::Mat<eT>& g);
158 
159  /*
160  * Calculate the gradient using the output delta and the input activation.
161  *
162  * @param input The input parameter used for calculating the gradient.
163  * @param error The calculated error.
164  * @param gradient The calculated gradient.
165  */
166  template<typename eT>
167  void Gradient(const arma::Mat<eT>& /* input */,
168  const arma::Mat<eT>& error,
169  arma::Mat<eT>& gradient);
170 
172  OutputDataType const& Parameters() const { return weights; }
174  OutputDataType& Parameters() { return weights; }
175 
177  arma::cube const& Weight() const { return weight; }
179  arma::cube& Weight() { return weight; }
180 
182  arma::mat const& Bias() const { return bias; }
184  arma::mat& Bias() { return bias; }
185 
187  OutputDataType const& OutputParameter() const { return outputParameter; }
189  OutputDataType& OutputParameter() { return outputParameter; }
190 
192  OutputDataType const& Delta() const { return delta; }
194  OutputDataType& Delta() { return delta; }
195 
197  OutputDataType const& Gradient() const { return gradient; }
199  OutputDataType& Gradient() { return gradient; }
200 
202  size_t InputWidth() const { return inputWidth; }
204  size_t& InputWidth() { return inputWidth; }
205 
207  size_t InputHeight() const { return inputHeight; }
209  size_t& InputHeight() { return inputHeight; }
210 
212  size_t OutputWidth() const { return outputWidth; }
214  size_t& OutputWidth() { return outputWidth; }
215 
217  size_t OutputHeight() const { return outputHeight; }
219  size_t& OutputHeight() { return outputHeight; }
220 
222  size_t InputSize() const { return inSize; }
223 
225  size_t OutputSize() const { return outSize; }
226 
228  size_t KernelWidth() const { return kernelWidth; }
230  size_t& KernelWidth() { return kernelWidth; }
231 
233  size_t KernelHeight() const { return kernelHeight; }
235  size_t& KernelHeight() { return kernelHeight; }
236 
238  size_t StrideWidth() const { return strideWidth; }
240  size_t& StrideWidth() { return strideWidth; }
241 
243  size_t StrideHeight() const { return strideHeight; }
245  size_t& StrideHeight() { return strideHeight; }
246 
248  size_t DilationWidth() const { return dilationWidth; }
250  size_t& DilationWidth() { return dilationWidth; }
251 
253  size_t DilationHeight() const { return dilationHeight; }
255  size_t& DilationHeight() { return dilationHeight; }
256 
258  ann::Padding<> const& Padding() const { return padding; }
260  ann::Padding<>& Padding() { return padding; }
261 
263  size_t WeightSize() const
264  {
265  return (outSize * inSize * kernelWidth * kernelHeight) + outSize;
266  }
267 
269  size_t InputShape() const
270  {
271  return inputHeight * inputWidth * inSize;
272  }
273 
277  template<typename Archive>
278  void serialize(Archive& ar, const uint32_t /* version */);
279 
280  private:
281  /*
282  * Return the convolution output size.
283  *
284  * @param size The size of the input (row or column).
285  * @param k The size of the filter (width or height).
286  * @param s The stride size (x or y direction).
287  * @param pSideOne The size of the padding (width or height) on one side.
288  * @param pSideTwo The size of the padding (width or height) on another side.
289  * @param d The dilation size.
290  * @return The convolution output size.
291  */
292  size_t ConvOutSize(const size_t size,
293  const size_t k,
294  const size_t s,
295  const size_t pSideOne,
296  const size_t pSideTwo,
297  const size_t d)
298  {
299  return std::floor(size + pSideOne + pSideTwo - d * (k - 1) - 1) / s + 1;
300  }
301 
302  /*
303  * Function to assign padding such that output size is same as input size.
304  */
305  void InitializeSamePadding(size_t& padWLeft,
306  size_t& padWRight,
307  size_t& padHBottom,
308  size_t& padHTop) const;
309 
310  /*
311  * Rotates a 3rd-order tensor counterclockwise by 180 degrees.
312  *
313  * @param input The input data to be rotated.
314  * @param output The rotated output.
315  */
316  template<typename eT>
317  void Rotate180(const arma::Cube<eT>& input, arma::Cube<eT>& output)
318  {
319  output = arma::Cube<eT>(input.n_rows, input.n_cols, input.n_slices);
320 
321  // * left-right flip, up-down flip */
322  for (size_t s = 0; s < output.n_slices; s++)
323  output.slice(s) = arma::fliplr(arma::flipud(input.slice(s)));
324  }
325 
326  /*
327  * Rotates a dense matrix counterclockwise by 180 degrees.
328  *
329  * @param input The input data to be rotated.
330  * @param output The rotated output.
331  */
332  template<typename eT>
333  void Rotate180(const arma::Mat<eT>& input, arma::Mat<eT>& output)
334  {
335  // * left-right flip, up-down flip */
336  output = arma::fliplr(arma::flipud(input));
337  }
338 
340  size_t inSize;
341 
343  size_t outSize;
344 
346  size_t batchSize;
347 
349  size_t kernelWidth;
350 
352  size_t kernelHeight;
353 
355  size_t strideWidth;
356 
358  size_t strideHeight;
359 
361  OutputDataType weights;
362 
364  arma::cube weight;
365 
367  arma::mat bias;
368 
370  size_t inputWidth;
371 
373  size_t inputHeight;
374 
376  size_t outputWidth;
377 
379  size_t outputHeight;
380 
382  size_t dilationWidth;
383 
385  size_t dilationHeight;
386 
388  arma::cube outputTemp;
389 
391  arma::cube inputPaddedTemp;
392 
394  arma::cube gTemp;
395 
397  arma::cube gradientTemp;
398 
400  ann::Padding<> padding;
401 
403  OutputDataType delta;
404 
406  OutputDataType gradient;
407 
409  OutputDataType outputParameter;
410 }; // class AtrousConvolution
411 
412 } // namespace ann
413 } // namespace mlpack
414 
415 // Include implementation.
416 #include "atrous_convolution_impl.hpp"
417 
418 #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:89
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.