mlpack 3.4.2
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
22
23#include "layer_types.hpp"
24#include "padding.hpp"
25
26namespace mlpack {
27namespace ann {
28
41template <
42 typename ForwardConvolutionRule = NaiveConvolution<ValidConvolution>,
43 typename BackwardConvolutionRule = NaiveConvolution<FullConvolution>,
44 typename GradientConvolutionRule = NaiveConvolution<ValidConvolution>,
45 typename InputDataType = arma::mat,
46 typename OutputDataType = arma::mat
47>
49{
50 public:
53
70 Convolution(const size_t inSize,
71 const size_t outSize,
72 const size_t kernelWidth,
73 const size_t kernelHeight,
74 const size_t strideWidth = 1,
75 const size_t strideHeight = 1,
76 const size_t padW = 0,
77 const size_t padH = 0,
78 const size_t inputWidth = 0,
79 const size_t inputHeight = 0,
80 const std::string& paddingType = "None");
81
102 Convolution(const size_t inSize,
103 const size_t outSize,
104 const size_t kernelWidth,
105 const size_t kernelHeight,
106 const size_t strideWidth,
107 const size_t strideHeight,
108 const std::tuple<size_t, size_t>& padW,
109 const std::tuple<size_t, size_t>& padH,
110 const size_t inputWidth = 0,
111 const size_t inputHeight = 0,
112 const std::string& paddingType = "None");
113
114 /*
115 * Set the weight and bias term.
116 */
117 void Reset();
118
126 template<typename eT>
127 void Forward(const arma::Mat<eT>& input, arma::Mat<eT>& output);
128
138 template<typename eT>
139 void Backward(const arma::Mat<eT>& /* input */,
140 const arma::Mat<eT>& gy,
141 arma::Mat<eT>& g);
142
143 /*
144 * Calculate the gradient using the output delta and the input activation.
145 *
146 * @param input The input parameter used for calculating the gradient.
147 * @param error The calculated error.
148 * @param gradient The calculated gradient.
149 */
150 template<typename eT>
151 void Gradient(const arma::Mat<eT>& /* input */,
152 const arma::Mat<eT>& error,
153 arma::Mat<eT>& gradient);
154
156 OutputDataType const& Parameters() const { return weights; }
158 OutputDataType& Parameters() { return weights; }
159
161 arma::cube const& Weight() const { return weight; }
163 arma::cube& Weight() { return weight; }
164
166 arma::mat const& Bias() const { return bias; }
168 arma::mat& Bias() { return bias; }
169
171 InputDataType const& InputParameter() const { return inputParameter; }
173 InputDataType& InputParameter() { return inputParameter; }
174
176 OutputDataType const& OutputParameter() const { return outputParameter; }
178 OutputDataType& OutputParameter() { return outputParameter; }
179
181 OutputDataType const& Delta() const { return delta; }
183 OutputDataType& Delta() { return delta; }
184
186 OutputDataType const& Gradient() const { return gradient; }
188 OutputDataType& Gradient() { return gradient; }
189
191 size_t InputWidth() const { return inputWidth; }
193 size_t& InputWidth() { return inputWidth; }
194
196 size_t InputHeight() const { return inputHeight; }
198 size_t& InputHeight() { return inputHeight; }
199
201 size_t OutputWidth() const { return outputWidth; }
203 size_t& OutputWidth() { return outputWidth; }
204
206 size_t OutputHeight() const { return outputHeight; }
208 size_t& OutputHeight() { return outputHeight; }
209
211 size_t InputSize() const { return inSize; }
212
214 size_t OutputSize() const { return outSize; }
215
217 size_t KernelWidth() const { return kernelWidth; }
219 size_t& KernelWidth() { return kernelWidth; }
220
222 size_t KernelHeight() const { return kernelHeight; }
224 size_t& KernelHeight() { return kernelHeight; }
225
227 size_t StrideWidth() const { return strideWidth; }
229 size_t& StrideWidth() { return strideWidth; }
230
232 size_t StrideHeight() const { return strideHeight; }
234 size_t& StrideHeight() { return strideHeight; }
235
237 size_t PadHTop() const { return padHTop; }
239 size_t& PadHTop() { return padHTop; }
240
242 size_t PadHBottom() const { return padHBottom; }
244 size_t& PadHBottom() { return padHBottom; }
245
247 size_t PadWLeft() const { return padWLeft; }
249 size_t& PadWLeft() { return padWLeft; }
250
252 size_t PadWRight() const { return padWRight; }
254 size_t& PadWRight() { return padWRight; }
255
257 size_t WeightSize() const
258 {
259 return (outSize * inSize * kernelWidth * kernelHeight) + outSize;
260 }
261
265 template<typename Archive>
266 void serialize(Archive& ar, const unsigned int /* 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 * @return The convolution output size.
278 */
279 size_t ConvOutSize(const size_t size,
280 const size_t k,
281 const size_t s,
282 const size_t pSideOne,
283 const size_t pSideTwo)
284 {
285 return std::floor(size + pSideOne + pSideTwo - k) / s + 1;
286 }
287
288 /*
289 * Function to assign padding such that output size is same as input size.
290 */
291 void InitializeSamePadding();
292
293 /*
294 * Rotates a 3rd-order tensor counterclockwise by 180 degrees.
295 *
296 * @param input The input data to be rotated.
297 * @param output The rotated output.
298 */
299 template<typename eT>
300 void Rotate180(const arma::Cube<eT>& input, arma::Cube<eT>& output)
301 {
302 output = arma::Cube<eT>(input.n_rows, input.n_cols, input.n_slices);
303
304 // * left-right flip, up-down flip */
305 for (size_t s = 0; s < output.n_slices; s++)
306 output.slice(s) = arma::fliplr(arma::flipud(input.slice(s)));
307 }
308
309 /*
310 * Rotates a dense matrix counterclockwise by 180 degrees.
311 *
312 * @param input The input data to be rotated.
313 * @param output The rotated output.
314 */
315 template<typename eT>
316 void Rotate180(const arma::Mat<eT>& input, arma::Mat<eT>& output)
317 {
318 // * left-right flip, up-down flip */
319 output = arma::fliplr(arma::flipud(input));
320 }
321
323 size_t inSize;
324
326 size_t outSize;
327
329 size_t batchSize;
330
332 size_t kernelWidth;
333
335 size_t kernelHeight;
336
338 size_t strideWidth;
339
341 size_t strideHeight;
342
344 size_t padWLeft;
345
347 size_t padWRight;
348
350 size_t padHBottom;
351
353 size_t padHTop;
354
356 OutputDataType weights;
357
359 arma::cube weight;
360
362 arma::mat bias;
363
365 size_t inputWidth;
366
368 size_t inputHeight;
369
371 size_t outputWidth;
372
374 size_t outputHeight;
375
377 arma::cube outputTemp;
378
380 arma::cube inputPaddedTemp;
381
383 arma::cube gTemp;
384
386 arma::cube gradientTemp;
387
389 ann::Padding<> padding;
390
392 OutputDataType delta;
393
395 OutputDataType gradient;
396
398 InputDataType inputParameter;
399
401 OutputDataType outputParameter;
402}; // class Convolution
403
404} // namespace ann
405} // namespace mlpack
406
408namespace boost {
409namespace serialization {
410
411template<
412 typename ForwardConvolutionRule,
413 typename BackwardConvolutionRule,
414 typename GradientConvolutionRule,
415 typename InputDataType,
416 typename OutputDataType
417>
418struct version<
419 mlpack::ann::Convolution<ForwardConvolutionRule, BackwardConvolutionRule,
420 GradientConvolutionRule, InputDataType, OutputDataType> >
421{
422 BOOST_STATIC_CONSTANT(int, value = 1);
423};
424
425} // namespace serialization
426} // namespace boost
427
428// Include implementation.
429#include "convolution_impl.hpp"
430
431#endif
Implementation of the Convolution class.
Definition: convolution.hpp:49
arma::cube const & Weight() const
Get the weight of the layer.
OutputDataType const & Delta() const
Get the delta.
size_t StrideHeight() const
Get the stride height.
OutputDataType const & Parameters() const
Get the parameters.
size_t & PadHTop()
Modify the top padding height.
size_t & InputHeight()
Modify the input height.
size_t PadWLeft() const
Get the left padding width.
size_t InputWidth() const
Get the input width.
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()
Modify the weight of the layer.
InputDataType & InputParameter()
Modify the input parameter.
OutputDataType const & OutputParameter() const
Get the output parameter.
size_t & PadHBottom()
Modify the bottom padding height.
size_t InputSize() const
Get the input size.
size_t & InputWidth()
Modify input the width.
size_t KernelWidth() const
Get the kernel width.
Convolution()
Create the Convolution object.
size_t KernelHeight() const
Get the kernel height.
Convolution(const size_t inSize, const size_t outSize, const size_t kernelWidth, const size_t kernelHeight, const size_t strideWidth=1, const size_t strideHeight=1, const size_t padW=0, const size_t padH=0, const size_t inputWidth=0, const size_t inputHeight=0, const std::string &paddingType="None")
Create the Convolution object using the specified number of input maps, output maps,...
size_t & StrideWidth()
Modify the stride width.
size_t PadHBottom() const
Get the bottom padding height.
size_t WeightSize() const
Get size of weights for the layer.
arma::mat const & Bias() const
Get the bias of the layer.
size_t & KernelWidth()
Modify the kernel width.
size_t PadWRight() const
Get the right padding width.
Convolution(const size_t inSize, const size_t outSize, const size_t kernelWidth, const size_t kernelHeight, const size_t strideWidth, const size_t strideHeight, const std::tuple< size_t, size_t > &padW, const std::tuple< size_t, size_t > &padH, const size_t inputWidth=0, const size_t inputHeight=0, const std::string &paddingType="None")
Create the Convolution object using the specified number of input maps, output maps,...
size_t & OutputHeight()
Modify the output height.
size_t & PadWRight()
Modify the right padding width.
size_t OutputHeight() const
Get the output height.
size_t OutputSize() const
Get the output size.
void Gradient(const arma::Mat< eT > &, const arma::Mat< eT > &error, arma::Mat< eT > &gradient)
size_t OutputWidth() const
Get the output width.
OutputDataType const & Gradient() const
Get the gradient.
InputDataType const & InputParameter() const
Get the input parameter.
size_t InputHeight() const
Get the input height.
OutputDataType & Gradient()
Modify the gradient.
size_t & KernelHeight()
Modify the kernel height.
size_t & PadWLeft()
Modify the left padding width.
size_t StrideWidth() const
Get the stride width.
size_t & StrideHeight()
Modify the stride height.
void Backward(const arma::Mat< eT > &, const 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...
OutputDataType & OutputParameter()
Modify the output parameter.
size_t & OutputWidth()
Modify the output width.
size_t PadHTop() const
Get the top padding height.
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
OutputDataType & Parameters()
Modify the parameters.
arma::mat & Bias()
Modify the bias of the layer.
OutputDataType & Delta()
Modify the delta.
Set the serialization version of the adaboost class.
Definition: adaboost.hpp:198
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
The core includes that mlpack expects; standard C++ includes and Armadillo.