天天看点

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 对比来看,实现的功能是互逆的。

特别是两者在前向运算与后向计算局部梯度 相互对偶(使用同样的函数),而偏移量的计算方式一致。

继续阅读