caffe各层解析以及参数说明(慢慢整理,做个笔记)
<div id="article_content" class="article_content clearfix">
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-211130ba7a.css">
<div id="content_views" class="markdown_views prism-atom-one-light">
<!-- flowchart 箭头图标 勿删 -->
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
<path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
</svg>
<p>本博客地址:<a href="https://blog.csdn.net/shanglianlm/article/details/84572758">https://blog.csdn.net/shanglianlm/article/details/84572758</a><br>
1 Data Layers
Data 通过Data Layers进入Caffe,Data Layers位于Net的底部。
Data 可以来自:1、高效的数据库(LevelDB 或 LMDB);2、内存;3、HDF5或image文件(效率低)。
基本的输入预处理(例如:减去均值,缩放,随机裁剪,镜像处理)可以通过指定TransformationParameter达到。
1-1 Database
1-2 In-Memory
1-3 HDF5 Input
1-4 HDF5 Output
1-5 Images
1-6 Windows
1-7 Dummy
Vision Layers
Vision layers 通常以图片images作为输入,运算后产生输出的也是图片images。对于图片而言,可能是单通道的(c=1),例如灰度图,或者三通道的 (c=3),例如RGB图。
Convolution
Pooling
Local Response Normalization LRN
im2col
Common Layers
Inner Product
Splitting
Flattening
Reshape
Concatenation
Slicing
Elementwise Operations
Argmax
Softmax
Mean-Variance Normalization
Activation Neuron Layers
激励层的操作都是element-wise的操作(针对每一个输入blob产生一个相同大小的输出)
ReLU Rectified-Linear and Leaky-ReLU
Sigmoid
TanH Hyperbolic Tangent
Absolute Value
Power
BNLL
Loss Layers
Caffe是通过最小化输出output与目标target之间的cost(loss)来驱动学习的。loss是由forward pass计算得出的,loss的gradient 是由backward pass计算得出的。
Softmax
Sum-of-Squares Euclidean
Hinge Margin
Sigmoid Cross-Entropy
Infogain
Accuracy and Top-k
- DummyData层
产生随机测试数据
layer {
type: 'DummyData'
name: 'x'
top: 'x'
dummy_data_param {
shape: { dim: 10 dim: 3 dim: 2 }
data_filler: { type: 'gaussian' }
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
Caffe中parameter filler的作用和类型
Fillers是caffe用特定算法随机生成的值来填充网络参数在blob里面的初始值。它只作用在参数的初始化阶段,与gpu无关的操作。
Constant : 令填充值x=0x=0
Uniform : 令填充值x∼U(a,b)x∼U(a,b)
Gaussian : 令填充值x=ax=a
PositiveUnitball : 令填充值x∈[0,1]x∈[0,1] ∀i∑jxij=1∀i∑jxij=1
Xavier :令填充值x∼U(−a,+a)x∼U(−a,+a); 其中 aa与输入节点,输出节点或着两者的均值成反比 (该算法是Bengio和Glorot 2010在Understanding the difficulty of training deep feedforward neuralnetworks里提出的)
MSRA : 令填充值x∼N(0,σ2)x∼N(0,σ2); 其中 aa与输入节点,输出节点或着两者的均值成反比 (该算法是Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun在Delving Deep into Rectifiers: Surpassing Human-Level Performance on ImageNet Classification里提出的)
bilinear : 一般用在deconvolution 层做upsampling;例子如下:
layer {
name: "upsample",
type: "Deconvolution"
bottom: "{bottom_name}"
top: "{top_name}"
convolution_param {
kernel_size: {2 * factor - factor % 2}
stride: {factor}
num_output: {C}
group: {C}
pad: {ceil((factor - 1) / 2.)}
weight_filler: {
type: "bilinear"
}
bias_term: false
}
param {
lr_mult: 0
decay_mult: 0
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
引用自:[Caffe]:关于filler typehttps://blog.csdn.net/cham_3/article/details/52765087
- Convolution层
layer {
name: "conv1"
type: "Convolution"
bottom: "data"
top: "conv1"
param {
lr_mult: 1
decay_mult: 1
}
param {
lr_mult: 2
decay_mult:0
}
convolution_param {
num_output: 20
kernel_size: 5
stride: 1
group: 3
bias_term: true
weight_filler {
type: "xavier"
}
bias_filler {
type: "msra"
}
engine: CAFFE
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
lr_mult: 1
学习率的系数,最终的学习率是这个数乘以solver.prototxt配置文件中的base_lr。如果有两个lr_mult, 则第一个表示权值的学习率,第二个表示偏置项的学习率。一般偏置项的学习率是权值学习率的两倍,能够加速收敛。
decay_mult: 1
权重衰减项,偏置,其衰减项一般设置为0
weight_filler
: 权值初始化。 默认为“constant",值全为0,很多时候我们用"xavier"算法来进行初始化,也可以设置为”gaussian"
bias_term
: 是否开启偏置项,默认为true, 开启
bias_filler
: 偏置项的初始化。一般设置为"constant",值全为0。
group
: 分组,默认为1组。如果大于1,我们限制卷积的连接操作在一个子集内。如果我们根据图像的通道来分组,那么第i个输出分组只能与第i个输入分组进行连接。
engine: CAFFE
引擎CAFFE vs. cuDNN。Default就是指向caffe engine的。
- Pooling层
layer {
name: "pool1"
type: "Pooling"
bottom: "conv1"
top: "pool1"
pooling_param {
pool: MAX
kernel_size: 3
stride: 2
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
pool
: 池化方法,默认为MAX。目前可用的方法有MAX, AVE, 或STOCHASTIC
- LRN(Local Response Normalization)层
layers {
name: "norm1"
type: LRN
bottom: "pool1"
top: "norm1"
lrn_param {
local_size: 5
alpha: 0.0001
beta: 0.75
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
local_size
: 默认为5。如果是跨通道LRN,则表示求和的通道数;如果是在通道内LRN,则表示求和的正方形区域长度。
alpha
: 默认为1,归一化公式中的参数。
beta
: 默认为5,归一化公式中的参数。
norm_region
: 默认为ACROSS_CHANNELS。有两个选择,ACROSS_CHANNELS表示在相邻的通道间求和归一化。WITHIN_CHANNEL表示在一个通道内部特定的区域内进行求和归一化。与前面的local_size参数对应。
- BatchNorm层
layer {
name: "conv1_bn"
type: "BatchNorm"
bottom: "conv1"
top: "conv1"
batch_norm_param {
use_global_stats: false
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
use_global_stats: false
在训练时要设置为false,代表需要批处理的均值方差要参考全局信息来更新。在测试时,要为true,代表不使用自己均值方差和全局的均值方差来得到批处理的均值和方差,而是使用全局均值方差,即训练好的均值方差。
- Scale层
Scale层主要完成 top=alpha∗bottom+betatop=alpha∗bottom+beta的过程
Layer {
name: "conv1_scale"
bottom: "conv1"
top: "conv1"
type: "Scale"
scale_param {
filler {
value: 1
}
bias_term: true
bias_filler {
value: 0
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
caffe 中为什么bn层要和scale层一起使用
这个问题首先你要理解batchnormal是做什么的。它其实做了两件事。
- 输入归一化 x_norm = (x-u)/std, 其中u和std是个累计计算的均值和方差。
- y=alpha×x_norm + beta,对归一化后的x进行比例缩放和位移。其中alpha和beta是通过迭代学习的。
那么caffe中的bn层其实只做了第一件事。scale层做了第二件事。
这样你也就理解了scale层里为什么要设置bias_term=True,这个偏置就对应2)件事里的beta。
- ReLU层
layer {
name: "conv1_relu"
type: "ReLU"
bottom: "conv1"
top: "conv1"
}
- 1
- 2
- 3
- 4
- 5
- 6
7. dropout 层
layer {
name: "drop1"
type: "Dropout"
bottom: "ip11"
top: "ip11"
dropout_param {
dropout_ratio: 0.3
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- Split 层
- 1
- Flatten 层
- 1
- Reshape 层
- 1
- Concat 层
layer {
name: "resx1_concat"
type: "Concat"
bottom: "resx1_match_conv"
bottom: "resx1_conv3"
top: "resx1_concat"
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
Concat 将多个bottom合并成一个top的过程。
将两个及以上的特征图按照在channel(建议)或num维度上进行拼接,一般都是channel上的拼接,并没有eltwise层的运算操作。如果说是在channel维度上进行拼接resx1_conv3和resx1_match_conv的话,首先除了channel维度可以不一样,其余维度必须一致(也就是num、H、W一致),这时候所做的操作仅仅是resx1_conv3 的channel k1加上resx1_match_conv的channel k2,Concat 层输出的blob可表示为:N*(k1+k2)HW。
实验表明,BN之后进行Concat操作,效果会更好。
- Slice 层
layer {
name: "data_each"
type: "Slice"
bottom: "data_all"
top: "data_classfier"
top: "data_boundingbox"
top: "data_facialpoints"
slice_param {
axis: 0
slice_point: 150
slice_point: 200
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
与Concat层作用相反,它是将一个 bottom切分成多个top的过程。slice_point表示从哪里开切,即指定切分点。
延伸阅读:
caffe层解读系列——slice和concat实现MultiTask
https://blog.csdn.net/shuzfan/article/details/54565776
- InnerProduct 层
- 1
- ConvolutionDepthwise 层
深度可分离卷积
layer {
name: "resx1_match_DWconv"
type: "ConvolutionDepthwise"
bottom: "pool1"
top: "resx1_match_DWconv"
convolution_param {
num_output: 24
kernel_size: 5
stride: 2
pad: 1
bias_term: false
weight_filler {
type: "msra"
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
延伸阅读:
caffe深度学习【十七】配置MobileNet,caffe添加ConvolutionDepthwise层
https://blog.csdn.net/qq_38451119/article/details/82663649
- ShuffleChannel 层(Shufflenet特有)
layer {
name: "shuffle2"
type: "ShuffleChannel"
bottom: "resx2_conv1"
top: "shuffle2"
shuffle_channel_param {
group: 3
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
在分组卷积的基础上,打乱不同通道的排序,使得下一层的操作的输入能吸收来上一层不同组的内容,使得学习更佳均衡。
延伸阅读:
caffe深度学习【十六】配置Shufflenet v1,caffe添加shuffle_channel_layer层
https://blog.csdn.net/qq_38451119/article/details/82657510
- Eltwise 层
layer {
name: "resx2_elewise"
type: "Eltwise"
bottom: "resx1_concat"
bottom: "resx2_conv3"
top: "resx2_elewise"
eltwise_param {
operation: SUM
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Eltwise层的操作有三个:product(点乘), sum(相加减) 和 max(取大值),其中sum是默认操作。
如果实现A-B,则eltwise_param 为:
eltwise_param {
operation: SUM
coeff: 1
coeff: -1
}
- 1
- 2
- 3
- 4
- 5
</div><div><div></div></div>
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-60ecaf1f42.css" rel="stylesheet">
<div></div>
<div class="person-messagebox">
<div class="left-message"><a href="https://blog.csdn.net/shanglianlm">
<img src="https://profile.csdnimg.cn/0/8/3/3_shanglianlm" class="avatar_pic" username="shanglianlm">
</a></div>
<div class="middle-message">
<div class="title"><span class="tit "><a href="https://blog.csdn.net/shanglianlm" data-report-click="{"mod":"popu_379","ab":"new"}" target="_blank">mingo_敏</a></span>
<!-- 等级,level -->
<img class="identity-icon" src="https://csdnimg.cn/identity/blog7.png"> <span class="flag expert">
<a href="https://blog.csdn.net/home/help.html#classicfication" target="_blank">
<img src="https://csdnimg.cn/release/phoenix/template/new_img/identityExpert.png" alt="">
博客专家
</a>
</span>
</div>
<div class="text"><span>原创文章 273</span><span>获赞 386</span><span>访问量 73万+</span></div>
</div>
<div class="right-message">
<a class="btn btn-sm bt-button personal-watch" data-report-click="{"mod":"popu_379","ab":"new"}">关注</a>
<a href="https://bbs.csdn.net/topics/395528930" target="_blank" class="btn btn-sm bt-button personal-messageboard">他的留言板
</a>
</div>
</div>
</div>
</article>