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 
22 
23 #include "layer_types.hpp"
24 #include "padding.hpp"
25 
26 namespace mlpack{
27 namespace ann {
28 
44 template <
45  typename ForwardConvolutionRule = NaiveConvolution<ValidConvolution>,
46  typename BackwardConvolutionRule = NaiveConvolution<FullConvolution>,
47  typename GradientConvolutionRule = NaiveConvolution<ValidConvolution>,
48  typename InputDataType = arma::mat,
49  typename OutputDataType = arma::mat
50 >
52 {
53  public:
56 
76  AtrousConvolution(const size_t inSize,
77  const size_t outSize,
78  const size_t kW,
79  const size_t kH,
80  const size_t dW = 1,
81  const size_t dH = 1,
82  const size_t padW = 0,
83  const size_t padH = 0,
84  const size_t inputWidth = 0,
85  const size_t inputHeight = 0,
86  const size_t dilationW = 1,
87  const size_t dilationH = 1,
88  const std::string paddingType = "None");
89 
113  AtrousConvolution(const size_t inSize,
114  const size_t outSize,
115  const size_t kW,
116  const size_t kH,
117  const size_t dW,
118  const size_t dH,
119  const std::tuple<size_t, size_t> padW,
120  const std::tuple<size_t, size_t> padH,
121  const size_t inputWidth = 0,
122  const size_t inputHeight = 0,
123  const size_t dilationW = 1,
124  const size_t dilationH = 1,
125  const std::string paddingType = "None");
126 
127  /*
128  * Set the weight and bias term.
129  */
130  void Reset();
131 
139  template<typename eT>
140  void Forward(const arma::Mat<eT>&& input, arma::Mat<eT>&& output);
141 
151  template<typename eT>
152  void Backward(const arma::Mat<eT>&& /* input */,
153  arma::Mat<eT>&& gy,
154  arma::Mat<eT>&& g);
155 
156  /*
157  * Calculate the gradient using the output delta and the input activation.
158  *
159  * @param input The input parameter used for calculating the gradient.
160  * @param error The calculated error.
161  * @param gradient The calculated gradient.
162  */
163  template<typename eT>
164  void Gradient(const arma::Mat<eT>&& /* input */,
165  arma::Mat<eT>&& error,
166  arma::Mat<eT>&& gradient);
167 
169  OutputDataType const& Parameters() const { return weights; }
171  OutputDataType& Parameters() { return weights; }
172 
174  OutputDataType const& OutputParameter() const { return outputParameter; }
176  OutputDataType& OutputParameter() { return outputParameter; }
177 
179  OutputDataType const& Delta() const { return delta; }
181  OutputDataType& Delta() { return delta; }
182 
184  OutputDataType const& Gradient() const { return gradient; }
186  OutputDataType& Gradient() { return gradient; }
187 
189  size_t const& InputWidth() const { return inputWidth; }
191  size_t& InputWidth() { return inputWidth; }
192 
194  size_t const& InputHeight() const { return inputHeight; }
196  size_t& InputHeight() { return inputHeight; }
197 
199  size_t const& OutputWidth() const { return outputWidth; }
201  size_t& OutputWidth() { return outputWidth; }
202 
204  size_t const& OutputHeight() const { return outputHeight; }
206  size_t& OutputHeight() { return outputHeight; }
207 
209  arma::mat& Bias() { return bias; }
210 
214  template<typename Archive>
215  void serialize(Archive& ar, const unsigned int /* version */);
216 
217  private:
218  /*
219  * Return the convolution output size.
220  *
221  * @param size The size of the input (row or column).
222  * @param k The size of the filter (width or height).
223  * @param s The stride size (x or y direction).
224  * @param pSideOne The size of the padding (width or height) on one side.
225  * @param pSideTwo The size of the padding (width or height) on another side.
226  * @param d The dilation size.
227  * @return The convolution output size.
228  */
229  size_t ConvOutSize(const size_t size,
230  const size_t k,
231  const size_t s,
232  const size_t pSideOne,
233  const size_t pSideTwo,
234  const size_t d)
235  {
236  return std::floor(size + pSideOne + pSideTwo - d * (k - 1) - 1) / s + 1;
237  }
238 
239  /*
240  * Function to assign padding such that output size is same as input size.
241  */
242  void InitializeSamePadding();
243 
244  /*
245  * Rotates a 3rd-order tensor counterclockwise by 180 degrees.
246  *
247  * @param input The input data to be rotated.
248  * @param output The rotated output.
249  */
250  template<typename eT>
251  void Rotate180(const arma::Cube<eT>& input, arma::Cube<eT>& output)
252  {
253  output = arma::Cube<eT>(input.n_rows, input.n_cols, input.n_slices);
254 
255  // * left-right flip, up-down flip */
256  for (size_t s = 0; s < output.n_slices; s++)
257  output.slice(s) = arma::fliplr(arma::flipud(input.slice(s)));
258  }
259 
260  /*
261  * Rotates a dense matrix counterclockwise by 180 degrees.
262  *
263  * @param input The input data to be rotated.
264  * @param output The rotated output.
265  */
266  template<typename eT>
267  void Rotate180(const arma::Mat<eT>& input, arma::Mat<eT>& output)
268  {
269  // * left-right flip, up-down flip */
270  output = arma::fliplr(arma::flipud(input));
271  }
272 
274  size_t inSize;
275 
277  size_t outSize;
278 
280  size_t batchSize;
281 
283  size_t kW;
284 
286  size_t kH;
287 
289  size_t dW;
290 
292  size_t dH;
293 
295  size_t padWLeft;
296 
298  size_t padWRight;
299 
301  size_t padHBottom;
302 
304  size_t padHTop;
305 
307  OutputDataType weights;
308 
310  arma::cube weight;
311 
313  arma::mat bias;
314 
316  size_t inputWidth;
317 
319  size_t inputHeight;
320 
322  size_t outputWidth;
323 
325  size_t outputHeight;
326 
328  size_t dilationW;
329 
331  size_t dilationH;
332 
334  arma::cube outputTemp;
335 
337  arma::cube inputTemp;
338 
340  arma::cube inputPaddedTemp;
341 
343  arma::cube gTemp;
344 
346  arma::cube gradientTemp;
347 
349  Padding<>* padding;
350 
352  OutputDataType delta;
353 
355  OutputDataType gradient;
356 
358  OutputDataType outputParameter;
359 }; // class AtrousConvolution
360 
361 } // namespace ann
362 } // namespace mlpack
363 
365 namespace boost {
366 namespace serialization {
367 
368 template<
369  typename ForwardConvolutionRule,
370  typename BackwardConvolutionRule,
371  typename GradientConvolutionRule,
372  typename InputDataType,
373  typename OutputDataType
374 >
375 struct version<
376  mlpack::ann::AtrousConvolution<ForwardConvolutionRule,
377  BackwardConvolutionRule, GradientConvolutionRule, InputDataType,
378  OutputDataType> >
379 {
380  BOOST_STATIC_CONSTANT(int, value = 1);
381 };
382 
383 } // namespace serialization
384 } // namespace boost
385 
386 // Include implementation
387 #include "atrous_convolution_impl.hpp"
388 
389 #endif
OutputDataType const & Parameters() const
Get the parameters.
OutputDataType const & OutputParameter() const
Get the output parameter.
void Backward(const arma::Mat< eT > &&, 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...
Set the serialization version of the adaboost class.
Definition: adaboost.hpp:180
size_t const & OutputHeight() const
Get the output height.
arma::mat & Bias()
Modify the bias weights of the layer.
.hpp
Definition: add_to_po.hpp:21
Implementation of the Padding module class.
Definition: layer_types.hpp:68
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
OutputDataType & Delta()
Modify the delta.
size_t & InputHeight()
Modify the input height.
The core includes that mlpack expects; standard C++ includes and Armadillo.
size_t const & InputHeight() const
Get the input height.
OutputDataType & OutputParameter()
Modify the output parameter.
OutputDataType & Parameters()
Modify the parameters.
AtrousConvolution()
Create the AtrousConvolution object.
size_t const & OutputWidth() const
Get the output width.
OutputDataType const & Gradient() const
Get the gradient.
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...
size_t & InputWidth()
Modify input the width.
size_t const & InputWidth() const
Get the input width.
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.
Implementation of the Atrous Convolution class.