首先建立一个net,net有多层构成,层有不同的类型。网络结构定义在.prototxt文件中。下面详细介绍:
1. 数据层即输入层。在caffe中数据以blob的格式进行存储和传输,在这一层中是实现数据其他格式与blob之间的转换,例如从高效的数据库lmdb或者level-db转换为blob,也可以从低效的数据格式如hdf5或者图片。另外数据的预处理也在本层实现,如减去均值, 放大缩小, 裁剪和镜像等。以Lenet_train_test.prototxt为例:
name: "LeNet"
layer {
name: "mnist"
type: "Data"
top: "data"
top: "label"
include {
phase: TRAIN
}
transform_param {
scale: 0.00390625
}
data_param {
source: "examples/mnist/mnist_train_lmdb"
batch_size: 64
backend: LMDB
}
}
最上面name:网络名称,可自己定义。下面是数据层layer的定义
name: 可自己取
type:层的类型。如果是Data,表示数据来源于LevelDB或LMDB。根据数据的来源不同,数据层的类型也不同。
(1)Data:数据来源于LevelDB或者LMDB,必须设置batch_size。source为包含数据库的目录名称,如examples/mnist/mnist_train_lmdb
(2)MemoryData: 数据来源于内存,必须设置batch_size, channels, width, height.
layer {
top: "data"
top: "label"
name: "memory_data"
type: "MemoryData"
memory_data_param{
batch_size: 2
height: 100
width: 100
channels: 1
}
transform_param {
scale: 0.0078125
mean_file: "mean.proto"
mirror: false
}
}
layer {
name: "data"
type: "HDF5Data"
top: "data"
top: "label"
hdf5_data_param {
source: "examples/hdf5_classification/data/train.txt"
batch_size: 10
}
}
(4)ImageData: 数据来源于图片。必须设置的参数:
source: 每一行是给定的图片路径和标签;
batch_size
可选设置的参数为:
rand_skip: 在开始的时候,路过某个数据的输入。通常对异步的SGD很有用。
shuffle: 随机打乱顺序,默认值为false
new_height,new_width: 如果设置,则将图片进行resize
layer {
name: "data"
type: "ImageData"
top: "data"
top: "label"
transform_param {
mirror: false
crop_size: 227
mean_file: "data/ilsvrc12/imagenet_mean.binaryproto"
}
image_data_param {
source: "examples/_temp/file_list.txt"
batch_size: 50
new_height: 256
new_width: 256
}
}
(5)WindowData: 来源于windows
layer {
name: "data"
type: "WindowData"
top: "data"
top: "label"
include {
phase: TRAIN
}
transform_param {
mirror: true
crop_size: 227
mean_file: "data/ilsvrc12/imagenet_mean.binaryproto"
}
window_data_param {
source: "examples/finetune_pascal_detection/window_file_2007_trainval.txt"
batch_size: 128
fg_threshold: 0.5
bg_threshold: 0.5
fg_fraction: 0.25
context_pad: 16
crop_mode: "warp"
}
}
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的第一个卷积层
layer {
name: "conv1"
type: "Convolution"
bottom: "data"
top: "conv1"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
convolution_param {
num_output: 20
kernel_size: 5
stride: 1
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
lr_mult:第一个表示权重w的学习率的系数,学习率=base_lr(定义在solver.prototxt)×lr_mult, 第二个表示偏重bias的学习率系数
layer {
name: "pool1"
type: "Pooling"
bottom: "conv1"
top: "pool1"
pooling_param {
pool: MAX
kernel_size: 2
stride: 2
}
}
必须设置的参数:
(3)层类型LRN
layers {
name: "norm1"
type: LRN
bottom: "pool1"
top: "norm1"
lrn_param {
local_size: 5
alpha: 0.0001
beta: 0.75
}
}
local_size: 默认为5。如果是跨通道LRN,则表示求和的通道数;如果是在通道内LRN,则表示求和的正方形区域长度。
layer {
name: "relu1"
type: "ReLU"
bottom: "ip1"
top: "ip1"
}
layer {
name: "layer"
bottom: "in"
top: "out"
type: "Power"
power_param {
power: 2
scale: 1
shift: 0
}
}
layer {
name: "layer"
bottom: "in"
top: "out"
type: “BNLL”
}
4. 其他层:softmax_loss层,Inner Product层,accuracy层,reshape层和dropout层
layer {
name: "loss"
type: "SoftmaxWithLoss"
bottom: "ip2"
bottom: "label"
top: "loss"
}
layer {
name: "ip2"
type: "InnerProduct"
bottom: "ip1"
top: "ip2"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {
num_output: 10
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
(3)accuracy,只有测试阶段才有
layer {
name: "accuracy"
type: "Accuracy"
bottom: "ip2"
bottom: "label"
top: "accuracy"
include {
phase: TEST
}
}
layer {
name: "reshape"
type: "Reshape"
bottom: "input"
top: "output"
reshape_param {
shape {
dim: 0 # copy the dimension from below
dim: 2
dim: 3
dim: -1 # infer it from the other dimensions
}
}
}
(5)Dropout层, 防止过拟合,可以随机让网络某些隐含层节点的权重不工作。
layer {
name: "drop7"
type: "Dropout"
bottom: "fc7-conv"
top: "fc7-conv"
dropout_param {
dropout_ratio: 0.5
}