Caffe学习(三) —— 运行第一个程序

Caffe优点之一是自带学习用例,所以初次接触caffe时,可以直接使用caffe自带的用例运行一次,感受一下caffe的使用。(因为caffe不自带练习数据,所以需要联网,下载)

下载数据

mnist是一个大型的手写数字库,由纽约大学Yannn LeCun教授整理,里面包括60000个训练集和10000个测试集,每个样本为28*28大小的黑白图片,手写数字为0-9,可以从官方网站http://yann.lecun.com/exdb/mnist/ 查看更多说明,这里因为是练习,所以不展开介绍。

下载mnist数据(默认当前是caffe根目录):

sudo sh data/mnist/get_mnist.sh

下载成功后,会在data/mnist/当前目录存在文件:
这里写图片描述
四个文件,分别是测试集图片、测试集对应标注、训练集样本、训练集对应标注。

转换成LMDB数据格式

这些数据不能在caffe直接使用,需要转出LMDB数据(caffe有自己规定的可识别的数据格式):

sh examples/mnist/create_mnist.sh

Caffe学习(三) —— 运行第一个程序_第1张图片
转换后在examples/mnist/生成目录mnist_test_lmdb和mnist_train_lmdb,分别是测试和训练数据集。

修改配置

vim examples/mnist/lenet_solver.prototxt
# 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: CPU

其中net: “examples/mnist/lenet_train_test.prototxt”表示训练的网络,其中设置了训练和测试的数据集文件地址;
solver_mode: CPU表示运行在CPU上;
其他都是一些训练网络模型的参数和快照保存目录;

执行

sudo sh examples/mnist/train_lenet.sh

Caffe学习(三) —— 运行第一个程序_第2张图片
运行时间跟机器性能有关,可以查看到最终训练后模型在测试集测试后的准确率。

Caffe学习(三) —— 运行第一个程序_第3张图片

你可能感兴趣的:(caffe)