-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
244 lines (187 loc) · 9.12 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import random
import time
import torch
import torch.nn.functional as F
from torch.nn.modules.batchnorm import _BatchNorm
class SAM(torch.optim.Optimizer):
def __init__(self, params, base_optimizer, rho=0.05, adaptive=False, **kwargs):
assert rho >= 0.0, f"Invalid rho, should be non-negative: {rho}"
defaults = dict(rho=rho, adaptive=adaptive, **kwargs)
super(SAM, self).__init__(params, defaults)
self.base_optimizer = base_optimizer(self.param_groups, **kwargs)
self.param_groups = self.base_optimizer.param_groups
self.defaults.update(self.base_optimizer.defaults)
@torch.no_grad()
def first_step(self, zero_grad=False):
grad_norm = self._grad_norm()
for group in self.param_groups:
scale = group["rho"] / (grad_norm + 1e-12)
for p in group["params"]:
if p.grad is None: continue
self.state[p]["old_p"] = p.data.clone()
e_w = (torch.pow(p, 2) if group["adaptive"] else 1.0) * p.grad * scale.to(p)
p.add_(e_w) # climb to the local maximum "w + e(w)"
if zero_grad: self.zero_grad()
@torch.no_grad()
def second_step(self, zero_grad=False):
for group in self.param_groups:
for p in group["params"]:
if p.grad is None: continue
p.data = self.state[p]["old_p"] # get back to "w" from "w + e(w)"
self.base_optimizer.step() # do the actual "sharpness-aware" update
if zero_grad: self.zero_grad()
@torch.no_grad()
def step(self, closure=None):
assert closure is not None, "Sharpness Aware Minimization requires closure, but it was not provided"
closure = torch.enable_grad()(closure) # the closure should do a full forward-backward pass
self.first_step(zero_grad=True)
closure()
self.second_step()
def _grad_norm(self):
shared_device = self.param_groups[0]["params"][
0].device # put everything on the same device, in case of model parallelism
norm = torch.norm(
torch.stack([
((torch.abs(p) if group["adaptive"] else 1.0) * p.grad).norm(p=2).to(shared_device)
for group in self.param_groups for p in group["params"]
if p.grad is not None
]),
p=2
)
return norm
def load_state_dict(self, state_dict):
super().load_state_dict(state_dict)
self.base_optimizer.param_groups = self.param_groups
class StepLR:
def __init__(self, optimizer, learning_rate: float, total_epochs: int):
self.optimizer = optimizer
self.total_epochs = total_epochs
self.base = learning_rate
def __call__(self, epoch):
if epoch < self.total_epochs * 3 / 10:
lr = self.base
elif epoch < self.total_epochs * 6 / 10:
lr = self.base * 0.2
elif epoch < self.total_epochs * 8 / 10:
lr = self.base * 0.2 ** 2
else:
lr = self.base * 0.2 ** 3
for param_group in self.optimizer.param_groups:
param_group["lr"] = lr
def lr(self) -> float:
return self.optimizer.param_groups[0]["lr"]
class Log:
def __init__(self, log_each: int, initial_epoch=-1):
self.best_accuracy = 0.0
self.log_each = log_each
self.epoch = initial_epoch
def train(self, len_dataset: int) -> None:
self.epoch += 1
if self.epoch == 0:
self._print_header()
else:
self.flush()
self.is_train = True
self.last_steps_state = {"loss": 0.0, "accuracy": 0.0, "steps": 0}
self._reset(len_dataset)
def eval(self, len_dataset: int) -> None:
self.flush()
self.is_train = False
self._reset(len_dataset)
def __call__(self, model, loss, accuracy, learning_rate: float = None) -> None:
if self.is_train:
self._train_step(model, loss, accuracy, learning_rate)
else:
self._eval_step(loss, accuracy)
def flush(self) -> None:
if self.is_train:
loss = self.epoch_state["loss"] / self.epoch_state["steps"]
accuracy = self.epoch_state["accuracy"] / self.epoch_state["steps"]
print(
f"\r┃{self.epoch:12d} ┃{loss:12.4f} │{100 * accuracy:10.2f} % ┃{self.learning_rate:12.3e} │{self._time():>12} ┃",
end="",
flush=True,
)
else:
loss = self.epoch_state["loss"] / self.epoch_state["steps"]
accuracy = self.epoch_state["accuracy"] / self.epoch_state["steps"]
print(f"{loss:12.4f} │{100 * accuracy:10.2f} % ┃", flush=True)
if accuracy > self.best_accuracy:
self.best_accuracy = accuracy
def _train_step(self, model, loss, accuracy, learning_rate: float) -> None:
self.learning_rate = learning_rate
self.last_steps_state["loss"] += loss.sum().item()
self.last_steps_state["accuracy"] += accuracy.sum().item()
self.last_steps_state["steps"] += loss.size(0)
self.epoch_state["loss"] += loss.sum().item()
self.epoch_state["accuracy"] += accuracy.sum().item()
self.epoch_state["steps"] += loss.size(0)
self.step += 1
if self.step % self.log_each == self.log_each - 1:
loss = self.last_steps_state["loss"] / self.last_steps_state["steps"]
accuracy = self.last_steps_state["accuracy"] / self.last_steps_state["steps"]
self.last_steps_state = {"loss": 0.0, "accuracy": 0.0, "steps": 0}
progress = self.step / self.len_dataset
print(
f"\r┃{self.epoch:12d} ┃{loss:12.4f} │{100 * accuracy:10.2f} % ┃{learning_rate:12.3e} │{self._time():>12}",
end="",
flush=True,
)
def _eval_step(self, loss, accuracy) -> None:
self.epoch_state["loss"] += loss.sum().item()
self.epoch_state["accuracy"] += accuracy.sum().item()
self.epoch_state["steps"] += loss.size(0)
def _reset(self, len_dataset: int) -> None:
self.start_time = time.time()
self.step = 0
self.len_dataset = len_dataset
self.epoch_state = {"loss": 0.0, "accuracy": 0.0, "steps": 0}
def _time(self) -> str:
time_seconds = int(time.time() - self.start_time)
return f"{time_seconds // 60:02d}:{time_seconds % 60:02d} min"
def _print_header(self) -> None:
# print(
# f"┏━━━━━━━━━━━━━━┳━━━━━━━╸T╺╸R╺╸A╺╸I╺╸N╺━━━━━━━┳━━━━━━━╸S╺╸T╺╸A╺╸T╺╸S╺━━━━━━━┳━━━━━━━╸V╺╸A╺╸L╺╸I╺╸D╺━━━━━━━┓")
print(
f"┃ epoch ┃ Train loss │ accuracy ┃ l.r. │ elapsed ┃ Valid loss │ accuracy ┃")
print(
f"┠──────────────╂──────────────┼──────────────╂──────────────┼──────────────╂──────────────┼──────────────┨")
class Cutout:
def __init__(self, size=16, p=0.5):
self.size = size
self.half_size = size // 2
self.p = p
def __call__(self, image):
if torch.rand([1]).item() > self.p:
return image
left = torch.randint(-self.half_size, image.size(1) - self.half_size, [1]).item()
top = torch.randint(-self.half_size, image.size(2) - self.half_size, [1]).item()
right = min(image.size(1), left + self.size)
bottom = min(image.size(2), top + self.size)
image[:, max(0, left): right, max(0, top): bottom] = 0
return image
def initialize(seed):
random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.enabled = True
torch.backends.cudnn.benchmark = True
torch.backends.cudnn.deterministic = False
def smooth_crossentropy(pred, gold, smoothing=0.1):
n_class = pred.size(1)
one_hot = torch.full_like(pred, fill_value=smoothing / (n_class - 1))
one_hot.scatter_(dim=1, index=gold.unsqueeze(1), value=1.0 - smoothing)
log_prob = F.log_softmax(pred, dim=1)
return F.kl_div(input=log_prob, target=one_hot, reduction='none').sum(-1)
def disable_running_stats(model):
def _disable(module):
if isinstance(module, _BatchNorm):
module.backup_momentum = module.momentum
module.momentum = 0
model.apply(_disable)
def enable_running_stats(model):
def _enable(module):
if isinstance(module, _BatchNorm) and hasattr(module, "backup_momentum"):
module.momentum = module.backup_momentum
model.apply(_enable)