caffe:自己的数据训练模型 (四)

来源:互联网 发布:京东校招java面试 编辑:程序博客网 时间:2024/04/30 22:13

(1)数据准备好,标签制作好之后,转数据格式,求均值文件


在Ubuntu下,用.sh文件转格式

#!/usr/bin/env sh# Create the imagenet lmdb inputs# N.B. set the path to the imagenet train + val data dirs# EXAMPLE: storage directory of coverted LMDB dataEXAMPLE=/maoz/converted# location of train and val labels' textsDATA=/maoz# convert tool from caffe#TOOLS=/caffe/build/TOOLSTOOLS=/caffe-fast-rcnn/tools# location of train imagesTRAIN_DATA_ROOT=/maoz/TrainExpd/# location of val imagesVAL_DATA_ROOT=/maoz/ValExpd/# Set RESIZE=true to resize the images to 224x224. Leave as false if images have# already been resized using another tool.RESIZE=falseif $RESIZE; then  RESIZE_HEIGHT=224  RESIZE_WIDTH=224else  RESIZE_HEIGHT=0  RESIZE_WIDTH=0fiif [ ! -d "$TRAIN_DATA_ROOT" ]; then  echo "Error: TRAIN_DATA_ROOT is not a path to a directory: $TRAIN_DATA_ROOT"  echo "Set the TRAIN_DATA_ROOT variable in create_imagenet.sh to the path" \       "where the ImageNet training data is stored."  exit 1fiif [ ! -d "$VAL_DATA_ROOT" ]; then  echo "Error: VAL_DATA_ROOT is not a path to a directory: $VAL_DATA_ROOT"  echo "Set the VAL_DATA_ROOT variable in create_imagenet.sh to the path" \       "where the ImageNet validation data is stored."  exit 1fiecho "Creating train lmdb..."GLOG_logtostderr=1 $TOOLS/convert_imageset \    --resize_height=$RESIZE_HEIGHT \    --resize_width=$RESIZE_WIDTH \    --shuffle \    $TRAIN_DATA_ROOT \    $DATA/train.txt \    $EXAMPLE/train_lmdbecho "Creating val lmdb..."GLOG_logtostderr=1 $TOOLS/convert_imageset \    --resize_height=$RESIZE_HEIGHT \    --resize_width=$RESIZE_WIDTH \    --shuffle \    $VAL_DATA_ROOT \    $DATA/val.txt \    $EXAMPLE/val_lmdbecho "Done."echo "Creating mean.binary..."$TOOLS/compute_image_mean $EXAMPLE/train_lmdb \$EXAMPLE/mean.binaryprotopython /maoz/meanbinary2npy.py $EXAMPLE/mean.binaryproto $EXAMPLE/mean.npyecho "Done."

(2)网络定义,训练模型

在配置好的自己的caffe目录下可以看googlenet的例子:

主要是三个文件:train_val.prototxt, solver.protxt:

① 设置train_val.prototxt文件

②设置solver.prototxt文件

solver文件配置参考:http://blog.csdn.net/qq_26898461/article/details/50445392


部分参数理解:

net:train_val.prototxt的路径,snapshot_prefix: 训练好的模型的存放路径

solver_mode:可选GPU或者CPU


caffe提供了六种优化算法来求解最优参数,在solver配置文件中,通过设置type类型来选择。

  • Stochastic Gradient Descent (type: "SGD"),
  • AdaDelta (type: "AdaDelta"),
  • Adaptive Gradient (type: "AdaGrad"),
  • Adam (type: "Adam"),
  • Nesterov’s Accelerated Gradient (type: "Nesterov") and
  • RMSprop (type: "RMSProp")

test_iter: 100

这个要与test layer中的batch_size结合起来理解。mnist数据中测试样本总数为10000,一次性执行全部数据效率很低,因此我们将测试数据分成几个批次来执行,每个批次的数量就是batch_size。假设我们设置batch_size为100,则需要迭代100次才能将10000个数据全部执行完。因此test_iter设置为100。执行完一次全部数据,称之为一个epoch

test_interval: 500

测试间隔。也就是每训练500次,才进行一次测试。

base_lr: 0.01
lr_policy: "inv"
gamma: 0.0001
power: 0.75

这四行可以放在一起理解,用于学习率的设置。只要是梯度下降法来求解优化,都会有一个学习率,也叫步长。base_lr用于设置基础学习率,在迭代的过程中,可以对基础学习率进行调整。怎么样进行调整,就是调整的策略,由lr_policy来设置。

lr_policy可以设置为下面这些值,相应的学习率的计算为:

    • - fixed:   保持base_lr不变.
    • - step:    如果设置为step,则还需要设置一个stepsize,  返回 base_lr * gamma ^ (floor(iter / stepsize)),其中iter表示当前的迭代次数
    • - exp:     返回base_lr * gamma ^ iter, iter为当前迭代次数
    • - inv:      如果设置为inv,还需要设置一个power, 返回base_lr * (1 + gamma * iter) ^ (- power)
    • - multistep: 如果设置为multistep,则还需要设置一个stepvalue。这个参数和step很相似,step是均匀等间隔变化,而multistep则是根据                                 stepvalue值变化
    • - poly:     学习率进行多项式误差, 返回 base_lr (1 - iter/max_iter) ^ (power)
    • - sigmoid: 学习率进行sigmod衰减,返回 base_lr ( 1/(1 + exp(-gamma * (iter - stepsize))))

接下来的参数:

momentum :0.9

上一次梯度更新的权重,具体可参看下一篇文章。

type: SGD

优化算法选择。这一行可以省掉,因为默认值就是SGD。总共有六种方法可选择,在本文的开头已介绍。

weight_decay: 0.0005

权重衰减项,防止过拟合的一个参数。

display: 100

每训练100次,在屏幕上显示一次。如果设置为0,则不显示。

max_iter: 20000

最大迭代次数。这个数设置太小,会导致没有收敛,精确度很低。设置太大,会导致震荡,浪费时间。

snapshot: 5000snapshot_prefix: "examples/mnist/lenet"

快照。将训练出来的model和solver状态进行保存,snapshot用于设置训练多少次后进行保存,默认为0,不保存snapshot_prefix设置保存路径。

还可以设置snapshot_diff,是否保存梯度值,默认为false,不保存。

也可以设置snapshot_format,保存的类型。有两种选择:HDF5 和BINARYPROTO ,默认为BINARYPROTO

solver_mode: CPU

设置运行模式。默认为GPU,如果你没有GPU,则需要改成CPU,否则会出错。

Solver的流程:

1.     设计好需要优化的对象,以及用于学习的训练网络和用于评估的测试网络。(通过调用另外一个配置文件prototxt来进行)

2.     通过forward和backward迭代的进行优化来跟新参数。

3.     定期的评价测试网络。 (可设定多少次训练后,进行一次测试)

4.     在优化过程中显示模型和solver的状态

在每一次的迭代过程中,solver做了这几步工作:

1、调用forward算法来计算最终的输出值,以及对应的loss

2、调用backward算法来计算每层的梯度

3、根据选用的slover方法,利用梯度进行参数更新

4、记录并保存每次迭代的学习率、快照,以及对应的状态。


③设置train_caffenet.sh文件(也可以在终端直接输入)

三个内容:

caffe.exe:用于训练网络的可执行文件

train:模式选择,现在是训练模式

solver:solver.prototxt文件的路径


备注:deploy.prototxt文件内容也是网络结构,在最后测试模型的时候用。



0 0