Caffe上配置和运行MNIST
2016-01-25 10:01
411 查看
MNIST,一个经典的手写数字库,包含60000个训练样本和10000个测试样本,图片大小28*28,在Caffe上配置的第一个案例
1首先,获取minist的数据包。 这个版本是四个数据包
cd $CAFFE_ROOT
./data/mnist/get_mnist.sh
[html] view plaincopy
#!/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
[html] view plaincopy
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
这个脚本调用的工具如下:
[html] view plaincopy
./build/tools/caffe train --solver=examples/mnist/lenet_solver.prototxt
还有其他的示例,如:
./examples/mnist/train_mnist_autoencoder.sh
这个脚本调用的工具如下:
[html] view plaincopy
./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] Test net output #0: accuracy = 0.9903
I0126 17:32:40.498236 18290 solver.cpp:315] Test net output #1: loss = 0.0309918 (* 1 = 0.0309918 loss)
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 http://yann.lecun.com/exdb/publis/pdf/lecun-01a.pdf
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) = max(0, x),是purelin的折线版。它的作用是如果计算出的值小于0,就让它等于0,否则保持原来的值不变。这是一种简单粗暴地强制某些数据为0的方法,然而经实践证明,训练后的网络完全具备适度的稀疏性。而且训练后的可视化效果和传统方式预训练出的效果很相似,这也说明了ReLU具备引导适度稀疏的能力。
来自讨论 http://tieba.baidu.com/p/3061925556)
LeNet的结构和CNN的结构是由相似之处的,是由两个 convolutional layer 和 pooling layer交错连接 ,然后两个fully connected layers结构组成。 具体可参见http://deeplearning.net/tutorial/lenet.html,此外DeepID
有个简单直观的结构图,可以辅助了解 http://blog.csdn.net/stdcoutzyx/article/details/42091205
定义MNIST网络和MNIST Solver:
从 ./examples/mnist/train_lenet.sh 脚本调用的指令我们就可以看到,solver是定义在了$CAFFE_ROOT/examples/mnist/lenet_solver.prototxt 中。
[html] view plaincopy
# 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>[b]span style="font-family: Arial, Helvetica, sans-serif;">examples/mnist/lenet</</span>[b]span> [/b][/b]
snapshot: 5000
snapshot_prefix: "examples/mnist/lenet"
[html] view plaincopy
solver_mode: CPU
再看一下./examples/mnist/train_mnist_autoencoder.sh 调用的 mnist_autoencoder_solver.prototxt
的solver定义
当所有数据都训练好之后,接下来就是如何将模型应用到实际数据了:
./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文件,这是一个文本文件,详细描述了网络结构和数据集信息。
1首先,获取minist的数据包。 这个版本是四个数据包
cd $CAFFE_ROOT
./data/mnist/get_mnist.sh
[html] view plaincopy
#!/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
[html] view plaincopy
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
这个脚本调用的工具如下:
[html] view plaincopy
./build/tools/caffe train --solver=examples/mnist/lenet_solver.prototxt
还有其他的示例,如:
./examples/mnist/train_mnist_autoencoder.sh
这个脚本调用的工具如下:
[html] view plaincopy
./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] Test net output #0: accuracy = 0.9903
I0126 17:32:40.498236 18290 solver.cpp:315] Test net output #1: loss = 0.0309918 (* 1 = 0.0309918 loss)
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 http://yann.lecun.com/exdb/publis/pdf/lecun-01a.pdf
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) = max(0, x),是purelin的折线版。它的作用是如果计算出的值小于0,就让它等于0,否则保持原来的值不变。这是一种简单粗暴地强制某些数据为0的方法,然而经实践证明,训练后的网络完全具备适度的稀疏性。而且训练后的可视化效果和传统方式预训练出的效果很相似,这也说明了ReLU具备引导适度稀疏的能力。
来自讨论 http://tieba.baidu.com/p/3061925556)
LeNet的结构和CNN的结构是由相似之处的,是由两个 convolutional layer 和 pooling layer交错连接 ,然后两个fully connected layers结构组成。 具体可参见http://deeplearning.net/tutorial/lenet.html,此外DeepID
有个简单直观的结构图,可以辅助了解 http://blog.csdn.net/stdcoutzyx/article/details/42091205
定义MNIST网络和MNIST Solver:
从 ./examples/mnist/train_lenet.sh 脚本调用的指令我们就可以看到,solver是定义在了$CAFFE_ROOT/examples/mnist/lenet_solver.prototxt 中。
[html] view plaincopy
# 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>[b]span style="font-family: Arial, Helvetica, sans-serif;">examples/mnist/lenet</</span>[b]span> [/b][/b]
snapshot: 5000
snapshot_prefix: "examples/mnist/lenet"
[html] view plaincopy
solver_mode: CPU
再看一下./examples/mnist/train_mnist_autoencoder.sh 调用的 mnist_autoencoder_solver.prototxt
的solver定义
当所有数据都训练好之后,接下来就是如何将模型应用到实际数据了:
./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文件,这是一个文本文件,详细描述了网络结构和数据集信息。
相关文章推荐
- browserify和webpack
- js阿拉伯变成中文数字
- 转:如何在Caffe中配置每一个层的结构
- caffe 模型下载
- AngularJS中使用$parse或$eval在运行时对Scope变量赋值
- JS/JQ用法小總
- JavaScript JSON
- Effective go
- ExtJS表单之一个实例
- jQuery学习之旅 1 选择器【一】
- CSS content内容生成技术以及应用
- css border-style三角
- caffe参数文件
- extJS 表单 Form的使用
- 初识Rest、JSR、JCP、JAX-RS及Jersey
- JavaScript 类型转换
- 9张思维导图学习Javascript
- poj Transferring Sylla(怎样高速的推断一个图是否是3—连通图,求割点,割边)
- 公历和农历转换的JS代码<转>
- 关于JavaScript中数组的清除