-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
152 lines (113 loc) · 4.13 KB
/
utils.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
145
146
147
148
149
150
151
152
import os
import torch
def adjust_learning_rate(optimizer, lr):
for param_group in optimizer.param_groups:
param_group['lr'] = lr
return lr
def save_checkpoint(dir, epoch, **kwargs):
state = {
'epoch': epoch,
}
state.update(kwargs)
filepath = os.path.join(dir, 'checkpoint-%d.pt' % epoch)
torch.save(state, filepath)
def train_epoch(loader, model, criterion, optimizer):
loss_sum = 0.0
correct = 0.0
model.train()
for i, (input, target) in enumerate(loader):
input = input.cuda(async=True)
target = target.cuda(async=True)
input_var = torch.autograd.Variable(input)
target_var = torch.autograd.Variable(target)
output = model(input_var)
loss = criterion(output, target_var)
optimizer.zero_grad()
loss.backward()
optimizer.step()
loss_sum += loss.data[0] * input.size(0)
pred = output.data.max(1, keepdim=True)[1]
correct += pred.eq(target_var.data.view_as(pred)).sum().item()
return {
'loss': loss_sum / len(loader.dataset),
'accuracy': correct / len(loader.dataset) * 100.0,
}
def eval(loader, model, criterion):
loss_sum = 0.0
correct = 0.0
model.eval()
for i, (input, target) in enumerate(loader):
input = input.cuda(async=True)
target = target.cuda(async=True)
input_var = torch.autograd.Variable(input)
target_var = torch.autograd.Variable(target)
output = model(input_var)
loss = criterion(output, target_var)
loss_sum += loss.data[0] * input.size(0)
pred = output.data.max(1, keepdim=True)[1]
correct += pred.eq(target_var.data.view_as(pred)).sum().item()
return {
'loss': loss_sum / len(loader.dataset),
'accuracy': correct / len(loader.dataset) * 100.0,
}
def moving_average(net1, net2, alpha=1):
for param1, param2 in zip(net1.parameters(), net2.parameters()):
param1.data *= (1.0 - alpha)
param1.data += param2.data * alpha
# def moving_average(net1, net2, alpha=1):
# for (name1, param1), (name2, param2) in zip(net1.named_parameters(), net2.named_parameters()):
# if 'mweight' in name1:
# param1.data = param2.data
# else:
# param1.data *= (1.0 - alpha)
# param1.data += param2.data * alpha
def _check_bn(module, flag):
if issubclass(module.__class__, torch.nn.modules.batchnorm._BatchNorm):
flag[0] = True
def check_bn(model):
flag = [False]
model.apply(lambda module: _check_bn(module, flag))
return flag[0]
def reset_bn(module):
if issubclass(module.__class__, torch.nn.modules.batchnorm._BatchNorm):
module.running_mean = torch.zeros_like(module.running_mean)
module.running_var = torch.ones_like(module.running_var)
def _get_momenta(module, momenta):
if issubclass(module.__class__, torch.nn.modules.batchnorm._BatchNorm):
momenta[module] = module.momentum
def _set_momenta(module, momenta):
if issubclass(module.__class__, torch.nn.modules.batchnorm._BatchNorm):
module.momentum = momenta[module]
def bn_update(loader, model, pct=None):
"""
BatchNorm buffers update (if any).
Performs 1 epochs to estimate buffers average using train dataset.
:param loader: train dataset loader for buffers average estimation.
:param model: model being update
:return: None
"""
if not check_bn(model):
return
model.train()
momenta = {}
model.apply(reset_bn)
model.apply(lambda module: _get_momenta(module, momenta))
n = 0
i = 0
if pct:
assert 0 <pct <= 1
until = int(pct*len(loader))
for input, _ in loader:
if pct:
if i == until:
break
i += 1
input = input.cuda(async=True)
input_var = torch.autograd.Variable(input)
b = input_var.data.size(0)
momentum = b / (n + b)
for module in momenta.keys():
module.momentum = momentum
model(input_var)
n += b
model.apply(lambda module: _set_momenta(module, momenta))