首页 > IT职业 > IT图书 > Caffe概览及mnist数据集训练
1. Caffe目录结构
data/用于存放下载的训练数据docs/帮助文档examples/代码样例matlab/MATLAB接口文件python/PYTHON接口文件models/一些配置好的模型参数scripts/一些文档和数据会用到的脚本
核心代码tools/保存的源码是用于生成二进制处理程序的,caffe在训练时实际是直接调用这些二进制文件include/caffe的实现代码的头文件src/实现caffe的源文件
lsvim ly.helloworld.proto
:q!退出
C++编译protoc -I=. --cpp_out=. ly.helloworld.proto
lsvim test_pb.cpp
:q!退出
编译gcc -o test_pb test_pb.cpp ly.helloworld.pb.cc -lprotobuf -lstdc++
ls ./test_pb ls
看到log文件输出成功
获取数据./data/mnist/get_mnist.sh
将数据转化为lmdb格式./examples/mnist/create_mnist.sh
训练./examples/mnist/train_lenet.sh
注意:
脚本的运行基于$Caffe_Root文件加下的路径执行
训练的时候,如果安装的时候选择了CPU_ONLY的话,在*.prototxt文件中,把mode:GPU改成mode:CPU
下载数据
cd caffe ./data/mnist/get_mnist.sh
转化数据
./examples/mnist/create_mnist.sh
./examples/mnist/create_mnist.shcreate_mnist.sh是利用caffe-master/build/examples/mnist/的convert_mnist_data.bin工具,将mnist date转化为可用的lmdb格式的文件。并将新生成的2个文件mnist-train-lmdb和mnist-test-lmdb放于create_mnist.sh同目录下。
训练
./examples/mnist/train_lenet.sh
如果没有GPU,使用CPU模式,修改$CAFFE_ROOT/examples/mnist/lenet_solver.prototxt:
# solver mode: CPU or GPUsolver_mode: CPU
# The train/test net protocol buffer definitionnet: "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 //test迭代次数,如果batch_size=100,则100张图一批,训练100次,则可以覆盖10000张图的需求# Carry out testing every 500 training iterations.test_interval: 500 //训练迭代500次测试一次# The base learning rate, momentum and the weight decay of the network.//网络参数:学习率,动量,权重的衰减base_lr: 0.01momentum: 0.9weight_decay: 0.0005# The learning rate policy //学习策略:有固定学习率和每步递减学习率lr_policy: "inv"gamma: 0.0001power: 0.75# Display every 100 iterations //每迭代100次显示一次display: 100# The maximum number of iterations //最大迭代次数max_iter: 10000# snapshot intermediate results //每5000次迭代存储一次数据,路径前缀如下snapshot: 5000snapshot_prefix: "examples/mnist/lenet"# solver mode: CPU or GPU //使用CPU或者GPUsolver_mode: GPU
./examples/mnist/train_lenet.sh
调用工具:./build/tools/caffe train --solver=examples/mnist/lenet_solver.prototxt
看到optimization done训练结束,~/caffe/examples/mnist/多了caffemodel
CPU:i3-3240 CPU @ 3.40GHz 训练耗时15分钟
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1064/viewspace-2809371/ ,如需转载,请注明出处,否则将追究法律责任。