当前位置: 代码迷 >> 综合 >> 绘制loss曲线图,根据caffe打印出的日志文档
  详细解决方案

绘制loss曲线图,根据caffe打印出的日志文档

热度:88   发布时间:2023-09-30 22:23:53.0

代码解析:

根据caffe日志文档,字符切割读取需要的x,y轴点,利用pylab 绘制曲线图。

注意:切取字符串的时候可能会丢失精度,同时也有可能因为截取的字符串太短出错,所以必须根据自己的日志设置需要截取字符串的合适的长度

#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 文件名:DrawPic.py
import os
import pylab as pl
# 函数名:draw_roc
# 参数说明:
# loss_flag:画loss或者loss_dis标记(1为loss)
# filepath:本地文件夹路径
# filename:本地文件名称
def draw_roc(loss_flag,filePath,fileName):
    # 本地文件名称
    myFile = filePath + fileName
    # 绘图变量
    fig = pl.figure()
    # 读取文件
    loss_file = open(myFile)
    iter_file = open(myFile)
    # x轴对象
    x1 = []
    # y轴对象
    y1 = []
    # 定位x轴元素出现的位置
    iter_first = 'Iteration'
    iter_last = ' / '
    # 定位y轴元素出现的位置(两组y轴对象)
    loss = '#0:'
    loss_dif = '#1: loss_dif'
    dic = {}
    # 最小loss
    try:
        # 读取x轴点坐标
        for line in iter_file:
            try:
                first = line.index(iter_first) + 10
                last = line.index(iter_last)
                x1.append(float(line[first:last]))
            except:
                pass
        # 当loss_fig=1时,读取y轴点坐标
        if loss_flag == 1:
            for line in loss_file:
                try:
                    loss_first_pos = line.index(loss) + 11
                    try:
                        temp = float(line[loss_first_pos:loss_first_pos + 7])
                        y1.append(temp)
                        print 'y1====', temp
                        # if float(line[loss_first_pos:loss_first_pos+7])>10:
                        # print line[loss_first_pos:loss_first_pos + 7]
                    except:
                        temp = float(line[loss_first_pos:loss_first_pos + 5])
                        y1.append(temp)
                        print 'y1====', temp
                except:
                    pass
        else:
            # 当loss_fig!= 1时,读取y轴点坐标
            for line in loss_file:
                try:
                    loss_dif_first_pos = line.index(loss_dif) + 15
                    try:
                        temp = float(line[loss_dif_first_pos:loss_dif_first_pos + 8])
                        y1.append(temp)
                    except:
                        temp = float(line[loss_dif_first_pos:loss_dif_first_pos + 6])
                        y1.append(temp)
                except:
                    pass
    finally:
        # 关闭文件资源
        loss_file.close()
        iter_file.close()
    # 最小Los值
    min_los = 1
    # 最小迭代次数
    min_los_iter = 0
    # 去y轴最小对象
    for temp in range(len(y1)):
        # print'temp===', temp
        test = min_los - y1[temp]
        if test > 0:
            min_los = y1[temp]
            min_los_iter = x1[temp]
    print'min_los===', min_los, '===min_los_iter===', min_los_iter
    pl.plot(x1, y1, label='min_loss')  # x1为x轴,y1为y轴
    # pl.xticks(x1, rotation=0)  # 绘制x轴坐标
    pl.margins(0.08)
    pl.title('Iteration|loss')  # give plot a title
    pl.xlabel('Iteration                       y_min=' + str(min_los) + '      x_min=' + str(
        min_los_iter))  # make axis labels
    pl.ylabel('loss')  # y轴名字
    pl.xlim(min(x1), x1[len(x1) - 1])  # set axis limits
    pl.ylim(min(y1), max(y1))
    pl.legend(loc='upper left')  # upper left 标识线条
    pl.show()  # 显示图像
    if loss_flag == 1:
        # 保存loss图像,本地地址:filepath+'loss.jpg'
        fig.savefig(filePath + 'loss.jpg')
    else:
        # 保存loss_dif图像,本地地址:filepath+'loss_dif.jpg'
        fig.savefig(filePath + 'loss_dif.jpg')
if __name__ == '__main__':
    # 函数名:draw_roc
    # 参数说明:
    # loss_flag:画loss或者loss_dis标记(1为loss)
    # filepath:本地文件夹路径
    # filename:本地文件名称
    draw_roc(1,'C:\\work\\1221\\3\\','caffe.xiaoran-Vostro-3900.xiaoran.log.INFO.20171222-110151.31630')
    draw_roc(2, 'C:\\work\\1221\\3\\', 'caffe.xiaoran-Vostro-3900.xiaoran.log.INFO.20171222-110151.31630')