caffe训练日志可视化代码(acc,train_loss,test_loss,lr 画在同一张图中)

caffe自带可视化训练log的脚本程序

但是这个程序会将:train_loss,test_loss,lr,acc每个指标单独画一张图(根据参数可选则画什么),如下: 

我觉得这样生成的图片太多了,而且train_loss和test_loss(实际上是训练过程中的val_loss)通常是要放在一起对比来看的,所以放在同一张图中会更清晰的看出是否过拟合或者欠拟合。因此我自己写了个程序提取训练log中的信息,将几个指标综合到一张图中显示,个人认为方便了很多,也只产生一张图而已。代码如下:

import os
import numpy as np
import matplotlib.pyplot as plt
import sys 
import math

#解析出训练和测试的log信息 
def get_loss_acc(log_path):
    train_iters=[]
    train_losses=[]
    test_iters=[]
    test_losses=[]
    test_accs=[]
    learn_rates=[]
    log=open(log_path,"r")
    for line in log:
        #train iter & loss
        if "Iteration" in line and "loss" in line:  
            iter=int((line.strip().split()[5]).split(",")[0])
            #print (iter)
            train_loss=float(line.strip().split()[-1])
            train_iters.append(iter)
            #print (train_loss)
            train_losses.append(train_loss)
        #test iter
        if "Iteration" in line and "Testing" in line:
            test_iter=int(line.strip().split()[5][:-1])
            test_iters.append(test_iter)
        #test loss
        if "#1" in line:
            test_loss=float((line.strip().split(' = ')[-1])[:-6])
            test_losses.append(test_loss)
        #test acc
        if "Test" in line and "acc" in line:
            acc=float(line.strip().split("=")[-1])
            test_accs.append(acc)
        #train lr
        if "lr" in line and "Iteration" in line:
            lr=float(line.strip().split("=")[-1])
            learn_rates.append(lr)     
    #有时学习率个数和train iter的个数不相等,这里使用最后几次的学习率补齐         
    while (len(learn_rates)<len(train_iters)):     
        learn_rates.append(learn_rates[-1]) 
    return train_iters,train_losses,test_iters,test_losses,test_accs,learn_rates

#画多个曲线,一张图上画acc,train loss,test loss,lr   
def plot_muilti_figs(log_path,log_file,log_fig):

    train_iters,train_losses,test_iters,test_losses,test_accs,learn_rates=get_loss_acc(log_path+"/"+log_file)
    print ("train iters: ",len(train_iters))
    print ("train losses: ",len(train_losses))
    print ("test iters: ",len(test_iters))
    print ("test losses: ",len(test_losses))
    print ("test accs: ",len(test_accs))
    print ("learn_rates: ",len(learn_rates))
    
    fig=plt.figure(figsize=(12,9)) #画布大小为1200*900,默认是800*600,也就是figsize=(8,6)
    plt.subplot(211)
    plt.plot(train_iters, train_losses, 'b-',label="train loss")
    plt.plot(test_iters, test_losses, 'r-',label="test loss")
    plt.legend(loc = 1, ncol = 1) # ajust ncol to fit the space
    plt.grid(True)
    plt.subplot(223)
    plt.plot(test_iters, test_accs, 'g-',label="acc")
    plt.legend(loc = 5, ncol = 1) # ajust ncol to fit the space
    plt.grid(True)
    plt.subplot(224)
    plt.plot(train_iters, learn_rates,"m",label="lr")
    plt.legend(loc = 1, ncol = 1) # ajust ncol to fit the space
    plt.grid(True)
    #置于所有画图程序最后,但在show函数前,使整个图更协调,文字不重叠,若数字太多,可适当增大画布尺寸  
    fig.tight_layout() 
    plt.savefig(log_path+"/"+log_fig+".png")
    plt.show()


if __name__=="__main__":

    log_path=os.getcwd() #与log文件放在一个文件夹下
    log_file=sys.argv[1] #训练log文件名
    log_fig=sys.argv[2]  #要保存的图片名称,不带后缀
    plot_muilti_figs(log_path,log_file,log_fig)
   

 使用时,将训练过程中产生的log到出到txt文件或者train.log文件,我是为了保持和caffe自带的程序一样,所以这里也默认使用train.log作为导出的日志文件,可以在训练命令中加入导出语句来实现log的导出:

caffe train --solver=efficeintNet_relu_se_solver.prototxt >log/train.log 2>&1

然后将本文的代码放在和train.log同文件夹下,再写个批处理文件draw.bat,内容如下:

python draw.py train.log log
pause

最后的目录如下图所示(plot_log.bat是我使用的caffe自带的画图工具的批处理命令):

然后双击draw.bat就可以生成一张图,log.png,当然名字是作为参数可修改的:

第一张长图里面就是train_loss和test_loss(实际就是val_loss)的对比图,左下角是训练过程中的验证集准确率,右下角是学习率的变化情况,一目了然,方便的很多。代码中画图参数以及log文件名等参数都是可以根据自己需要修改的。

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值