从log文件中提取训练数据并生成指标图

最近打算把网络训练后的指标生成图片,但是网络训练时只写入log文件里,
现在又不想添加tensorboard对网络重新训练
于是打算从log文件中提取训练数据并生成指标图
python正则表达式模块re
http://www.runoob.com/python/python-reg-expressions.html
https://www.cnblogs.com/tina-python/p/5508402.html

# -*- coding:utf-8 -*-
import re
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, FormatStrFormatter 
import numpy as np

def main_grid(flog):
    file = open(flog,'r')
    tr_acc = []
    tr_diag=[]
    te_acc = []
    te_diag=[]
    # search the line including accuracy
    for line in file:
        a=re.search('] Train-exact_acc', line)
        if a:
            an=re.search('[0-1]\.[0-9]+', line) # 正则表达式
            if an is not None:
                tr_acc.append(an.group()) # 提取精度数字
        b=re.search('] Train-diag_acc', line)
        if b:
            bn=re.search('[0-1]\.[0-9]+', line) # 正则表达式
            if bn is not None:
                tr_diag.append(bn.group()) # 提取精度数字
        a=re.search('] Validation-exact_acc', line)
        if a:
            an=re.search('[0-1]\.[0-9]+', line) # 正则表达式
            if an is not None:
                te_acc.append(an.group()) # 提取精度数字
        b=re.search('] Validation-diag_acc', line)
        if b:
            bn=re.search('[0-1]\.[0-9]+', line) # 正则表达式
            if bn is not None:
                te_diag.append(bn.group()) # 提取精度数字
    file.close()
    tr_acc=[float(v) for v in tr_acc]
    tr_diag=[float(v) for v in tr_diag]
    te_acc=[float(v) for v in te_acc]
    te_diag=[float(v) for v in te_diag]
    print(len(tr_acc),len(tr_diag),len(te_acc),len(te_diag))

    xmajorLocator   = MultipleLocator(100) #将x主刻度标签设置为20的倍数  
    xmajorFormatter = FormatStrFormatter('%1.1f') #设置x轴标签文本的格式  
    xminorLocator   = MultipleLocator(50) #将x轴次刻度标签设置为5的倍数  

    ymajorLocator   = MultipleLocator(0.5) #将y轴主刻度标签设置为0.5的倍数  
    ymajorFormatter = FormatStrFormatter('%1.1f') #设置y轴标签文本的格式  
    yminorLocator   = MultipleLocator(0.1) #将此y轴次刻度标签设置为0.1的倍数 
    # f = plt.figure(figsize=(8,8))
    ax = plt.subplot(1,1,1) #注意:一般都在ax中设置,不再plot中设置     
    plt.plot(range(len(tr_acc)),tr_acc, 'b', label='train accuracy')
    plt.plot(range(len(tr_diag)),tr_diag, 'r', label='train 1off')
    plt.plot(range(len(te_acc)),te_acc, 'c', label='test accuracy')
    plt.plot(range(len(te_diag)),te_diag, 'g', label='test 1off')
    plt.xlabel('count')
    plt.ylabel('accuracy')
    plt.title('Accuracy')
    plt.legend() # 显示图例

    ax.xaxis.set_major_locator(xmajorLocator)  
    ax.xaxis.set_major_formatter(xmajorFormatter)  

    ax.yaxis.set_major_locator(ymajorLocator)  
    ax.yaxis.set_major_formatter(ymajorFormatter)  

    # 显示次刻度标签的位置,没有标签文本  
    ax.xaxis.set_minor_locator(xminorLocator)  
    ax.yaxis.set_minor_locator(yminorLocator)  

    # ax.xaxis.grid(True, which='minor') #x坐标轴的网格使用主刻度  
    # ax.yaxis.grid(True, which='major') #x坐标轴的网格使用主刻度  
    # ax.yaxis.grid(True, which='minor') #y坐标轴的网格使用次刻度
    plt.savefig('')
    plt.show()

def main_nogrid(flog):
    file = open(flog,'r')
    tr_acc = []
    tr_diag=[]
    te_acc = []
    te_diag=[]
    # search the line including accuracy
    for line in file:
        a=re.search('] Train-exact_acc=', line)
        if a:
            an=re.search('[0-1]\.[0-9]+', line) # 正则表达式
            if an is not None:
                tr_acc.append(an.group()) # 提取精度数字
        b=re.search('] Train-diag_acc=', line)
        if b:
            bn=re.search('[0-1]\.[0-9]+', line) # 正则表达式
            if bn is not None:
                tr_diag.append(bn.group()) # 提取精度数字
        a=re.search('] Validation-exact_acc=', line)
        if a:
            an=re.search('[0-1]\.[0-9]+', line) # 正则表达式
            if an is not None:
                te_acc.append(an.group()) # 提取精度数字
        b=re.search('] Validation-diag_acc=', line)
        if b:
            bn=re.search('[0-1]\.[0-9]+', line) # 正则表达式
            if bn is not None:
                te_diag.append(bn.group()) # 提取精度数字
    file.close()
    tr_acc=[float(v) for v in tr_acc]
    tr_diag=[float(v) for v in tr_diag]
    te_acc=[float(v) for v in te_acc]
    te_diag=[float(v) for v in te_diag]
    print(len(tr_acc),len(tr_diag),len(te_acc),len(te_diag))
    f = plt.figure(figsize=(8,8))
    ax = plt.subplot(1,1,1) #注意:一般都在ax中设置,不再plot中设置
    plt.plot(range(len(tr_acc)),tr_acc, 'b', label='train accuracy')
    plt.plot(range(len(tr_diag)),tr_diag, 'r', label='train 1off')
    plt.plot(range(len(te_acc)),te_acc, 'c', label='test accuracy')
    plt.plot(range(len(te_diag)),te_diag, 'g', label='test 1off')
    plt.xlabel('count')
    plt.ylabel('acc/1off')
    plt.title('acc/1off')
    plt.legend() # 显示图例

    plt.savefig('')
    plt.show()


if __name__=='__main__':
    # flog=''
    # main_grid(flog)
    # main_nogrid(flog)
    # main_loss(flog)

猜你喜欢

转载自blog.csdn.net/u013381011/article/details/80637715