mlpack  master
convolution.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_ANN_LAYER_CONVOLUTION_HPP
13 #define MLPACK_METHODS_ANN_LAYER_CONVOLUTION_HPP
14 
15 #include <mlpack/prereqs.hpp>
16 
21 
22 #include "layer_types.hpp"
23 
24 namespace mlpack {
25 namespace ann {
26 
39 template <
40  typename ForwardConvolutionRule = NaiveConvolution<ValidConvolution>,
41  typename BackwardConvolutionRule = NaiveConvolution<FullConvolution>,
42  typename GradientConvolutionRule = NaiveConvolution<ValidConvolution>,
43  typename InputDataType = arma::mat,
44  typename OutputDataType = arma::mat
45 >
47 {
48 public:
50  Convolution();
51 
67  Convolution(const size_t inSize,
68  const size_t outSize,
69  const size_t kW,
70  const size_t kH,
71  const size_t dW = 1,
72  const size_t dH = 1,
73  const size_t padW = 0,
74  const size_t padH = 0,
75  const size_t inputWidth = 0,
76  const size_t inputHeight = 0);
77 
78  /*
79  * Set the weight and bias term.
80  */
81  void Reset();
82 
90  template<typename eT>
91  void Forward(const arma::Mat<eT>&& input, arma::Mat<eT>&& output);
92 
102  template<typename eT>
103  void Backward(const arma::Mat<eT>&& /* input */,
104  arma::Mat<eT>&& gy,
105  arma::Mat<eT>&& g);
106 
107  /*
108  * Calculate the gradient using the output delta and the input activation.
109  *
110  * @param input The input parameter used for calculating the gradient.
111  * @param error The calculated error.
112  * @param gradient The calculated gradient.
113  */
114  template<typename eT>
115  void Gradient(const arma::Mat<eT>&& /* input */,
116  arma::Mat<eT>&& error,
117  arma::Mat<eT>&& gradient);
118 
120  OutputDataType const& Parameters() const { return weights; }
122  OutputDataType& Parameters() { return weights; }
123 
125  InputDataType const& InputParameter() const { return inputParameter; }
127  InputDataType& InputParameter() { return inputParameter; }
128 
130  OutputDataType const& OutputParameter() const { return outputParameter; }
132  OutputDataType& OutputParameter() { return outputParameter; }
133 
135  OutputDataType const& Delta() const { return delta; }
137  OutputDataType& Delta() { return delta; }
138 
140  OutputDataType const& Gradient() const { return gradient; }
142  OutputDataType& Gradient() { return gradient; }
143 
145  size_t const& InputWidth() const { return inputWidth; }
147  size_t& InputWidth() { return inputWidth; }
148 
150  size_t const& InputHeight() const { return inputHeight; }
152  size_t& InputHeight() { return inputHeight; }
153 
155  size_t const& OutputWidth() const { return outputWidth; }
157  size_t& OutputWidth() { return outputWidth; }
158 
160  size_t const& OutputHeight() const { return outputHeight; }
162  size_t& OutputHeight() { return outputHeight; }
163 
167  template<typename Archive>
168  void Serialize(Archive& ar, const unsigned int /* version */);
169 
170  private:
171 
172  /*
173  * Return the convolution output size.
174  *
175  * @param size The size of the input (row or column).
176  * @param k The size of the filter (width or height).
177  * @param s The stride size (x or y direction).
178  * @param p The size of the padding (width or height).
179  * @return The convolution output size.
180  */
181  size_t ConvOutSize(const size_t size,
182  const size_t k,
183  const size_t s,
184  const size_t p)
185  {
186  return std::floor(size + p * 2 - k) / s + 1;
187  }
188 
189  /*
190  * Rotates a 3rd-order tensor counterclockwise by 180 degrees.
191  *
192  * @param input The input data to be rotated.
193  * @param output The rotated output.
194  */
195  template<typename eT>
196  void Rotate180(const arma::Cube<eT>& input, arma::Cube<eT>& output)
197  {
198  output = arma::Cube<eT>(input.n_rows, input.n_cols, input.n_slices);
199 
200  // * left-right flip, up-down flip */
201  for (size_t s = 0; s < output.n_slices; s++)
202  output.slice(s) = arma::fliplr(arma::flipud(input.slice(s)));
203  }
204 
205  /*
206  * Rotates a dense matrix counterclockwise by 180 degrees.
207  *
208  * @param input The input data to be rotated.
209  * @param output The rotated output.
210  */
211  template<typename eT>
212  void Rotate180(const arma::Mat<eT>& input, arma::Mat<eT>& output)
213  {
214  // * left-right flip, up-down flip */
215  output = arma::fliplr(arma::flipud(input));
216  }
217 
218  /*
219  * Pad the given input data.
220  *
221  * @param input The input to be padded.
222  * @param wPad Padding width of the input.
223  * @param hPad Padding height of the input.
224  * @param output The padded output data.
225  */
226  template<typename eT>
227  void Pad(const arma::Mat<eT>& input,
228  size_t wPad,
229  size_t hPad,
230  arma::Mat<eT>& output)
231  {
232  if (output.n_rows != input.n_rows + wPad * 2 ||
233  output.n_cols != input.n_cols + hPad * 2)
234  {
235  output = arma::zeros(input.n_rows + wPad * 2, input.n_cols + hPad * 2);
236  }
237 
238  output.submat(wPad, hPad, wPad + input.n_rows - 1,
239  hPad + input.n_cols - 1) = input;
240  }
241 
242  /*
243  * Pad the given input data.
244  *
245  * @param input The input to be padded.
246  * @param wPad Padding width of the input.
247  * @param hPad Padding height of the input.
248  * @param output The padded output data.
249  */
250  template<typename eT>
251  void Pad(const arma::Cube<eT>& input,
252  size_t wPad,
253  size_t hPad,
254  arma::Cube<eT>& output)
255  {
256  output = arma::zeros(input.n_rows + wPad * 2,
257  input.n_cols + hPad * 2, input.n_slices);
258 
259  for (size_t i = 0; i < input.n_slices; ++i)
260  {
261  Pad<double>(input.slice(i), wPad, hPad, output.slice(i));
262  }
263  }
264 
266  size_t inSize;
267 
269  size_t outSize;
270 
272  size_t kW;
273 
275  size_t kH;
276 
278  size_t dW;
279 
281  size_t dH;
282 
284  size_t padW;
285 
287  size_t padH;
288 
290  OutputDataType weights;
291 
293  arma::cube weight;
294 
296  arma::mat bias;
297 
299  size_t inputWidth;
300 
302  size_t inputHeight;
303 
305  size_t outputWidth;
306 
308  size_t outputHeight;
309 
311  arma::cube outputTemp;
312 
314  arma::cube inputTemp;
315 
317  arma::cube inputPaddedTemp;
318 
320  arma::cube gTemp;
321 
323  arma::cube gradientTemp;
324 
326  OutputDataType delta;
327 
329  OutputDataType gradient;
330 
332  InputDataType inputParameter;
333 
335  OutputDataType outputParameter;
336 }; // class Convolution
337 
338 } // namespace ann
339 } // namespace mlpack
340 
341 // Include implementation.
342 #include "convolution_impl.hpp"
343 
344 #endif
size_t outSize
Locally-stored number of output units.
size_t & OutputHeight()
Modify the output height.
OutputDataType gradient
Locally-stored gradient object.
OutputDataType const & Delta() const
Get the delta.
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: binarize.hpp:18
size_t ConvOutSize(const size_t size, const size_t k, const size_t s, const size_t p)
size_t & InputWidth()
Modify input the width.
OutputDataType const & Parameters() const
Get the parameters.
The core includes that mlpack expects; standard C++ includes and Armadillo.
arma::cube outputTemp
Locally-stored transformed output parameter.
Implementation of the Convolution class.
Definition: convolution.hpp:46
void Serialize(Archive &ar, const unsigned int)
Serialize the layer.
arma::cube inputPaddedTemp
Locally-stored transformed padded input parameter.
InputDataType & InputParameter()
Modify the input parameter.
size_t padH
Locally-stored padding height.
size_t inputHeight
Locally-stored input height.
size_t inSize
Locally-stored number of input units.
size_t kW
Locally-stored filter/kernel width.
size_t & OutputWidth()
Modify the output width.
size_t kH
Locally-stored filter/kernel height.
size_t padW
Locally-stored padding width.
void Pad(const arma::Mat< eT > &input, size_t wPad, size_t hPad, arma::Mat< eT > &output)
arma::mat bias
Locally-stored bias term object.
size_t const & OutputWidth() const
Get the output width.
OutputDataType delta
Locally-stored delta object.
arma::cube gTemp
Locally-stored transformed error parameter.
OutputDataType const & Gradient() const
Get the gradient.
void Pad(const arma::Cube< eT > &input, size_t wPad, size_t hPad, arma::Cube< eT > &output)
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 weight
Locally-stored weight object.
Convolution()
Create the Convolution object.
size_t const & InputWidth() const
Get the input width.
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 outputHeight
Locally-stored output height.
void Rotate180(const arma::Mat< eT > &input, arma::Mat< eT > &output)
OutputDataType outputParameter
Locally-stored output parameter object.
size_t const & OutputHeight() const
Get the output height.
OutputDataType & Delta()
Modify the delta.
size_t dW
Locally-stored stride of the filter in x-direction.
size_t inputWidth
Locally-stored input width.
arma::cube gradientTemp
Locally-stored transformed gradient parameter.
size_t const & InputHeight() const
Get the input height.
OutputDataType & Gradient()
Modify the gradient.
OutputDataType weights
Locally-stored weight object.
size_t outputWidth
Locally-stored output width.
OutputDataType & OutputParameter()
Modify the output parameter.
void Rotate180(const arma::Cube< eT > &input, arma::Cube< eT > &output)
OutputDataType & Parameters()
Modify the parameters.
size_t & InputHeight()
Modify the input height.
OutputDataType const & OutputParameter() const
Get the output parameter.
InputDataType const & InputParameter() const
Get the input parameter.
InputDataType inputParameter
Locally-stored input parameter object.
arma::cube inputTemp
Locally-stored transformed input parameter.
size_t dH
Locally-stored stride of the filter in y-direction.