我们学习Caffe提供的简单例程,目的是为了让初学者轻松上手,以examples/cifar10/为例,主要用于小图片的分类。
1 cifar10数据集
60000张32*32彩色图片,50000张训练,10000张测试
下载cifar10数据集:http://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz
将下载好并解压好的数据存放于/data/cifar10/路径
下载好的原始数据是BINARY(二进制)格式的,需要转换成LMDB或LEVELDB格式才能被Caffe识别(本文以LEVELDB格式为例)。
2 转换格式
我们已经编译好Caffe,在data\cifar10路径下建立脚本convert_cifar_data.bat,内容如下:
D:\WorkSpace\caffe\caffe\Build\x64\Release\convert_cifar_data.exe D:\WorkSpace\caffe\caffe\data\cifar10\cifar-10-batches-bin D:\WorkSpace\caffe\caffe\examples\cifar10 leveldb
pause
D:\WorkSpace\caffe\caffe\Build\x64\Release\convert_cifar_data.exe
D:\WorkSpace\caffe\caffe\data\cifar10\cifar-10-batches-bin 表示输入数据文件路径
D:\WorkSpace\caffe\caffe\examples\cifar10 表示输出数据文件路径
leveldb 表示数据格式,你也可以尝试生成lmdb格式
3 图像数据均值
在data\cifar10路径下建立脚本compute_image_mean.bat,内容如下 :
D:\WorkSpace\caffe\caffe\Build\x64\Release\compute_image_mean.exe -backend=leveldb D:\WorkSpace\caffe\caffe\examples\cifar10\cifar10_train_leveldb D:\WorkSpace\caffe\caffe\examples\cifar10\mean.binaryproto
pause
D:\WorkSpace\caffe\caffe\Build\x64\Release\compute_image_mean.exe 表示图像数据进行初始化处理,需要compute_image_mean.exe可执行文件
-backend=leveldb 表示数据格式,如果不添加这句话的,默认转化为lmdb
D:\WorkSpace\caffe\caffe\examples\cifar10\cifar10_train_leveldb 表示刚才生成的训练数据集的路径,而不是val验证数据集的路径
D:\WorkSpace\caffe\caffe\examples\cifar10\mean.binaryproto 表示输出均值文件名,后缀名为binaryproto。
4 配置网络文件
该版本的网络配置文件有很多,我这里使用/examples/cifar10/cifar10_quick_train_test.prototxt。打开之后修改训练数据和验证数据以及均值文件的路径,如下图所示,红色部分为修改过后的:
name: "CIFAR10_quick" layer { name: "cifar" type: "Data" top: "data" top: "label" include { phase: TRAIN } transform_param { mean_file: "D:\\WorkSpace\\caffe\\caffe\\examples\\cifar10\\mean.binaryproto" } data_param { source: "D:\\WorkSpace\\caffe\\caffe\\examples\\cifar10\\cifar10_train_leveldb" batch_size: 100 backend: LEVELDB } } layer { name: "cifar" type: "Data" top: "data" top: "label" include { phase: TEST } transform_param { mean_file: "D:\\WorkSpace\\caffe\\caffe\\examples\\cifar10\\mean.binaryproto" } data_param { source: "D:\\WorkSpace\\caffe\\caffe\\examples\\cifar10\\cifar10_test_leveldb" batch_size: 100 backend: LEVELDB } }
如果要修改为CPU模式,所以还需要修改一下/examples/cifar10/cifar10_quick_solver.prototx文件里面的模式,如下图所示,红色圆圈为修改过的,原先为GPU模式。
# reduce the learning rate after 8 epochs (4000 iters) by a factor of 10 # The train/test net protocol buffer definition net: "D:\\WorkSpace\\caffe\\caffe\\examples\\cifar10\\cifar10_quick_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.001 momentum: 0.9 weight_decay: 0.004 # The learning rate policy lr_policy: "fixed" # Display every 100 iterations display: 100 # The maximum number of iterations max_iter: 4000 # snapshot intermediate results snapshot: 4000 snapshot_format: HDF5 snapshot_prefix: "D:\\WorkSpace\\caffe\\caffe\\examples\\cifar10\\cifar10_quick" # solver mode: CPU or GPU solver_mode: GPU
5 训练日志
在data\cifar10路径下建立脚本start_train.bat,内容如下:
D:\\WorkSpace\\caffe\\caffe\\Build\\x64\\Release\\caffe.exe train --solver=D:\\WorkSpace\\caffe\\caffe\\examples\\cifar10\\cifar10_quick_solver.prototxt pause
上面的命令是,先进行训练,然后进行测试D:\\WorkSpace\\caffe\\caffe\\Build\\x64\\Release\\caffe.exe test --model=D:\\WorkSpace\\caffe\\caffe\\examples\\cifar10\\cifar10_quick_train_test.prototxt -weights=D:\\WorkSpace\\caffe\\caffe\\examples\\cifar10\\cifar10_quick_iter_4000.caffemodel.h5 -gpu=0 pause