- mnist训练集训练
(1).将终端定位到Caffe根目录
cd ~/caffe
(2).下载MNIST数据库并解压缩
./data/mnist/get_mnist.sh
(3).将其转换成Lmdb数据库格式
./examples/mnist/create_mnist.sh
(4).训练网络
./examples/mnist/train_lenet.sh #此处调用两个文件:一个网络结构文件lenet.prototxt和网络参数设置文件lenet_solver.prototxt
如下所示lenet_solver.prototxt文件设置了该网络的各种参数及仿真设置:
# The train/test net protocol buffer definition
net: "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: "examples/mnist/lenet"
# solver mode: CPU or GPU
solver_mode: GPU --选择训练的模式
训练在10000次迭代后结束,最终模型的精度为99.08%.
I0420 11:34:37.033186 15506 data_layer.cpp:73] Restarting data prefetching from start.
I0420 11:34:37.040712 15498 solver.cpp:397] Test net output #0: accuracy = 0.9908
I0420 11:34:37.040753 15498 solver.cpp:397] Test net output #1: loss = 0.0281926 (* 1 = 0.0281926 loss)
I0420 11:34:37.040760 15498 solver.cpp:315] Optimization Done.
I0420 11:34:37.040763 15498 caffe.cpp:259] Optimization Done.
训练结束之后生成了4个文件:
lenet_iter_5000.caffemodel --5000次迭代的模型
lenet_iter_5000.solverstate --5000次迭代的中断点
lenet_iter_10000.caffemodel --10000次迭代的模型
lenet_iter_10000.solverstate --10000次迭代的中断点
通过caffe中python文件夹下自带的draw_net.py文件可以画出该网络的结构图:
python draw_net.py ../examples/mnist/lenet_train_test.prototxt lenet_train_test.png
图中根据网络定义train_mode的batch大小为64,而test_mode的batch大小为100,网络共有两个卷积层,两个pooling层和两个全连接(ip)层。
- cifar10
(1)cifar10_train_quick(4000次迭代的结果) 精度为73.02%
....
....
I0419 15:46:15.571383 1294 solver.cpp:310] Iteration 4000, loss = 0.614619
I0419 15:46:15.571403 1294 solver.cpp:330] Iteration 4000, Testing net (#0)
I0419 15:46:16.195946 1303 data_layer.cpp:73] Restarting data prefetching from start.
I0419 15:46:16.214529 1294 solver.cpp:397] Test net output #0: accuracy = 0.7302
I0419 15:46:16.214576 1294 solver.cpp:397] Test net output #1: loss = 0.807498 (* 1 = 0.807498 loss)
I0419 15:46:16.214596 1294 solver.cpp:315] Optimization Done.
I0419 15:46:16.214601 1294 caffe.cpp:259] Optimization Done.
I0419 15:46:16.317147 1328 caffe.cpp:218] Using GPUs 0
I0419 15:46:16.330811 1328 caffe.cpp:223] GPU 0: GeForce GTX 950M
....
....
(2)cifar10_train_full(60000次迭代的结果) 精度为78.61%
....
....
I0420 13:41:59.294299 2622 data_layer.cpp:73] Restarting data prefetching from start.
I0420 13:41:59.343652 2614 solver.cpp:397] Test net output #0: accuracy = 0.7861
I0420 13:41:59.343695 2614 solver.cpp:397] Test net output #1: loss = 0.624516 (* 1 = 0.624516 loss)
I0420 13:41:59.343717 2614 solver.cpp:315] Optimization Done.
I0420 13:41:59.343721 2614 caffe.cpp:259] Optimization Done.
I0420 13:41:59.560854 3113 caffe.cpp:218] Using GPUs 0
I0420 13:41:59.574221 3113 caffe.cpp:223] GPU 0: GeForce GTX 950M
....
....
所以cifar10的网络结构示意图可以表示为:
(1)cifar10_quick
(2)cifar10_full
在cifar10_full中多出了batch_norm层 该层可以提高训练模型的精度
caffe利用训练好的model进行图片识别与分类
Caffe学习系列(20):用训练好的caffemodel来进行分类
上面我们已经解决了caffe利用数据和网络文本定义来训练网络的问题,如何通过训练出的caffemodel进行图片的识别和分类是这一步需要解决的问题。caffe团队利用imagenet图片和caffenet模型训练好了一个caffemodel供大家下载。要进行图片的分类,这个caffemodel是最好不过的了。所以,不管是用c++来进行分类,还是用python接口来分类,我们都应该准备这样三个文件:caffemodel文件
,均值文件
,synset_words.txt文件
。
- caffemodel文件可以利用写好的model下载脚步直接下载(对于其他model zoo中的model文件同样适用)
sudo ./scripts/download_model_binary.py models/bvlc_reference_caffenet
- 均值文件 在测试阶段,需要把测试数据减去均值,这个文件我们用脚本来下载,在caffe根目录下执行:
sudo sh ./data/ilsvrc12/get_ilsvrc_aux.sh
#执行并下载后,均值文件放在 data/ilsvrc12/ 文件夹里,其文件名为imagenet_mean.binaryproto
- synset_words.txt文件 在调用脚本文件下载均值的时候,这个文件也一并下载好了,里面放的是1000个类的名称
在caffe根目录下的 examples/cpp-classification/ 文件夹下面有个classification.cpp文件,就是用来分类的。当然编译后,放在/build/examples/cpp_classification/ 下面,我们就直接运行命令:
sudo ./build/examples/cpp_classification/classification.bin \ #调用分类执行程序
models/bvlc_reference_caffenet/deploy.prototxt \ #加载和训练模型类似的简化模型文件
models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel \ #加载训练好的model文件
data/ilsvrc12/imagenet_mean.binaryproto \ #加载均值文件
data/ilsvrc12/synset_words.txt \ #加载分类名称文件
examples/images/cat.jpg #加载需要分类的图像
执行之后就可以得到:
---------- Prediction for examples/images/cat.jpg ----------
0.3134 - "n02123045 tabby, tabby cat"
0.2380 - "n02123159 tiger cat"
0.1235 - "n02124075 Egyptian cat"
0.1003 - "n02119022 red fox, Vulpes vulpes"
0.0715 - "n02127052 lynx, catamount"
可以看出为tabby cat
的概率相对与其他类别而言要大一些,该输出为top-5的结果。
利用python接口需要修改classify.py文件,然后执行下面的命令也可以得出top-5的结果:
sudo python classify.py ../examples/images/cat.jpg result.npy
....
....
Done in 1.25 s.
281 n02123045 tabby, tabby cat
282 n02123159 tiger cat
285 n02124075 Egyptian cat
287 n02127052 lynx, catamount
278 n02119789 kit fox, Vulpes macrotis
Saving results into result.npy
tips:classify.py需要修改的地方
1、修改均值计算:
mean = np.load(args.mean_file)
在这一行的下一行添加:
mean=mean.mean(1).mean(1)
则可以解决报错的问题。
2、修改文件,使得结果显示在命令行下:
# Classify.
start = time.time()
predictions = classifier.predict(inputs, not args.center_only)
print("Done in %.2f s." % (time.time() - start))
在这一行的下面添加上:
# Classify.
start = time.time()
predictions = classifier.predict(inputs, not args.center_only)
print("Done in %.2f s." % (time.time() - start))
imagenet_labels_filename = '../data/ilsvrc12/synset_words.txt'
labels = np.loadtxt(imagenet_labels_filename, str, delimiter='\t')
top_k = predictions.flatten().argsort()[-1:-6:-1]
for i in np.arange(top_k.size):
print top_k[i], labels[top_k[i]]