1. 程式人生 > >Caffe原始碼(五):conv_layer 分析

Caffe原始碼(五):conv_layer 分析

目錄

簡單介紹

首先要明確的一點是:ConvolutionLayer 是 BaseConvolutionLayer的子類,BaseConvolutionLayer 是 Layer 的子類。ConvolutionLayer 除了繼承了相應的成員變數和函式以外,自己的成員函式主要有:compute_output_shape,Forward_cpu,Backward_cpu 。

主要函式

1. compute_output_shape 函式:

計算輸出feature map 的shape。

template <typename Dtype>
void ConvolutionLayer<Dtype>::compute_output_shape() {
  this
->height_out_ = (this->height_ + 2 * this->pad_h_ - this->kernel_h_) / this->stride_h_ + 1; //輸出feature map 的 height this->width_out_ = (this->width_ + 2 * this->pad_w_ - this->kernel_w_) / this->stride_w_ + 1; //輸出 feature map 的 width }

2.Forward_cpu 函式:

該函式在Layer 中宣告,實現前向傳播功能。

template <typename Dtype>
void ConvolutionLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top) {
  const Dtype* weight = this->blobs_[0]->cpu_data();
   //blobs_ 用來儲存可學習的引數blobs_[0] 是weight,blobs_[1]是bias
for (int i = 0; i < bottom.size(); ++i) { //這裡的i為輸入bottom的個數,輸入多少個bottom就產生相應個數的輸出 top。 const Dtype* bottom_data = bottom[i]->cpu_data(); Dtype* top_data = top[i]->mutable_cpu_data(); for (int n = 0; n < this->num_; ++n) { this->forward_cpu_gemm(bottom_data + bottom[i]->offset(n), weight, top_data + top[i]->offset(n));//計算卷積操作之後的輸出 if (this->bias_term_) { const Dtype* bias = this->blobs_[1]->cpu_data(); this->forward_cpu_bias(top_data + top[i]->offset(n), bias); }//加上bias } } }

Layer的建構函式

explicit Layer(const LayerParameter& param)
    : layer_param_(param) {
      // Set phase and copy blobs (if there are any).
      phase_ = param.phase();
      if (layer_param_.blobs_size() > 0) {
        blobs_.resize(layer_param_.blobs_size());
        for (int i = 0; i < layer_param_.blobs_size(); ++i) {
          blobs_[i].reset(new Blob<Dtype>());
          blobs_[i]->FromProto(layer_param_.blobs(i));
        }
      }
    }//用從protobuf 讀入message LayerParameter 中的blobs 初始化 blobs_ 
     //blobs_定義:vector<shared_ptr<Blob<Dtype> > > blobs_

3.Backward_cpu 函式

實現反向傳播,根據上一層傳下來的導數計算相應的bottom data , weight, bias 的導數

template <typename Dtype>
void ConvolutionLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
      const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
  const Dtype* weight = this->blobs_[0]->cpu_data();
  Dtype* weight_diff = this->blobs_[0]->mutable_cpu_diff();
  if (this->param_propagate_down_[0]) {
    caffe_set(this->blobs_[0]->count(), Dtype(0), weight_diff);
  }
  if (this->bias_term_ && this->param_propagate_down_[1]) {
    caffe_set(this->blobs_[1]->count(), Dtype(0),
        this->blobs_[1]->mutable_cpu_diff());
  }
  for (int i = 0; i < top.size(); ++i) {
    const Dtype* top_diff = top[i]->cpu_diff();//上一層傳下來的導數
    const Dtype* bottom_data = bottom[i]->cpu_data();
    Dtype* bottom_diff = bottom[i]->mutable_cpu_diff();//傳給下一層的導數
    // Bias gradient, if necessary.
    if (this->bias_term_ && this->param_propagate_down_[1]) {
      Dtype* bias_diff = this->blobs_[1]->mutable_cpu_diff();
      for (int n = 0; n < this->num_; ++n) {
        this->backward_cpu_bias(bias_diff, top_diff + top[i]->offset(n));
      }
    }
    if (this->param_propagate_down_[0] || propagate_down[i]) {
      for (int n = 0; n < this->num_; ++n) {
        // gradient w.r.t. weight. Note that we will accumulate diffs.
        if (this->param_propagate_down_[0]) {
          this->weight_cpu_gemm(bottom_data + bottom[i]->offset(n),
              top_diff + top[i]->offset(n), weight_diff);
        }//對weight 計算導數(用來更新weight)
        // gradient w.r.t. bottom data, if necessary.
        if (propagate_down[i]) {
          this->backward_cpu_gemm(top_diff + top[i]->offset(n), weight,
              bottom_diff + bottom[i]->offset(n));
        }//對bottom資料計算導數(傳給下一層)
      }
    }
  }
}