Caffe学习系列(19): 绘制loss和accuracy曲线

如同前几篇的可视化,这里采用的也是jupyter notebook来进行曲线绘制。

In [1]:
    #加载必要的库
    import numpy as np
    import matplotlib.pyplot as plt
    %matplotlib inline
    import sys,os,caffe

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

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

In [2]:
    # set the solver prototxt
    caffe.set_device(0)
    caffe.set_mode_gpu()
    #如果使用cpu需要把以上两行代码换成 caffe.set_mode_cpu()
    solver = caffe.SGDSolver('examples/cifar10/cifar10_quick_solver.prototxt')

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

In [5]:
    %%time
    niter =4000
    test_interval = 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 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
Iteration 0 testing... accuracy: 0.10000000149
Iteration 200 testing... accuracy: 0.419999986887
Iteration 400 testing... accuracy: 0.479999989271
Iteration 600 testing... accuracy: 0.540000021458
Iteration 800 testing... accuracy: 0.620000004768
Iteration 1000 testing... accuracy: 0.629999995232
Iteration 1200 testing... accuracy: 0.649999976158
Iteration 1400 testing... accuracy: 0.660000026226
Iteration 1600 testing... accuracy: 0.660000026226
Iteration 1800 testing... accuracy: 0.670000016689
Iteration 2000 testing... accuracy: 0.709999978542
Iteration 2200 testing... accuracy: 0.699999988079
Iteration 2400 testing... accuracy: 0.75
Iteration 2600 testing... accuracy: 0.740000009537
Iteration 2800 testing... accuracy: 0.769999980927
Iteration 3000 testing... accuracy: 0.75
Iteration 3200 testing... accuracy: 0.699999988079
Iteration 3400 testing... accuracy: 0.740000009537
Iteration 3600 testing... accuracy: 0.72000002861
Iteration 3800 testing... accuracy: 0.769999980927
CPU times: user 41.7 s, sys: 54.2 s, total: 1min 35s
Wall time: 1min 18s

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

In [6]:
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')
[ 0.1         0.41999999  0.47999999  0.54000002  0.62        0.63
  0.64999998  0.66000003  0.66000003  0.67000002  0.70999998  0.69999999
  0.75        0.74000001  0.76999998  0.75        0.69999999  0.74000001
  0.72000003  0.76999998]
Out[6]:
    0x7fd1297bfcd0>

Caffe学习系列(19): 绘制loss和accuracy曲线_第1张图片

原文链接:Caffe学习系列(19): 绘制loss和accuracy曲线

你可能感兴趣的:(caffe,caffe学习之路)