-
Notifications
You must be signed in to change notification settings - Fork 40
/
process.py
144 lines (111 loc) · 4.93 KB
/
process.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import logging
import math
import operator
import time
import torch as t
from util import AverageMeter
__all__ = ['train', 'validate', 'PerformanceScoreboard']
logger = logging.getLogger()
def accuracy(output, target, topk=(1,)):
"""Computes the accuracy over the k top predictions for the specified values of k"""
with t.no_grad():
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].view(-1).float().sum(0, keepdim=True)
res.append(correct_k.mul_(100.0 / batch_size))
return res
def train(train_loader, model, criterion, optimizer, lr_scheduler, epoch, monitors, args):
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
batch_time = AverageMeter()
total_sample = len(train_loader.sampler)
batch_size = train_loader.batch_size
steps_per_epoch = math.ceil(total_sample / batch_size)
logger.info('Training: %d samples (%d per mini-batch)', total_sample, batch_size)
model.train()
end_time = time.time()
for batch_idx, (inputs, targets) in enumerate(train_loader):
inputs = inputs.to(args.device.type)
targets = targets.to(args.device.type)
outputs = model(inputs)
loss = criterion(outputs, targets)
acc1, acc5 = accuracy(outputs.data, targets.data, topk=(1, 5))
losses.update(loss.item(), inputs.size(0))
top1.update(acc1.item(), inputs.size(0))
top5.update(acc5.item(), inputs.size(0))
if lr_scheduler is not None:
lr_scheduler.step(epoch=epoch, batch=batch_idx)
optimizer.zero_grad()
loss.backward()
optimizer.step()
batch_time.update(time.time() - end_time)
end_time = time.time()
if (batch_idx + 1) % args.log.print_freq == 0:
for m in monitors:
m.update(epoch, batch_idx + 1, steps_per_epoch, 'Training', {
'Loss': losses,
'Top1': top1,
'Top5': top5,
'BatchTime': batch_time,
'LR': optimizer.param_groups[0]['lr']
})
logger.info('==> Top1: %.3f Top5: %.3f Loss: %.3f\n',
top1.avg, top5.avg, losses.avg)
return top1.avg, top5.avg, losses.avg
def validate(data_loader, model, criterion, epoch, monitors, args):
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
batch_time = AverageMeter()
total_sample = len(data_loader.sampler)
batch_size = data_loader.batch_size
steps_per_epoch = math.ceil(total_sample / batch_size)
logger.info('Validation: %d samples (%d per mini-batch)', total_sample, batch_size)
model.eval()
end_time = time.time()
for batch_idx, (inputs, targets) in enumerate(data_loader):
with t.no_grad():
inputs = inputs.to(args.device.type)
targets = targets.to(args.device.type)
outputs = model(inputs)
loss = criterion(outputs, targets)
acc1, acc5 = accuracy(outputs.data, targets.data, topk=(1, 5))
losses.update(loss.item(), inputs.size(0))
top1.update(acc1.item(), inputs.size(0))
top5.update(acc5.item(), inputs.size(0))
batch_time.update(time.time() - end_time)
end_time = time.time()
if (batch_idx + 1) % args.log.print_freq == 0:
for m in monitors:
m.update(epoch, batch_idx + 1, steps_per_epoch, 'Validation', {
'Loss': losses,
'Top1': top1,
'Top5': top5,
'BatchTime': batch_time
})
logger.info('==> Top1: %.3f Top5: %.3f Loss: %.3f\n', top1.avg, top5.avg, losses.avg)
return top1.avg, top5.avg, losses.avg
class PerformanceScoreboard:
def __init__(self, num_best_scores):
self.board = list()
self.num_best_scores = num_best_scores
def update(self, top1, top5, epoch):
""" Update the list of top training scores achieved so far, and log the best scores so far"""
self.board.append({'top1': top1, 'top5': top5, 'epoch': epoch})
# Keep scoreboard sorted from best to worst, and sort by top1, top5 and epoch
curr_len = min(self.num_best_scores, len(self.board))
self.board = sorted(self.board,
key=operator.itemgetter('top1', 'top5', 'epoch'),
reverse=True)[0:curr_len]
for idx in range(curr_len):
score = self.board[idx]
logger.info('Scoreboard best %d ==> Epoch [%d][Top1: %.3f Top5: %.3f]',
idx + 1, score['epoch'], score['top1'], score['top5'])
def is_best(self, epoch):
return self.board[0]['epoch'] == epoch