我们通过命令命令行进行训练的时候,每一次都会打印出训练loss和测试的准确率,这些都是衡量这个网络的指标,直接看日志,这个准确率和loss变化趋势是不清晰的,最好的方式就是能把它绘制成图形化,曲线图是最适合表现变化趋势的,下面就说说怎么画。
1、编写python脚本
#Step1 import
import numpy as np
import matplotlib.pyplot as plt
import sys,os
caffe_root = 'E:/caffe/caffecase'
sys.path.insert(0, caffe_root + 'python')
import caffe
#Step2 Setting
caffe.set_mode_gpu()
solver = caffe.SGDSolver('E:/caffe/caffecase/mnist/lenet_solver.prototxt')
niter =1000
test_interval = 200
train_loss = np.zeros(niter)
test_acc = np.zeros(int(np.ceil(niter / test_interval)))
#Step3 Draw
for it in range(niter):
solver.step(1) # SGD by Caffe
# store the train loss
train_loss[it] = solver.net.blobs['loss'].data
solver.test_nets[0].forward(start='conv1')
if it % test_interval == 0:
acc=solver.test_nets[0].blobs['accuracy'].data
print 'Iteration', it, 'testing...','accuracy:',acc
test_acc[it // test_interval] = acc
print test_acc
_, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(np.arange(niter), train_loss)
ax2.plot(test_interval * np.arange(len(test_acc)), test_acc, 'r')
ax1.set_xlabel('iteration')
ax1.set_ylabel('train loss')
ax2.set_ylabel('test accuracy')
plt.show()
2、执行脚本
执行脚本,如果缺少模块就导入。
3、绘制结果
红色的线是测试的准确率,蓝色的线是训练的loss值。
从图上就能看到,1000次迭代的准确率并不是最高的,而是700次左右达到了一个峰值