数据集准备
数据集下载
https://download.youkuaiyun.com/download/weixin_62505365/90292140?spm=1001.2014.3001.5501
也可以前往image-net下载,我也是那里下载的,需要科学上网+学校邮箱认证
通过smb传输到caffe/data/cifar目录下
转换成LMDB格式
cd caffe/examples/cifar10
./create_cifar10.sh
会报错not found
cd caffe
./caffe/examples/cifar10/create_cifar10.sh
会成功
/caffe/examples/cifar10/
会生成cifar10_train_lmdb cifar10_test_lmdb mean.binaryproto
修改求解器文件
cd caffe/examples/cifar10
修改权限
chmod 777 ./*
vi cifar10_full_solver.prototxt
找到最后一行将GPU改为CPU:设置求解器模式为CPU
wq!
模型训练
cd caffe
./build/tools/caffe train --solver=/caffe/examples/cifar10/cifar10_full_solver.prototxt
出现下面的内容表示开始训练,训练时间较长
I0121 10:38:31.551041 21158 caffe.cpp:197] Use CPU.
I0121 10:38:31.552023 21158 solver.cpp:45] Initializing solver from parameters:
test_iter: 100
test_interval: 1000
base_lr: 0.001
display: 200
max_iter: 65000
lr_policy: "step"
gamma: 0.1
momentum: 0.9
weight_decay: 0.004
stepsize: 60000
snapshot: 10000
snapshot_prefix: "examples/cifar10/cifar10_full"
solver_mode: CPU
net: "examples/cifar10/cifar10_full_train_test.prototxt"
train_state {
level: 0
stage: ""
}
.
.
.
训练的时间实在太久了换一下求解器
./build/tools/caffe train --solver=/home/work/caffe/caffe/examples/cifar10/cifar10_quick_solver.prototxt
还是很慢,耐心等待吧,谁叫我不支持GPU直连呢
训练结果
I0121 11:28:19.593346 22505 data_layer.cpp:73] Restarting data prefetching from start.
I0121 11:28:21.620898 22499 solver.cpp:464] Snapshotting to binary proto file examples/cifar10/cifar10_quick_iter_4000.caffemodel
I0121 11:28:21.623148 22499 sgd_solver.cpp:284] Snapshotting solver state to binary proto file examples/cifar10/cifar10_quick_iter_4000.solverstate
I0121 11:28:21.830902 22499 solver.cpp:327] Iteration 4000, loss = 0.618966
I0121 11:28:21.830960 22499 solver.cpp:347] Iteration 4000, Testing net (#0)
I0121 11:28:41.681440 22506 data_layer.cpp:73] Restarting data prefetching from start.
I0121 11:28:42.519287 22499 solver.cpp:414] Test net output #0: accuracy = 0.7252
I0121 11:28:42.519381 22499 solver.cpp:414] Test net output #1: loss = 0.817143 (* 1 = 0.817143 loss)
I0121 11:28:42.519390 22499 solver.cpp:332] Optimization Done.
I0121 11:28:42.520335 22499 caffe.cpp:250] Optimization Done.
准确率(accuracy):0.7252,表示模型在测试集上的准确率为 72.52%。
测试损失(loss):0.817143,表示模型在测试集上的损失值。
模型的权重(.caffemodel
)和求解器状态(.solverstate
)被保存为快照文件
可以用于分类识别和测试
测试结果
./build/tools/caffe test --model=/home/work/caffe/caffe/examples/cifar10/cifar10_quick_train_test.prototxt --weights=/home/work/caffe/caffe/examples/cifar10/cifar10_quick_iter_4000.caffemodel --iterations=100
/home/myshare/caffe/caffe-1.0/build/tools/caffe test \
--model=/home/myshare/caffe/caffe-1.0/examples/cifar10/cifar10_quick_train_test.prototxt \
--weights=/home/myshare/caffe/caffe-1.0/examples/cifar10/cifar10_quick_iter_4000.caffemodel \
--iterations=100
I0121 12:00:52.224802 7442 caffe.cpp:304] Batch 0, accuracy = 0.76
I0121 12:00:52.224864 7442 caffe.cpp:304] Batch 0, loss = 0.765442
I0121 12:00:52.426918 7442 caffe.cpp:304] Batch 1, accuracy = 0.7
I0121 12:00:52.426975 7442 caffe.cpp:304] Batch 1, loss = 0.920233
I0121 12:00:52.628732 7442 caffe.cpp:304] Batch 2, accuracy = 0.66
I0121 12:00:52.628791 7442 caffe.cpp:304] Batch 2, loss = 0.893439
I0121 12:00:52.831324 7442 caffe.cpp:304] Batch 3, accuracy = 0.74
I0121 12:00:52.831384 7442 caffe.cpp:304] Batch 3, loss = 0.745415
I0121 12:00:53.033861 7442 caffe.cpp:304] Batch 4, accuracy = 0.7
I0121 12:00:53.033916 7442 caffe.cpp:304] Batch 4, loss = 0.83797
I0121 12:00:53.235775 7442 caffe.cpp:304] Batch 5, accuracy = 0.72
I0121 12:00:53.235826 7442 caffe.cpp:304] Batch 5, loss = 0.656984
I0121 12:00:53.438779 7442 caffe.cpp:304] Batch 6, accuracy = 0.72
I0121 12:00:53.438831 7442 caffe.cpp:304] Batch 6, loss = 0.780185
I0121 12:00:53.641988 7442 caffe.cpp:304] Batch 7, accuracy = 0.69
I0121 12:00:53.642043 7442 caffe.cpp:304] Batch 7, loss = 0.910562
I0121 12:00:53.843426 7442 caffe.cpp:304] Batch 8, accuracy = 0.65
I0121 12:00:53.843480 7442 caffe.cpp:304] Batch 8, loss = 0.994824
I0121 12:00:54.045399 7442 caffe.cpp:304] Batch 9, accuracy = 0.75
I0121 12:00:54.045457 7442 caffe.cpp:304] Batch 9, loss = 0.706145
I0121 12:00:54.248553 7442 caffe.cpp:304] Batch 10, accuracy = 0.79
I0121 12:00:54.248607 7442 caffe.cpp:304] Batch 10, loss = 0.709923
I0121 12:00:54.451359 7442 caffe.cpp:304] Batch 11, accuracy = 0.71
I0121 12:00:54.451432 7442 caffe.cpp:304] Batch 11, loss = 0.918768
I0121 12:00:54.653391 7442 caffe.cpp:304] Batch 12, accuracy = 0.76
I0121 12:00:54.653445 7442 caffe.cpp:304] Batch 12, loss = 0.723632
I0121 12:00:54.855151 7442 caffe.cpp:304] Batch 13, accuracy = 0.69
I0121 12:00:54.855209 7442 caffe.cpp:304] Batch 13, loss = 0.748781
I0121 12:00:55.057772 7442 caffe.cpp:304] Batch 14, accuracy = 0.71
I0121 12:00:55.057832 7442 caffe.cpp:304] Batch 14, loss = 0.721086
I0121 12:00:55.260303 7442 caffe.cpp:304] Batch 15, accuracy = 0.76
I0121 12:00:55.260370 7442 caffe.cpp:304] Batch 15, loss = 0.819781
I0121 12:00:55.462256 7442 caffe.cpp:304] Batch 16, accuracy = 0.7
I0121 12:00:55.462314 7442 caffe.cpp:304] Batch 16, loss = 0.996849
I0121 12:00:55.663707 7442 caffe.cpp:304] Batch 17, accuracy = 0.77
I0121 12:00:55