Caffe之prototxt

1、可视化工具:

http://ethereon.github.io/netscope/quickstart.html

2、常用网络模型caffe-model之.prototxt:

https://github.com/soeaver/caffe-model

3、python生成.prototxt文件工具:

http://blog.csdn.net/c406495762/article/details/70306550

4、caffe的.prototxt文件解读

https://wenku.baidu.com/view/a38c6aae5901020207409cde.html

5、caffe源码文件的.prototxt

https://github.com/BVLC/caffe

6 .prototxt:网络结构定义个别解析

1. 数据层即输入层。

在caffe中数据以blob的格式进行存储和传输,在这一层中是实现数据其他格式与blob之间的转换,例如从高效的数据库lmdb或者level-db转换为blob,也可以从低效的数据格式如hdf5或者图片。

另外数据的预处理也在本层实现,减去均值, 放大缩小, 裁剪和镜像等。以Lenet_train_test.prototxt为例:

[plain]  view plain  copy
  1. name: "LeNet"  
  2. layer {  
  3.   name: "mnist"  
  4.   type: "Data"  
  5.   top: "data"  
  6.   top: "label"  
  7.   include {  
  8.     phase: TRAIN  
  9.   }  
  10.   transform_param {  
  11.     scale: 0.00390625  
  12.   }  
  13.   data_param {  
  14.     source: "examples/mnist/mnist_train_lmdb"  
  15.     batch_size: 64  
  16.     backend: LMDB  
  17.   }  
  18. }  
最上面name :网络名称,可自己定义。

数据层layer的定义:

name: 可自己取

type:层的类型。

(1)Data:数据来源于LevelDB或者LMDB,必须设置batch_size。source为包含数据库的目录名称,如examples/mnist/mnist_train_lmdb

(2)MemoryData: 数据来源于内存,必须设置batch_size, channels, width, height.

[html]  view plain  copy
  1. layer {  
  2.   top: "data"  
  3.   top: "label"  
  4.   name: "memory_data"  
  5.   type: "MemoryData"  
  6.   memory_data_param{  
  7.     batch_size: 2  
  8.     height: 100  
  9.     width: 100  
  10.     channels: 1  
  11.   }  
  12.   transform_param {  
  13.     scale: 0.0078125  
  14.     mean_file: "mean.proto"  
  15.     mirror: false  
  16.   }  
  17. }  
(3)HDF5Data: 数据来源于Hdf5, 必须设置batch_size和source,读取的文件名称

[html]  view plain  copy
  1. layer {  
  2.   name: "data"  
  3.   type: "HDF5Data"  
  4.   top: "data"  
  5.   top: "label"  
  6.   hdf5_data_param {  
  7.     source: "examples/hdf5_classification/data/train.txt"  
  8.     batch_size: 10  
  9.   }  
  10. }  
(4)ImageData: 数据来源于图片。

必须设置的参数:

  source: 每一行是给定的图片路径和标签; 

  batch_size

可选设置的参数为:

  rand_skip: 在开始的时候,路过某个数据的输入。通常对异步的SGD很有用。

  shuffle: 随机打乱顺序,默认值为false

  new_height,new_width: 如果设置,则将图片进行resize

[html]  view plain  copy
  1. layer {  
  2.   name: "data"  
  3.   type: "ImageData"  
  4.   top: "data"  
  5.   top: "label"  
  6.   transform_param {  
  7.     mirror: false  
  8.     crop_size: 227  
  9.     mean_file: "data/ilsvrc12/imagenet_mean.binaryproto"  
  10.   }  
  11.   image_data_param {  
  12.     source: "examples/_temp/file_list.txt"  
  13.     batch_size: 50  
  14.     new_height: 256  
  15.     new_width: 256  
  16.   }  
  17. }  

(5)WindowData: 来源于windows

[html]  view plain  copy
  1. layer {  
  2.   name: "data"  
  3.   type: "WindowData"  
  4.   top: "data"  
  5.   top: "label"  
  6.   include {  
  7.     phase: TRAIN  
  8.   }  
  9.   transform_param {  
  10.     mirror: true  
  11.     crop_size: 227  
  12.     mean_file: "data/ilsvrc12/imagenet_mean.binaryproto"  
  13.   }  
  14.   window_data_param {  
  15.     source: "examples/finetune_pascal_detection/window_file_2007_trainval.txt"  
  16.     batch_size: 128  
  17.     fg_threshold: 0.5  
  18.     bg_threshold: 0.5  
  19.     fg_fraction: 0.25  
  20.     context_pad: 16  
  21.     crop_mode: "warp"  
  22.   }  
  23. }  

