当前位置:   article > 正文

读取txt文件并绘制loss曲线_loss.txt文件格式

loss.txt文件格式

格式一:

txt文件内loss格式如图

  1. import matplotlib.pyplot as plt
  2. import os
  3. import numpy as np
  4. fi_1 = open('./train_log.txt', 'r', encoding='utf-8') # 1、读取路径,改为自己的路径
  5. iters_num = int('39750') # 2、自己估计下坐标轴x,这里是总迭代次数
  6. lines = fi_1.readlines()
  7. total_loss = []
  8. iou_loss = []
  9. l1_loss = []
  10. conf_loss = []
  11. cls_loss = []
  12. for line in lines:
  13. if 'total_loss' in line:
  14. print(line)
  15. line0 = line.split('total_loss: ')[-1].split(', iou_loss:')[0] #利用固定字符段分割
  16. line1 = line.split('iou_loss: ')[-1].split(', l1_loss:')[0]
  17. line2 = line.split('l1_loss: ')[-1].split(', conf_loss:')[0]
  18. line3 = line.split('conf_loss: ')[-1].split(', cls_loss:')[0]
  19. line4 = line.split('cls_loss: ')[-1].split(', lr:')[0]
  20. total_loss.append(float(line0))
  21. iou_loss.append(float(line1))
  22. l1_loss.append(float(line2))
  23. conf_loss.append(float(line3))
  24. cls_loss.append(float(line4))
  25. print('-----------', line0, line1, line2, line3, line4)
  26. # break
  27. print(len(total_loss))
  28. # plt.style.use('ggplot')
  29. plt.rc('font', family='Times New Roman', size=13) # 全局中英文为字体“罗马字体”
  30. # 设置坐标轴刻度向内
  31. plt.rcParams['xtick.direction'] = 'in'
  32. plt.rcParams['ytick.direction'] = 'in'
  33. plt.figure(0, figsize=(25, 10)) # 这里修改绘制的图的大小
  34. x = np.arange(0, iters_num, 10) ################################ 自己估计下坐标轴x,这里10是源代码默认iter=10输出一次loss
  35. plt.subplot(2, 3, 1)
  36. plt.plot(x, total_loss, color='darkorange', label="Total Loss")
  37. plt.xlabel("Steps")
  38. plt.ylabel("Loss")
  39. plt.grid(True)
  40. plt.legend(loc="upper right", fontsize='xx-small')
  41. plt.subplot(2, 3, 2)
  42. plt.plot(x, iou_loss, color='darkorange', label="iou Loss")
  43. plt.xlabel("Steps")
  44. plt.ylabel("Loss")
  45. plt.grid(True)
  46. plt.legend(loc="upper right", fontsize='xx-small')
  47. plt.subplot(2, 3, 3)
  48. plt.plot(x, l1_loss, color='darkorange', label="l1 Loss")
  49. plt.xlabel("Steps")
  50. plt.ylabel("Loss")
  51. plt.grid(True)
  52. plt.legend(loc="upper right", fontsize='xx-small')
  53. plt.subplot(2, 3, 4)
  54. plt.plot(x, conf_loss, color='darkorange', label="conf Loss")
  55. plt.xlabel("Steps")
  56. plt.ylabel("Loss")
  57. plt.grid(True)
  58. plt.legend(loc="upper right", fontsize='xx-small')
  59. plt.subplot(2, 3, 5)
  60. plt.plot(x, cls_loss, color='darkorange', label="cls Loss")
  61. plt.xlabel("Steps")
  62. plt.ylabel("Loss")
  63. plt.grid(True)
  64. plt.legend(loc="upper right", fontsize='xx-small')
  65. plt.annotate("Loss", (-2, 10), xycoords='data', xytext=(-2, 10), fontsize=15)
  66. plt.savefig('losses.png', dpi=600, bbox_inches='tight')
  67. plt.show()

绘制结果

 格式二:

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

  1. import matplotlib.pyplot as plt
  2. import os
  3. import numpy as np
  4. import re
  5. # list1 = "a b c d e f"
  6. # print("re", re.split(r"[ ]+", list1))
  7. fi_1 = open('./run.log', 'r', encoding='utf-8') # 1、读取路径,改为自己的路径
  8. iters_num = int('5312') # 2、自己估计下坐标轴x,这里是总迭代次数
  9. lines = fi_1.readlines()
  10. box_loss = []
  11. conf_loss = []
  12. id_loss = []
  13. total_loss = []
  14. for line in lines:
  15. if 'Epoch' not in line and 'Start' not in line:
  16. print(line)
  17. line0 = re.split(r'[ ]+', line)[5] # 多个空格的情况下分割空格前后的字段
  18. line1 = re.split(r'[ ]+', line)[6]
  19. line2 = re.split(r'[ ]+', line)[7]
  20. line3 = re.split(r'[ ]+', line)[8]
  21. box_loss.append(float(line0))
  22. conf_loss.append(float(line1))
  23. id_loss.append(float(line2))
  24. total_loss.append(float(line3))
  25. # print('-----------', line0, line1, line2, line3)
  26. # break
  27. print(len(total_loss))
  28. # plt.style.use('ggplot')
  29. plt.rc('font', family='Times New Roman', size=13) # 全局中英文为字体“罗马字体”
  30. plt.rcParams['xtick.direction'] = 'in'
  31. plt.rcParams['ytick.direction'] = 'in'
  32. plt.figure(0, figsize=(25, 10))
  33. x = np.arange(0, iters_num, 8) ################################ 自己估计下坐标轴x,这里10是源代码默认iter=10输出一次loss
  34. plt.subplot(2, 2, 1)
  35. plt.plot(x, box_loss, color='darkorange', label="box Loss")
  36. plt.xlabel("Steps")
  37. plt.ylabel("Loss")
  38. plt.grid(True)
  39. plt.legend(loc="upper right", fontsize='xx-small')
  40. plt.subplot(2, 2, 2)
  41. plt.plot(x, conf_loss, color='darkorange', label="conf Loss")
  42. plt.xlabel("Steps")
  43. plt.ylabel("Loss")
  44. plt.grid(True)
  45. plt.legend(loc="upper right", fontsize='xx-small')
  46. plt.subplot(2, 2, 3)
  47. plt.plot(x, id_loss, color='darkorange', label="id Loss")
  48. plt.xlabel("Steps")
  49. plt.ylabel("Loss")
  50. plt.grid(True)
  51. plt.legend(loc="upper right", fontsize='xx-small')
  52. plt.subplot(2, 2, 4)
  53. plt.plot(x, total_loss, color='darkorange', label="total Loss")
  54. # plt.ylim(-12, 0) # 设置y轴范围
  55. plt.xlabel("Steps")
  56. plt.ylabel("Loss")
  57. plt.grid(True)
  58. plt.legend(loc="upper right", fontsize='xx-small')
  59. plt.annotate("Loss", (-2, 10), xycoords='data', xytext=(-2, 10), fontsize=15)
  60. plt.savefig('losses.png', dpi=600, bbox_inches='tight')
  61. plt.show()

 绘制结果

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/635972
推荐阅读
相关标签
  

闽ICP备14008679号