资讯动态

【caffe】Caffe:常见层的书写方式

发布时间:2026/10/22 12:21:58 来源:尧图企业网站定制
1、Convolution层就是卷积层是卷积神经网络CNN的核心层。层类型Convolutionlr_mult: 学习率的系数最终的学习率是这个数乘以solver.prototxt配置文件中的base_lr。如果有两个lr_mult, 则第一个表示权值的学习率第二个表示偏置项的学习率。一般偏置项的学习率是权值学习率的两倍。在后面的convolution_param中我们可以设定卷积层的特有参数。必须设置的参数num_output: 卷积核filter)的个数kernel_size: 卷积核的大小。如果卷积核的长和宽不等需要用kernel_h和kernel_w分别设定其它参数stride: 卷积核的步长默认为1。也可以用stride_h和stride_w来设置。pad: 扩充边缘默认为0不扩充。 扩充的时候是左右、上下对称的比如卷积核的大小为5*5那么pad设置为2则四个边缘都扩充2个像素即宽度和高度都扩充了4个像素,这样卷积运算之后的特征图就不会变小。也可以通过pad_h和pad_w来分别设定。weight_filler: 权值初始化。 默认为“constant,值全为0很多时候我们用xavier算法来进行初始化也可以设置为”gaussianbias_filler: 偏置项的初始化。一般设置为constant,值全为0。bias_term: 是否开启偏置项默认为true, 开启group: 分组默认为1组。如果大于1我们限制卷积的连接操作在一个子集内。如果我们根据图像的通道来分组那么第i个输出分组只能与第i个输入分组进行连接。输入n * c0 * w0 * h0输出n * c1 * w1 * h1其中c1就是参数中的num_output生成的特征图个数w1(w02 * pad-kernel_size)/stride1;h1(h02 * pad-kernel_size)/stride1;如果设置stride为1前后两次卷积部分存在重叠。如果设置pad(kernel_size-1)/2,则运算后宽度和高度不变。2 BatchNormcaffeproto 中 BatchNorm 的定义message LayerParameter{optional BatchNormParameter batch_norm_param139;}message BatchNormParameter{// If false, normalization is performed over the current mini-batch// and global statistics are accumulated (but not yet used) by a moving// average.// 如果 use_global_stats 0则对当前 mini-batch 内的数据归一化 同时 global statistics 通过滑动平均逐渐累加.// If true, those accumulated mean and variance values are used for the// normalization.// 如果 use_global_stats 1则采用累加的 均值和方差 对数据进行归一化.// By default, it is set to false when the network is in the training// phase and true when the network is in the testing phase.// 默认情况下网络训练时 use_global_stats 0网络测试时 use_global_stats 1.optional bool use_global_stats1;// What fraction of the moving average remains each iteration?// 滑动平均时每次迭代保留的百分比// Smaller values make the moving average decay faster, giving more// weight to the recent values.// 较小的值使得平均累加过程衰退较快给予最近的值较大的权重// Each iteration updates the moving average f$S_{t- 1}f$ with the// current mean f$ Y_t f$ by// f$ S_t (1-\beta)Y_t \beta \cdot S_{t-1} f$, where f$ \beta f$// is the moving_average_fraction parameter.optionalfloatmoving_average_fraction2[default.999];// Small value to add to the variance estimate so that we dont divide by// zero.// 保持数值稳定性optionalfloateps3[default1e-5];}filter类型先上proto上关于FillerParameter的数据结构描述对应的Caffe源码的头文件为filler.hpp。message FillerParameter{// The filler type.optional string type1[defaultconstant];optionalfloatvalue2[default0];// the value in constant filleroptionalfloatmin3[default0];// the min value in uniform filleroptionalfloatmax4[default1];// the max value in uniform filleroptionalfloatmean5[default0];// the mean value in Gaussian filleroptionalfloatstd6[default1];// the std value in Gaussian filler// The expected number of non-zero output weights for a given input in// Gaussian filler -- the default -1 means dont perform sparsification.optional int32 sparse7[default-1];// Normalize the filler variance by fan_in, fan_out, or their average.// Applies to xavier and msra fillers.enumVarianceNorm{FAN_IN0;FAN_OUT1;AVERAGE2;}optional VarianceNorm variance_norm8[defaultFAN_IN];}从上面我们可以看到有关的filler参数及其默认值。如默认filler填充类型为constantvalue默认值为0xavier和msra的VarianceNorm类型默认为FAN_IN等等。重点注意下sparse这个参数它标志着初始化的数据有没有稀疏性。Constant作用默认将Blob系数x初始化为0。如果定义了value的值value a则x a。不支持sparseGaussian作用默认将Blob系数x初始化为满足mean0std1的高斯分布x∼N(mean,std2)x∼N(mean,std2)。mean和std的值可自定义支持sparse。Positive_unitball作用默认将Blob系数x初始化为满足x∈[0,1]x∈[0,1]∀i∑jxij1∀i∑jxij1.不支持sparseUniform作用默认将Blob系数x初始化为满足min0,max1的均匀分布。x∼U(min,max)x∼U(min,max)不支持sparseXavier不适用于inner product layers.作用默认将Blob系数x初始化为满足x∼U(−a,a)x∼U(−a,a)的均匀分布 其中 aa sqrt(3 / n)。不支持sparse假设输入blob的shape为num, a, b, c。对于n的取值下面分三种情况FAN_IN: 默认为这种类型。该类型下n a * b * cFAN_OUT: n num * b * cAVERAGE: n ( FAN_IN FAN_OUT )/2参考paper [Bengio and Glorot 2010]: Understanding the difficulty of training deep feedforward neural networks.Msra不适用于inner product layers.作用默认将Blob系数x初始化为满足x∼N(0,σ2)x∼N(0,σ2)的高斯分布其中σσ sqrt(2 / n)。和Xavier一样对于n的取值分为三种类型FAN_IN: 默认为这种类型。该类型下n a * b * cFAN_OUT: n num * b * cAVERAGE: n ( FAN_IN FAN_OUT )/2参考paper [He, Zhang, Ren and Sun 2015]: Specifically accounts for ReLU nonlinearities.Bilinear作用一般用在deconvolution 层做upsampling例子如下layer { name: upsample, type: Deconvolution bottom: {{bottom_name}} top: {{top_name}} convolution_param { kernel_size: {{2 * factor - factor % 2}} stride: {{factor}} num_output: {{C}} group: {{C}} pad: {{ceil((factor - 1) / 2.)}} weight_filler: { type: bilinear } bias_term: false } param { lr_mult: 0 decay_mult: 0 } }参考地址https://blog.csdn.net/zziahgf/article/details/78843350https://blog.csdn.net/wenlin33/article/details/53378613

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价 →
↑