caffe卷积层代码阅读笔记
卷积的实现思想:
- 通过im2col将image转为一个matrix,将卷积操作转为矩阵乘法运算
- 通过调用GEMM完毕运算操作
- 以下两个图是我在知乎中发现的,“盗”用一下,确实非常好。能帮助理解。
參数剖析
配置參数:(从配置文件得来)
kernel_h_ pad_h_ hole_h_ stride_h_
kernel_w_ pad_w_ hole_w_ stride_w_
is_1x1_:上面8个參数都为1时,该參数为true和输入有关的參数:(从bottom得来)
num_
channels_
height_
width_和卷积核有关的參数:(前两个參数从配置文件得来)
num_output_
group_
this->blobs_[0].reset(new Blob(num_output_, channels_ / group_, kernel_h_, kernel_w_));
this->blobs_[1].reset(new Blob(1, 1, 1, num_output_));
this->param_propagate_down_和输出有关的參数:(计算得来)
const int kernel_h_eff = kernel_h_ + (kernel_h_ - 1) * (hole_h_ - 1);
const int kernel_w_eff = kernel_w_ + (kernel_w_ - 1) * (hole_w_ - 1);
height_out_ = (height_ + 2 * pad_h_ - kernel_h_eff) / stride_h_ + 1;
width_out_ = (width_ + 2 * pad_w_ - kernel_w_eff) / stride_w_ + 1;和矩阵运算有关的參数:(计算得来)
M_ = num_output_ / group_;
K_ = channels_ * kernel_h_ * kernel_w_ / group_;
N_ = height_out_ * width_out_;
col_buffer_.Reshape(1, channels_*kernel_h_*kernel_w_, height_out_, width_out_);// is_1x1_为false的时候用
bias_multiplier_.Reshape(1, 1, 1, N_); //所有为1
输入大小:(num_, channels_, height_, width_)
输出大小:(num_, num_output_, height_out_, width_out_)
重点函数剖析
函数一:
im2col_cpu(bottom_data + bottom[i]->offset(n),
1, channels_, height_, width_,
kernel_h_, kernel_w_, pad_h_, pad_w_,
stride_h_, stride_w_, hole_h_, hole_w_,
col_buff);该函数的目的是:依据配置參数,将一幅(1, channels_, height_, width_)的输入feature map expand成 (1, channels_*kernel_h_*kernel_w_, height_out_, width_out_)大小的矩阵。
详细的实现方法是:
内部主要有两套索引
一套是在输入图像上的索引,各自是:c_im(channels), h_im(height), w_im(width)
还有一套是在输出的col_buff上的。各自是:c(channels_col), h(height_col), w(width_col)循环变量来自输出的col_buff的维数,依据输出的位置计算相应在输入图像上的位置(col2imh函数和im2col函数是一个道理。两套坐标反着来即可)。把索引的代码整合出来。对着源码看。非常easy懂:
const int kernel_h_eff = kernel_h + (kernel_h - 1) * (hole_h - 1);
const int kernel_w_eff = kernel_w + (kernel_w - 1) * (hole_w - 1);
int height_col = (height + 2 * pad_h - kernel_h_eff) / stride_h + 1;
int width_col = (width + 2 * pad_w - kernel_w_eff) / stride_w + 1;
int channels_col = channels * kernel_h * kernel_w;
int w_offset = (c % kernel_w) * hole_w;
int h_offset = ((c / kernel_w) % kernel_h) * hole_h;
int c_im = c / kernel_w / kernel_h;
const int h_im = h * stride_h + h_offset - pad_h;
const int w_im = w * stride_w + w_offset - pad_w;
函数二:
caffe_cpu_gemm(CblasNoTrans, CblasNoTrans, M_, N_, K_,
(Dtype)1., weight + weight_offset * g, col_buff + col_offset * g,
(Dtype)0., top_data + top[i]->offset(n) + top_offset * g);该函数的目的是:
将(num_output_/group_, channels_ /group_, kernel_h_, kernel_w_)卷积核看成一个(num_output_/group_, channels_*kernel_h_*kernel_w_/group_)的矩阵A,即大小为M_x K_。将(1, channels_*kernel_h_*kernel_w_, height_out_, width_out_)的col_buff看成group_个(channels_*kernel_h_*kernel_w_/group_, height_out_*width_out_)的矩阵B。即大小为K_x N_。
两者相乘再加上偏置项。就能得到卷积的结果。
解释caffe_cpu_gemm函数:
事实上其内部包了一个cblas_sgemm函数。void cblas_sgemm(const enum CBLAS_ORDER Order, const enum CBLAS_TRANSPOSE TransA,
const enum CBLAS_TRANSPOSE TransB, const int M, const int N,
const int K, const float alpha, const float *A,
const int lda, const float *B, const int ldb,
const float beta, float *C, const int ldc)得到的结果是:
C = alpha*op( A )*op( B ) + beta*Cconst enum CBLAS_ORDER Order,这是指的数据的存储形式,在CBLAS的函数中不管一维还是二维数据都是用一维数组存储,这就要涉及是行主序还是列主序。在C语言中数组是用 行主序。fortran中是列主序。
假设是习惯于是用行主序,所以这个參数是用CblasRowMajor。假设是列主序的话就是 CblasColMajor。
const int M,矩阵A的行,矩阵C的行
const int N,矩阵B的列。矩阵C的列
const int K,矩阵A的列。矩阵B的行
caffe卷积层代码阅读笔记的更多相关文章
- [置顶] Linux协议栈代码阅读笔记(二)网络接口的配置
Linux协议栈代码阅读笔记(二)网络接口的配置 (基于linux-2.6.11) (一)用户态通过C库函数ioctl进行网络接口的配置 例如,知名的ifconfig程序,就是通过C库函数sys_io ...
- Linux协议栈代码阅读笔记(二)网络接口的配置
Linux协议栈代码阅读笔记(二)网络接口的配置 (基于linux-2.6.11) (一)用户态通过C库函数ioctl进行网络接口的配置 例如,知名的ifconfig程序,就是通过C库函数sys_io ...
- [置顶] Linux协议栈代码阅读笔记(一)
Linux协议栈代码阅读笔记(一) (基于linux-2.6.21.7) (一)用户态通过诸如下面的C库函数访问协议栈服务 int socket(int domain, int type, int p ...
- 【caffe】卷积层代码解析
1.Forward_cpu conv_layer.cpp template <typename Dtype> void ConvolutionLayer<Dtype>::For ...
- Linux-3.0.8 input subsystem代码阅读笔记
先乱序记录一下阅读Linux input subsystem代码的笔记. 在input device driver的入口代码部分,需要分配并初始化input device结构,内核提供的API是inp ...
- 〖Android〗OK6410a的Android HAL层代码编写笔记
一.编写LED灯的Linux驱动程序代码 之所以使用存在HAL层,是为了保护对硬件驱动过程的逻辑与原理: 所以,残留在Linux驱动层的代码,只保留了基本的读写操作,而不含有关键的逻辑思维: 1. l ...
- caffe 代码阅读笔记1
首先查看caffe.cpp里的train函数: // Train / Finetune a model. //训练,微调一个网络模型 int train() { // google的glog库,检查- ...
- caffe卷积层实现
下图是jiayangqing在知乎上的回答,其实过程就是把image转换成矩阵,然后进行矩阵运算 卷积的实现在conv_layer层,conv_layer层继承了base_conv_layer层,ba ...
- Typecho 代码阅读笔记(二) - 数据库访问
转载请注明出处:http://blog.csdn.net/jh_zzz 这一块比较复杂,我还没有完全理解为什么要把 SQL 语句的组装搞这么复杂. 从一个普通皮肤页面开始 themes/default ...
随机推荐
- Create Windows Server 2008 cluster from the command line
How to create a Windows Server 2008 cluster from the command line? Creating a cluster in Server 2008 ...
- HDU2041 简单DP+规律
超级楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- Codevs 2080 特殊的质数肋骨
题目描述 Description 农民约翰的母牛总是产生最好的肋骨. 你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们. 农民约翰确定他卖给买方的是真正的质数肋骨,是因为从右边开始切下肋 ...
- linux select函数详解【转】
转自:http://www.cnblogs.com/ccsccs/articles/4224253.html 在Linux中,我们可以使用select函数实现I/O端口的复用,传递给 select函数 ...
- android hook 框架 ADBI 如何实现so注入
Android so注入-libinject2 简介.编译.运行 Android so注入-libinject2 如何实现so注入 Android so注入-Libinject 如何实现so注入 A ...
- glxgears刷新只有60FPS解决办法
问题原因在于屏幕的垂直同步刷新率的限制,解决办法是关闭垂直同步刷新. 编辑~/.drirc <driconf> <device screen=" driver=" ...
- python每日一类(4):slice
class slice(stop)class slice(start, stop[, step]) Return a slice object representing the set of indi ...
- 先刷一波简单的web前端面试题
1简述一下src与href的区别href 是指向网络资源所在位置,建立和当前元素(锚点)或当前文档(链接)之间的链接,用于超链接.src是指向外部资源的位置,指向的内容将会嵌入到文档中当前标签所在位置 ...
- HDU 1074 Doing Homework(状压DP)
第一次写博客ORZ…… http://acm.split.hdu.edu.cn/showproblem.php?pid=1074 http://acm.hdu.edu.cn/showproblem.p ...
- 用jmeter进行多用户并发压力测试
测试要求如下,多用户同时登陆web应用程序,并进行操作,查看在多用户操作下,程序的performence.恰好,jemter下有个CSV Data Set Config,它用来设定一组参数,以便在向程 ...