caffe的model是需要用prototxt来定义的,如果要训练自己的model我们就需要自己来写prototxt文件,这篇博文主要写caffe的layer是如何来定义的。
参考:http://caffe.berkeleyvision.org/tutorial/layers.html
layer的源代码在$caffe-master/src/caffe/layers
卷积层
- layer类型:
Convolution
- 参数:(
ConvolutionParameter convolution_param
)- 必选
num_output
(c_o
):filter的的数目kernel_size
(orkernel_h
andkernel_w
):filter的尺寸,即长、宽
- 推荐
-
weight_filler
:对权值的初始化设置,默认是{type:"constant" value:0},即初始化为常数0。type也可以选择高斯分布,这需要给定标准差和均值
-
- 可选
bias_term
[defaulttrue
]:是否对filter设定偏置项,默认为truepad
(orpad_h
andpad_w
) [default 0]:对输入的每个边进行填充像素的个数,默认为0-
stride
(orstride_h
andstride_w
) [default 1]:filter在输入图像上的滑动步长 -
group
(g) [default 1]:当g>1时,将输入和输出的channels分为g个组,第i个输入group channels只与第i个输出group channels相对应。
- 必选
- 输入
n * c_i * h_i * w_i
- 输出
n * c_o * h_o * w_o
,其中h_o = (h_i + 2 * pad_h - kernel_h) / stride_h + 1
,w_o也类似
layer {
name: "conv1" //层的名字
type: "Convolution" //层的类型
bottom: "data" //底层的blob
top: "conv1" //上层的blob
//filters的学习率和衰减参数
param { lr_mult: 1 decay_mult: 1 }
// 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" // 用Gaussian分布初始化
std: 0.01 // 标准差为0.01,均值默认为0
}
bias_filler {
type: "constant" //初始化为常数
value: 0
}
}
}
池化层
- layer类型:
Pooling
- 参数:
PoolingParameter pooling_param
- 必选
kernel_size
:filter的尺寸
- 可选
-
pool
[default MAX]:pool的类型,MAX、AVE或者 STOCHASTIC pad
(orpad_h
andpad_w
) [default 0]:对输入的每个边进行填充像素的个数,默认为0stride
(orstride_h
andstride_w
) [default 1]:filter在输入图像上的滑动步长
-
- 必选
- 输入
n * c_i * h_i * w_i
- 输出
n * c_o * h_o * w_o
,其中h_o = (h_i + 2 * pad_h - kernel_h) / stride_h + 1
,w_o也类似
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
}
}
ReLU层
layer {
name: "relu1"
type: "ReLU"
bottom: "conv1"
top: "conv1"
}
Dropout层
layer {
name: "drop7"
type: "Dropout"
bottom: "fc7"
top: "fc7"
dropout_param {
dropout_ratio: 0.5 //dropout率
}
}
全连接层
- layer类型:
Pooling
- 参数:
InnerProductParameter inner_product_param
- 必选
kernel_size
:filter的尺寸
- 推荐
-
weight_filler
:对权值的初始化设置,默认是{type:"constant" value:0},即初始化为常数0。type也可以选择高斯分布,这需要给定标准差和均值
-
- 可选
-
bias_term
[defaulttrue
]:是否对filter设定偏置项,默认为true -
bias_filler
[defaulttype: 'constant' value: 0
]
-
- 必选
- 输入
n * c_i * h_i * w_i
- 输出
n * c_o * h_o * w_o
,其中h_o = (h_i + 2 * pad_h - kernel_h) / stride_h + 1
,w_o也类似
layer {
name: "fc8"
type: "InnerProduct"
param { lr_mult: 1 decay_mult: 1 }
param { lr_mult: 2 decay_mult: 0 }
inner_product_param {
num_output: 1000
weight_filler {
type: "gaussian"
std: 0.01
}
bias_filler {
type: "constant"
value: 0
}
}
bottom: "fc7"
top: "fc8"
}
SoftMax层
layer {
name: "loss"
type: "SoftmaxWithLoss"
bottom: "fc8"
bottom: "label"
top: "loss"
}
Accuracy层
layer {
name: "accuracy"
type: "Accuracy"
bottom: "fc8"
bottom: "label"
top: "accuracy"
include {
phase: TEST
}
}