运行环境 Ubuntu14.04.4
MNIST数据集是大型的手写数字数据库,60000个训练集和10000个测试集。
一、下载MNIST数据集
cd /home/wjx/caffe/data/mnist
./get_mnist.sh
二、格式转换
下载的原始数据集为二进制文件,需要转化成LEVELDB或LMDB才能被Caffe识别。
cd /home/wjx/caffe/examples/mnist
./create_mnist.sh
如果没有修改create_mnist.sh文件的话,会出现下面的情况:
说明路径出现问题,要对
create_mnist.sh做如下修改:
更改EXAMPLE,DATA,BUILD为绝对路径
然后再次运行./create_mnist.sh,得到结果:
说明已经改好了,浏览所在的目录 example/mnist,发现生成了mnist_train_lmdb/和mnist_test_lmdb两个目录,每个目录下都有两个文件:data.mdb和lock.mdb。接下来可以训练
三、训练
./train_lenet.sh
直接在终端输入会出现下面的情况:无法找到路径
对train_lenet.sh做如下的修改:修改路径为绝对路径
再次输入命令进行训练得到如下结果:
发现问题,还是出现路径的问题,这次修改lenet_solver.prototxt文件
特别注意最后一行的CPU/GPU模式
# The train/test net protocol buffer definition
net: "/home/neutech/caffe/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: "/home/neutech/caffe/examples/mnist/lenet" #修改为绝对路径
# solver mode: CPU or GPU
solver_mode: CPU #如果为CPU模式改成CPU,默认为GPU
再次运行命令开始训练,出现下面的情况:
还是训练失败,同样的问题,找不到mnist_train_lmdb和
mnist_train_lmdb文件的路径。通过查看
lenet_solver.prototxt的内容
- net: "/home/neutech/caffe/examples/mnist/lenet_train_test.prototxt" #修改为绝对路径
这一行描述的用于训练/预测的网络描述文件(ProtoBuffer文件格式),打开lenet_train_test.prototxt文件,修改:
name: "LeNet"
layer {
name: "mnist"
type: "Data"
top: "data"
top: "label"
include {
phase: TRAIN
}
transform_param {
scale: 0.00390625
}
data_param {
source: "/home/neutech/caffe/examples/mnist/mnist_train_lmdb" #此处文件路径要检查清楚,修改为绝对路径
batch_size: 64
backend: LMDB
}
}
layer {
name: "mnist"
type: "Data"
top: "data"
top: "label"
include {
phase: TEST
}
transform_param {
scale: 0.00390625
}
data_param {
source: "/home/neutech/caffe/examples/mnist/mnist_test_lmdb"
#此处文件路径要检查清楚,修改为绝对路径batch_size: 100
backend: LMDB
}
}
layer {
name: "conv1"
type: "Convolution"
bottom: "data"
top: "conv1"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
convolution_param {
num_output: 20
kernel_size: 5
stride: 1
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "pool1"
type: "Pooling"
bottom: "conv1"
top: "pool1"
pooling_param {
pool: MAX
kernel_size: 2
stride: 2
}
}
layer {
name: "conv2"
type: "Convolution"
bottom: "pool1"
top: "conv2"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
convolution_param {
num_output: 50
kernel_size: 5
stride: 1
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "pool2"
type: "Pooling"
bottom: "conv2"
top: "pool2"
pooling_param {
pool: MAX
kernel_size: 2
stride: 2
}
}
layer {
name: "ip1"
type: "InnerProduct"
bottom: "pool2"
top: "ip1"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {
num_output: 500
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "relu1"
type: "ReLU"
bottom: "ip1"
top: "ip1"
}
layer {
name: "ip2"
type: "InnerProduct"
bottom: "ip1"
top: "ip2"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {
num_output: 10
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "accuracy"
type: "Accuracy"
bottom: "ip2"
bottom: "label"
top: "accuracy"
include {
phase: TEST
}
}
layer {
name: "loss"
type: "SoftmaxWithLoss"
bottom: "ip2"
bottom: "label"
top: "loss"
}
接下来可以进行训练了。会出现下面的训练日志:
OK,大功告成!