天天看點

caffe之deconv

卷積與反卷積的直覺感受:

卷積:

caffe之deconv

反卷積:

caffe之deconv

特别說明,此處分析的反卷積主要針對 caffe中DeconvolutionLayer類别來說。

反向傳播分析,參考部落格:

https://grzegorzgwardys.wordpress.com/2016/04/22/8/

http://www.jefkine.com/general/2016/09/05/backpropagation-in-convolutional-neural-networks/

http://www.cnblogs.com/tornadomeet/p/3468450.html

http://blog.csdn.net/ck1798333105/article/details/52369122

http://www.cnblogs.com/pinard/p/6494810.html

反向傳播結論: L層局部梯度 δ =L+1層局部梯度*(卷積)核的轉秩

卷積核梯度 ▽W=輸入*(卷積)本層局部梯度

卷積及反向傳播

要想了解反卷積就要首先了解卷積的反向傳播方式,因為他們之間有着千絲萬縷的聯系。

卷積之反向傳播

見代碼如下:

/
//                                 正向傳播
/
template <typename Dtype>
void ConvolutionLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top) {
  const Dtype* weight = this->blobs_[]->cpu_data();
  for (int i = ; i < bottom.size(); ++i) {
    const Dtype* bottom_data = bottom[i]->cpu_data();
    Dtype* top_data = top[i]->mutable_cpu_data();
    for (int n = ; n < this->num_; ++n) {
      ///forward_cpu_gemm 實作卷積運算
      //  bottom_data*weight=top_data
      this->forward_cpu_gemm(bottom_data + n * this->bottom_dim_, weight,
          top_data + n * this->top_dim_);
      if (this->bias_term_) {
        const Dtype* bias = this->blobs_[]->cpu_data();
        this->forward_cpu_bias(top_data + n * this->top_dim_, 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_[]->cpu_data();
  Dtype* weight_diff = this->blobs_[]->mutable_cpu_diff();
  for (int i = ; 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_[]) {
      Dtype* bias_diff = this->blobs_[]->mutable_cpu_diff();
      for (int n = ; n < this->num_; ++n) {
      //  計算▽b,偏移量梯度
        this->backward_cpu_bias(bias_diff, top_diff + n * this->top_dim_);
      }
    }
    if (this->param_propagate_down_[] || propagate_down[i]) {
      for (int n = ; n < this->num_; ++n) {
        // gradient w.r.t. weight. Note that we will accumulate diffs.
        if (this->param_propagate_down_[]) {     
          this->weight_cpu_gemm(bottom_data + n * this->bottom_dim_,        //計算 ▽W ,權重梯度
              top_diff + n * this->top_dim_, weight_diff);
        }
        // gradient w.r.t. bottom data, if necessary.
        if (propagate_down[i]) {
   重點,backward_cpu_gemm 實作矩陣局部梯度域反向計算
   top_diff*weight^T(轉制)=bottom_diff 實質上還是卷積運算
          this->backward_cpu_gemm(top_diff + n * this->top_dim_, weight,  //計算局部梯度δ
              bottom_diff + n * this->bottom_dim_);
        }
      }
    }
  }
}
           
caffe之deconv

從convlayer 與 deconvlayer 對比來看,實作的功能是互逆的。

特别是兩者在前向運算與後向計算局部梯度 互相對偶(使用同樣的函數),而偏移量的計算方式一緻。

繼續閱讀