caffe学习:绘制loss曲线和accuracy曲线

#加载必要的库
import numpy as np
import matplotlib.pyplot as plt

# 将由matplotlib得到的图显示在页面内而不是弹出一个窗口
%matplotlib inline
import sys,os,caffe

#设置当前目录
caffe_root = '/home/keysen/caffe/' 
sys.path.insert(0, caffe_root + 'python')
os.chdir(caffe_root)

设置求解器,和caffe一样,需要一个solver配置文件

# set the solver prototxt
caffe.set_device(0)
caffe.set_mode_gpu()
solver = caffe.SGDSolver('examples/mnist/lenet_solver.prototxt')

如果不需要绘制曲线,只需要训练出一个caffemodel, 直接调用solver.solve()就可以了。如果要绘制曲线,就需要把迭代过程中的值保存下来,因此不能直接调用solver.solve(), 需要迭代。在迭代过程中,每迭代200次测试一次

# 记录时间, 其作用于一个代码块
%%time

niter = 10000 # 迭代次数
test_interval = 200 # 每200次测试一次
train_loss = np.zeros(niter) # 记录训练过程中的损失
test_acc = np.zeros(int(np.ceil(niter / test_interval))) # 记录训练过程中的测试精度

# the main solver loop
for it in range(niter):
    solver.step(1) # 选择SGD

    # store the tain 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 # 记录当前测试下的精度

output:

Iteration 0 testing… accuracy: 1.0
Iteration 200 testing… accuracy: 1.0
Iteration 400 testing… accuracy: 1.0
Iteration 600 testing… accuracy: 1.0
Iteration 800 testing… accuracy: 1.0
Iteration 1000 testing… accuracy: 1.0
Iteration 1200 testing… accuracy: 1.0
Iteration 1400 testing… accuracy: 1.0
Iteration 1600 testing… accuracy: 1.0
Iteration 1800 testing… accuracy: 1.0
Iteration 2000 testing… accuracy: 1.0
Iteration 2200 testing… accuracy: 1.0
Iteration 2400 testing… accuracy: 1.0
Iteration 2600 testing… accuracy: 1.0
Iteration 2800 testing… accuracy: 1.0
Iteration 3000 testing… accuracy: 1.0
Iteration 3200 testing… accuracy: 1.0
Iteration 3400 testing… accuracy: 1.0
Iteration 3600 testing… accuracy: 1.0
Iteration 3800 testing… accuracy: 1.0
Iteration 4000 testing… accuracy: 1.0
Iteration 4200 testing… accuracy: 1.0
Iteration 4400 testing… accuracy: 1.0
Iteration 4600 testing… accuracy: 1.0
Iteration 4800 testing… accuracy: 1.0
Iteration 5000 testing… accuracy: 1.0
Iteration 5200 testing… accuracy: 1.0
Iteration 5400 testing… accuracy: 1.0
Iteration 5600 testing… accuracy: 1.0
Iteration 5800 testing… accuracy: 1.0
Iteration 6000 testing… accuracy: 1.0
Iteration 6200 testing… accuracy: 1.0
Iteration 6400 testing… accuracy: 1.0
Iteration 6600 testing… accuracy: 1.0
Iteration 6800 testing… accuracy: 1.0
Iteration 7000 testing… accuracy: 1.0
Iteration 7200 testing… accuracy: 1.0
Iteration 7400 testing… accuracy: 1.0
Iteration 7600 testing… accuracy: 1.0
Iteration 7800 testing… accuracy: 1.0
Iteration 8000 testing… accuracy: 1.0
Iteration 8200 testing… accuracy: 1.0
Iteration 8400 testing… accuracy: 1.0
Iteration 8600 testing… accuracy: 1.0
Iteration 8800 testing… accuracy: 1.0
Iteration 9000 testing… accuracy: 1.0
Iteration 9200 testing… accuracy: 1.0
Iteration 9400 testing… accuracy: 1.0
Iteration 9600 testing… accuracy: 1.0
Iteration 9800 testing… accuracy: 1.0
CPU times: user 21.3 s, sys: 2.22 s, total: 23.5 s
Wall time: 19.1 s

绘制train过程中的loss曲线,和测试过程中的accuracy曲线

print test_acc
_, ax1 = plt.subplots()
ax2 = ax1.twinx() # 共用x轴, 创建第二y轴
ax1.plot(np.arange(niter), train_loss)
ax2.plot(test_interval * np.arange(len(test_acc)), test_acc, 'r')
ax1.set_xlabel('iteraion')
ax1.set_ylabel('train loss')
ax2.set_ylabel('test accuracy')

[ 0.08 0.88999999 0.94999999 0.94999999 0.94 0.99000001
0.99000001 0.99000001 1. 0.99000001 1. 0.99000001
0.99000001 1. 1. 0.98000002 0.99000001 1.
0.99000001 1. 1. 1. 1. 0.99000001
1. 1. 0.99000001 1. 1. 1. 1.
1. 1. 1. 1. 1. 1. 1.
1. 1. 1. 1. 1. 1. 1.
1. 1. 1. 1. 1. ]

output:

caffe学习:绘制loss曲线和accuracy曲线_第1张图片

你可能感兴趣的:(caffe)