caffe中各层的作用:

关于caffe中的solver:

cafffe中的sover的方法都有:

  • Stochastic Gradient Descent (type: "SGD"),
  • AdaDelta (type: "AdaDelta"),
  • Adaptive Gradient (type: "AdaGrad"),
  • Adam (type: "Adam"),
  • Nesterov’s Accelerated Gradient (type: "Nesterov") and
  • RMSprop (type: "RMSProp")

solver都干了点什么?

1,创建训练网络与测试网网络.

2,进行前向传播与误差反向传播,更新参数,优化网络.

3, 间歇地进行用验证集进行测试test网络.

4, 在优化过程中,可以选择进行快照,进行保存中间状态.

 

数据输入层 (data layer):

在caffe中, 数据通过caffe进入.通常情况下,数据可以直接从内存中读中,可以从高效的LevelDB/LMDBD Database 中读入, 也可以从HDF5格式的硬盘文件中读去或着通常的图片文件.

通常我们对数据的预处理,我们可以参考TransformationParams里的设定.

 

Database 层: 类型为Data

这就是从LMDB/LeverlDB文件中读取数据的层;

 

In-Memory层: type:MemoryData

当我们想直接从内存中读取数据的话,那就需要调用MemoryDataLayer::Reset(c++程序)或Net.input_arrays(python程序),来指明数据源.

 

HDF5 Input层,类型为:HDF5Data

它的作用为从HDF5文件类型里读放数据,我们就看一个例子哦

