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 
25 namespace mlpack{
26 namespace ann {
27 
43 template <
44  typename ForwardConvolutionRule = NaiveConvolution<ValidConvolution>,
45  typename BackwardConvolutionRule = NaiveConvolution<FullConvolution>,
46  typename GradientConvolutionRule = NaiveConvolution<ValidConvolution>,
47  typename InputDataType = arma::mat,
48  typename OutputDataType = arma::mat
49 >
51 {
52  public:
55 
74  AtrousConvolution(const size_t inSize,
75  const size_t outSize,
76  const size_t kW,
77  const size_t kH,
78  const size_t dW = 1,
79  const size_t dH = 1,
80  const size_t padW = 0,
81  const size_t padH = 0,
82  const size_t inputWidth = 0,
83  const size_t inputHeight = 0,
84  const size_t dilationW = 1,
85  const size_t dilationH = 1);
86 
87  /*
88  * Set the weight and bias term.
89  */
90  void Reset();
91 
99  template<typename eT>
100  void Forward(const arma::Mat<eT>&& input, arma::Mat<eT>&& output);
101 
111  template<typename eT>
112  void Backward(const arma::Mat<eT>&& /* input */,
113  arma::Mat<eT>&& gy,
114  arma::Mat<eT>&& g);
115 
116  /*
117  * Calculate the gradient using the output delta and the input activation.
118  *
119  * @param input The input parameter used for calculating the gradient.
120  * @param error The calculated error.
121  * @param gradient The calculated gradient.
122  */
123  template<typename eT>
124  void Gradient(const arma::Mat<eT>&& /* input */,
125  arma::Mat<eT>&& error,
126  arma::Mat<eT>&& gradient);
127 
129  OutputDataType const& Parameters() const { return weights; }
131  OutputDataType& Parameters() { return weights; }
132 
134  OutputDataType const& OutputParameter() const { return outputParameter; }
136  OutputDataType& OutputParameter() { return outputParameter; }
137 
139  OutputDataType const& Delta() const { return delta; }
141  OutputDataType& Delta() { return delta; }
142 
144  OutputDataType const& Gradient() const { return gradient; }
146  OutputDataType& Gradient() { return gradient; }
147 
149  size_t const& InputWidth() const { return inputWidth; }
151  size_t& InputWidth() { return inputWidth; }
152 
154  size_t const& InputHeight() const { return inputHeight; }
156  size_t& InputHeight() { return inputHeight; }
157 
159  size_t const& OutputWidth() const { return outputWidth; }
161  size_t& OutputWidth() { return outputWidth; }
162 
164  size_t const& OutputHeight() const { return outputHeight; }
166  size_t& OutputHeight() { return outputHeight; }
167 
171  template<typename Archive>
172  void serialize(Archive& ar, const unsigned int /* version */);
173 
174  private:
175  /*
176  * Return the convolution output size.
177  *
178  * @param size The size of the input (row or column).
179  * @param k The size of the filter (width or height).
180  * @param s The stride size (x or y direction).
181  * @param p The size of the padding (width or height).
182  * @param d The dilation size.
183  * @return The convolution output size.
184  */
185  size_t ConvOutSize(const size_t size,
186  const size_t k,
187  const size_t s,
188  const size_t p,
189  const size_t d)
190  {
191  return std::floor(size + p * 2 - d * (k - 1) - 1) / s + 1;
192  }
193 
194  /*
195  * Rotates a 3rd-order tensor counterclockwise by 180 degrees.
196  *
197  * @param input The input data to be rotated.
198  * @param output The rotated output.
199  */
200  template<typename eT>
201  void Rotate180(const arma::Cube<eT>& input, arma::Cube<eT>& output)
202  {
203  output = arma::Cube<eT>(input.n_rows, input.n_cols, input.n_slices);
204 
205  // * left-right flip, up-down flip */
206  for (size_t s = 0; s < output.n_slices; s++)
207  output.slice(s) = arma::fliplr(arma::flipud(input.slice(s)));
208  }
209 
210  /*
211  * Rotates a dense matrix counterclockwise by 180 degrees.
212  *
213  * @param input The input data to be rotated.
214  * @param output The rotated output.
215  */
216  template<typename eT>
217  void Rotate180(const arma::Mat<eT>& input, arma::Mat<eT>& output)
218  {
219  // * left-right flip, up-down flip */
220  output = arma::fliplr(arma::flipud(input));
221  }
222 
223  /*
224  * Pad the given input data.
225  *
226  * @param input The input to be padded.
227  * @param wPad Padding width of the input.
228  * @param hPad Padding height of the input.
229  * @param output The padded output data.
230  */
231  template<typename eT>
232  void Pad(const arma::Mat<eT>& input,
233  size_t wPad,
234  size_t hPad,
235  arma::Mat<eT>& output)
236  {
237  if (output.n_rows != input.n_rows + wPad * 2 ||
238  output.n_cols != input.n_cols + hPad * 2)
239  {
240  output = arma::zeros(input.n_rows + wPad * 2, input.n_cols + hPad * 2);
241  }
242 
243  output.submat(wPad, hPad, wPad + input.n_rows - 1,
244  hPad + input.n_cols - 1) = input;
245  }
246 
247  /*
248  * Pad the given input data.
249  *
250  * @param input The input to be padded.
251  * @param wPad Padding width of the input.
252  * @param hPad Padding height of the input.
253  * @param output The padded output data.
254  */
255  template<typename eT>
256  void Pad(const arma::Cube<eT>& input,
257  size_t wPad,
258  size_t hPad,
259  arma::Cube<eT>& output)
260  {
261  output = arma::zeros(input.n_rows + wPad * 2,
262  input.n_cols + hPad * 2, input.n_slices);
263 
264  for (size_t i = 0; i < input.n_slices; ++i)
265  {
266  Pad<double>(input.slice(i), wPad, hPad, output.slice(i));
267  }
268  }
269 
271  size_t inSize;
272 
274  size_t outSize;
275 
277  size_t batchSize;
278 
280  size_t kW;
281 
283  size_t kH;
284 
286  size_t dW;
287 
289  size_t dH;
290 
292  size_t padW;
293 
295  size_t padH;
296 
298  OutputDataType weights;
299 
301  arma::cube weight;
302 
304  arma::mat bias;
305 
307  size_t inputWidth;
308 
310  size_t inputHeight;
311 
313  size_t outputWidth;
314 
316  size_t outputHeight;
317 
319  size_t dilationW;
320 
322  size_t dilationH;
323 
325  arma::cube outputTemp;
326 
328  arma::cube inputTemp;
329 
331  arma::cube inputPaddedTemp;
332 
334  arma::cube gTemp;
335 
337  arma::cube gradientTemp;
338 
340  OutputDataType delta;
341 
343  OutputDataType gradient;
344 
346  OutputDataType outputParameter;
347 }; // class AtrousConvolution
348 
349 } // namespace ann
350 } // namespace mlpack
351 
352 // Include implementation
353 #include "atrous_convolution_impl.hpp"
354 
355 #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...
size_t const & OutputHeight() const
Get the output height.
.hpp
Definition: add_to_po.hpp:21
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.