您的位置:首页 > 理论基础 > 计算机网络

Caffe下卷积神经网络(CNN)中的一些特殊层

2018-01-09 00:57 711 查看


Batch Normalization

意义: 网络训练时,用来加速收敛速度
提醒:

已经将BN集成为一个layer了,使用时需要和scale层一起使用
训练的时候,将BN层的use_global_stats设置为false; 测试的时候将use_global_stats设置为true,不然训练的时候会报“NAN”或者模型不收敛 – 师兄的经验,我还没试验过

用法: 详见 残差神经网络的使用


Dropout

意义: 防止模型过拟合;训练模型时,随机让网络某些隐含层节点的权重不工作(不工作的那些节点可以暂时认为不是网络结构的一部分,但是它的权重得保留下来,只是暂时不更新而已,因为下次样本输入时它可能又得工作了)
用法:

layer {

name: “drop7”

type: “Dropout”

bottom: “fc7-conv”

top: “fc7-conv”

dropout_param {

dropout_ratio: 0.5

}

}


ReLU

意义: 激活函数的一种;对于给定的一个输入值x,如果x > 0,ReLU层的输出为x,如果x < 0,ReLU层的输出为0。
提醒: 可选参数
negative_slope
,此参数使得x < 0时,ReLU层的输出为negative_slope * x;目前已经有了ReLU的进化版 – PReLU
用法:

layer {

name: “relu1”

type: “ReLU”

bottom: “conv1”

top: “conv1”

relu_param{

negative_slope: [默认:0]

}

}


PReLU

意义: ReLu的进化版;。
提醒: 在负半轴的输出乘以一个系数,而这个系数是可学习的(你可以为其指定学习率),其中value是系数的初始值,channel_shared指定是否在各个通道间共享这个系数。 据说有的实验更快更好地收敛,但有的实验准确率却有所下降 - 具体效果还是得以具体实验为准(自己没有用过,不加评论

-用法:

layer {

name: “relu1”

type: “PReLU”

bottom: “conv1”

top: “conv1”

param {

lr_mult: 1

decay_mult: 0

}

prelu_param {

filler: {

value: 0.33 #: 默认为0.25

}

channel_shared: false

}

}


Split

意义: 将一份blob复制为n份
提醒: caffe会隐式地做这个操作,我也不知道什么时候会显式地用到这个操作,先搁这儿吧(没实际用过这个操作,所以下面的用法不一定对)
用法:

layer {

name: “split”

type: “split”

bottom: “rois”

top: “rois1”

top: “rois2”

}


Reshape

意义: 改变blob的维度,而不改变其自身的数据
提醒: 每个blob为4维,故有4个dim参数【0代表不改变维度的值,-1代表由caffe计算出值,正数代表将维度更改为对应的值】

layer {

name: “reshape”

type: “Reshape”

bottom: “conv1”

top: “conv1”

reshape_param {

shape {

dim: 0 # copy the dimension from below

dim: 2

dim: 3

dim: -1 # infer it from the other dimensions

}

}

}

注: 若1个参数分别为 dim:1 dim:3 dim:2 dim:-1的reshape层,输入为1个维度为1*2*3*4的blob,那么输出的blob维度为1*3*2*4(其中4是由caffe计算而来的)

(补充)Reshape 改变输入数据的维度

常见配置如下:
layer {
name: "reshape"
type: "Reshape"
bottom: "input"
top: "output"
reshape_param {
shape {
dim: 0  # copy the dimension from below
dim: 2
dim: 3
dim: -1 # infer it from the other dimensions
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[/code]

输入和输出都是类似blob的四维张量,N*C*W*H

通过reshape_param中的shape参数改变张量的维度四个dim分别代表output(top)中的数据维度,后面的数字表示给维度具体要变成什么样。

举个例子,设输入为 32*3*28*28

shape {

dim: 0 # copy the dimension from below

dim: 0

dim: 14

dim: -1 # infer it from the other dimensions

}

dim:0 表示 output(top)中的N 直接复制 input(bottom)中N的维度 为 32

dim:0表示 output(top)中的直接复制 input(bottom)中C的维度 为3

dim:3表示 output(top)中的W 变为28维 变为 14

dim:-1 表示这一维度的具体数字通过其他维度推算 为56,推算过程如下:总的数据量不变,只改变 NCWH 四个维度的具体数字

(32*3*28*28)/(32*3*14) = 56

因此,经过Reshape后输出为32*3*14*56


InnerProduct

意义: 将输入数据以简单的向量形式进行处理,并且输出一个简单的向量;简单来说,这是一个卷积操作,只不过卷积核尺寸和feature map相同,故输出向量大小为1*1
缺点:使用包含全连接层的模型(如AlexNet)必须使用固定大小的输入,有时这是非常不合理的,因为必须对输入图片进行变形。
提醒:

必要参数:

num_output (c_o):滤波器数量
强烈建议参数:

weight_filler:滤波器的初始分布和分布参数。
可选参数:

bias_filler:[默认: type: ‘constant’ value: 0]

bias_term:[默认:true] 指定是否在滤波器输出之后学习并应用附加的偏置。

用法:

layer {

name: “fc8”

type: “InnerProduct”

bottom: “fc7”

top: “fc8”

param { # learning rate and decay multipliers for the weights

lr_mult: 1 decay_mult: 1

}

param { # learning rate and decay multipliers for the biases

lr_mult: 2 decay_mult: 0

}

inner_product_param {

num_output: 1000

weight_filler {

type: “xavier”

std: 0.01

}

bias_filler {

type: “constant”

value: 0

}

}

}

注: 比如上面层的输入为 n * c_i * h_i * w_i,那么输入为 n * 1000 * 1 * 1


Crop

意义:输入两个blob,将bottom[0] 按照bottom[1]的尺寸进行剪裁
提醒:

axis=0,1,2,3分别表示为N,C,H,W;默认axis等于2,即默认从H开始裁剪(裁剪H和W);可以只设置1个,也可以为每个dimension分别设置
offset表示裁剪时的偏移量(如果还是不太清楚的话,戳这儿

用法:

layer {

type: “Crop”

name: ‘crop’

bottom: ‘score-dsn1-up’

bottom: ‘data’

top: ‘upscore-dsn1’

crop_param {

axis: 2

offset: 5

}

}

Flatten变多维矩阵为一维向量

常见配置如下:
layer {
name: "rpn_cls_score_flat"
type: "Flatten"
bottom: "rpn_cls_score_perm"
top: "rpn_cls_score_flat"
flatten_param {
axis: 1
}
}
1
2
3
4
5
6
7
8
9
[/code]

Flatten层是把一个输入的大小为n * c * h * w变成一个简单的向量,其大小为 n * (c*h*w) * 1 * 1

axis [default 1]:0代表链接N,1代表链接C

3.Concat按照axis连接多个blob

常见配置如下:
layer {
name: "concat"
bottom: "in1"
bottom: "in2"
top: "out"
type: "Concat"
concat_param {
axis: 1
}
}
1
2
3
4
5
6
7
8
9
10
[/code]

输入输出均为N*C*W*H的blob张量

可选参数:

axis [default 1]:0代表链接N,1代表链接C

通过全连接层后的大小变化:

输入:从1到K的每一个blob的大小:ni×ci×h×w

输出:

如果axis = 0: (n1+n2+…+nK)×c1×h×w,需要保证所有输入的ci相同。

如果axis = 1: n1×(c1+c2+…+cK)×h×w,需要保证所有输入的n_i 相同。

通过Concatenation层,可以把多个的blobs链接成一个blob。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: