Caffe - Python 读取日志文件的 损失与准确率
Caffe - Python 读取日志文件的 损失与准确率
# -*- coding: utf-8 -*-
'''
读取Caffe Log文件 并进行图表显示
'''
import re
import string
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
Kai = matplotlib.font_manager.FontProperties(fname=r'C:\Windows\Fonts\simkai.ttf') #设置中文
def ReadLog(url):
file = open(url,'r')
accuracy_test=[]
loss_test=[]
loss_train=[]
for line in file:
if re.search('Test net output #0: accuracy =',line):
n = re.search('accuracy = .*?\n',line)
accuracy_test.append(line[n.start()+10:n.end()-1])
elif re.search('Test net output #1: loss_attr =',line):
m = re.search('#1: loss_attr = .*? \(',line)
loss_test.append(line[m.start()+15:m.end()-1])
elif re.search('Train net output #0: loss_attr =', line):
k = re.search('#0: loss_attr = .*? \(', line)
loss_train.append(line[k.start()+15:k.end()-1])
file.close()
plt.figure(1)
ax1 = plt.subplot(221)
ax2 = plt.subplot(223)
ax3 = plt.subplot(222)
plt.plot(loss_train)
plt.title('训练损失图',fontproperties=Kai)
plt.xlabel('次数',fontproperties=Kai)
plt.ylabel('损失',fontproperties=Kai)
plt.sca(ax1)
plt.plot(loss_test)
plt.title('测试损失图',fontproperties=Kai)
plt.xlabel('次数', fontproperties=Kai)
plt.ylabel('损失', fontproperties=Kai)
plt.sca(ax2)
X = []
Y = []
for i in range(0,len(accuracy_test)):
X.append(i)
Y.append(0.8)
plt.plot(X,accuracy_test,'g-',X,Y,'r--')
plt.title('训练准确率图',fontproperties=Kai)
plt.xlabel('次数', fontproperties=Kai)
plt.ylabel('准确率', fontproperties=Kai)
plt.ylim(0,1)
plt.grid()
plt.sca(ax3)
plt.show()
if __name__ == '__main__':
ReadLog("D:\ProjectWork\Caffe\Vgg_Color\Log\caffe.exe.DESKTOP-I092MGR.YSG.log.INFO.20170622-182320.15036");
结果:

浙公网安备 33010602011771号