這裡我們在ipython notebook中繪製曲線
#載入必要的庫
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配置檔案
# 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次測試一次。
%%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
forit
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')
ifit % 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曲線
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]
caffe 繪製accuracy和loss曲線
我們在訓練的時候會用到caffe buile tools caffe 這個裡面的train這個選項。在輸入之後,正常會顯示訓練日誌的詳細資訊。想要畫出這裡面顯示的loss和accuracy圖,就可以把這些輸出的日誌內容重定向到乙個檔案內,然後利用shell命令檢索出其中的loss和accuracy值...
caffe繪製train和loss曲線
caffe tools extra下面有幾個檔案,必備的 1 caffe tools extra parse log.sh 2 caffe tools extra extract seconds.py 3 caffe tools extra plot training log.py.example ...
使用Caffe自帶工具繪製loss曲線
修改訓練命令為 python solve.py 2 1 tee out.log用 caffe 自帶的指令碼繪製 loss 曲線 將 caffe tools extra目錄下的plot training log.py.example,extract seconds.py,parse log.sh,pa...