conv_layer.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #ifndef CAFFE_CONV_LAYER_HPP_
  2. #define CAFFE_CONV_LAYER_HPP_
  3. #include <vector>
  4. #include "caffe/blob.hpp"
  5. #include "caffe/layer.hpp"
  6. #include "caffe/proto/caffe.pb.h"
  7. #include "caffe/layers/base_conv_layer.hpp"
  8. namespace caffe {
  9. /**
  10. * @brief Convolves the input image with a bank of learned filters,
  11. * and (optionally) adds biases.
  12. *
  13. * Caffe convolves by reduction to matrix multiplication. This achieves
  14. * high-throughput and generality of input and filter dimensions but comes at
  15. * the cost of memory for matrices. This makes use of efficiency in BLAS.
  16. *
  17. * The input is "im2col" transformed to a channel K' x H x W data matrix
  18. * for multiplication with the N x K' x H x W filter matrix to yield a
  19. * N' x H x W output matrix that is then "col2im" restored. K' is the
  20. * input channel * kernel height * kernel width dimension of the unrolled
  21. * inputs so that the im2col matrix has a column for each input region to
  22. * be filtered. col2im restores the output spatial structure by rolling up
  23. * the output channel N' columns of the output matrix.
  24. */
  25. template <typename Dtype>
  26. class ConvolutionLayer : public BaseConvolutionLayer<Dtype> {
  27. public:
  28. /**
  29. * @param param provides ConvolutionParameter convolution_param,
  30. * with ConvolutionLayer options:
  31. * - num_output. The number of filters.
  32. * - kernel_size / kernel_h / kernel_w. The filter dimensions, given by
  33. * kernel_size for square filters or kernel_h and kernel_w for rectangular
  34. * filters.
  35. * - stride / stride_h / stride_w (\b optional, default 1). The filter
  36. * stride, given by stride_size for equal dimensions or stride_h and stride_w
  37. * for different strides. By default the convolution is dense with stride 1.
  38. * - pad / pad_h / pad_w (\b optional, default 0). The zero-padding for
  39. * convolution, given by pad for equal dimensions or pad_h and pad_w for
  40. * different padding. Input padding is computed implicitly instead of
  41. * actually padding.
  42. * - dilation (\b optional, default 1). The filter
  43. * dilation, given by dilation_size for equal dimensions for different
  44. * dilation. By default the convolution has dilation 1.
  45. * - group (\b optional, default 1). The number of filter groups. Group
  46. * convolution is a method for reducing parameterization by selectively
  47. * connecting input and output channels. The input and output channel dimensions must be divisible
  48. * by the number of groups. For group @f$ \geq 1 @f$, the
  49. * convolutional filters' input and output channels are separated s.t. each
  50. * group takes 1 / group of the input channels and makes 1 / group of the
  51. * output channels. Concretely 4 input channels, 8 output channels, and
  52. * 2 groups separate input channels 1-2 and output channels 1-4 into the
  53. * first group and input channels 3-4 and output channels 5-8 into the second
  54. * group.
  55. * - bias_term (\b optional, default true). Whether to have a bias.
  56. * - engine: convolution has CAFFE (matrix multiplication) and CUDNN (library
  57. * kernels + stream parallelism) engines.
  58. */
  59. explicit ConvolutionLayer(const LayerParameter& param)
  60. : BaseConvolutionLayer<Dtype>(param) {}
  61. virtual inline const char* type() const { return "Convolution"; }
  62. protected:
  63. virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
  64. const vector<Blob<Dtype>*>& top);
  65. virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
  66. const vector<Blob<Dtype>*>& top);
  67. virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
  68. const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
  69. virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
  70. const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
  71. virtual inline bool reverse_dimensions() { return false; }
  72. virtual void compute_output_shape();
  73. };
  74. } // namespace caffe
  75. #endif // CAFFE_CONV_LAYER_HPP_