读取txt文件并绘制loss曲线

格式一:

txt文件内loss格式如图

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

fi_1 = open('./train_log.txt', 'r', encoding='utf-8')  # 1、读取路径,改为自己的路径
iters_num = int('39750')  # 2、自己估计下坐标轴x,这里是总迭代次数

lines = fi_1.readlines()

total_loss = []
iou_loss = []
l1_loss = []
conf_loss = []
cls_loss = []
for line in lines:
    if 'total_loss' in line:
        print(line)
        line0 = line.split('total_loss: ')[-1].split(', iou_loss:')[0] #利用固定字符段分割
        line1 = line.split('iou_loss: ')[-1].split(', l1_loss:')[0]
        line2 = line.split('l1_loss: ')[-1].split(', conf_loss:')[0]
        line3 = line.split('conf_loss: ')[-1].split(', cls_loss:')[0]
        line4 = line.split('cls_loss: ')[-1].split(', lr:')[0]
        total_loss.append(float(line0))
        iou_loss.append(float(line1))
        l1_loss.append(float(line2))
        conf_loss.append(float(line3))
        cls_loss.append(float(line4))
        print('-----------', line0, line1, line2, line3, line4)
        # break
print(len(total_loss))
# plt.style.use('ggplot')
plt.rc('font', family='Times New Roman', size=13)  # 全局中英文为字体“罗马字体”
# 设置坐标轴刻度向内
plt.rcParams['xtick.direction'] = 'in'
plt.rcParams['ytick.direction'] = 'in'
plt.figure(0, figsize=(25, 10))   # 这里修改绘制的图的大小
x = np.arange(0, iters_num, 10)  ################################ 自己估计下坐标轴x,这里10是源代码默认iter=10输出一次loss
plt.subplot(2, 3, 1)
plt.plot(x, total_loss, color='darkorange', label="Total Loss")
plt.xlabel("Steps")
plt.ylabel("Loss")
plt.grid(True)
plt.legend(loc="upper right", fontsize='xx-small')
plt.subplot(2, 3, 2)
plt.plot(x, iou_loss, color='darkorange', label="iou Loss")
plt.xlabel("Steps")
plt.ylabel("Loss")
plt.grid(True)
plt.legend(loc="upper right", fontsize='xx-small')
plt.subplot(2, 3, 3)
plt.plot(x, l1_loss, color='darkorange', label="l1 Loss")
plt.xlabel("Steps")
plt.ylabel("Loss")
plt.grid(True)
plt.legend(loc="upper right", fontsize='xx-small')
plt.subplot(2, 3, 4)
plt.plot(x, conf_loss, color='darkorange', label="conf Loss")
plt.xlabel("Steps")
plt.ylabel("Loss")
plt.grid(True)
plt.legend(loc="upper right", fontsize='xx-small')
plt.subplot(2, 3, 5)
plt.plot(x, cls_loss, color='darkorange', label="cls Loss")
plt.xlabel("Steps")
plt.ylabel("Loss")
plt.grid(True)
plt.legend(loc="upper right", fontsize='xx-small')
plt.annotate("Loss", (-2, 10), xycoords='data', xytext=(-2, 10), fontsize=15)
plt.savefig('losses.png', dpi=600, bbox_inches='tight')
plt.show()

绘制结果

 格式二:

txt内loss以表格形式写入,如上图

import matplotlib.pyplot as plt
import os
import numpy as np
import re
# list1 = "a  b  c    d      e    f"
# print("re", re.split(r"[ ]+", list1))
fi_1 = open('./run.log', 'r', encoding='utf-8')  # 1、读取路径,改为自己的路径
iters_num = int('5312')  # 2、自己估计下坐标轴x,这里是总迭代次数

lines = fi_1.readlines()

box_loss = []
conf_loss = []
id_loss = []
total_loss = []
for line in lines:
    if 'Epoch' not in line and 'Start' not in line:
        print(line)
        line0 = re.split(r'[ ]+', line)[5]  # 多个空格的情况下分割空格前后的字段
        line1 = re.split(r'[ ]+', line)[6]
        line2 = re.split(r'[ ]+', line)[7]
        line3 = re.split(r'[ ]+', line)[8]
        box_loss.append(float(line0))
        conf_loss.append(float(line1))
        id_loss.append(float(line2))
        total_loss.append(float(line3))
        # print('-----------', line0, line1, line2, line3)
        # break
print(len(total_loss))
# plt.style.use('ggplot')
plt.rc('font', family='Times New Roman', size=13)  # 全局中英文为字体“罗马字体”
plt.rcParams['xtick.direction'] = 'in'
plt.rcParams['ytick.direction'] = 'in'
plt.figure(0, figsize=(25, 10))
x = np.arange(0, iters_num, 8)  ################################ 自己估计下坐标轴x,这里10是源代码默认iter=10输出一次loss

plt.subplot(2, 2, 1)
plt.plot(x, box_loss, color='darkorange', label="box Loss")
plt.xlabel("Steps")
plt.ylabel("Loss")
plt.grid(True)
plt.legend(loc="upper right", fontsize='xx-small')
plt.subplot(2, 2, 2)
plt.plot(x, conf_loss, color='darkorange', label="conf Loss")
plt.xlabel("Steps")
plt.ylabel("Loss")
plt.grid(True)
plt.legend(loc="upper right", fontsize='xx-small')
plt.subplot(2, 2, 3)
plt.plot(x, id_loss, color='darkorange', label="id Loss")
plt.xlabel("Steps")
plt.ylabel("Loss")
plt.grid(True)
plt.legend(loc="upper right", fontsize='xx-small')
plt.subplot(2, 2, 4)
plt.plot(x, total_loss, color='darkorange', label="total Loss")
# plt.ylim(-12, 0)  # 设置y轴范围
plt.xlabel("Steps")
plt.ylabel("Loss")
plt.grid(True)
plt.legend(loc="upper right", fontsize='xx-small')

plt.annotate("Loss", (-2, 10), xycoords='data', xytext=(-2, 10), fontsize=15)
plt.savefig('losses.png', dpi=600, bbox_inches='tight')
plt.show()

 绘制结果

 参考:YOLOX-绘制五种loss曲线_自在犹仙的博客-CSDN博客_loss曲线绘制

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值