找回密码
 nanjixiong2018注册

QQ登录

只需一步,快速开始

发表帖子

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

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



In [1]:
  1. #加载必要的库
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. %matplotlib inline
  5. import sys,os,caffe
  6. #设置当前目录
  7. caffe_root = '/home/bnu/caffe/'
  8. sys.path.insert(0, caffe_root + 'python')
  9. os.chdir(caffe_root)
复制代码

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

In [2]:
  1. # set the solver prototxt
  2. caffe.set_device(0)
  3. caffe.set_mode_gpu()
  4. solver = caffe.SGDSolver('examples/cifar10/cifar10_quick_solver.prototxt')
复制代码

如果不需要绘制曲线,只需要训练出一个caffemodel, 直接调用solver.solve()就可以了。如果要绘制曲线,就需要把迭代过程中的值
保存下来,因此不能直接调用solver.solve(), 需要迭代。在迭代过程中,每迭代200次测试一次
In [5]:
  1. %%time
  2. niter =4000
  3. test_interval = 200
  4. train_loss = np.zeros(niter)
  5. test_acc = np.zeros(int(np.ceil(niter / test_interval)))

  6. # the main solver loop
  7. for it in range(niter):
  8.     solver.step(1)  # SGD by Caffe
  9.    
  10.     # store the train loss
  11.     train_loss[it] = solver.net.blobs['loss'].data
  12.     solver.test_nets[0].forward(start='conv1')
  13.    
  14.     if it % test_interval == 0:
  15.         acc=solver.test_nets[0].blobs['accuracy'].data
  16.         print 'Iteration', it, 'testing...','accuracy:',acc
  17.         test_acc[it // test_interval] = acc
复制代码
  1. Iteration 0 testing... accuracy: 0.10000000149
  2. Iteration 200 testing... accuracy: 0.419999986887
  3. Iteration 400 testing... accuracy: 0.479999989271
  4. Iteration 600 testing... accuracy: 0.540000021458
  5. Iteration 800 testing... accuracy: 0.620000004768
  6. Iteration 1000 testing... accuracy: 0.629999995232
  7. Iteration 1200 testing... accuracy: 0.649999976158
  8. Iteration 1400 testing... accuracy: 0.660000026226
  9. Iteration 1600 testing... accuracy: 0.660000026226
  10. Iteration 1800 testing... accuracy: 0.670000016689
  11. Iteration 2000 testing... accuracy: 0.709999978542
  12. Iteration 2200 testing... accuracy: 0.699999988079
  13. Iteration 2400 testing... accuracy: 0.75
  14. Iteration 2600 testing... accuracy: 0.740000009537
  15. Iteration 2800 testing... accuracy: 0.769999980927
  16. Iteration 3000 testing... accuracy: 0.75
  17. Iteration 3200 testing... accuracy: 0.699999988079
  18. Iteration 3400 testing... accuracy: 0.740000009537
  19. Iteration 3600 testing... accuracy: 0.72000002861
  20. Iteration 3800 testing... accuracy: 0.769999980927
  21. CPU times: user 41.7 s, sys: 54.2 s, total: 1min 35s
  22. Wall time: 1min 18s
复制代码

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

In [6]:
  1. print test_acc
  2. _, ax1 = plt.subplots()
  3. ax2 = ax1.twinx()
  4. ax1.plot(np.arange(niter), train_loss)
  5. ax2.plot(test_interval * np.arange(len(test_acc)), test_acc, 'r')
  6. ax1.set_xlabel('iteration')
  7. ax1.set_ylabel('train loss')
  8. ax2.set_ylabel('test accuracy')
复制代码
  1. [ 0.1         0.41999999  0.47999999  0.54000002  0.62        0.63
  2.   0.64999998  0.66000003  0.66000003  0.67000002  0.70999998  0.69999999
  3.   0.75        0.74000001  0.76999998  0.75        0.69999999  0.74000001
  4.   0.72000003  0.76999998]
复制代码
Out[6]:
  1. <matplotlib.text.Text at 0x7fd1297bfcd0>
复制代码

转自TonyYang1995


使用道具 举报 回复
您需要登录后才可以回帖 登录 | nanjixiong2018注册

本版积分规则