在训练中加入log,记录每轮epoch的loss值。
# 打印训练log
log_file.write("Epoch %d/%d | training loss = %.3f, avg_loss = %.3f, lr= %f\n" %
(epoch, opts.epoch, loss.item(), losssum / num_train,
optimizer.state_dict()['param_groups'][0]['lr']))
log_file.write("TrainLoss:obj loss = %.2f, cls loss = %.2f, bbox loss = %.2f\n" %
(conf_loss.item(), cls_loss.item(), bbox_loss.item()))
# 打印的示例如下:
2023-03-17 11:42:04
======================training epoch 1======================
Epoch 1/300 | training loss = 4.946, avg_loss = 0.118, lr= 0.000100
TrainLoss:obj loss = 175.42, cls loss = 1121.56, bbox loss = 345.44
Training consumes 467.38 second
导入训练的打印的log,通过逐行读取并筛选出epoch与loss。
import matplotlib.pyplot as plt
import numpy as np
class curve():
def __init__(self, path='./log.txt'):
self.train_losses = []
self.avg_losses = []
self.obj_losses = []
self.cls_losses = []
self.bbox_losses = []
self.lrs = []
self.filename = path
self.epoch = 0
def dataload(self):
with open(self.filename, 'r') as f:
lines = f.readlines()
for line in lines:
if line[0] == 'E':
a = line.split('/')[0]
epoch = ''.join([x for x in a if x.isdigit()]) # 判断是否为数字
self.epoch = int(epoch)
train_loss = line.split('=')[1].split(',')[0]
train_loss = float(train_loss)
self.train_losses.append(train_loss)
avg_loss = line.split('=')[2].split(',')[0]
self.avg_losses.append(avg_loss)
lr = line.split('=')[3]
lr = float(lr)
self.lrs.append(lr)
a = line[0:9]
elif line[0:9] == 'TrainLoss':
objloss = line.split('=')[1].split(',')[0]
self.obj_losses.append(objloss)
classloss = line.split('=')[2].split(',')[0]
self.cls_losses.append(classloss)
bboxloss = float(line.split('=')[3].split(',')[0])
self.bbox_losses.append(bboxloss)
def plot(self, title, data, ylim, color):
epoch = self.epoch
plt.xlim(1, epoch)
if ylim:
plt.ylim(ylim)
plt.plot(np.arange(epoch), data, color=color)
plt.xlabel('epoches')
plt.title(title)
plt.show()
def show(self, tl, lr, obj, cls, bbox):
if tl:
title = 'train loss'
self.plot(title, data=self.train_losses, ylim=None, color='red')
if lr:
title = 'learning rate'
self.plot(title, data=self.lrs, ylim=(0.0001, 0.01), color='blue')
if obj:
title = 'object loss'
self.plot(title, data=self.obj_losses, ylim=None, color='red')
if cls:
title = 'class loss'
self.plot(title, data=self.cls_losses, ylim=None, color='red')
if bbox:
title = 'boundbox loss'
self.plot(title,data=self.bbox_losses, ylim=None, color='red')
c = curve()
c.dataload()
c.show(tl=True, lr=True, obj=False, cls=False, bbox=True)