mnist示例
* mnist数据集合
* 大型手写识别图片:600000个训练集合和100000个测试集合。
- 每个图片归一化,数字居中,28*28
* 下载MNIST数据集
- 下载数据集: data/mnist下使用脚本:get_mnist.sh
cd data/mnist/
./get_mnist.sh
tree
* mnsit数据格式
- 训练集,图片(train-images-...)-像素值(0-255)
- 训练集,标签(train-label-...)-标签值(0-9)
- 测试集,图片(t10k-images-...)
- 测试集,标签(t10k-labels-...)
* 格式转换
* 将二进制转换为levelDB或者LMDB
./examples/mnist/create_mnist.sh
//需要从caffe的根目录执行,否则报错
//build/examples/mnist/convert_mnist_data.bin: not found这样的错误
* Data Layer
- 从lmdb中读出数据,数据层
layer {
name: "mnist" //名字
type: "Data" //数据类型(数据层)
transform_param {
scale: 0.00390625 //数据变化的数据缩放因子( 1 divided by 256)
}
data_param { //数据层参数
source: "mnist_train_lmdb" //LMDB的路径
backend: LMDB //数据格式
batch_size: 64 //批处理的数目
}
top: "data" //层的输出blob两个,data和label
top: "label"
}
Blob 是Caffe处理和传输的真实数据的包装类,同时它还隐含提供了在CPU和GPU之间同步数据的能力。
caffe使用blob存储和交换数据。blob对不同数据提供了统一的内存接口;例如:一批图片,模型参数,优化过程的偏导数等。
Blob是Caffe的基本数据结构,具有CPU和GPU之间同步的能力,它是4维的数组(Num, Channels, Height, Width)。
* 卷积层:Convolution Layer
layer {
name: "conv1"
type: "Convolution"
param { lr_mult: 1 } //权值学习速率倍乘因子,1表示与全局参数一致
param { lr_mult: 2 } //bias学习速率倍乘因子,是全局参数的2倍
convolution_param { //卷积计算参数
num_output: 20 //输出feature_map的数目:20
kernel_size: 5 //卷积核额尺寸:5*5
stride: 1 //卷积输出的间隔跳跃,1表示连续输出,无跳跃
weight_filler { //权值使用xavier填充器
type: "xavier"
}
bias_filler { //bias使用常数填充器,默认是0
type: "constant"
}
}
bottom: "data"
top: "conv1"
}
* pooling层
layer {
name: "pool1"
type: "Pooling"
pooling_param {
kernel_size: 2 //使用最大值采样方法:2*2
stride: 2 //下采样输出跳跃间隔2*2
pool: MAX //下窗口尺寸2*2
}
bottom: "conv1"
top: "pool1"
}
* Fully Connected Layer
layer {
name: "ip1"
type: "InnerProduct"
param { lr_mult: 1 }
param { lr_mult: 2 }
inner_product_param {
num_output: 500 //输出元素个数为500
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
bottom: "pool2"
top: "ip1"
}
* ReLU Layer
layer {
name: "relu1"
type: "ReLU"
bottom: "ip1"
top: "ip1"
}
* innerproduct layer(内积层)
layer {
name: "ip2"
type: "InnerProduct"
param { lr_mult: 1 }
param { lr_mult: 2 }
inner_product_param {
num_output: 10
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
bottom: "ip1"
top: "ip2"
}
* accuracy层
layer {
name: "accuracy"
type: "Accuracy"
bottom: "ip2"
bottom: "label"
top: "accuracy"
include {
phase: TEST
}
}
* loss层
layer {
name: "loss"
type: "SoftmaxWithLoss"
bottom: "ip2"
bottom: "label"
}
2. 训练超参数
* 运行examples/mnist/lenet_train_test.prototxt
* lenet_solver.protext训练超参数
# 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
# Carry out testing every 500 training iterations.
test_interval: 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
display: 100
# The maximum number of iterations
max_iter: 10000
# snapshot intermediate results
snapshot: 5000
snapshot_prefix: "examples/mnist/lenet"
# solver mode: CPU or GPU
solver_mode: GPU
3. 训练日志
- GLOG格式:日期-时间-进程号-源码文件-代码行号] 输出信息
4. 使用训练好的模型预测
./build/tools/caffe.bin test \ //表示只做预测,并进行参数更新
- model examples/mnist/lenet_train_test.portptxt \
- weights examples/mnist/lenet_iter_10000.caffemodel \
- iterations 100 //迭代次数
5. build/tools/caffe.bin
http://blog.csdn.net/langb2014/article/details/50458014
* 命令
- train----训练或finetune模型(model),
- test-----测试模型
- device_query---显示gpu信息
- time-----显示程序执行时间
* 参数
-solver -gpu -snapshot -weights -iteration -model -sighup_effect -sigint_effect