一、训练日志可视化(基于Darknet)
1.损失函数和学习率曲线(Python3)
# encoding:utf-8
import matplotlib.pyplot as plt
'''
根据关键词在日志文件中提取有用信息行
Key:关键词
LogPath:日志文件路径,默认路径为LogPath='train_yolov3_tiny.log'
'''
def extractInforLine(Key,LogPath='train_yolov3_tiny.log'):
#定义关键行
KeyInforL=[]
#用with语句读取文本文件的所有行
with open(LogPath,encoding='utf-8') as f:
Lines=f.readlines()
#用find()提取关键词所在的信息行
for line in Lines:
if line.find(Key)!=-1:
KeyInforL.append(line)
return KeyInforL
'''
根据关键信息类型提取关键信息
KeyInformL:关键信息行
KeyType:关键信息类型
'''
def extractKeyInfor(KeyInformL,KeyType):
KeyInfor=[]
#提取总损失
if KeyType == 'AllLoss':
for Line in KeyInformL:
KeyInfor.append(float(str(Line).split(',')[0].split(':')[1]))
#提取平均损失
elif KeyType=='AvgLoss':
for Line in KeyInformL:
KeyInfor.append(float(str(Line).split(',')[1].split()[0]))
#提取学习率
elif KeyType=='rate':
for Line in KeyInformL:
KeyInfor.append(float(str(Line).split(',')[2].split()[0]))
return KeyInfor
'''
本地画图函数
figureId:figure编号
Value: 纵轴数据
'''
def localPlot(figureId,Value,Lable,title,xLabel,yLabel):
fig = plt.figure(figureId, figsize=(6, 4))
ax = fig.add_subplot(1, 1, 1)
ax.plot(Value, label=Lable, color="#ff7043")
ax.legend(loc="best")
ax.set_title(title)
ax.set_xlabel(xLabel)
ax.set_ylabel(yLabel)
'''
画损失函数
'''
def drawLoss():
#根据images关键词提取关键信息行
InforLine = extractInforLine('images')
#提取总损失
AllLoss = extractKeyInfor(InforLine,'AllLoss')
#提取平均损失
AvgLoss= extractKeyInfor(InforLine,'AvgLoss')
#画总损失
localPlot(1,AllLoss,"Loss","Loss","Batches","Loss")
#画平均损失
localPlot(2,AvgLoss,"Avg Loss","Avg Loss","Batches","Avg Loss")
'''
画学习率曲线
'''
def drawLR():
#根据images关键词提取关键信息行
InforLine = extractInforLine('images')
#提取学习率
LR= extractKeyInfor(InforLine,'rate')
#画学习率曲线
localPlot(3,LR,"LR","LR","Batches","LR")
'''
仅在本文件中执行以下内容
'''
if __name__=='__main__':
#画损失曲线
drawLoss()
#画学习率曲线
drawLR()
#显示
plt.show()