-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathsde_utils.py
594 lines (446 loc) · 20.2 KB
/
sde_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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
import math
import torch
import abc
from tqdm import tqdm
import torchvision.utils as tvutils
import os
from scipy import integrate
class SDE(abc.ABC):
def __init__(self, T, device=None):
self.T = T
self.dt = 1 / T
self.device = device
@abc.abstractmethod
def drift(self, x, t):
pass
@abc.abstractmethod
def dispersion(self, x, t):
pass
@abc.abstractmethod
def sde_reverse_drift(self, x, score, t):
pass
@abc.abstractmethod
def ode_reverse_drift(self, x, score, t):
pass
@abc.abstractmethod
def score_fn(self, x, t):
pass
################################################################################
def forward_step(self, x, t):
return x + self.drift(x, t) + self.dispersion(x, t)
def reverse_sde_step_mean(self, x, score, t):
return x - self.sde_reverse_drift(x, score, t)
def reverse_sde_step(self, x, score, t):
return x - self.sde_reverse_drift(x, score, t) - self.dispersion(x, t)
def reverse_ode_step(self, x, score, t):
return x - self.ode_reverse_drift(x, score, t)
def forward(self, x0, T=-1):
T = self.T if T < 0 else T
x = x0.clone()
for t in tqdm(range(1, T + 1)):
x = self.forward_step(x, t)
return x
def reverse_sde(self, xt, T=-1):
T = self.T if T < 0 else T
x = xt.clone()
for t in tqdm(reversed(range(1, T + 1))):
score = self.score_fn(x, t)
x = self.reverse_sde_step(x, score, t)
return x
def reverse_ode(self, xt, T=-1):
T = self.T if T < 0 else T
x = xt.clone()
for t in tqdm(reversed(range(1, T + 1))):
score = self.score_fn(x, t)
x = self.reverse_ode_step(x, score, t)
return x
#############################################################################
class IRSDE(SDE):
'''
Let timestep t start from 1 to T, state t=0 is never used
'''
def __init__(self, max_sigma, T=100, schedule='cosine', eps=0.01, device=None):
super().__init__(T, device)
self.max_sigma = max_sigma / 255 if max_sigma >= 1 else max_sigma
self._initialize(self.max_sigma, T, schedule, eps)
def _initialize(self, max_sigma, T, schedule, eps=0.01):
def constant_theta_schedule(timesteps, v=1.):
"""
constant schedule
"""
print('constant schedule')
timesteps = timesteps + 1 # T from 1 to 100
return torch.ones(timesteps, dtype=torch.float32)
def linear_theta_schedule(timesteps):
"""
linear schedule
"""
print('linear schedule')
timesteps = timesteps + 1 # T from 1 to 100
scale = 1000 / timesteps
beta_start = scale * 0.0001
beta_end = scale * 0.02
return torch.linspace(beta_start, beta_end, timesteps, dtype=torch.float32)
def cosine_theta_schedule(timesteps, s = 0.008):
"""
cosine schedule
"""
print('cosine schedule')
timesteps = timesteps + 2 # for truncating from 1 to -1
steps = timesteps + 1
x = torch.linspace(0, timesteps, steps, dtype=torch.float32)
alphas_cumprod = torch.cos(((x / timesteps) + s) / (1 + s) * math.pi * 0.5) ** 2
alphas_cumprod = alphas_cumprod / alphas_cumprod[0]
betas = 1 - alphas_cumprod[1:-1]
return betas
def get_thetas_cumsum(thetas):
return torch.cumsum(thetas, dim=0)
def get_sigmas(thetas):
return torch.sqrt(max_sigma**2 * 2 * thetas)
def get_sigma_bars(thetas_cumsum):
return torch.sqrt(max_sigma**2 * (1 - torch.exp(-2 * thetas_cumsum * self.dt)))
if schedule == 'cosine':
thetas = cosine_theta_schedule(T)
elif schedule == 'linear':
thetas = linear_theta_schedule(T)
elif schedule == 'constant':
thetas = constant_theta_schedule(T)
else:
print('Not implemented such schedule yet!!!')
sigmas = get_sigmas(thetas)
thetas_cumsum = get_thetas_cumsum(thetas) - thetas[0] # for that thetas[0] is not 0
self.dt = -1 / thetas_cumsum[-1] * math.log(eps)
sigma_bars = get_sigma_bars(thetas_cumsum)
self.thetas = thetas.to(self.device)
self.sigmas = sigmas.to(self.device)
self.thetas_cumsum = thetas_cumsum.to(self.device)
self.sigma_bars = sigma_bars.to(self.device)
self.mu = 0.
self.model = None
#####################################
# set mu for different cases
def set_mu(self, mu):
self.mu = mu
# set score model for reverse process
def set_model(self, model):
self.model = model
#####################################
def mu_bar(self, x0, t):
return self.mu + (x0 - self.mu) * torch.exp(-self.thetas_cumsum[t] * self.dt)
def sigma_bar(self, t):
return self.sigma_bars[t]
def drift(self, x, t):
return self.thetas[t] * (self.mu - x) * self.dt
def sde_reverse_drift(self, x, score, t):
return (self.thetas[t] * (self.mu - x) - self.sigmas[t]**2 * score) * self.dt
def ode_reverse_drift(self, x, score, t):
return (self.thetas[t] * (self.mu - x) - 0.5 * self.sigmas[t]**2 * score) * self.dt
def dispersion(self, x, t):
return self.sigmas[t] * (torch.randn_like(x) * math.sqrt(self.dt)).to(self.device)
def get_score_from_noise(self, noise, t):
return -noise / self.sigma_bar(t)
def score_fn(self, x, t, **kwargs):
# need to pre-set mu and score_model
noise = self.model(x, self.mu, t, **kwargs)
return self.get_score_from_noise(noise, t)
def noise_fn(self, x, t, **kwargs):
# need to pre-set mu and score_model
return self.model(x, self.mu, t, **kwargs)
# optimum x_{t-1}
def reverse_optimum_step(self, xt, x0, t):
A = torch.exp(-self.thetas[t] * self.dt)
B = torch.exp(-self.thetas_cumsum[t] * self.dt)
C = torch.exp(-self.thetas_cumsum[t-1] * self.dt)
term1 = A * (1 - C**2) / (1 - B**2)
term2 = C * (1 - A**2) / (1 - B**2)
return term1 * (xt - self.mu) + term2 * (x0 - self.mu) + self.mu
def reverse_optimum_std(self, t):
A = torch.exp(-2*self.thetas[t] * self.dt)
B = torch.exp(-2*self.thetas_cumsum[t] * self.dt)
C = torch.exp(-2*self.thetas_cumsum[t-1] * self.dt)
posterior_var = (1 - A) * (1 - C) / (1 - B)
# return torch.sqrt(posterior_var)
min_value = (1e-20 * self.dt).to(self.device)
log_posterior_var = torch.log(torch.clamp(posterior_var, min=min_value))
return (0.5 * log_posterior_var).exp() * self.max_sigma
def reverse_posterior_step(self, xt, noise, t):
x0 = self.get_init_state_from_noise(xt, noise, t)
mean = self.reverse_optimum_step(xt, x0, t)
std = self.reverse_optimum_std(t)
return mean + std * torch.randn_like(xt)
def sigma(self, t):
return self.sigmas[t]
def theta(self, t):
return self.thetas[t]
def get_real_noise(self, xt, x0, t):
return (xt - self.mu_bar(x0, t)) / self.sigma_bar(t)
def get_real_score(self, xt, x0, t):
return -(xt - self.mu_bar(x0, t)) / self.sigma_bar(t)**2
def get_init_state_from_noise(self, xt, noise, t):
A = torch.exp(self.thetas_cumsum[t] * self.dt)
return (xt - self.mu - self.sigma_bar(t) * noise) * A + self.mu
# forward process to get x(T) from x(0)
def forward(self, x0, T=-1, save_dir='forward_state'):
T = self.T if T < 0 else T
x = x0.clone()
for t in tqdm(range(1, T + 1)):
x = self.forward_step(x, t)
os.makedirs(save_dir, exist_ok=True)
tvutils.save_image(x.data, f'{save_dir}/state_{t}.png', normalize=False)
return x
def reverse_sde(self, xt, T=-1, save_states=False, save_dir='sde_state', **kwargs):
T = self.T if T < 0 else T
x = xt.clone()
for t in tqdm(reversed(range(1, T + 1))):
score = self.score_fn(x, t, **kwargs)
x = self.reverse_sde_step(x, score, t)
if save_states: # only consider to save 100 images
interval = self.T // 100
if t % interval == 0:
idx = t // interval
os.makedirs(save_dir, exist_ok=True)
tvutils.save_image(x.data, f'{save_dir}/state_{idx}.png', normalize=False)
return x
def reverse_ode(self, xt, T=-1, save_states=False, save_dir='ode_state', **kwargs):
T = self.T if T < 0 else T
x = xt.clone()
for t in tqdm(reversed(range(1, T + 1))):
score = self.score_fn(x, t, **kwargs)
x = self.reverse_ode_step(x, score, t)
if save_states: # only consider to save 100 images
interval = self.T // 100
if t % interval == 0:
idx = t // interval
os.makedirs(save_dir, exist_ok=True)
tvutils.save_image(x.data, f'{save_dir}/state_{idx}.png', normalize=False)
return x
def reverse_posterior(self, xt, T=-1, save_states=False, save_dir='posterior_state', **kwargs):
T = self.T if T < 0 else T
x = xt.clone()
for t in tqdm(reversed(range(1, T + 1))):
noise = self.noise_fn(x, t, **kwargs)
x = self.reverse_posterior_step(x, noise, t)
if save_states: # only consider to save 100 images
interval = self.T // 100
if t % interval == 0:
idx = t // interval
os.makedirs(save_dir, exist_ok=True)
tvutils.save_image(x.data, f'{save_dir}/state_{idx}.png', normalize=False)
return x
# sample ode using Black-box ODE solver (not used)
def ode_sampler(self, xt, rtol=1e-5, atol=1e-5, method='RK45', eps=1e-3,):
shape = xt.shape
def to_flattened_numpy(x):
"""Flatten a torch tensor `x` and convert it to numpy."""
return x.detach().cpu().numpy().reshape((-1,))
def from_flattened_numpy(x, shape):
"""Form a torch tensor with the given `shape` from a flattened numpy array `x`."""
return torch.from_numpy(x.reshape(shape))
def ode_func(t, x):
t = int(t)
x = from_flattened_numpy(x, shape).to(self.device).type(torch.float32)
score = self.score_fn(x, t)
drift = self.ode_reverse_drift(x, score, t)
return to_flattened_numpy(drift)
# Black-box ODE solver for the probability flow ODE
solution = integrate.solve_ivp(ode_func, (self.T, eps), to_flattened_numpy(xt),
rtol=rtol, atol=atol, method=method)
x = torch.tensor(solution.y[:, -1]).reshape(shape).to(self.device).type(torch.float32)
return x
def optimal_reverse(self, xt, x0, T=-1):
T = self.T if T < 0 else T
x = xt.clone()
for t in tqdm(reversed(range(1, T + 1))):
x = self.reverse_optimum_step(x, x0, t)
return x
################################################################
def weights(self, t):
return torch.exp(-self.thetas_cumsum[t] * self.dt)
# sample states for training
def generate_random_states(self, x0, mu):
x0 = x0.to(self.device)
mu = mu.to(self.device)
self.set_mu(mu)
batch = x0.shape[0]
timesteps = torch.randint(1, self.T + 1, (batch, 1, 1, 1)).long()
state_mean = self.mu_bar(x0, timesteps)
noises = torch.randn_like(state_mean)
noise_level = self.sigma_bar(timesteps)
noisy_states = noises * noise_level + state_mean
return timesteps, noisy_states.to(torch.float32)
def noise_state(self, tensor):
return tensor + torch.randn_like(tensor) * self.max_sigma
################################################################################
################################################################################
############################ Denoising SDE ##################################
################################################################################
################################################################################
class DenoisingSDE(SDE):
'''
Let timestep t start from 1 to T, state t=0 is never used
'''
def __init__(self, max_sigma, T, schedule='cosine', device=None):
super().__init__(T, device)
self.max_sigma = max_sigma / 255 if max_sigma > 1 else max_sigma
self._initialize(self.max_sigma, T, schedule)
def _initialize(self, max_sigma, T, schedule, eps=0.04):
def linear_beta_schedule(timesteps):
timesteps = timesteps + 1
scale = 1000 / timesteps
beta_start = scale * 0.0001
beta_end = scale * 0.02
return torch.linspace(beta_start, beta_end, timesteps, dtype = torch.float32)
def cosine_beta_schedule(timesteps, s = 0.008):
"""
cosine schedule
as proposed in https://openreview.net/forum?id=-NEXDKk8gZ
"""
timesteps = timesteps + 2
steps = timesteps + 1
x = torch.linspace(0, timesteps, steps, dtype = torch.float32)
alphas_cumprod = torch.cos(((x / timesteps) + s) / (1 + s) * math.pi * 0.5) ** 2
alphas_cumprod = alphas_cumprod / alphas_cumprod[0]
# betas = 1 - (alphas_cumprod[1:] / alphas_cumprod[:-1])
betas = 1 - alphas_cumprod[1:-1]
return betas
def get_thetas_cumsum(thetas):
return torch.cumsum(thetas, dim=0)
def get_sigmas(thetas):
return torch.sqrt(max_sigma**2 * 2 * thetas)
def get_sigma_bars(thetas_cumsum):
return torch.sqrt(max_sigma**2 * (1 - torch.exp(-2 * thetas_cumsum * self.dt)))
if schedule == 'cosine':
thetas = cosine_beta_schedule(T)
else:
thetas = linear_beta_schedule(T)
sigmas = get_sigmas(thetas)
thetas_cumsum = get_thetas_cumsum(thetas) - thetas[0]
self.dt = -1 / thetas_cumsum[-1] * math.log(eps)
sigma_bars = get_sigma_bars(thetas_cumsum)
self.thetas = thetas.to(self.device)
self.sigmas = sigmas.to(self.device)
self.thetas_cumsum = thetas_cumsum.to(self.device)
self.sigma_bars = sigma_bars.to(self.device)
self.mu = 0.
self.model = None
# set noise model for reverse process
def set_model(self, model):
self.model = model
def sigma(self, t):
return self.sigmas[t]
def theta(self, t):
return self.thetas[t]
def mu_bar(self, x0, t):
return x0
def sigma_bar(self, t):
return self.sigma_bars[t]
def drift(self, x, x0, t):
return self.thetas[t] * (x0 - x) * self.dt
def sde_reverse_drift(self, x, score, t):
A = torch.exp(-2 * self.thetas_cumsum[t] * self.dt)
return -0.5 * self.sigmas[t]**2 * (1 + A) * score * self.dt
def ode_reverse_drift(self, x, score, t):
A = torch.exp(-2 * self.thetas_cumsum[t] * self.dt)
return -0.5 * self.sigmas[t]**2 * A * score * self.dt
def dispersion(self, x, t):
return self.sigmas[t] * (torch.randn_like(x) * math.sqrt(self.dt)).to(self.device)
def get_score_from_noise(self, noise, t):
return -noise / self.sigma_bar(t)
def get_init_state_from_noise(self, x, noise, t):
return x - self.sigma_bar(t) * noise
def get_init_state_from_score(self, x, score, t):
return x + self.sigma_bar(t)**2 * score
def score_fn(self, x, t):
# need to preset the score_model
noise = self.model(x, t)
return self.get_score_from_noise(noise, t)
############### reverse sampling ################
def get_real_noise(self, xt, x0, t):
return (xt - self.mu_bar(x0, t)) / self.sigma_bar(t)
def get_real_score(self, xt, x0, t):
return -(xt - self.mu_bar(x0, t)) / self.sigma_bar(t)**2
def reverse_sde(self, xt, x0=None, T=-1, save_states=False, save_dir='sde_state'):
T = self.T if T < 0 else T
x = xt.clone()
for t in tqdm(reversed(range(1, T + 1))):
if x0 is not None:
score = self.get_real_score(x, x0, t)
else:
score = self.score_fn(x, t)
x = self.reverse_sde_step(x, score, t)
if save_states:
interval = self.T // 100
if t % interval == 0:
idx = t // interval
os.makedirs(save_dir, exist_ok=True)
tvutils.save_image(x.data, f'{save_dir}/state_{idx}.png', normalize=False)
return x
def reverse_ode(self, xt, x0=None, T=-1, save_states=False, save_dir='ode_state'):
T = self.T if T < 0 else T
x = xt.clone()
for t in tqdm(reversed(range(1, T + 1))):
if x0 is not None:
real_score = self.get_real_score(x, x0, t)
score = self.score_fn(x, t)
x = self.reverse_ode_step(x, score, t)
if save_states:
interval = self.T // 100
if t % interval == 0:
state = x.clone()
if x0 is not None:
state = torch.cat([x, score, real_score], dim=0)
os.makedirs(save_dir, exist_ok=True)
idx = t // interval
tvutils.save_image(state.data, f'{save_dir}/state_{idx}.png', normalize=False)
return x
def ode_sampler(self, xt, rtol=1e-5, atol=1e-5, method='RK45', eps=1e-3,):
shape = xt.shape
def to_flattened_numpy(x):
"""Flatten a torch tensor `x` and convert it to numpy."""
return x.detach().cpu().numpy().reshape((-1,))
def from_flattened_numpy(x, shape):
"""Form a torch tensor with the given `shape` from a flattened numpy array `x`."""
return torch.from_numpy(x.reshape(shape))
def ode_func(t, x):
t = int(t)
x = from_flattened_numpy(x, shape).to(self.device).type(torch.float32)
score = self.score_fn(x, t)
drift = self.ode_reverse_drift(x, score, t)
return to_flattened_numpy(drift)
# Black-box ODE solver for the probability flow ODE
solution = integrate.solve_ivp(ode_func, (self.T, eps), to_flattened_numpy(xt),
rtol=rtol, atol=atol, method=method)
x = torch.tensor(solution.y[:, -1]).reshape(shape).to(self.device).type(torch.float32)
return x
def get_optimal_timestep(self, sigma, eps=1e-6):
sigma = sigma / 255 if sigma > 1 else sigma
thetas_cumsum_hat = -1 / (2 * self.dt) * math.log(1 - sigma**2/self.max_sigma**2 + eps)
T = torch.argmin((self.thetas_cumsum - thetas_cumsum_hat).abs())
return T
##########################################################
########## below functions are used for training #########
##########################################################
def reverse_optimum_step(self, xt, x0, t):
A = torch.exp(-self.thetas[t] * self.dt)
B = torch.exp(-self.thetas_cumsum[t] * self.dt)
C = torch.exp(-self.thetas_cumsum[t-1] * self.dt)
term1 = A * (1 - C**2) / (1 - B**2)
term2 = C * (1 - A**2) / (1 - B**2)
return term1 * (xt - x0) + term2 * (x0 - x0) + x0
def optimal_reverse(self, xt, x0, T=-1):
T = self.T if T < 0 else T
x = xt.clone()
for t in tqdm(reversed(range(1, T + 1))):
x = self.reverse_optimum_step(x, x0, t)
return x
def weights(self, t):
# return 0.1 + torch.exp(-self.thetas_cumsum[t] * self.dt)
return self.sigmas[t]**2
def generate_random_states(self, x0):
x0 = x0.to(self.device)
batch = x0.shape[0]
timesteps = torch.randint(1, self.T + 1, (batch, 1, 1, 1)).long()
noises = torch.randn_like(x0, dtype=torch.float32)
noise_level = self.sigma_bar(timesteps)
noisy_states = noises * noise_level + x0
return timesteps, noisy_states