在Windows10 上完成了配置,还没来得及测试,在Ubuntu下完成测试非常简单,在Caffe的根目录中有一个data的文件夹,这里面又包含了三个文件夹,分别对应了三个数据来源:
数据集下载
每个目录下都有 一个shell的脚本文件,在Ubuntu下的terminal运行,自动下载数据。
比如下载mnist数据集的脚本是get_mnist.sh
,其中的脚本是:
#!/usr/bin/env sh
# This scripts downloads the mnist data and unzips it.
DIR="$( cd "$(dirname "$0")" ; pwd -P )"
cd "$DIR"
echo "Downloading..."
for fname in train-images-idx3-ubyte train-labels-idx1-ubyte t10k-images-idx3-ubyte t10k-labels-idx1-ubyte
do
if [ ! -e $fname ]; then
wget --no-check-certificate http://yann.lecun.com/exdb/mnist/${fname}.gz
gunzip ${fname}.gz
fi
done
就是下载这四个文件,当然也可以在mnist的网站上直接下载。
这个数据还需要转换,但是在Windows上编译的数据格式转格式的工程一直没配置好,所以就先用别人转换好的吧。
在这里下载,这个数据文件不全,只有leveldb的,另外一种没有,不过这个一个也足够完成一次测试了。
在Caffe根目录下会有一个example的文件夹,里面是caffe提供的测试,网络结构还有运行的shell脚本都写好了,但是在Win下不能直接用。在Win10下,采用这种方式编译的 caffe,会在script目录下的build文件夹中,caffe可执行文件在这个目录下的tools目录下的Release中,也就是caffe.exe。
把example中的mnist复制一份,命名为mnist_win,然后将下载的数据放在这个目录下,有两个文件夹:
- mnist-test-leveldb
- mnist-train-leveldb
修改训练测试文件
修改训练配置文件lenet_train_test.prototxt
,将文件中的数据路径和backed都修改,需要注意的是文件路径中的斜杠问题,要用双斜杠,否则会仿作转义符,如下所示(一共有四个地方,注意文件名中的下划线与横杆,找不到文件就是因为文件名不对):
name: "LeNet"
layer {
name: "mnist"
type: "Data"
top: "data"
top: "label"
include {
phase: TRAIN
}
transform_param {
scale: 0.00390625
}
data_param {
source: "D:\\Application\\CaffeNew\\caffe\\examples\\mnist\\mnist-train-leveldb"
batch_size: 64
backend: LEVELDB
}
}
layer {
name: "mnist"
type: "Data"
top: "data"
top: "label"
include {
phase: TEST
}
transform_param {
scale: 0.00390625
}
data_param {
source: "D:\\Application\\CaffeNew\\caffe\\examples\\mnist\\mnist-test-leveldb"
batch_size: 100
backend: LEVELDB
}
}
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"
}
修改网络配置参数
修改参数配置文件lenet_solver.prototxt
,这里面制定了训练测试文件的目录,还有其他参数,也是要注意路径中的斜杠问题,下面是我的配置文件。
# The train/test net protocol buffer definition
net: "D:\\Application\\CaffeNew\\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: "D:\\Application\\CaffeNew\\caffe\\examples\\mnist\\lenet"
# solver mode: CPU or GPU
solver_mode: GPU
运行一下
然后就可以运行了,可以直接在命令行中写命令:
D:\Application\CaffeNew\caffe\scripts\build\tools\Release\caffe.exe train --solver=D:\Application\CaffeNew\caffe\examples\mnist\lenet_solver.prototxt
也可以写一个批处理文件放在当前目录,我写一个批处理文件run.bat
,双击就可以运行了:
D:\Application\CaffeNew\caffe\scripts\build\tools\Release\caffe.exe train --solver=D:\Application\CaffeNew\caffe\examples\mnist\lenet_solver.prototxt
Pause
如果之前都配置的没错的话,这里应该没有问题,准确率0.9+。