-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
362 lines (302 loc) · 16.4 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import torch
import torch.nn as nn
import torch.nn.functional as F
import math
import functools
from collections.abc import Iterable
from math import (log, cos, pi, floor)
from torch.optim.lr_scheduler import _LRScheduler
from torch.optim.lr_scheduler import (StepLR, ExponentialLR, ReduceLROnPlateau)
class CyclicCosineDecayLR(_LRScheduler):
def __init__(self,
optimizer,
init_decay_epochs,
min_decay_lr,
restart_interval=None,
restart_interval_multiplier=None,
restart_lr=None,
warmup_epochs=None,
warmup_start_lr=None,
last_epoch=-1,
verbose=False):
"""
Initialize new CyclicCosineDecayLR object.
:param optimizer: (Optimizer) - Wrapped optimizer.
:param init_decay_epochs: (int) - Number of initial decay epochs.
:param min_decay_lr: (float or iterable of floats) - Learning rate at the end of decay.
:param restart_interval: (int) - Restart interval for fixed cycles.
Set to None to disable cycles. Default: None.
:param restart_interval_multiplier: (float) - Multiplication coefficient for geometrically increasing cycles.
Default: None.
:param restart_lr: (float or iterable of floats) - Learning rate when cycle restarts.
If None, optimizer's learning rate will be used. Default: None.
:param warmup_epochs: (int) - Number of warmup epochs. Set to None to disable warmup. Default: None.
:param warmup_start_lr: (float or iterable of floats) - Learning rate at the beginning of warmup.
Must be set if warmup_epochs is not None. Default: None.
:param last_epoch: (int) - The index of the last epoch. This parameter is used when resuming a training job. Default: -1.
:param verbose: (bool) - If True, prints a message to stdout for each update. Default: False.
"""
if not isinstance(init_decay_epochs, int) or init_decay_epochs < 1:
raise ValueError("init_decay_epochs must be positive integer, got {} instead".format(init_decay_epochs))
if isinstance(min_decay_lr, Iterable) and len(min_decay_lr) != len(optimizer.param_groups):
raise ValueError("Expected len(min_decay_lr) to be equal to len(optimizer.param_groups), "
"got {} and {} instead".format(len(min_decay_lr), len(optimizer.param_groups)))
if restart_interval is not None and (not isinstance(restart_interval, int) or restart_interval < 1):
raise ValueError("restart_interval must be positive integer, got {} instead".format(restart_interval))
if restart_interval_multiplier is not None and \
(not isinstance(restart_interval_multiplier, float) or restart_interval_multiplier <= 0):
raise ValueError("restart_interval_multiplier must be positive float, got {} instead".format(
restart_interval_multiplier))
if isinstance(restart_lr, Iterable) and len(restart_lr) != len(optimizer.param_groups):
raise ValueError("Expected len(restart_lr) to be equal to len(optimizer.param_groups), "
"got {} and {} instead".format(len(restart_lr), len(optimizer.param_groups)))
if warmup_epochs is not None:
if not isinstance(warmup_epochs, int) or warmup_epochs < 1:
raise ValueError(
"Expected warmup_epochs to be positive integer, got {} instead".format(type(warmup_epochs)))
if warmup_start_lr is None:
raise ValueError("warmup_start_lr must be set when warmup_epochs is not None")
if not (isinstance(warmup_start_lr, float) or isinstance(warmup_start_lr, Iterable)):
raise ValueError("warmup_start_lr must be either float or iterable of floats, got {} instead".format(
warmup_start_lr))
if isinstance(warmup_start_lr, Iterable) and len(warmup_start_lr) != len(optimizer.param_groups):
raise ValueError("Expected len(warmup_start_lr) to be equal to len(optimizer.param_groups), "
"got {} and {} instead".format(len(warmup_start_lr), len(optimizer.param_groups)))
group_num = len(optimizer.param_groups)
self._warmup_start_lr = [warmup_start_lr] * group_num if isinstance(warmup_start_lr, float) else warmup_start_lr
self._warmup_epochs = 0 if warmup_epochs is None else warmup_epochs
self._init_decay_epochs = init_decay_epochs
self._min_decay_lr = [min_decay_lr] * group_num if isinstance(min_decay_lr, float) else min_decay_lr
self._restart_lr = [restart_lr] * group_num if isinstance(restart_lr, float) else restart_lr
self._restart_interval = restart_interval
self._restart_interval_multiplier = restart_interval_multiplier
super(CyclicCosineDecayLR, self).__init__(optimizer, last_epoch, verbose=verbose)
def get_lr(self):
if self._warmup_epochs > 0 and self.last_epoch < self._warmup_epochs:
return self._calc(self.last_epoch,
self._warmup_epochs,
self._warmup_start_lr,
self.base_lrs)
elif self.last_epoch < self._init_decay_epochs + self._warmup_epochs:
return self._calc(self.last_epoch - self._warmup_epochs,
self._init_decay_epochs,
self.base_lrs,
self._min_decay_lr)
else:
if self._restart_interval is not None:
if self._restart_interval_multiplier is None:
cycle_epoch = (self.last_epoch - self._init_decay_epochs - self._warmup_epochs) % self._restart_interval
lrs = self.base_lrs if self._restart_lr is None else self._restart_lr
return self._calc(cycle_epoch,
self._restart_interval,
lrs,
self._min_decay_lr)
else:
n = self._get_n(self.last_epoch - self._warmup_epochs - self._init_decay_epochs)
sn_prev = self._partial_sum(n)
cycle_epoch = self.last_epoch - sn_prev - self._warmup_epochs - self._init_decay_epochs
interval = self._restart_interval * self._restart_interval_multiplier ** n
lrs = self.base_lrs if self._restart_lr is None else self._restart_lr
return self._calc(cycle_epoch,
interval,
lrs,
self._min_decay_lr)
else:
return self._min_decay_lr
def _calc(self, t, T, lrs, min_lrs):
return [min_lr + (lr - min_lr) * ((1 + cos(pi * t / T)) / 2)
for lr, min_lr in zip(lrs, min_lrs)]
def _get_n(self, epoch):
_t = 1 - (1 - self._restart_interval_multiplier) * epoch / self._restart_interval
return floor(log(_t, self._restart_interval_multiplier))
def _partial_sum(self, n):
return self._restart_interval * (1 - self._restart_interval_multiplier ** n) / (
1 - self._restart_interval_multiplier)
class SquareRootScheduler:
def __init__(self, base_lr):
self.lr = base_lr
def __call__(self, num_update):
return self.lr * pow(num_update + 1.0, -0.5)
class CosineScheduler:
def __init__(self, max_update, base_lr, final_lr, warmup_steps, warmup_begin_lr):
self.base_lr_orig = base_lr
self.max_update = max_update
self.final_lr = final_lr
self.warmup_steps = warmup_steps
self.warmup_begin_lr = warmup_begin_lr
self.max_steps = self.max_update - self.warmup_steps
def get_warmup_lr(self, epoch):
increase = (self.base_lr_orig - self.warmup_begin_lr) * float(epoch) / float(self.warmup_steps)
return self.warmup_begin_lr + increase
def __call__(self, epoch):
if epoch < self.warmup_steps:
return self.get_warmup_lr(epoch)
if epoch <= self.max_update:
self.base_lr = self.final_lr + (self.base_lr_orig - self.final_lr) * (1 + math.cos(math.pi * (epoch - self.warmup_steps) / self.max_steps)) / 2
return self.base_lr
def get_optimizer(params, cfg):
"""
Set optimizer.
Args:
params: model trainable parameters
cfg: Optimization configuration
Returns:
optimizer [torch.optim]
"""
if cfg.optim_alg == "Adam":
optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad, params), lr=cfg.optim_lr)
elif cfg.optim_alg == "AdamL2":
optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad, params), lr=cfg.optim_lr, weight_decay=cfg.optim_wd)
elif cfg.optim_alg == "AdamW":
optimizer = torch.optim.AdamW(filter(lambda p: p.requires_grad, params), lr=cfg.optim_lr, weight_decay=cfg.optim_wd)
return optimizer
def get_scheduler(optimizer, cfg):
"""get learning scheduler.
Args:
optimizer [torch.optim]
cfg: Scheduler configuration.
Returns:
scheduler [torch.optim]
"""
if cfg.name == "StepLR":
params = cfg.StepLR
scheduler = StepLR(optimizer, step_size=params.step_size, gamma=params.gamma)
elif cfg.name == "ExponentialLR":
params = cfg.ExponentialLR
scheduler = ExponentialLR(optimizer, gamma=params.gamma)
elif cfg.name == "ReduceLROnPlateau":
params = cfg.ReduceLROnPlateau
scheduler = ReduceLROnPlateau(optimizer, mode='min', factor=params.factor, patience=params.patience, verbose=True, threshold=0.0001, threshold_mode='rel', cooldown=0, min_lr=0, eps=1e-8)
elif cfg.name == "CyclicCosineDecayLR":
params = cfg.CyclicCosineDecayLR
scheduler = CyclicCosineDecayLR(optimizer,
init_decay_epochs=params.init_decay_epochs,
min_decay_lr=params.min_decay_lr,
restart_interval = params.restart_interval,
restart_lr=params.restart_lr,
warmup_epochs=params.warmup_epochs,
warmup_start_lr=params.warmup_start_lr)
return scheduler
class FocalFrequencyLoss(nn.Module):
"""The torch.nn.Module class that implements focal frequency loss - a
frequency domain loss function for optimizing generative models.
Ref:
Focal Frequency Loss for Image Reconstruction and Synthesis. In ICCV 2021.
<https://arxiv.org/pdf/2012.12821.pdf>
Args:
loss_weight (float): weight for focal frequency loss. Default: 1.0
alpha (float): the scaling factor alpha of the spectrum weight matrix for flexibility. Default: 1.0
patch_factor (int): the factor to crop image patches for patch-based focal frequency loss. Default: 1
ave_spectrum (bool): whether to use minibatch average spectrum. Default: False
log_matrix (bool): whether to adjust the spectrum weight matrix by logarithm. Default: False
batch_matrix (bool): whether to calculate the spectrum weight matrix using batch-based statistics. Default: False
"""
def __init__(self, loss_weight=1.0, alpha=1.0, patch_factor=1, ave_spectrum=False, log_matrix=False, batch_matrix=False):
super(FocalFrequencyLoss, self).__init__()
self.loss_weight = loss_weight
self.alpha = alpha
self.patch_factor = patch_factor
self.ave_spectrum = ave_spectrum
self.log_matrix = log_matrix
self.batch_matrix = batch_matrix
def tensor2freq(self, x):
# crop image patches
patch_factor = self.patch_factor
_, _, h, w = x.shape
assert h % patch_factor == 0 and w % patch_factor == 0, (
'Patch factor should be divisible by image height and width')
patch_list = []
patch_h = h // patch_factor
patch_w = w // patch_factor
for i in range(patch_factor):
for j in range(patch_factor):
patch_list.append(x[:, :, i * patch_h:(i + 1) * patch_h, j * patch_w:(j + 1) * patch_w])
# stack to patch tensor
y = torch.stack(patch_list, 1)
# perform 2D DFT (real-to-complex, orthonormalization)
freq = torch.fft.fft2(y, norm='ortho')
freq = torch.stack([freq.real, freq.imag], -1)
return freq
def loss_formulation(self, recon_freq, real_freq, matrix=None):
# spectrum weight matrix
if matrix is not None:
# if the matrix is predefined
weight_matrix = matrix.detach()
else:
# if the matrix is calculated online: continuous, dynamic, based on current Euclidean distance
matrix_tmp = (recon_freq - real_freq) ** 2
matrix_tmp = torch.sqrt(matrix_tmp[..., 0] + matrix_tmp[..., 1]) ** self.alpha
# whether to adjust the spectrum weight matrix by logarithm
if self.log_matrix:
matrix_tmp = torch.log(matrix_tmp + 1.0)
# whether to calculate the spectrum weight matrix using batch-based statistics
if self.batch_matrix:
matrix_tmp = matrix_tmp / matrix_tmp.max()
else:
matrix_tmp = matrix_tmp / matrix_tmp.max(-1).values.max(-1).values[:, :, :, None, None]
matrix_tmp[torch.isnan(matrix_tmp)] = 0.0
matrix_tmp = torch.clamp(matrix_tmp, min=0.0, max=1.0)
weight_matrix = matrix_tmp.clone().detach()
assert weight_matrix.min().item() >= 0 and weight_matrix.max().item() <= 1, (
'The values of spectrum weight matrix should be in the range [0, 1], '
'but got Min: %.10f Max: %.10f' % (weight_matrix.min().item(), weight_matrix.max().item()))
# frequency distance using (squared) Euclidean distance
tmp = (recon_freq - real_freq) ** 2
freq_distance = tmp[..., 0] + tmp[..., 1]
# dynamic spectrum weighting (Hadamard product)
loss = weight_matrix * freq_distance
return torch.mean(loss)
def forward(self, pred, target, matrix=None, **kwargs):
"""Forward function to calculate focal frequency loss.
Args:
pred (torch.Tensor): of shape (N, C, H, W). Predicted tensor.
target (torch.Tensor): of shape (N, C, H, W). Target tensor.
matrix (torch.Tensor, optional): Element-wise spectrum weight matrix.
Default: None (If set to None: calculated online, dynamic).
"""
pred_freq = self.tensor2freq(pred)
target_freq = self.tensor2freq(target)
# whether to use minibatch average spectrum
if self.ave_spectrum:
pred_freq = torch.mean(pred_freq, 0, keepdim=True)
target_freq = torch.mean(target_freq, 0, keepdim=True)
# calculate focal frequency loss
return self.loss_formulation(pred_freq, target_freq, matrix) * self.loss_weight
class focal_pixel_learning(torch.nn.Module):
def __init__(self):
super().__init__()
self.alpha_sp, self.gamma_sp = 1, 0.5
self.alpha_lp, self.gamma_lp = 1, 1
self.upscale_func = functools.partial(F.interpolate, mode='bicubic', align_corners=False)
self.weig_func = lambda x, y, z: torch.exp((x-x.min()) / (x.max()-x.min()) * y) * z
def forward(self, x, hr, lr):
f_BI_x = self.upscale_func(lr, size=hr.size()[2:])
y_sp = torch.abs(hr - f_BI_x)
w_y_sp = self.weig_func(y_sp, self.alpha_sp, self.gamma_sp).detach()
y_lp = torch.abs(hr - f_BI_x - x)
w_y_lp = self.weig_func(y_lp, self.alpha_lp, self.gamma_lp).detach()
y_hat = hr - f_BI_x
loss = torch.mean(w_y_sp * w_y_lp * torch.abs(x - y_hat))
return loss
def get_loss(loss):
"""
Set loss.
Args:
loss: string.
Returns:
Loss function will be use for modeling.
"""
if loss == "MSELoss":
criterion = torch.nn.MSELoss(reduction="sum")
elif loss == "L1Loss":
criterion = torch.nn.L1Loss(reduction="sum")
elif loss == "FocalLoss":
criterion = FocalFrequencyLoss()
elif loss == "FPLoss":
criterion = focal_pixel_learning()
return criterion
def toNumpy(tensor):
"""
Converts Pytorch tensor to numpy array
"""
return tensor.detach().cpu().numpy()