2 layer {
  3   name: "mnist"
  4   type: "HDF5Data"
  5   top: "data"        
  6   top: "label"
  7   include {
  8     phase: TRAIN
  9   }
 13   hdf5_data_param {
 14     source: "mydata/train_list.txt"    //是个坑哦,下面下面解释;
 15     batch_size: 200
 17   }

上面的代码需要一地方解释:在定义.proto文件里的data层时注意,hdf5_data_param的source不要直接写我们生成的HDF5文件的路径,而是写一个.txt文件的,并在.txt文件里写入你生成的HDF5文件的路经,一个HDF5文件路径占一行,一定要这样哦。原因是因为,我们可以要读入多个HDF5文件,所以要这样写哦。

 

HDF5 Output层 类型为:HDF5Output

它的作用为把数据写成HDF5格式

 

另外还有,Images, Windows, Dummy.

 

激活函数层: 如果要用激活函数的话,就相当于增加一层哦

ReLU激活函数(rectified-linear and Leaky-ReLU):类型为ReLU.

在标准的ReLU激活函数中,当输入为x时,如果x>0,则输出 x,如果输入<=0,则输出0,即输出为max(0,x).

在非标准的ReLU激活函数中,当输入x<=0时, 输出为x * negative_slop(它是一个参数,默认为0).

 

sigmoid激活函数:类型为 Sigmoid.

这个不用多解释了.

 

tanh激活函数,类型为: TanH. 

这个也不用多解释了.

 

Abolute Value:类型为 AbsVal.

计算绝对值的函数.

 

power激活函数:类型为:Power.

The Power layer computes the output as (shift + scale * x) ^ power for each input element x.

 

BNLL激活函数:类型为 BNLL.    

The BNLL (binomial normal log likelihood) layer computes the output as log(1 + exp(x)) for each input element x.

 

 

caffe中的layer:

convolution层:

layer的类型为:Convolution.,它有很参数,具体可以看caffe.proto里的message ConvolutionParam{}的定义.

num_output :输出的 feature map的个数啦,是否有偏置项啦,是否有把图像的边缘补充/卷积核的大小./步长/权值如何填充/偏置如何填充等.

看一个例子:

layer {
  name: "conv1"
  type: "Convolution"
  bottom: "data"
  top: "conv1"
  # learning rate and decay multipliers for the filters
  param { lr_mult: 1 decay_mult: 1 }
  # learning rate and decay multipliers for the biases
  param { lr_mult: 2 decay_mult: 0 }
  convolution_param {
    num_output: 96     # learn 96 filters
    kernel_size: 11    # each filter is 11x11
    stride: 4          # step 4 pixels between each filter application
    weight_filler {
      type: "gaussian" # initialize the filters from a Gaussian
      std: 0.01        # distribution with stdev 0.01 (default mean: 0)
    }
    bias_filler {
      type: "constant" # initialize the biases to zero (0)
      value: 0
    }
  }
}

pooling层:类型为:Pooling

这一层也有很多参数选择, 如pooling的核的大小/步长/pad(即上面的是否为边界加一些默认的值), 还有pooling的方法:现在有max/ average/stochastic三个方法.,具体看一下caffe.proto里的定义.

layer {
  name: "pool1"
  type: "Pooling"
  bottom: "conv1"
  top: "pool1"
  pooling_param {
    pool: MAX
    kernel_size: 3 # pool over a 3x3 region
    stride: 2      # step two pixels (in the bottom blob) between pooling regions
  }
}

 

LRN层:,类型为LRN.      即local response normalizaiton,它的作用是通过normalizing 局部输入区域,达到侧向抑制的目的(为什么,还不确定,记得在论文里看到过类似的方法).它分为两种模式,一种是across-channels,一种是within_channels .在arcoss-channel里, 分选择在相邻的feature-map之间进行,它的区域差不多为local_size *1 *1, 如果选择within_channels的话,该操作会选择在同一个feature map上操作,它的区域相当于 1* local_size * local_size.image

Inner Product 层: 类型为:InnerProduct

它就是我们据说的全连接层.

 

 

caffe中的dropout 层

对于dropout层,说一下(论文中我们都会看到当我们有训练过程中dropout为(比如)0.3时,我们在测试时我们不会进行dropout,而是把输出乘以0.7。所以呢,caffe是怎么实现的呢?

当我们定义的net.prorotxt文件中,有了dropout这一层的时候,然后呢,caffe会根据你是训练还是测试进行不同的操作,当训练时,我们有一部分的神经元被dropout,然后,剩余的乘以1/(1-0.3). 然后,在测试时,dropout直接把数据进行从上一层复制到下一层,不进行操作。

明白了吧。。所以,我们不用在网络中进行定义 乘以0.7这一步。。你只需要分成训练还是测试就可以了。下面是一个dropout的例子:

layer {
name: "drop1"
type: "Dropout"
bottom: "ip11"
top: "ip11"
dropout_param {
dropout_ratio: 0.3
}
}

 

loss层:

在caffe中,默认的以loss结尾的layer可以作为loss层,但是中间的层同样可以作为loss层.原因是这样的:

有一个和这个相关的参数:loss_weight,它决定了你的每个loss层占最好的loss的大小.

在以loss结尾的layer里面, loss_wight的大小为1. 在不是以loss结尾的layer里面,它的loss_weight为0.

如:

layer {
  name: "loss"
  type: "SoftmaxWithLoss"
  bottom: "pred"
  bottom: "label"
  top: "loss"
  loss_weight: 1    #这个是默认的,可以不写的.
}

如果我们想在一个net里,包含多个loss层的话,我们就可以设置他们对应的loss_weight在大小,这就相当于一个权值.(如一个网络中,我们即用softmaxWithloss用来分类,也用EuclideanLoss用来计算重构输入的loss).

最后在计算总的loss的时候,它的输出可以用下面的伪代码表示:

loss := 0
for layer in layers:
  for top, loss_weight in layer.tops, layer.loss_weights:
    loss += loss_weight * sum(top)

 

softmax: 类型为:SoftmaxWithLoss

它的类型为:SoftmaxWithLoss.它其实就是一个 softmax层,然后跟了个multinomial logistic loss层. 它比单独用softmax层可以使梯度值更稳定.

 

sum-of-squares/也叫euclidean:

就是平时我们说的平方差代价函数.

 

hinge Loss: 类型:HingeLoss

最常用在 SVM 中的最大化间隔分类中等. hinge loss常分为1vs all hinge和squared hinge loss,即 L1 与L2hange.

# L1 Norm
layer {
  name: "loss"
  type: "HingeLoss"
  bottom: "pred"
  bottom: "label"
}

# L2 Norm
layer {
  name: "loss"
  type: "HingeLoss"
  bottom: "pred"
  bottom: "label"
  top: "loss"
  hinge_loss_param {
    norm: L2
  }
}

 

sigmoid cross-entropy loss:

就是平常所见的交叉熵损失函数. 类型:SigmoidCrossEntropyLoss

 

infogain loss:信息增益损失函数: ,类型:InfogainLoss

 

 

一些功能的layer:

splitting(把一个输入分成多个输出),类型:splitting

在caffe.proto里为什么没有找到呢.它的作用就是把一个输入复制为多个输入哦;

 

Flattening:类型为:Flatten

偏平的意思,如 flattens an input of shape n * c * h * w to a simple vector output of shape n * (c*h*w))。

 

Reshape:(重新调整维度),类型为:Reshape

 

Cocatenation(把多个输入可以串联起来):类型为:Concat

 

Slicing(可以对输入进行切片)

类型为:Slice: 它的作用是把输入按维度进行切片。具体看一个例子哈:

layer {
  name: "slicer_label"
  type: "Slice"
  bottom: "label"
  ## 假设label的维度是:N x 5 x 1 x 1
  top: "label1"
  top: "label2"
  top: "label3"
  top: "label4"
  slice_param {
    axis: 1                            # 指定维度,维度应该是从0开始的;
    slice_point: 1                 # 将label[~][:1][~][~]赋给label1
    slice_point: 2                 # 将label[~][1:3][~][~]赋给label2
    slice_point: 3                 # 将label[~][3:4][~][~]赋给label3
    slice_point: 4                #  将label[~][4:][~][~]赋给label3
  }
}

还有,切片的位置数,肯定比最后切出来的片数少一个哈。不用解释的啦。

 

 

另外还有:Elementwise Operations(类型为Eltwise), Argmax(类型为ArgMax), Softmax(类型为Softmax),

Mean-Variance Normalization(类型为MVN)

 

 

 

由于以上内容我只用到了一少部分,所以大部分没有详细写叱 ,随着不断的深入学习,我会再详细补充的,

 

 

 

 

  

 

 

原文链接: https://www.cnblogs.com/yinheyi/p/6070213.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    caffe中各层的作用:

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/395594

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月4日 上午9:52
下一篇 2023年4月4日 上午9:52

相关推荐