atrous_convolution.hpp
Go to the documentation of this file.
1 // Temporarily drop.
14 #ifndef MLPACK_METHODS_ANN_LAYER_ATROUS_CONVOLUTION_HPP
15 #define MLPACK_METHODS_ANN_LAYER_ATROUS_CONVOLUTION_HPP
16 
17 #include <mlpack/prereqs.hpp>
18 
24 
25 #include "layer_types.hpp"
26 #include "padding.hpp"
27 
28 namespace mlpack{
29 namespace ann {
30 
46 template <
47  typename ForwardConvolutionRule = NaiveConvolution<ValidConvolution>,
48  typename BackwardConvolutionRule = NaiveConvolution<FullConvolution>,
49  typename GradientConvolutionRule = NaiveConvolution<ValidConvolution>,
50  typename InputType = arma::mat,
51  typename OutputType = arma::mat
52 >
53 class AtrousConvolution : public Layer<InputType, OutputType>
54 {
55  public:
58 
79  AtrousConvolution(const size_t inSize,
80  const size_t outSize,
81  const size_t kernelWidth,
82  const size_t kernelHeight,
83  const size_t strideWidth = 1,
84  const size_t strideHeight = 1,
85  const size_t padW = 0,
86  const size_t padH = 0,
87  const size_t inputWidth = 0,
88  const size_t inputHeight = 0,
89  const size_t dilationWidth = 1,
90  const size_t dilationHeight = 1,
91  const std::string& paddingType = "None");
92 
117  AtrousConvolution(const size_t inSize,
118  const size_t outSize,
119  const size_t kernelWidth,
120  const size_t kernelHeight,
121  const size_t strideWidth,
122  const size_t strideHeight,
123  const std::tuple<size_t, size_t>& padW,
124  const std::tuple<size_t, size_t>& padH,
125  const size_t inputWidth = 0,
126  const size_t inputHeight = 0,
127  const size_t dilationWidth = 1,
128  const size_t dilationHeight = 1,
129  const std::string& paddingType = "None");
130 
131  /*
132  * Set the weight and bias term.
133  */
134  void Reset();
135 
143  void Forward(const InputType& input, OutputType& output);
144 
154  void Backward(const InputType& /* input */,
155  const OutputType& gy,
156  OutputType& g);
157 
158  /*
159  * Calculate the gradient using the output delta and the input activation.
160  *
161  * @param input The input parameter used for calculating the gradient.
162  * @param error The calculated error.
163  * @param gradient The calculated gradient.
164  */
165  void Gradient(const InputType& /* input */,
166  const OutputType& error,
167  OutputType& gradient);
168 
170  OutputType const& Parameters() const { return weights; }
172  OutputType& Parameters() { return weights; }
173 
175  const arma::Cube<typename OutputType::elem_type>& Weight() const
176  {
177  return weight;
178  }
180  arma::Cube<typename OutputType::elem_type>& Weight() { return weight; }
181 
182  const std::vector<size_t>& OutputDimensions() const
183  {
184  std::vector<size_t> result(inputDimensions.size(), 0);
185  result[0] = outputWidth;
186  result[1] = outputHeight;
187  return result;
188  }
189 
191  const OutputType& Bias() const { return bias; }
193  OutputType& Bias() { return bias; }
194 
196  const size_t& InputWidth() const { return inputWidth; }
198  size_t& InputWidth() { return inputWidth; }
199 
201  const size_t& InputHeight() const { return inputHeight; }
203  size_t& InputHeight() { return inputHeight; }
204 
206  const size_t& OutputWidth() const { return outputWidth; }
208  size_t& OutputWidth() { return outputWidth; }
209 
211  const size_t& OutputHeight() const { return outputHeight; }
213  size_t& OutputHeight() { return outputHeight; }
214 
216  const size_t& KernelWidth() const { return kernelWidth; }
218  size_t& KernelWidth() { return kernelWidth; }
219 
221  const size_t& KernelHeight() const { return kernelHeight; }
223  size_t& KernelHeight() { return kernelHeight; }
224 
226  const size_t& StrideWidth() const { return strideWidth; }
228  size_t& StrideWidth() { return strideWidth; }
229 
231  const size_t& StrideHeight() const { return strideHeight; }
233  size_t& StrideHeight() { return strideHeight; }
234 
236  const size_t& DilationWidth() const { return dilationWidth; }
238  size_t& DilationWidth() { return dilationWidth; }
239 
241  const size_t& DilationHeight() const { return dilationHeight; }
243  size_t& DilationHeight() { return dilationHeight; }
244 
246  PaddingType<InputType, OutputType> const& Padding() const { return padding; }
249 
251  size_t WeightSize() const
252  {
253  return (outSize * inSize * kernelWidth * kernelHeight) + outSize;
254  }
255 
257  size_t InputShape() const
258  {
259  return inputHeight * inputWidth * inSize;
260  }
261 
265  template<typename Archive>
266  void serialize(Archive& ar, const uint32_t /* version */);
267 
268  private:
269  /*
270  * Return the convolution output size.
271  *
272  * @param size The size of the input (row or column).
273  * @param k The size of the filter (width or height).
274  * @param s The stride size (x or y direction).
275  * @param pSideOne The size of the padding (width or height) on one side.
276  * @param pSideTwo The size of the padding (width or height) on another side.
277  * @param d The dilation size.
278  * @return The convolution output size.
279  */
280  size_t ConvOutSize(const size_t size,
281  const size_t k,
282  const size_t s,
283  const size_t pSideOne,
284  const size_t pSideTwo,
285  const size_t d)
286  {
287  return std::floor(size + pSideOne + pSideTwo - d * (k - 1) - 1) / s + 1;
288  }
289 
290  /*
291  * Function to assign padding such that output size is same as input size.
292  */
293  void InitializeSamePadding(size_t& padWLeft,
294  size_t& padWRight,
295  size_t& padHBottom,
296  size_t& padHTop) const;
297 
298  /*
299  * Rotates a 3rd-order tensor counterclockwise by 180 degrees.
300  *
301  * @param input The input data to be rotated.
302  * @param output The rotated output.
303  */
304  template<typename eT>
305  void Rotate180(const arma::Cube<eT>& input, arma::Cube<eT>& output)
306  {
307  output = arma::Cube<eT>(input.n_rows, input.n_cols, input.n_slices);
308 
309  // * left-right flip, up-down flip */
310  for (size_t s = 0; s < output.n_slices; s++)
311  output.slice(s) = arma::fliplr(arma::flipud(input.slice(s)));
312  }
313 
314  /*
315  * Rotates a dense matrix counterclockwise by 180 degrees.
316  *
317  * @param input The input data to be rotated.
318  * @param output The rotated output.
319  */
320  template<typename eT>
321  void Rotate180(const arma::Mat<eT>& input, arma::Mat<eT>& output)
322  {
323  // * left-right flip, up-down flip */
324  output = arma::fliplr(arma::flipud(input));
325  }
326 
328  size_t inSize;
329 
331  size_t outSize;
332 
334  size_t batchSize;
335 
337  size_t kernelWidth;
338 
340  size_t kernelHeight;
341 
343  size_t strideWidth;
344 
346  size_t strideHeight;
347 
349  OutputType weights;
350 
352  arma::Cube<typename OutputType::elem_type> weight;
353 
355  OutputType bias;
356 
358  size_t inputWidth;
359 
361  size_t inputHeight;
362 
364  size_t outputWidth;
365 
367  size_t outputHeight;
368 
370  size_t dilationWidth;
371 
373  size_t dilationHeight;
374 
376  arma::Cube<typename OutputType::elem_type> outputTemp;
377 
379  arma::Cube<typename OutputType::elem_type> inputPaddedTemp;
380 
382  arma::Cube<typename OutputType::elem_type> gTemp;
383 
385  arma::Cube<typename OutputType::elem_type> gradientTemp;
386 
389 }; // class AtrousConvolution
390 
391 } // namespace ann
392 } // namespace mlpack
393 
394 // Include implementation.
395 #include "atrous_convolution_impl.hpp"
396 
397 #endif
const size_t & StrideHeight() const
Get the stride height.
const arma::Cube< typename OutputType::elem_type > & Weight() const
Get the weight of the layer.
std::vector< size_t > inputDimensions
Logical input dimensions of each point.
Definition: layer.hpp:302
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.
OutputType const & Parameters() const
Get the parameters.
constexpr auto size(Container const &container) noexcept -> decltype(container.size())
Definition: iterator.hpp:29
Linear algebra utility functions, generally performed on matrices or vectors.
OutputType & Parameters()
Modify the parameters.
size_t & InputHeight()
Modify the input height.
The core includes that mlpack expects; standard C++ includes and Armadillo.
PaddingType< InputType, OutputType > const & Padding() const
Get the internal Padding layer.
size_t WeightSize() const
Get size of the weight matrix.
AtrousConvolution()
Create the AtrousConvolution object.
const size_t & InputHeight() const
Get the input height.
void Forward(const InputType &input, OutputType &output)
Ordinary feed forward pass of a neural network, evaluating the function f(x) by propagating the activ...
const OutputType & Bias() const
Get the bias of the layer.
OutputType & Bias()
Modify the bias of the layer.
arma::Cube< typename OutputType::elem_type > & Weight()
Modify the weight of the layer.
size_t & InputWidth()
Modify input the width.
size_t & StrideHeight()
Modify the stride height.
size_t & KernelWidth()
Modify the kernel width.
PaddingType< InputType, OutputType > & Padding()
Modify the internal Padding layer.
const size_t & DilationHeight() const
Get the dilation rate on the Y axis.
const size_t & KernelHeight() const
Get the kernel height.
const size_t & OutputWidth() const
Get the output width.
void Backward(const InputType &, const OutputType &gy, OutputType &g)
Ordinary feed backward pass of a neural network, calculating the function f(x) by propagating x backw...
size_t & DilationWidth()
Modify the dilation rate on the X axis.
A layer is an abstract class implementing common neural networks operations, such as convolution...
Definition: layer.hpp:52
const size_t & DilationWidth() const
Get the dilation rate on the X axis.
size_t & KernelHeight()
Modify the kernel height.
void Gradient(const InputType &, const OutputType &error, OutputType &gradient)
const std::vector< size_t > & OutputDimensions() const
const size_t & InputWidth() const
Get the input width.
size_t & OutputWidth()
Modify the output width.
const size_t & KernelWidth() const
Get the kernel width.
const size_t & OutputHeight() const
Get the output height.
size_t & OutputHeight()
Modify the output height.
const size_t & StrideWidth() const
Get the stride width.
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
Implementation of the Atrous Convolution class.