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  InputDataType const& InputParameter() const { return inputParameter; }
136  InputDataType& InputParameter() { return inputParameter; }
137 
139  OutputDataType const& OutputParameter() const { return outputParameter; }
141  OutputDataType& OutputParameter() { return outputParameter; }
142 
144  OutputDataType const& Delta() const { return delta; }
146  OutputDataType& Delta() { return delta; }
147 
149  OutputDataType const& Gradient() const { return gradient; }
151  OutputDataType& Gradient() { return gradient; }
152 
154  size_t const& InputWidth() const { return inputWidth; }
156  size_t& InputWidth() { return inputWidth; }
157 
159  size_t const& InputHeight() const { return inputHeight; }
161  size_t& InputHeight() { return inputHeight; }
162 
164  size_t const& OutputWidth() const { return outputWidth; }
166  size_t& OutputWidth() { return outputWidth; }
167 
169  size_t const& OutputHeight() const { return outputHeight; }
171  size_t& OutputHeight() { return outputHeight; }
172 
176  template<typename Archive>
177  void serialize(Archive& ar, const unsigned int /* version */);
178 
179  private:
180  /*
181  * Return the convolution output size.
182  *
183  * @param size The size of the input (row or column).
184  * @param k The size of the filter (width or height).
185  * @param s The stride size (x or y direction).
186  * @param p The size of the padding (width or height).
187  * @param d The dilation size.
188  * @return The convolution output size.
189  */
190  size_t ConvOutSize(const size_t size,
191  const size_t k,
192  const size_t s,
193  const size_t p,
194  const size_t d)
195  {
196  return std::floor(size + p * 2 - d * (k - 1) - 1) / s + 1;
197  }
198 
199  /*
200  * Rotates a 3rd-order tensor counterclockwise by 180 degrees.
201  *
202  * @param input The input data to be rotated.
203  * @param output The rotated output.
204  */
205  template<typename eT>
206  void Rotate180(const arma::Cube<eT>& input, arma::Cube<eT>& output)
207  {
208  output = arma::Cube<eT>(input.n_rows, input.n_cols, input.n_slices);
209 
210  // * left-right flip, up-down flip */
211  for (size_t s = 0; s < output.n_slices; s++)
212  output.slice(s) = arma::fliplr(arma::flipud(input.slice(s)));
213  }
214 
215  /*
216  * Rotates a dense matrix counterclockwise by 180 degrees.
217  *
218  * @param input The input data to be rotated.
219  * @param output The rotated output.
220  */
221  template<typename eT>
222  void Rotate180(const arma::Mat<eT>& input, arma::Mat<eT>& output)
223  {
224  // * left-right flip, up-down flip */
225  output = arma::fliplr(arma::flipud(input));
226  }
227 
228  /*
229  * Pad the given input data.
230  *
231  * @param input The input to be padded.
232  * @param wPad Padding width of the input.
233  * @param hPad Padding height of the input.
234  * @param output The padded output data.
235  */
236  template<typename eT>
237  void Pad(const arma::Mat<eT>& input,
238  size_t wPad,
239  size_t hPad,
240  arma::Mat<eT>& output)
241  {
242  if (output.n_rows != input.n_rows + wPad * 2 ||
243  output.n_cols != input.n_cols + hPad * 2)
244  {
245  output = arma::zeros(input.n_rows + wPad * 2, input.n_cols + hPad * 2);
246  }
247 
248  output.submat(wPad, hPad, wPad + input.n_rows - 1,
249  hPad + input.n_cols - 1) = input;
250  }
251 
252  /*
253  * Pad the given input data.
254  *
255  * @param input The input to be padded.
256  * @param wPad Padding width of the input.
257  * @param hPad Padding height of the input.
258  * @param output The padded output data.
259  */
260  template<typename eT>
261  void Pad(const arma::Cube<eT>& input,
262  size_t wPad,
263  size_t hPad,
264  arma::Cube<eT>& output)
265  {
266  output = arma::zeros(input.n_rows + wPad * 2,
267  input.n_cols + hPad * 2, input.n_slices);
268 
269  for (size_t i = 0; i < input.n_slices; ++i)
270  {
271  Pad<double>(input.slice(i), wPad, hPad, output.slice(i));
272  }
273  }
274 
276  size_t inSize;
277 
279  size_t outSize;
280 
282  size_t kW;
283 
285  size_t kH;
286 
288  size_t dW;
289 
291  size_t dH;
292 
294  size_t padW;
295 
297  size_t padH;
298 
300  OutputDataType weights;
301 
303  arma::cube weight;
304 
306  arma::mat bias;
307 
309  size_t inputWidth;
310 
312  size_t inputHeight;
313 
315  size_t outputWidth;
316 
318  size_t outputHeight;
319 
321  size_t dilationW;
322 
324  size_t dilationH;
325 
327  arma::cube outputTemp;
328 
330  arma::cube inputTemp;
331 
333  arma::cube inputPaddedTemp;
334 
336  arma::cube gTemp;
337 
339  arma::cube gradientTemp;
340 
342  OutputDataType delta;
343 
345  OutputDataType gradient;
346 
348  InputDataType inputParameter;
349 
351  OutputDataType outputParameter;
352 }; // class AtrousConvolution
353 
354 } // namespace ann
355 } // namespace mlpack
356 
357 // Include implementation
358 #include "atrous_convolution_impl.hpp"
359 
360 #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.
InputDataType & InputParameter()
Modify the input parameter.
.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.
InputDataType const & InputParameter() const
Get the input parameter.
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.