转学习笔记:Caffe上配置和运行MNIST
2016-07-14 17:14
501 查看
MNIST,一个经典的手写数字库,包含60000个训练样本和10000个测试样本,图片大小28*28,在Caffe上配置的第一个案例
1首先,获取minist的数据包。这个版本是四个数据包
cd $CAFFE_ROOT
./data/mnist/get_mnist.sh
[html] viewplaincopy
#!/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] viewplaincopy
Creating lmdb...
Done.
在这一步做了什么工作呢?
create_mnist.sh是利用caffe-master/build/examples/mnist/的convert_mnist_data.bin工具,将mnistdate转化为可用的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] viewplaincopy
./build/tools/caffe train --solver=examples/mnist/lenet_solver.prototxt
还有其他的示例,如:
./examples/mnist/train_mnist_autoencoder.sh
这个脚本调用的工具如下:
[html] viewplaincopy
./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] OptimizationDone.
I0126 17:32:40.498249 18290 caffe.cpp:121] OptimizationDone.
首先讨论训练的网络模型: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] viewplaincopy
# 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"
[html] viewplaincopy
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] viewplaincopy
#!/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] viewplaincopy
Creating lmdb...
Done.
在这一步做了什么工作呢?
create_mnist.sh是利用caffe-master/build/examples/mnist/的convert_mnist_data.bin工具,将mnistdate转化为可用的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] viewplaincopy
./build/tools/caffe train --solver=examples/mnist/lenet_solver.prototxt
还有其他的示例,如:
./examples/mnist/train_mnist_autoencoder.sh
这个脚本调用的工具如下:
[html] viewplaincopy
./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] OptimizationDone.
I0126 17:32:40.498249 18290 caffe.cpp:121] OptimizationDone.
首先讨论训练的网络模型: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] viewplaincopy
# 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"
[html] viewplaincopy
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文件,这是一个文本文件,详细描述了网络结构和数据集信息。
相关文章推荐
- Ubuntu 默认壁纸历代记
- Ubuntu Remix Cinnamon 20.04 评测:Ubuntu 与 Cinnamon 的完美融合
- 关于Ubuntu 11.10启动提示waiting for the network configuration的问题
- 在 Ubuntu 桌面中使用文件和文件夹
- ubuntu下chrome无法同步问题解决
- Ubuntu Linux使用体验
- 使用 GNOME 优化工具自定义 Linux 桌面的 10 种方法
- 以Ubuntu 9.04为例 将工作环境迁移到 Linux
- VirtualBox虚拟机XP与宿主机Ubuntu互访共享文件夹
- 从USB安装Ubuntu Server 10.04.3 图文详解
- Ubuntu 15.04 正式版发布下载
- Linux-Ubuntu 10.04安装Cadence-ic610 方法总结图解
- Ubuntu 12.04和Windows 7双系统安装图解
- Ubuntu /home下中文目录如何修改成英文?
- 开机出现:grub rescue的修复方法
- Ubuntu连接Android真机调试
- 你应该选择 Ubuntu 还是 Fedora?
- 100 个最佳 Ubuntu 应用(中)
- 看看我的Ubuntu Linux截图
- Ubuntu 12.04 LTS服务器版安装过程及使用图解