top:本层的输出,例子表明有两个输出,data和label是分类问题所必须的

bottom:本层的输入

include:在其中规定是训练还是测试的层。如果没有定义则表明训练和测试均有此层。如例,此层为训练层,有训练数据和标签

transform_param:数据预处理,scale表明对数据由0-255转换到了[0,1)。mirror(1表示开启,0表示关闭), mean_file_size(后面跟配置文件mean.binaryproto, 进行去均值的处理),crop_size(剪裁,训练数据随机剪裁,测试数据从中间剪裁)

data_param:定义数据,source是数据路径;将全部的图片分为不同的批次batch,batch_size是一个批次包含的图片数目;backend表明所用的数据库

2. 视觉层

包括convolution卷积层, pooling池化层, Local Response Normalization (LRN)局部极大值抑制, im2col等层。

(1)层类型:Convolution,如lenet的第一个卷积层

[html]  view plain  copy
  1. layer {  
  2.   
  3.   name: "conv1"  
  4.   type: "Convolution"  
  5.   bottom: "data"  
  6.   top: "conv1"  
  7.   param {  
  8.     lr_mult: 1    #权重w的学习率的系数,学习率=base_lr(定义在solver.prototxt)×lr_mult
  9.   }  
  10.   param {  
  11.     lr_mult: 2          #表示偏重bias的学习率系数
  12.   }  
  13.   convolution_param {  
  14.     num_output: 20     #卷积核kernel的个数
  15.     kernel_size: 5     #kernel大小,如果卷积核的长和宽不等,需要用kernel_h和kernel_w分别设定
  16.     stride: 1          #卷积运算的步长,默认为1。也可以用stride_h和stride_w来设置
  17.     weight_filler {  
  18.       type: "xavier"  
  19.     }  
  20.     bias_filler {  
  21.       type: "constant"  
  22.     }  
  23.   }  
  24. }  
pad: 填充边缘的大小。如设置,可是得到的特征图与原图大小相等,pad_h和pad_w来分别设定

weight_filler: 权值初始化,若设置为constant, 则默认为0。也可使用"xavier"或者”gaussian"进行初始化

bias_filler: 偏置项的初始化,与weight_filter类似

bias_term: 是否开启偏置项(0或1)

group: 分组,默认为1组。如果大于1,我们限制卷积的连接操作在一个子集内。如果我们根据图像的通道来分组,那么第i个输出分组只能与第i个输入分组进行连接。

(2)层类型:Pooling。
[html]  view plain  copy
  1. layer {  
  2.   
  3.   name: "pool1"  
  4.   type: "Pooling"  
  5.   bottom: "conv1"  
  6.   top: "pool1"  
  7.   pooling_param {  
  8.     pool: MAX  
  9.     kernel_size: 2   #必须设置的参数:池化的核大小。也可以用kernel_h和kernel_w分别设定。
  10.     stride: 2  
  11.   }  
  12. }  
 pool: 池化方法,默认为MAX。目前可用的方法有MAX, AVE, 或STOCHASTIC
 pad: 和卷积层的pad的一样,进行边缘扩充。默认为0
 stride: 池化的步长,默认为1。一般我们设置为2,即不重叠。也可以用stride_h和stride_w来设置。

(3)层类型:LRN 

[html]  view plain  copy
  1.  
  2. layers {  
  3.   name: "norm1"  
  4.   type: LRN  
  5.   bottom: "pool1"  
  6.   top: "norm1"  
  7.   lrn_param {  
  8.     local_size: 5  
  9.     alpha: 0.0001  
  10.     beta: 0.75  
  11.   }  
  12. }  
local_size: 默认为5。如果是跨通道LRN,则表示求和的通道数;如果是在通道内LRN,则表示求和的正方形区域长度。

alpha: 默认为1,归一化公式中的参数。

beta: 默认为5,归一化公式中的参数。

norm_region:  默认为ACROSS_CHANNELS。

 1.ACROSS_CHANNELS表示在相邻的通道间求和归一化。

2.WITHIN_CHANNEL表示在一个通道内部特定的区域内进行求和归一化。与前面的local_size参数对应。


