caffe学习系列--层解读

1.accuracy layers

层类型:Accuracy

layer {
  name: "accuracy"
  type: "Accuracy"
  bottom: "fc8"
  bottom: "label"
  top: "accuracy"
  include {
    phase: TEST
  }
}

可以看到,caffe中计算Accuracy时,是通过比较最后一个全连接层(神经元个数=类别数、但没有加入activation function)的输出和数据集的labels来得到的,计算过程在AccuracyLayer中实现;
我也一直非常困惑,计算accuracy应该使用计算得到的labels与数据集真正的labels去做计算,为什么caffe的accuracy要将fc8接入Accuray层呢?原来,在AccuracyLayer内部,实现了“利用fc8的输出得到数据集的预测labels”(数值最大的那个值得idnex就是样本的类别),那么,再与输入的数据集真实lebels作对比,就实现了accuray的计算!

实际上,如果仅仅是做预测,利用fc8的输出就够了(输出值最大的那个位置即为输入的label),该输出表示了输入的样本属于每一类的可能性大小,但并不是概率值; 
如果为了使输出具有统计意义,需要加入softmax function,它只是使前面的全连接层的输出(fc8)具有了概率意义,并不改变这些输出之前的大小关系,因为softmax function本身就是增函数; 
为了利用误差反向传播,还需要构造loss function(验证和训练的不同仅是验证打印出accuracy值),需要利用softmax function的输出,即需要利用输入样本属于每一类的概率值;
/*下面代码含全连接层,accuracy,SoftmaxWithLoss层*/
//最后一个全连接层
layer {
  name: "fc8"                              # 名称:fc8
  type: "InnerProduct"                     # 类型:全连接层
  bottom: "fc7"                            # 输入层:fc7
  top: "fc8"                               # 输出层:fc8                   
  # 权重(weights)的学习速率因子和衰减因子
  param { lr_mult: 1 decay_mult: 1 }
  # 偏置项(biases)的学习速率因子和衰减因子
  param { lr_mult: 2 decay_mult: 0 }
  inner_product_param {
    num_output: 1000                       # 1000个滤波器(filters)
    weight_filler {
      type: "gaussian"                     # 初始化高斯滤波器(Gaussian)
      std: 0.01                            # 标准差为0.01, 均值默认为0
    }
    bias_filler {
      type: "constant"                     # 初始化偏置项(bias)为零
      value: 0
    }
  }
}
layer {
name: "accuracy"
type: "Accuracy"
bottom: "fc8"
bottom: "label"
top: "accuracy"
include {
phase: TEST //仅供测试(验证)用
}
}
/*fc8后面接的SoftmaxWithLoss层做的工作分2步
第一步:对fc8的输出计算softmax function(结果为概率值)
第二步:利用求得的概率值计算Loss值*/
layer {
name: "loss"
type: "SoftmaxWithLoss"
bottom: "fc8"
bottom: "label" //跟label有啥关系??预测对的才要计算loss
top: "loss"
}

2.InnerProduct Layers

示例代码见上;
层类型:InnerProduct(全连接层)
输入:N(batch_size大小)*C_i(通道数)*H*W
输出:N*C_o*1*1
输入可视为一个vector,输出也是一个vector(height和width被设为1)
全连接层可将学习到的特征映射到样本空间的作用。

3.Slicing layers

层类型:Slice (对比Split层)
作用:将一个input layer分割成多个output layers,根据给定的维度(目前只能指定num或者channel)(也可以分割labels)
参考链接:http://blog.csdn.net/u012235274/article/details/52438479

//分割labels实例代码
layer {
  name: "slicer_label"
  type: "Slice"
  bottom: "label"
  ## 假设label的维度是:N x 3 x 1 x 1
  top: "label1"
  top: "label2"
  top: "label3"
  slice_param {
    axis: 1                        # 指定维度为channel
    slice_point: 1                 # 将label[~][1][~][~]赋给label1
    slice_point: 2                 # 将label[~][2][~][~]赋给label2
                                   # 将label[~][3][~][~]赋给label3
  }
}

axis表明是(NCHW中)哪一个维度,slice_point(拆分点)是该维度的索引,slice_point的数量必须是top blobs的数量减1.

4.Concatenation

类型(type):Concat(连结层)
CPU 实现: ./src/caffe/layers/concat_layer.cpp
CUDA、GPU实现: ./src/caffe/layers/concat_layer.cu
参数 (concat_param):(可选):
axis [default 1]: 0代表连结num,1代表连结channel
输入(Input) :
-n_i * c_i * h * w: 第i个blob的维度是n_i * c_i * h * w,共K个
输出(Output):
if axis = 0: (n_1 + n_2 + … + n_K) * c_1 * h * w, and all input c_i should be the same.(axis = 0时,输出 blob的维度为(n_1 + n_2 + … + n_K) * c_1 * h * w,要求所有的input的channel相同)
if axis = 1: n_1 * (c_1 + c_2 + … + c_K) * h * w, and all input n_i should be the same.(axis = 0时,输出 blob的维度为n_1 * (c_1 + c_2 + … + c_K) * h * w,要求所有的input的num相同)
作用:与Slicing layer相对应,Concat layer用于把多个输入blob连结成一个输出blob。

layer {
  name: "concat"
  bottom: "in1"
  bottom: "in2"
  top: "out"
  type: "Concat"
  concat_param {
    axis: 1
  }
}

5.Eltwise layer

layer {  
  name: "fuse"  
  type: "Eltwise"  
  bottom: "A"  
  bottom: "B"  
  top: "C"  
  eltwise_param {  
    operation: SUM  
  }  
} 

Eltwise层的操作有三个:product(点乘)、sum(相加减)、max(取最大值),其中,sum是默认操作。
PROD表示将A、B按元素相乘,SUM表示将A、B按元素求和,MAX表示将A、B按元素求最大值。

6.Reshape

类型(type):Reshape(对比Flatten层)
CPU 实现: ./src/caffe/layers/reshape_layer.cpp
参数 (reshape_param):(可选)
shape(改变后的维度,详见下面解释)
输入(Input):
a single blob with arbitrary dimensions(一个任意维度的blob)
输出(Output):
the same blob, with modified dimensions, as specified by reshape_param(相同内容的blob,但维度根据reshape_param改变)

 layer {
    name: "reshape"                       # 名称:reshape
    type: "Reshape"                       # 类型:Reshape
    bottom: "input"                       # 输入层名称:input
    top: "output"                         # 输出层名称:output
    reshape_param {
      shape {
        dim: 0  # 这个维度与输入相同
        dim: 2
        dim: 3
        dim: -1 # 根据其他维度自动推测,reshape_param中至多只能有一个-1 //四个dim分别代表:N,C,H,W
      }
    }
  }

其中:当reshape_param参数为:{ shape { dim: 0 dim: -1 } } ,那么输出和Flattening layer的输出是完全一样的。

7.Flattening layer

层类型:Flatten
作用:用于把一个维度为n * c * h * w的输入转化为一个维度为 n * (c*h*w)的向量输出。

8.solver文件详解

https://www.cnblogs.com/denny402/p/5074049.html
关于:几种lr_policy:
“fixed”
“inv”
“step”
“multistep”
“stepearly”
“poly”

图形化表示参考:https://stackoverflow.com/questions/30033096/what-is-lr-policy-in-caffe

9.prototxt文件参数说明

https://blog.csdn.net/cyh_24/article/details/51537709

猜你喜欢

转载自blog.csdn.net/u012617944/article/details/78473004