-
Notifications
You must be signed in to change notification settings - Fork 2
/
helper.py
140 lines (99 loc) · 3.69 KB
/
helper.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
import time
import pickle
import logging
import torch
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def accuracy(output, target, topk=(1, )):
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(model, train_loader, criterion, optimizer, epoch_log):
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
model.train()
end = time.time()
train_iter = len(train_loader)
for i, (input, target) in enumerate(train_loader):
data_time.update(time.time() - end)
input = input.cuda(async=True)
target = target.cuda(async=True)
input_var = torch.autograd.Variable(input, volatile=True)
target_var = torch.autograd.Variable(target, volatile=True)
output = model(input_var)
loss = criterion(output, target_var)
prec1, prec5 = accuracy(output.data, target, topk=(1, 5))
losses.update(loss.data, input.size(0))
top1.update(prec1[0], input.size(0))
top5.update(prec5[0], input.size(0))
optimizer.zero_grad()
loss.backward()
optimizer.step()
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
print(f'{epoch_log} \n'
f'Iter: [{i}/{train_iter}] \n'
f'Time {batch_time.val:.3f} ({batch_time.avg:.3f}) \n'
f'Data {data_time.val:.3f} ({data_time.avg:.3f}) \n'
f'Loss {losses.val:.4f} ({losses.avg:.4f}) \n'
f'Prec@1 {top1.val:.3f} ({top1.avg:.3f}) \n'
f'Prec@5 {top5.val:.3f} ({top5.avg:.3f}) \n')
def valid(model, valid_loader, criterion):
batch_time = AverageMeter()
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
model.eval()
end = time.time()
valid_iter = len(valid_loader)
for i, (input, target) in enumerate(valid_loader):
input = input.cuda(async=True)
target = target.cuda(async=True)
input_var = torch.autograd.Variable(input, volatile=True)
target_var = torch.autograd.Variable(target, volatile=True)
output = model(input_var)
loss = criterion(output, target_var)
prec1, prec5 = accuracy(output.data, target, topk=(1, 5))
losses.update(loss.data, input.size(0))
top1.update(prec1[0], input.size(0))
top5.update(prec5[0], input.size(0))
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
print(f'Iter: [{i}/{valid_iter}]\n'
f'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\n'
f'Loss {losses.val:.4f} ({losses.avg:.4f})\n'
f'Prec@1 {top1.val:.3f} ({top1.avg:.3f})\n'
f'Prec@5 {top5.val:.3f} ({top5.avg:.3f})\n')
print(f' * Prec@1 {top1.avg:.3f} Prec@5 {top5.avg:.3f} \n')
return top1.avg, top5.avg
def save_pkl(data, path):
with open(path, 'wb') as f:
pickle.dump(data, f)
def load_pkl(path):
with open(path, 'rb') as f:
data = pickle.load(f)
return data