caffe 画损失曲线和准确率曲线

1. 为了方便查看模型如何,是否存在过拟合,收敛等情况,最好的方法是把 损失曲线和验证集的准确率曲线画出来。直观的查看分析。

是否需要降学习率继续训练,通过准确率图来选取最好的模型。

代码在caffe/python里面有,这里面很多Python接口,熟练应用这些接口能够方便快捷的很多。


2、 首先要安装相关的库。

. 安装pycaffe必须的一些依赖项:

$ sudo apt-get install -y python-numpy python-scipy python-matplotlib python-sklearn python-skimage python-h5py python-protobuf python-leveldb python-networkx python-nose python-pandas python-gflags Cython ipython

$ sudo apt-get install -y protobuf-c-compiler protobuf-compiler

http://www.linuxdiyf.com/linux/12708.html  这个博客的安装指南不错,可以参考一下

3、编译 pycaffe 

make  pycaffe  

最终查看python接口是否编译成功:

进入python环境,进行import操作

# python
>>> import caffe

如果没有提示错误,则编译成功。

make distribute

执行完后修改bashrc文件,添加

PYTHONPATH=${HOME}/caffe/distribute/python:$PYTHONPATH

LD_LIBRARY_PATH=${HOME}/caffe/build/lib:$LD_LIBRARY_PATH

使得python能够找到caffe的依赖。

进入python,import caffe,如果成功则说明一切ok,否则检查路径从头再来,甚至需要重新编译python。


加载必要的库
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()
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]:
 
       
 
       
-------------------------------------------------------------------------------------------------------------------------------------------------
上边那种方法有个画准确度和损失曲线,但是每次都得重新运行程序,当数据量过大,网络过大的时候,费时费力。十分不方便,
在我们训练网络的时候。我们已经得到了info.txt 信息。那么我们可以通过这里面的信息来画准确度曲线和损失曲线。
下边是我写的代码;
 
       
#!/usr/bin/env python
"""
 Copyright (c) 
2016,01,13  Zhang Meng. All rights reserved.  
"""import numpy as npimport matplotlib.pyplot as plt#%matplotlib inlineimport sys,osroot_dir = os.getcwd()#info_name = root_dir + "/work-21x61/info.txt"info_name = root_dir + "/work-21x61/info.txt"info = open(info_name, "r")train_loss = []test_loss = []train_acc = []test_acc = []for line in info:Train_acc_nPos = line.find("Train net output #0: accuracy =")if Train_acc_nPos != -1:#print Train_acc_nPostrain_acc_temp = line[Train_acc_nPos + 32:-1]train_acc.append(train_acc_temp)#print train_accTrain_loss_nPos = line.find("Train net output #1: loss =")if Train_loss_nPos != -1:#print Train_loss_nPostrain_loss_temp = line[Train_loss_nPos + 28:Train_loss_nPos + 34]train_loss.append(train_loss_temp)#print train_lossTest_acc_nPos = line.find("Test net output #0: accuracy = ")if Test_acc_nPos != -1:#print Test_acc_nPostest_acc_temp = line[Test_acc_nPos + 31:-1]test_acc.append(test_acc_temp)#print test_accTest_loss_nPos = line.find("Test net output #1: loss =")if Test_loss_nPos != -1:#print Test_loss_nPostest_loss_temp = line[Test_loss_nPos + 27:Test_loss_nPos + 33]test_loss.append(test_loss_temp)#print test_lossmax_iter_nPos = line.find("max_iter:")if max_iter_nPos != -1:#print Train_acc_nPosmax_iter = line[max_iter_nPos + 10:-1]#print train_acctest_interval_nPos = line.find("test_interval:")if test_interval_nPos != -1:#print Train_acc_nPostest_interval = line[test_interval_nPos + 15:-1]#print train_accinfo.close()# print len(train_acc)# print len(train_loss)# print len(test_acc)# print len(test_loss)# print max_iter# print (test_interval)*2 test_interval define string ,here should be converted to intidx = 0for acc in test_acc:#print acc#print test_acc[idx]iter_test = int(test_interval)*idx#iter_test = int(iter_test)#print iter_testp_train_acc = plt.scatter(iter_test, train_acc[idx], marker = 'x', color = 'r', s = 1)p_train_loss= plt.scatter(iter_test, train_loss[idx], marker = 'x', color = 'y', s = 1)p_test_acc = plt.scatter(iter_test, test_acc[idx], marker = 'x', color = 'g', s = 1)p_test_loss = plt.scatter(iter_test, test_loss[idx], marker = 'x', color = 'b', s = 1)test_accuracy = test_acc[idx]idx = idx +1idx =0# p_train_acc = plt.plot(iter_test, train_acc[idx])# p_train_loss= plt.plot(iter_test, train_loss[idx])# p_test_acc = plt.plot(iter_test, test_acc[idx])# p_test_loss = plt.plot(iter_test, test_loss[idx])#print max_iter#plt.xlim((0,max_iter))#plt.text()plt.text(iter_test*0.6, 0.8, 'test accuracy:%s' % float(test_accuracy))plt.legend((p_train_acc,p_train_loss,p_test_acc,p_test_loss),('train_acc', 'train_loss','test_acc', 'test_loss'),numpoints=1, loc='upper right', ncol=6, fontsize=12)plt.ylim((0,1.2))plt.xlabel("num_iter")#plt.ylabel("Volt")plt.show()

 
      


结果图如下: 结果有的粗糙,需要的话,在改进。
 

你可能感兴趣的:(caffe)