caffe两种绘制loss曲线的方法
我搜索了一下,然后发现一种是自己写然后绘制,
#这段代码可以在没有桌面的服务器上运行,历时两天终于搞定了,需要注意的是solver中路径参数都需要为全路径,显示所在文件夹的全路径命令为 pwd。
#如果出现中文解析问题,请删除中文注释。
#!usr/bin/env python #coding:utf-8 from pylab import * #import numpy as np import matplotlib matplotlib.use('Agg') import sys,os import matplotlib.pyplot as plt caffe_root = '/home/hugo/caffe/' sys.path.insert(0, caffe_root + 'python') import caffe #caffe.set_device(all) caffe.set_mode_cpu() solver = caffe.SGDSolver('/home/hugo/caffe/examples/mnist/lenet_solver.prototxt') #max_iter niter =1000 display_iter = 100 test_iter = 100 test_interval = 500 train_loss = zeros(ceil(niter * 1.0 / display_iter)) # test loss test_loss = zeros(ceil(niter * 1.0 / test_interval)) # test accuracy test_acc = zeros(ceil(niter * 1.0 / test_interval)) solver.step(1) _train_loss = 0; _test_loss = 0; _accuracy = 0 for it in range(niter): # 进行一次解算 solver.step(1) # 计算train loss _train_loss += solver.net.blobs['loss'].data if it % display_iter == 0: # 计算平均train loss train_loss[it // display_iter] = _train_loss / display_iter _train_loss = 0 if it % test_interval == 0: for test_it in range(test_iter): # 进行一次测试 solver.test_nets[0].forward() # 计算test loss _test_loss += solver.test_nets[0].blobs['loss'].data # 计算test accuracy _accuracy += solver.test_nets[0].blobs['accuracy'].data # 计算平均test loss test_loss[it / test_interval] = _test_loss / test_iter # 计算平均test accuracy test_acc[it / test_interval] = _accuracy / test_iter _test_loss = 0 _accuracy = 0 # 绘制train loss、test loss和accuracy曲线 print '\nplot the train loss and test accuracy\n' # train loss -> 绿色 plt.plot(display_iter * arange(len(train_loss)), train_loss, 'g',label='train_loss') # test loss -> 黄色 plt.plot(test_interval * arange(len(test_loss)), test_loss, 'y',label='test_loss') # test accuracy -> 红色 plt.plot(test_interval * arange(len(test_acc)), test_acc, 'r',label='test_acc') plt.xlabel('iteration') plt.ylabel('loss') plt.savefig('/home/hugo/caffe/examples/mnist/lenet.png')
运行上述代码时如果出现 TypeError: 'numpy.float64' object cannot be interpreted as an index
请执行sudo pip install -U numpy==1.11.0,降低numpy版本就可以了
或者,这段代码也可以运行,但是由于调用了twinx(),需要桌面,所以我在服务器上总跑不通。
import numpy as np import matplotlib.pyplot as plt import sys,os caffe_root = '/home/tyd/caffe/' # this file should be run from {caffe_root}/examples (otherwise change this line) sys.path.insert(0, caffe_root + 'python') import caffe #caffe.set_device(0) caffe.set_mode_cpu() solver = caffe.SGDSolver('/home/tyd/caffe/examples/mnist/lenet_solver.prototxt') niter =1000 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 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()
还有一种是利用caffe自带的工具,像下面这篇博客里写的http://blog.csdn.net/u013078356/article/details/51154847
#!/usr/bin/env sh set -e LOG=/home/hugo/caffe/examples/mnist/log-'data +%Y-%m-%d-%H-%S'.log ./build/tools/caffe train --solver=examples/mnist/lenet_solver.prototxt 2>&1 | tee $LOG
抓log
cp parse_log.sh extract_seconds.py plot_training_log.py.example /home/hugo/caffe/examples/mnist/
复制caffe/tools/extra/,里面的三个脚本到需要绘制图片的log所在文件夹,
python plot_training_log.py 3 test.png log3wnocenter.log
直接调用就可以生成了,但是这个方法缺点很多,由于对数据处理的不准确,绘制的图像也不准确,4-7的参数还不能用,真是太坑人了,我用后面的方法汇报给主管,主管很生气,也怪我不认真,mother fucker。
在服务器上运行还有一个小技巧,就是远程ssh连接进行训练时,由于时间较长,一不小心关掉命令行,还得把model加到脚本里,继续进行训练,关键看不到停止岂不是浪费时间,
nohup python loss.py > nohup.log 2>&1 &
这样就可以在关掉命令行后还继续在后台进行训练了。