-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
86 lines (71 loc) · 2.69 KB
/
train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import os
import torch
from terminaltables import AsciiTable
from tqdm import tqdm
def train(model, optimizer, scheduler, dataloader, epoch, opt, logger, best_mAP=0):
model.train()
for i, (images, targets, indexes) in enumerate(tqdm(dataloader)):
optimizer.zero_grad()
# targets: [index, class_id, x, y, h, w] in yolo format
rep_targets = []
for ri in range(torch.cuda.device_count()):
rep_targets.append(targets.unsqueeze(dim=0))
rep_targets = torch.cat(rep_targets, dim=0)
if opt.gpu:
images = images.cuda()
indexes = indexes.cuda()
rep_targets = rep_targets.cuda()
targets = targets.cuda()
loss, detections = model.forward(images, rep_targets, indexes)
# detections = to_cpu(detections)
if torch.cuda.device_count() > 1:
loss = loss.sum()
loss.backward()
optimizer.step()
# logging
if torch.cuda.device_count() > 1:
metric_keys = model.module.yolo_layers[0].metrics.keys()
yolo_metrics = [model.module.yolo_layers[i].metrics for i in range(len(model.module.yolo_layers))]
else:
metric_keys = model.yolo_layers[0].metrics.keys()
yolo_metrics = [model.yolo_layers[i].metrics for i in range(len(model.yolo_layers))]
layer_header = ['YOLO Layer {}'.format(i) for i in range(len(yolo_metrics))]
metric_table_data = [['Metrics', *layer_header]]
formats = {m: '%.6f' for m in metric_keys}
for metric in metric_keys:
row_metrics = [formats[metric] % ym.get(metric, 0) for ym in yolo_metrics]
metric_table_data += [[metric, *row_metrics]]
metric_table_data += [['total loss', '{:.6f}'.format(loss.item()), '', '']]
# beautify log message
metric_table = AsciiTable(
metric_table_data,
title='[Epoch {:d}/{:d}, Batch {:d}/{:d}, Current best mAP {:4f}]'.format(epoch, opt.num_epochs, i,
len(dataloader), best_mAP))
metric_table.inner_footing_row_border = True
logger.print_and_write('{}\n'.format(metric_table.table))
scheduler.step()
# print("current best mAP:" + str(best_mAP))
# save checkpoints
if torch.cuda.device_count() > 1:
states = {
'epoch': epoch + 1,
'model': opt.model,
'state_dict': model.module.state_dict(),
'optimizer': optimizer.state_dict(),
'scheduler': scheduler.state_dict(),
'best_mAP': best_mAP,
}
else:
states = {
'epoch': epoch + 1,
'model': opt.model,
'state_dict': model.state_dict(),
'optimizer': optimizer.state_dict(),
'scheduler': scheduler.state_dict(),
'best_mAP': best_mAP,
}
save_file_path = os.path.join(opt.checkpoint_path, 'last.pth'.format(epoch))
torch.save(states, save_file_path)
if epoch % opt.checkpoint_interval == 0:
save_file_path = os.path.join(opt.checkpoint_path, 'epoch_{}.pth'.format(epoch))
torch.save(states, save_file_path)