caffe框架:
结构:
Blob:stores data and derivatives
Layer: transform bottom blobs to top blobs
Net:Many layers;computes gradients via forward/backward
Solver:Uses gradients to updata weights
流程:
no need to write code
1.convert data (run a script) 数据转换:create LMDB using convert_imageset
2.define net (edit prototxt) 定义网络
3.define solver(edit prototxt)) 定义配置参数
4.train (run a script)
学习资源:Model Zoo
#定义层 例
name:""
layer{
name:""
trandform_param{
scale:0.03}
}
layer{
name:"conv1"
type:"Convolution"
bottom:"data"
top:"conv1"
param{
lr_mult:1
}
convolution_param{
num_output:20
kernel_size:5
stride:1
weight_filler{
type:"xavier" #权重初始化方式
}
bias_filler{
type:"constant"
}
}
}
layer{
name:"pool1"
type:"Pooling"
bottom:"conv1"
top:"pool1"
pooling_param{
pool:MAX
kernel_size:2
stride:2
}
}
#训练 例
./bulid/tools/caffe train \
-gpu 0 \
-model path/to/trainval.prototxt \
-solver
-weights
#卷积神经网络各层示例
#数据层
layer{
name:" " #网络名称
type="Data"
top="data" #用bottom表示输入,top表示输出,多个top代表多个输出
top="label"
include{
phase:TRAIN #训练网络分为训练阶段和自测试阶段,如果没写include则表示该层既在测试层又在训练层
}
transform_param{
mean_file:"examples/cifar10/mean.binaryproto" #用一个配置文件进行均值的操作 R,G,B三通道
transform_param{
scale:0.0039 #1/255 (0-255)压缩到(0,1)
mirror:1 #1表示开启镜像 0表示关闭 图像翻转
#裁剪一个227*224的图块,在训练阶段随机裁剪,在测试阶段从中间裁剪
crop_size : 227
}
}
data_param{
source:"examples/cifar10/train_lmb" #数据库来源
batch_size:100 #每次批处理的个数
backend:LMDB #选用数据的名称
}
}
#卷积层
layer{
name:"conv1"
type:"Convolution"
bottom:"data"
top:"conv1"
param{
lr_mult:1
}
param{
lr_mult:2
}
convolution_param{
num_output:20 #卷积核(filter)的个数
kernael_size:5#卷积核的大小
stride:1 #卷积核的步长,默认为1
pad:0 #扩充边缘,默认为0,不扩充
weight_filler{
type:"xavier"#权值初始化。 默认为"constant", 值全为0.
}
bias_filler{
type:"constant" #偏置项的初始化。一般设置为"constant",值全为0
}
}
}
输入: n*c0*w0*h0
输出: n*c1*w1*h1
#池化层
layer{
name:"pool1"
type:"Pooling"
bottom:"conv1"
top:"pool1"
pooling_param{
pool:MAX #池化方法 目前可用的方法有max,ave
kernel_size:3#池化的核大小
stride:2 #池化的步长,默认为1。
}
}
#激活函数
layer{
name:"test"
bottom:"conv"
top:"test"
type:"Sigmoid"
}
#Relu是目前使用最多的激活函数,主要因为其收敛快,并能保持同样效果
f(x)=max(x,0)
layer{
name:"relu1"
type:"ReLU"
bottom:"pool1"
top:"pool1"
}
#全连接层
layer{
name:"ip1"
type:"InnerProduct"
bottom:"pool2"
top:"ip1"
param{
lr_mult:1
}
param{
lr_mult:2
}
inner_product_param{
num_output:500
weight_filler{
type:"xavier"
}
bias_fiiler{
type:"constant"
}
}
}
#测试准确率
layer{
name:"accuracy"
type:"Accuracy"
bottom:"ip2"
bottom:"label"
top:"accuracy"
include{
phase:TEST
}
}
#输出loss值
layer{
name:"loss"
type:"SoftmaxWithLoss"
bottom:"ip1"
bottom:"label"
top:"loss"
}
#输出似然值
layer{
bottom:"cls3_fc"
top:"prob"
name:"prob"
type:"Softmax"
}
#改变数据维度
layer{
name:"reshape"
type:"Reshape"
bottom:"input"
top:"output"
reshape_param{
shape{
dim:0 #维度不变
dim:x #维度变为x
dim:-1 #维度机器去猜
}
}
}
#dropout层
layer{
name:"drop7"
type:"Dropout"
bottom:"fc7-conv"
top:""
dropout_param{
dropout_ratio:0.5
}
}
#往往loss function是非凸的
#caffe提供了六种优化算法来求解最优参数
Stochastic Gradient Descent(type: “SGD”)
AdaDelta(type:“AdaDelta”)
Adaptive Gradient(type:“AdaGrad”)
Adam(type:“Adam”)
Nesterov’s Accelerated Gradient(type:“Nesterov”)
RMSprop(type:“RMSProp”)
net:" .prototxt" #网络位置
test_iter:100 #一次迭代多少样本 batchsize * test_iter = 测试总量
test_interval:500 #测试间隔
base_lr:0.01 #设置基础学习率
lr_policy:"inv" #学习率调整的策略
-fixed:保持base_lr不变
-step: 如果设置为step,则还需要设置一个stepsize
-exp:
-inv:
momentum:0.9 #动量
display:100 #每训练100次,在屏幕上显示一次。
max_iter:20000 #最大迭代次数
snapshot:5000 #快照。将训练出来的model和solver状态进行保存.
solver_mode:CPU #设置运行模式,默认为gpu
制作数据源
#制作LMDB数据源
EXAMPLE = #新建文件夹位置
DATA=
TOOLS= #caffe安装的路径
TRAIN_DATA_ROOT =
VAL_DATA_ROOT =
train.txt #从子文件夹开始写
val.txt #直接写文件名称加label
制作: sh face-lmdb.sh(cmd窗口下找到当前文件夹)
训练: sh train.sh
label #回归任务 找关键点 座标(),(),()
##使用HDF5数据源 多label情况
layer{
name:"data"
type:"HDF5Data"
top:"data"
top:"label"
hdf5_data_param{
source:"examples/hdf5_classification/data/train.txt"
batch_size:10
}
}