赞
踩
基本功能:生成loss图片,保存到log日志的同一目录下,取名与log文件一致;内容涉及正则化&plot画图技巧
在跑深度学习项目的时候,可能会有各种各样的log日志,写一个工具包可以快速画出loss图很方便。如果大家的日志和我的类似,可以取用我的代码做修改。
我本次的项目日志分两种,分别是打印1个loss和打印3个loss,相关变量值可以在文件开头做修改。以下是我的两种日志:
- # -------------------------------------------------------------------------------
- # Description: 快速画出loss曲线
- # Description: 生成loss图片,保存到log日志的同一目录下,取名与log文件一致
- # Reference:
- # Author: Sophia
- # Date: 2021/7/12
- # -------------------------------------------------------------------------------
- import re
- import matplotlib.pyplot as plt
- import os.path as osp
-
- # fullpath = osp.abspath('../../log/log_train_train_img_model_xent_htri20210712-191400.txt')
- fullpath = osp.abspath('../log/log_train20210712-163303.txt')
- # mode = {'Loss'}
- mode = {'Loss', 'CLoss', 'TLoss'}
-
- filedir, filename = osp.split(fullpath)
- count = 0
- Loss, CLoss, TLoss, x = [], [], [], []
- with open(fullpath, 'r') as f:
- while True:
- line = f.readline()
- if line == '':
- break
- if not line.startswith('Epoch: ['):
- continue
- count += 1
- line = line.replace(' ', '').replace('\t', '')
- pattern = re.compile(r'[Loss]\w*.\w+[(](\w*.\w+)[)]')
- find_list = pattern.findall(line)
- if mode == {'Loss'}:
- Loss.append(float(find_list[0]))
- elif mode == {'Loss', 'CLoss', 'TLoss'}:
- Loss.append(float(find_list[0]))
- CLoss.append(float(find_list[1]))
- TLoss.append(float(find_list[2]))
- x.append(count)
-
- pngName = filename.split('.')[0]
-
- if mode == {'Loss'}:
- plt.plot(x, Loss)
- elif mode == {'Loss', 'CLoss', 'TLoss'}:
- plt.plot(x, Loss, color='red', marker='o', linestyle='dashed', linewidth=2, markersize=1)
- plt.plot(x, CLoss, color='green', marker='o', linestyle='dashed', linewidth=2, markersize=1)
- plt.plot(x, TLoss, color='blue', marker='o', linestyle='dashed', linewidth=2, markersize=1)
- plt.legend(labels=('Loss', 'CLoss', 'TLoss'))
-
- plt.savefig(osp.join(filedir, pngName))
- plt.show()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。