Caffe源码(五):conv_layer 分析
2015-08-14 18:23
423 查看
目录
目录简单介绍
主要函数
compute_output_shape 函数
Forward_cpu 函数
Backward_cpu 函数
简单介绍
首先要明确的一点是: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数据计算导数(传给下一层) } } } }
相关文章推荐
- ASP.NET MVC 中使用JavaScriptResult
- js判断对象的类型
- js的闭包
- 深度学习工具箱和Vlfeat库的配置及使用
- Caffe源码(四):base_conv_layer 分析
- 利用filter 过滤掉html标签
- 【JavaScript】BOM总结
- codeforces 232D. Fence
- css控制UL LI 的样式详解(推荐)
- JavaScript之Window对象
- 解决跨域访问(服务器端、Js端)
- Notepad++巨大灾难!史诗级错误~
- JavaScript中一些常用API使用方法简析
- JavaScript——概述
- 《Node即学即用》—— 读后总结
- 史上最全的CSS hack方式一览
- Javascript面向对象之继承
- BZOJ 题目1211: [HNOI2004]树的计数(组合数学,prufer)
- 如何在JavaScript各种各样的上下文中确定this指的是什么?
- SSH中的ajax jquery配置(注册验证用户名是否存在)