1首先,获取minist的数据包。 这个版本是四个数据包
cd $CAFFE_ROOT
./data/mnist/get_mnist.sh
- #!/usr/bin/env
sh - #
This scripts downloads the mnist data and unzips it. - DIR="$(
cd "$(dirname "$0")" ; pwd -P )" - cd
$DIR - echo
"Downloading..." -
- wget
--no-check-certificate http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz - wget
--no-check-certificate http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz - wget
--no-check-certificate http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz - wget
--no-check-certificate http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz -
- echo
"Unzipping..." -
-
- gunzip
train-images-idx3-ubyte.gz - gunzip
train-labels-idx1-ubyte.gz - gunzip
t10k-images-idx3-ubyte.gz - gunzip
t10k-labels-idx1-ubyte.gz -
-
- #
Creation is split out because leveldb sometimes causes segfault - #
and needs to be re-created. -
- echo
"Done."
然后执行
./examples/mnist/create_mnist.sh
- Creating
lmdb... - Done.
在这一步做了什么工作呢?
create_mnist.sh是利用caffe-master/build/examples/mnist/的convert_mnist_data.bin工具,将mnist date转化为可用的lmdb格式的文件。并将新生成的2个文件mnist-train-lmdb 和 mnist-test-lmdb放于create_mnist.sh同目录下。
2 数据准备好了,那么接下来的工作就是训练了。
http://caffe.berkeleyvision.org/gathered/examples/mnist.html
给出来的例子是
./examples/mnist/train_lenet.sh
这个脚本调用的工具如下:
- ./build/tools/caffe
train --solver=examples/mnist/lenet_solver.prototxt
还有其他的示例,如:
./examples/mnist/train_mnist_autoencoder.sh
这个脚本调用的工具如下:
- ./build/tools/caffe
train \ -
--solver=examples/mnist/mnist_autoencoder_solver.prototxt
运行完结果如下:
生成四个文件
lenet_iter_10000.caffemodel
lenet_iter_10000.solverstate
lenet_iter_5000.caffemodel
lenet_iter_5000.solverstate
屏幕显示每次
.......................
I0126 17:32:32.171516 18290 solver.cpp:246] Iteration 10000, loss = 0.00453533
I0126 17:32:32.171550 18290 solver.cpp:264] Iteration 10000, Testing net (#0)
I0126 17:32:40.498195 18290 solver.cpp:315]
I0126 17:32:40.498236 18290 solver.cpp:315]
I0126 17:32:40.498245 18290 solver.cpp:251] Optimization Done.
I0126 17:32:40.498249 18290 caffe.cpp:121] Optimization Done.
首先讨论训练的网络模型:LeNet: the MNIST Classification Model
LeNet 模型之前在手写识别上就有非常好的表现。caffe 这里提供的是一个改进版的LeNet模型,其中的 sigmoid 被rectified linear units (ReLUs) 替换。
(标准的sigmoid输出不具备稀疏性,需要用一些惩罚因子来训练出一大堆接近0的冗余数据来,从而产生稀疏数据,例如L1、L1/L2或Student-t作惩罚因子。因此需要进行无监督的预训练。多层的神经网络如果用sigmoid或tanh激活函数也不做pre-training的话会因为 gradient vanishing problem 而会无法收敛。ReLU则这没有这个问题。ReLU是线性修正,公式为:g(x)
LeNet的结构和CNN的结构是由相似之处的,是由两个 convolutional layer 和 pooling layer交错连接 ,然后两个fully
定义MNIST网络和MNIST Solver:
从 ./examples/mnist/train_lenet.sh
- #
The train/test net protocol buffer definition - net:
"examples/mnist/lenet_train_test.prototxt" //网络协议具体定义 - #
test_iter specifies how many forward passes the test should carry out. - #
In the case of MNIST, we have test batch size 100 and 100 test iterations, - #
covering the full 10,000 testing images. - test_iter:
100 //test迭代次数 如果batch_size =100,则100张图一批,训练100次,则可以覆盖10000张图的需求 - #
Carry out testing every 500 training iterations. - test_interval:
500 //训练迭代500次,测试一次 - #
The base learning rate, momentum and the weight decay of the network. //网络参数:学习率,动量,权重的衰减 - base_lr:
0.01 - momentum:
0.9 - weight_decay:
0.0005 - #
The learning rate policy //学习策略:有固定学习率和每步递减学习率 - lr_policy:
"inv" - gamma:
0.0001 - power:
0.75 - #
Display every 100 iterations //每迭代100次显示一次 - display:
100 - #
The maximum number of iterations //最大迭代次数 - max_iter:
10000 - #
snapshot intermediate results // 每5000次迭代存储一次数据,路径前缀是<</span>span style="font-family: Arial, Helvetica, sans-serif;">examples/mnist/lenet</</span>span> - snapshot:
5000 - snapshot_prefix:
"examples/mnist/lenet"
- solver_mode:
CPU
再看一下./examples/mnist/train_mnist_autoencoder.sh 调用的
当所有数据都训练好之后,接下来就是如何将模型应用到实际数据了:
./build/tools/caffe.bin test -model=examples/mnist/lenet_train_test.prototxt -weights=examples/mnist/lenet_iter_10000.caffemodel -gpu=0
如果没有GPU则使用
./build/tools/caffe.bin test -model=examples/mnist/lenet_train_test.prototxt -weights=examples/mnist/lenet_iter_10000.caffemodel
test:表示对训练好的模型进行Testing,而不是training。其他参数包括train, time, device_query。
-model=XXX:指定模型prototxt文件,这是一个文本文件,详细描述了网络结构和数据集信息。