1 #ifndef CAFFE_BASE_CONVOLUTION_LAYER_HPP_ 2 #define CAFFE_BASE_CONVOLUTION_LAYER_HPP_ 6 #include "caffe/blob.hpp" 7 #include "caffe/layer.hpp" 8 #include "caffe/proto/caffe.pb.h" 9 #include "caffe/util/im2col.hpp" 17 template <
typename Dtype>
35 void forward_cpu_gemm(
const Dtype* input,
const Dtype* weights,
36 Dtype* output,
bool skip_im2col =
false);
37 void forward_cpu_bias(Dtype* output,
const Dtype* bias);
38 void backward_cpu_gemm(
const Dtype* input,
const Dtype* weights,
40 void weight_cpu_gemm(
const Dtype* input,
const Dtype* output, Dtype*
42 void backward_cpu_bias(Dtype* bias,
const Dtype* input);
45 void forward_gpu_gemm(
const Dtype* col_input,
const Dtype* weights,
46 Dtype* output,
bool skip_im2col =
false);
47 void forward_gpu_bias(Dtype* output,
const Dtype* bias);
48 void backward_gpu_gemm(
const Dtype* input,
const Dtype* weights,
50 void weight_gpu_gemm(
const Dtype* col_input,
const Dtype* output, Dtype*
52 void backward_gpu_bias(Dtype* bias,
const Dtype* input);
57 return (*bottom_shape_)[channel_axis_ + i];
61 virtual bool reverse_dimensions() = 0;
63 virtual void compute_output_shape() = 0;
79 const vector<int>* bottom_shape_;
81 int num_spatial_axes_;
94 bool force_nd_im2col_;
98 inline void conv_im2col_cpu(
const Dtype* data, Dtype* col_buff) {
99 if (!force_nd_im2col_ && num_spatial_axes_ == 2) {
100 im2col_cpu(data, conv_in_channels_,
101 conv_input_shape_.cpu_data()[1], conv_input_shape_.cpu_data()[2],
102 kernel_shape_.cpu_data()[0], kernel_shape_.cpu_data()[1],
103 pad_.cpu_data()[0], pad_.cpu_data()[1],
104 stride_.cpu_data()[0], stride_.cpu_data()[1],
105 dilation_.cpu_data()[0], dilation_.cpu_data()[1], col_buff);
107 im2col_nd_cpu(data, num_spatial_axes_, conv_input_shape_.cpu_data(),
108 col_buffer_shape_.data(), kernel_shape_.cpu_data(),
109 pad_.cpu_data(), stride_.cpu_data(), dilation_.cpu_data(), col_buff);
112 inline void conv_col2im_cpu(
const Dtype* col_buff, Dtype* data) {
113 if (!force_nd_im2col_ && num_spatial_axes_ == 2) {
114 col2im_cpu(col_buff, conv_in_channels_,
115 conv_input_shape_.cpu_data()[1], conv_input_shape_.cpu_data()[2],
116 kernel_shape_.cpu_data()[0], kernel_shape_.cpu_data()[1],
117 pad_.cpu_data()[0], pad_.cpu_data()[1],
118 stride_.cpu_data()[0], stride_.cpu_data()[1],
119 dilation_.cpu_data()[0], dilation_.cpu_data()[1], data);
121 col2im_nd_cpu(col_buff, num_spatial_axes_, conv_input_shape_.cpu_data(),
122 col_buffer_shape_.data(), kernel_shape_.cpu_data(),
123 pad_.cpu_data(), stride_.cpu_data(), dilation_.cpu_data(), data);
127 inline void conv_im2col_gpu(
const Dtype* data, Dtype* col_buff) {
128 if (!force_nd_im2col_ && num_spatial_axes_ == 2) {
129 im2col_gpu(data, conv_in_channels_,
130 conv_input_shape_.cpu_data()[1], conv_input_shape_.cpu_data()[2],
131 kernel_shape_.cpu_data()[0], kernel_shape_.cpu_data()[1],
132 pad_.cpu_data()[0], pad_.cpu_data()[1],
133 stride_.cpu_data()[0], stride_.cpu_data()[1],
134 dilation_.cpu_data()[0], dilation_.cpu_data()[1], col_buff);
136 im2col_nd_gpu(data, num_spatial_axes_, num_kernels_im2col_,
137 conv_input_shape_.gpu_data(), col_buffer_.gpu_shape(),
138 kernel_shape_.gpu_data(), pad_.gpu_data(),
139 stride_.gpu_data(), dilation_.gpu_data(), col_buff);
142 inline void conv_col2im_gpu(
const Dtype* col_buff, Dtype* data) {
143 if (!force_nd_im2col_ && num_spatial_axes_ == 2) {
144 col2im_gpu(col_buff, conv_in_channels_,
145 conv_input_shape_.cpu_data()[1], conv_input_shape_.cpu_data()[2],
146 kernel_shape_.cpu_data()[0], kernel_shape_.cpu_data()[1],
147 pad_.cpu_data()[0], pad_.cpu_data()[1],
148 stride_.cpu_data()[0], stride_.cpu_data()[1],
149 dilation_.cpu_data()[0], dilation_.cpu_data()[1], data);
151 col2im_nd_gpu(col_buff, num_spatial_axes_, num_kernels_col2im_,
152 conv_input_shape_.gpu_data(), col_buffer_.gpu_shape(),
153 kernel_shape_.gpu_data(), pad_.gpu_data(), stride_.gpu_data(),
154 dilation_.gpu_data(), data);
159 int num_kernels_im2col_;
160 int num_kernels_col2im_;
161 int conv_out_channels_;
162 int conv_in_channels_;
163 int conv_out_spatial_dim_;
174 #endif // CAFFE_BASE_CONVOLUTION_LAYER_HPP_ Blob< int > kernel_shape_
The spatial dimensions of a filter kernel.
Definition: base_conv_layer.hpp:66
An interface for the units of computation which can be composed into a Net.
Definition: layer.hpp:33
A layer factory that allows one to register layers. During runtime, registered layers can be called b...
Definition: blob.hpp:14
int input_shape(int i)
The spatial dimensions of the input.
Definition: base_conv_layer.hpp:56
virtual void LayerSetUp(const vector< Blob< Dtype > *> &bottom, const vector< Blob< Dtype > *> &top)
Does layer-specific setup: your layer should implement this function as well as Reshape.
Definition: base_conv_layer.cpp:12
Abstract base class that factors out the BLAS code common to ConvolutionLayer and DeconvolutionLayer...
Definition: base_conv_layer.hpp:18
virtual bool EqualNumBottomTopBlobs() const
Returns true if the layer requires an equal number of bottom and top blobs.
Definition: base_conv_layer.hpp:29
virtual int MinBottomBlobs() const
Returns the minimum number of bottom blobs required by the layer, or -1 if no minimum number is requi...
Definition: base_conv_layer.hpp:27
Blob< int > pad_
The spatial dimensions of the padding.
Definition: base_conv_layer.hpp:70
virtual int MinTopBlobs() const
Returns the minimum number of top blobs required by the layer, or -1 if no minimum number is required...
Definition: base_conv_layer.hpp:28
vector< int > col_buffer_shape_
The spatial dimensions of the col_buffer.
Definition: base_conv_layer.hpp:76
vector< int > output_shape_
The spatial dimensions of the output.
Definition: base_conv_layer.hpp:78
virtual void Reshape(const vector< Blob< Dtype > *> &bottom, const vector< Blob< Dtype > *> &top)
Adjust the shapes of top blobs and internal buffers to accommodate the shapes of the bottom blobs...
Definition: base_conv_layer.cpp:185
Blob< int > conv_input_shape_
The spatial dimensions of the convolution input.
Definition: base_conv_layer.hpp:74
Blob< int > stride_
The spatial dimensions of the stride.
Definition: base_conv_layer.hpp:68
A wrapper around SyncedMemory holders serving as the basic computational unit through which Layers...
Definition: blob.hpp:24
Blob< int > dilation_
The spatial dimensions of the dilation.
Definition: base_conv_layer.hpp:72