归一化公式为:除以


(4)层类型:img2col

 将一个大矩阵,重叠地划分为多个子矩阵,对每个子矩阵序列化成向量,最后得到另外一个矩阵。

在caffe中,卷积运算就是先对数据进行im2col操作,再进行内积运算(inner product)。这样做,比原始的卷积操作速度更快。

看看两种卷积操作的异同:

3. 激活层

对输入数据进行激活操作,常用的激活函数有:Sigmoid、TanH、AbsVal(Absolute Value)RELU(ReLU / Rectified-Linear and Leaky-ReLU)收敛速度最快。

[html]  view plain  copy
  1. layer {  
  2.   
  3.   name: "relu1"  
  4.   type: "ReLU"  
  5.   bottom: "ip1"  
  6.   top: "ip1"  
  7. }  
RELU函数为:max(x, 0)
可选参数:
  negative_slope:默认为0. 对标准的ReLU函数进行变化,如果设置了这个值,那么数据为负数时,就不再设置为0,而是用原始数据乘以negative_slope
Power:f(x)= (shift + scale * x) ^ power

[html]  view plain  copy
  1. layer {  
  2.   name: "layer"  
  3.   bottom: "in"  
  4.   top: "out"  
  5.   type: "Power"  
  6.   power_param {  
  7.     power: 2  
  8.     scale: 1  
  9.     shift: 0  
  10.   }  
  11. }  

BNLL: binomial normal log likelihood

[html]  view plain  copy
  1. layer {  
  2.   name: "layer"  
  3.   bottom: "in"  
  4.   top: "out"  
  5.   type: “BNLL”  
  6. }  

4. 其他层

softmax_loss层,Inner Product层,accuracy层,reshape层和dropout层
(1)softmax_loss层

[html]  view plain  copy
  1. layer {  
  2.   name: "loss"  
  3.   type: "SoftmaxWithLoss"  
  4.   bottom: "ip2"  
  5.   bottom: "label"  
  6.   top: "loss"  
  7. }  

(2)全连接层,把输入当作成一个向量,输出也是一个简单向量(把输入数据blobs的width和height全变为1)。

输入: n*c0*h*w

输出: n*c1*1*1

全连接层实际上也是一种卷积层,只是它的卷积核大小和原数据大小一致。因此它的参数基本和卷积层的参数一样。
[html]  view plain  copy
  1. layer {  
  2.   name: "ip2"  
  3.   type: "InnerProduct"  
  4.   bottom: "ip1"  
  5.   top: "ip2"  
  6.   param {  
  7.     lr_mult: 1  
  8.   }  
  9.   param {  
  10.     lr_mult: 2  
  11.   }  
  12.   inner_product_param {  
  13.     num_output: 10  
  14.     weight_filler {  
  15.       type: "xavier"  
  16.     }  
  17.     bias_filler {  
  18.       type: "constant"  
  19.     }  
  20.   }  
  21. }  
(3)accuracy,只有测试阶段才有
[html]  view plain  copy
  1. layer {  
  2.   name: "accuracy"  
  3.   type: "Accuracy"  
  4.   bottom: "ip2"  
  5.   bottom: "label"  
  6.   top: "accuracy"  
  7.   include {  
  8.     phase: TEST  
  9.   }  
  10. }  

(4)Reshape层,改变数据维度

[html]  view plain  copy
  1. layer {  
  2.     name: "reshape"  
  3.     type: "Reshape"  
  4.     bottom: "input"  
  5.     top: "output"  
  6.     reshape_param {  
  7.       shape {  
  8.         dim: 0  # copy the dimension from below  维度不变
  9.         dim: 2   #维度变为2
  10.         dim: 3   #维度变为3
  11.         dim: -1 # infer it from the other dimensions 计算出来的(总数不变) 
  12.       }  
  13.     }  
  14.   }  

(5)Dropout层, 防止过拟合,可以随机让网络某些隐含层节点的权重不工作。

[html]  view plain  copy
  1. layer {  
  2.   name: "drop7"  
  3.   type: "Dropout"  
  4.   bottom: "fc7-conv"  
  5.   top: "fc7-conv"  
  6.   dropout_param {  
  7.     dropout_ratio: 0.5  
  8.   }  



  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

女王の专属领地

您的鼓励是我最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值