Caffe基本类Blob,Layer,Net
- 可以有多个bottom输出一个top,如concat层。
前向传播源码:
//前向传播
template <typename Dtype>
void ConvolutionLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
//blobs_[0]保存权值, blobs_[1]保存偏置
const Dtype* weight = this->blobs_[0]->cpu_data();
//bottom.size()是bottom中blob的数量,等于top中blob的数量,一般情况下为1
for (int i = 0; i < bottom.size(); ++i) {
//获取输入,输出数据指针
const Dtype* bottom_data = bottom[i]->cpu_data();
Dtype* top_data = top[i]->mutable_cpu_data();
//第n张图片
for (int n = 0; n < this->num_; ++n) {
//卷积操作,采用矩阵乘积实现
// bottom_dim_ =3*28*28
// kernel = 20*5*5
// top_dim_ = 20*24*24
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_[1]->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_[0]->cpu_data();
Dtype* weight_diff = this->blobs_[0]->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 + n * this->top_dim_);
}
}
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.
// 对weight 计算导数(用来更新weight)
// /将下一层残差与weight进行相关计算,得到卷积层的残差
if (this->param_propagate_down_[0]) {
this->weight_cpu_gemm(bottom_data + n * this->bottom_dim_,
top_diff + n * this->top_dim_, weight_diff);
}
// gradient w.r.t. bottom data, if necessary.
// 对bottom数据计算导数(传给下一层)
// bottom_data与top_diff做相关计算,得到w权值更新量
if (propagate_down[i]) {
this->backward_cpu_gemm(top_diff + n * this->top_dim_, weight,
bottom_diff + n * this->bottom_dim_);
}
}
}
}
}
实战手写卷积:
《 Implementing convolution as a matrix multiplication》
参考链接:https://buptldy.github.io/2016/10/01/2016-10-01-im2col/
这里有一份很好的教程,准备有空的时候练一练。