Caffe框架源码剖析(5)—卷积层ConvolutionLayer

ConvolutionLayer是BaseConvolutionLayer的子类,功能较为简单。类中不包含成员变量,仅包含几个虚函数的实现。

conv_layer.hpp头文件的定义如下:

  1. template <typename Dtype>  
  2. class ConvolutionLayer : public BaseConvolutionLayer<Dtype> {  
  3.  public:  
  4.   explicit ConvolutionLayer(const LayerParameter& param)  
  5.       : BaseConvolutionLayer<Dtype>(param) {}  
  6.   virtual inline const char* type() const { return "Convolution"; }  
  7.   
  8.  protected:  
  9.   // CPU正向传导虚函数  
  10.   virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,  
  11.       const vector<Blob<Dtype>*>& top);  
  12.   // GPU正向传导虚函数  
  13.   virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,  
  14.       const vector<Blob<Dtype>*>& top);  
  15.   // CPU反向传导虚函数  
  16.   virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,  
  17.       const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);  
  18.   // GPU反向传导虚函数  
  19.   virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,  
  20.       const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);  
  21.   // 标明是卷积过程(反卷积层则return true)  
  22.   virtual inline bool reverse_dimensions() { return false; }  
  23.   // 计算卷积后输出尺寸虚函数  
  24.   virtual void compute_output_shape();  
  25. };  


conv_layer.cpp定义如下,

  1. // 计算卷积后输出尺寸(虚函数,被父类中的Reshape函数调用)  
  2. template <typename Dtype>  
  3. void ConvolutionLayer<Dtype>::compute_output_shape() {  
  4.   const int* kernel_shape_data = this->kernel_shape_.cpu_data();  
  5.   const int* stride_data = this->stride_.cpu_data();  
  6.   const int* pad_data = this->pad_.cpu_data();  
  7.   const int* dilation_data = this->dilation_.cpu_data();  
  8.   this->output_shape_.clear();  
  9.   for (int i = 0; i < this->num_spatial_axes_; ++i) {  
  10.     // i + 1 to skip channel axis  
  11.     const int input_dim = this->input_shape(i + 1);  
  12.     const int kernel_extent = dilation_data[i] * (kernel_shape_data[i] - 1) + 1;  
  13.     const int output_dim = (input_dim + 2 * pad_data[i] - kernel_extent)  
  14.         / stride_data[i] + 1;  
  15.     this->output_shape_.push_back(output_dim);  
  16.   }  
  17. }  
  18.   
  19. // CPU正向传导  
  20. template <typename Dtype>  
  21. void ConvolutionLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,  
  22.       const vector<Blob<Dtype>*>& top) {  
  23.   // blobs_[0]中存放的是卷积核  
  24.   const Dtype* weight = this->blobs_[0]->cpu_data();  
  25.   for (int i = 0; i < bottom.size(); ++i) {  
  26.     const Dtype* bottom_data = bottom[i]->cpu_data();  
  27.     Dtype* top_data = top[i]->mutable_cpu_data();  
  28.     for (int n = 0; n < this->num_; ++n) {  
  29.       // 调用父类函数进行卷积  
  30.       this->forward_cpu_gemm(bottom_data + n * this->bottom_dim_, weight,  
  31.           top_data + n * this->top_dim_);  
  32.       // 如果含有偏置项,则加上偏置项  
  33.       if (this->bias_term_) {  
  34.         // blobs_[1]中存放的是偏置项  
  35.         const Dtype* bias = this->blobs_[1]->cpu_data();  
  36.         this->forward_cpu_bias(top_data + n * this->top_dim_, bias);  
  37.       }  
  38.     }  
  39.   }  
  40. }  
  41.   
  42. // CPU反向传导  
  43. template <typename Dtype>  
  44. void ConvolutionLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,  
  45.       const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {  
  46.   const Dtype* weight = this->blobs_[0]->cpu_data();  
  47.   Dtype* weight_diff = this->blobs_[0]->mutable_cpu_diff();  
  48.   for (int i = 0; i < top.size(); ++i) {  
  49.     const Dtype* top_diff = top[i]->cpu_diff();  
  50.     const Dtype* bottom_data = bottom[i]->cpu_data();  
  51.     Dtype* bottom_diff = bottom[i]->mutable_cpu_diff();  
  52.     // 计算偏置的偏导  
  53.     if (this->bias_term_ && this->param_propagate_down_[1]) {  
  54.       Dtype* bias_diff = this->blobs_[1]->mutable_cpu_diff();  
  55.       for (int n = 0; n < this->num_; ++n) {  
  56.         this->backward_cpu_bias(bias_diff, top_diff + n * this->top_dim_);  
  57.       }  
  58.     }  
  59.     if (this->param_propagate_down_[0] || propagate_down[i]) {  
  60.       for (int n = 0; n < this->num_; ++n) {  
  61.         // bottom_data * top_diff -> weight_diff  
  62.         if (this->param_propagate_down_[0]) {  
  63.           this->weight_cpu_gemm(bottom_data + n * this->bottom_dim_,  
  64.               top_diff + n * this->top_dim_, weight_diff);  
  65.         }  
  66.         // top_diff * weight -> bottom_diff  
  67.         if (propagate_down[i]) {  
  68.           this->backward_cpu_gemm(top_diff + n * this->top_dim_, weight,  
  69.               bottom_diff + n * this->bottom_dim_);  
  70.         }  
  71.       }  
  72.     }  
  73.   }  
  74. }  
  75.   
  76. // 如果CPU_ONLY模式则禁止Forward_gpu和Backward_gpu函数  
  77. #ifdef CPU_ONLY  
  78. STUB_GPU(ConvolutionLayer);  
  79. #endif 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值