转载于http://blog.csdn.net/u012939857/article/details/70740283
BN层的设定一般是按照conv->bn->scale->relu的顺序来形成一个block。
关于bn,有一个注意点,caffe实现中的use_global_stats参数在训练时设置为false,测试时设置为true。
因为在训练时bn作用的对象是一个batch_size,而不是整个训练集,如果没有将其设置为false,则有可能造成bn后数据更加偏离中心点,导致nan或87.3365的问题。
caffe 中为什么bn层要和scale层一起使用
这个问题首先要理解batchnormal是做什么的。它其实做了两件事。
1) 输入归一化 x_norm = (x-u)/std, 其中u和std是个累计计算的均值和方差。
2)y=alpha×x_norm + beta,对归一化后的x进行比例缩放和位移。其中alpha和beta是通过迭代学习的。
那么caffe中的bn层其实只做了第一件事。scale层做了第二件事。
这样也就理解了scale层里为什么要设置bias_term=True,这个偏置就对应2)件事里的beta。
代码:
第一种情况,phase: TRAIN/TEST都不加 ,caffe会自动匹配去设置use_global_stats的值
-
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:
32
-
kernel_size:
5
-
weight_filler{
-
type:
"xavier"
-
}
-
bias_filler{
-
type:
"constant"
-
}
-
}
-
}
-
layer {
-
name:
"BatchNorm1"
-
type:
"BatchNorm"
-
bottom:
"conv1"
-
top:
"conv1"
-
param {
-
lr_mult:
0
-
decay_mult:
0
-
}
-
param {
-
lr_mult:
0
-
decay_mult:
0
-
}
-
param {
-
lr_mult:
0
-
decay_mult:
0
-
}
-
}
-
-
layer {
-
name:
"scale1"
-
type:
"Scale"
-
bottom:
"conv1"
-
top:
"conv1"
-
scale_param {
-
bias_term: true
-
}
-
}
-
layer{
-
name:
"relu1"
-
type:
"ReLU"
-
bottom:
"conv1"
-
top:
"conv1"
-
}
第二种情况:加上
use_global_stats, 测试的时候再改成true
-
layer {
-
name:
"BatchNorm1"
-
type:
"BatchNorm"
-
bottom:
"conv1"
-
top:
"conv1"
-
param {
-
lr_mult:
0
-
decay_mult:
0
-
}
-
param {
-
lr_mult:
0
-
decay_mult:
0
-
}
-
param {
-
lr_mult:
0
-
decay_mult:
0
-
}
-
batch_norm_param {
-
use_global_stats: false
-
}
-
}