-
Notifications
You must be signed in to change notification settings - Fork 3
/
sampling.py
373 lines (277 loc) · 12.5 KB
/
sampling.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
"""
Library with the samplers and functions related to the sampling processes both forward and backwards
"""
import abc
import torch
import numpy as np
from tqdm import tqdm
from models import utils as mutils
_SAMPLERS = {}
def register_sampler(cls=None, *, name=None):
"""A decorator for registering sampler classes."""
def _register(cls):
if name is None:
local_name = cls.__name__
else:
local_name = name
if local_name in _SAMPLERS:
raise ValueError(f'Already registered model with name: {local_name}')
_SAMPLERS[local_name] = cls
return cls
if cls is None:
return _register
else:
return _register(cls)
def get_sampler(name):
return _SAMPLERS[name]
def progressive_encoding(x, diffusion, config, n_samples=15):
""" Runs a forward process iteratively
We assume t=0 is already a one-step perturbed image
Args:
x: batch tensor NxCXHxW
diffusion: an object from the DiffusionProcess class
config: the config settings as set in the ml-collections file
n_samples: a scalar value representing the number of samples equally distributed over the trajectory
returns:
n_samples at times T//15
"""
xs = []
# Initial sample - sampling from given state
x = x.to(config.device)
# equally subsample noisy states
indx = np.linspace(0, diffusion.T - 1, n_samples, dtype=int)
with torch.no_grad():
# time partition [0,T]
timesteps = torch.arange(0, diffusion.T, device=config.device)
for t in timesteps:
t_vec = torch.ones(x.shape[0], dtype=torch.int64, device=t.device) * t
x, noise = diffusion.forward_step(x, t_vec)
if t.item() in indx:
xs.append(x)
xs = torch.stack(xs, dim=1)
return xs
class Sampler(abc.ABC):
"""The abstract class for a predictor algorithm."""
def __init__(self, diffusion, model_fn):
super().__init__()
self.diffusion = diffusion
self.model_fn = model_fn
@abc.abstractmethod
def update_fn(self, x, t):
"""One update of the sampler.
Args:
x: A PyTorch tensor representing the current state
t: A PyTorch tensor representing the current time step.
Returns:
x: A PyTorch tensor of the next state.
x_mean: A PyTorch tensor. The next state without random noise. Useful for denoising.
"""
pass
@register_sampler(name='ancestral_sampling')
class AncestralSampling(Sampler):
"""The ancestral sampler used in the DDPM paper"""
def __init__(self, diffusion, model_fn):
super().__init__(diffusion, model_fn)
def denoise_update_fn(self, x, t):
diffusion = self.diffusion
beta = diffusion.discrete_betas.to(t.device)[t.long()]
std = diffusion.sqrt_1m_alphas_cumprod.to(t.device)[t.long()]
predicted_noise = self.model_fn(x, t) # set the model either for training or evaluation
score = - predicted_noise / std[:, None, None, None]
x_mean = (x + beta[:, None, None, None] * score) / torch.sqrt(1. - beta)[:, None, None, None]
noise = torch.randn_like(x)
x = x_mean + torch.sqrt(beta)[:, None, None, None] * noise
return x, x_mean
def update_fn(self, x, t):
return self.denoise_update_fn(x, t)
def sampling_fn(config, diffusion, model, shape, inverse_scaler, T=None, denoise=True):
"""If T is given then starts from that diffusion time step"""
model_fn = mutils.get_model_fn(model, train=False) # get noise predictor model in evaluation mode
sampler_method = get_sampler(config.sampling.sampler.lower())
sampler = sampler_method(diffusion, model_fn)
if T is None:
T = diffusion.T
with torch.no_grad():
# Initial sample - sampling from tractable prior
x = diffusion.prior_sampling(shape).to(config.device)
# reverse time partition [T, 0]
timesteps = torch.flip(torch.arange(0, T, device=config.device), dims=(0,))
for timestep in tqdm(timesteps):
t = torch.ones(shape[0], device=config.device) * timestep
x, x_mean = sampler.denoise_update_fn(x, t)
return inverse_scaler(x_mean if denoise else x)
def progressive_generation(config, diffusion, model, shape, inverse_scaler, n_samples=12, denoise=True):
model_fn = mutils.get_model_fn(model, train=False) # get noise predictor model in evaluation mode
sampler_method = get_sampler(config.sampling.sampler.lower())
sampler = sampler_method(diffusion, model_fn)
with torch.no_grad():
# sample from the prior distribution
x = diffusion.prior_sampling(shape).to(config.device)
# reverse time partition [T, 0]
timesteps =torch.flip(torch.arange(0, diffusion.T, device=config.device), dims=(0,))
# equally subsample noisy states
indx = np.linspace(0, diffusion.T - 1, n_samples, dtype=int)
# create array of shape n_samples
xs = torch.zeros((n_samples, *x.shape))
i=0
for timestep in tqdm(timesteps):
t = torch.ones(shape[0], device=config.device) * timestep
x, x_mean = sampler.denoise_update_fn(x, t)
if timestep.item() in indx:
if timestep.item()==999:
xs[i] = x_mean if denoise else x
else:
xs[i] = x
i+=1
return inverse_scaler(xs), indx
######################### Fast Samplers ########################################
class FastSampler(abc.ABC):
"""The abstract class for a Fast Sampler algorithm."""
def __init__(self, diffusion, model_fn):
super().__init__()
self.diffusion = diffusion
self.model_fn = model_fn
@abc.abstractmethod
def update_fn(self, x, t, t_next):
"""One update of the sampler.
Args:
x: A PyTorch tensor representing the current state
t: A PyTorch tensor representing the current time step.
t_next: A PyTorch tensor representing the next time step.
Returns:
x: A PyTorch tensor of the next state.
x_mean: A PyTorch tensor. The next state without random noise. Useful for denoising.
"""
pass
@register_sampler(name='ddim')
class DDIM(FastSampler):
"""The DDIM sampler"""
def __init__(self, diffusion, model_fn):
super().__init__(diffusion, model_fn)
def update_fn(self, x, t, t_next, eta=0):
# NOTE: We are producing each predicted x0, not x_{t-1} at timestep t.
at = self.diffusion.alphas_cumprod[t.long()].to(x.device)[:, None, None, None]
at_next = self.diffusion.alphas_cumprod[t_next.long()].to(x.device)[:, None, None, None]
# noise estimation
et = self.model_fn(x, t.float())
# predicts x_0 by direct substitution
x0_t = (x - et * (1 - at).sqrt()) / at.sqrt()
# noise controlling the Markovia/Non-Markovian property
sigma_t = eta * ((1 - at / at_next) * (1 - at_next) / (1 - at)).sqrt()
# update using forward posterior q(x_t-1|x_t, x0_t)
x = at_next.sqrt() * x0_t + (1 - at_next- sigma_t**2).sqrt() * et + sigma_t * torch.randn_like(x)
return x, x0_t
################################################ Utils for PSDM sampler #########################################
def transfer(x, t, t_next, et, alphas_cump):
at = alphas_cump[t.long() + 1].view(-1, 1, 1, 1)
at_next = alphas_cump[t_next.long() + 1].view(-1, 1, 1, 1)
x_delta = (at_next - at) * ((1 / (at.sqrt() * (at.sqrt() + at_next.sqrt()))) * x - \
1 / (at.sqrt() * (((1 - at_next) * at).sqrt() + ((1 - at) * at_next).sqrt())) * et)
x_next = x + x_delta
return x_next
def runge_kutta(x, t_list, model, alphas_cump, ets):
"""
"""
e_1 = model(x, t_list[0])
ets.append(e_1)
x_2 = transfer(x, t_list[0], t_list[1], e_1, alphas_cump)
e_2 = model(x_2, t_list[1])
x_3 = transfer(x, t_list[0], t_list[1], e_2, alphas_cump)
e_3 = model(x_3, t_list[1])
x_4 = transfer(x, t_list[0], t_list[2], e_3, alphas_cump)
e_4 = model(x_4, t_list[2])
et = (1 / 6) * (e_1 + 2 * e_2 + 2 * e_3 + e_4)
return et
def gen_order_4(img, t, t_next, model, alphas_cump, ets):
t_list = [t, (t+t_next)/2, t_next]
if len(ets) > 2:
noise_ = model(img, t)
ets.append(noise_)
noise = (1 / 24) * (55 * ets[-1] - 59 * ets[-2] + 37 * ets[-3] - 9 * ets[-4])
else:
noise = runge_kutta(img, t_list, model, alphas_cump, ets)
img_next = transfer(img, t, t_next, noise, alphas_cump)
return img_next
@register_sampler(name='psdm')
class PSDM(FastSampler):
"""The PSDM sampler"""
def __init__(self, diffusion, model_fn):
super().__init__(diffusion, model_fn)
def update_fn(self, x, t, t_next, eta=0):
alphas_cump = self.diffusion.alphas_cumprod.to(x.device)
# update
x = gen_order_4(x, t, t_next, self.model_fn, alphas_cump, ets)
# update using forward posterior q(x_t-1|x_t, x0_t)
# x = at_next.sqrt() * x0_t + (1 - at_next- sigma_t**2).sqrt() * et + sigma_t * torch.randn_like(x)
#TODO TO BE UPDATED
return 0
def get_fast_sampler(config, diffusion, model, inverse_scaler, sampler_name="ddim"):
""" Creates a fast sampling function
Note that DDPM fast sampler runs DDIM with eta=1.0
"""
model_fn = mutils.get_model_fn(model, train=False) # get noise predictor model in evaluation mode
# sampler_method = get_sampler(config.sampling.sampler.lower())
if sampler_name == "ddpm":
sampler_method = get_sampler("ddim")
else:
sampler_method = get_sampler(sampler_name)
sampler = sampler_method(diffusion, model_fn)
def ddim_sampling_fn(x, seq, seq_next, denoise=True):
with torch.no_grad():
# reverse time partition [T, 0]
for i, j in tqdm(zip(reversed(seq), reversed(seq_next)), total=len(seq)):
t = (torch.ones(x.shape[0]) * i)
t_next = (torch.ones(x.shape[0]) * j)
x, x_mean = sampler.update_fn(x, t, t_next)
return inverse_scaler(x_mean if denoise else x)
def ddpm_sampling_fn(x, seq, seq_next, denoise=True):
with torch.no_grad():
# reverse time partition [T, 0]
for i, j in tqdm(zip(reversed(seq), reversed(seq_next)), total=len(seq)):
t = (torch.ones(x.shape[0]) * i)
t_next = (torch.ones(x.shape[0]) * j)
x, x_mean = sampler.update_fn(x, t, t_next, eta=1.0)
return inverse_scaler(x_mean if denoise else x)
def pndm_sampling_fn(x, seq, seq_next, denoise=True):
start = True
alphas_cump = diffusion.alphas_cumprod.to(config.device)
ets = []
with torch.no_grad():
# reverse time partition [T, 0]
for i, j in tqdm(zip(reversed(seq), reversed(seq_next)), total=len(seq)):
t = (torch.ones(x.shape[0]) * i)
t_next = (torch.ones(x.shape[0]) * j)
x = gen_order_4(x, t, t_next, model_fn, alphas_cump, ets)
return inverse_scaler(x if denoise else x)
if sampler_name == "ddim":
return ddim_sampling_fn
elif sampler_name == "ddpm":
return ddpm_sampling_fn
elif sampler_name == "psdm":
return pndm_sampling_fn
else:
raise ValueError(f"Sampler name {sampler_name} unknown.")
def get_deterministic_trajectories(config, diffusion, model):
"""Run DDIM sampler"""
model_fn = mutils.get_model_fn(model, train=False) # get noise predictor model in evaluation mode
sampler_method = DDIM
sampler = sampler_method(diffusion, model_fn)
def ddim_sampling_fn(x, seq, seq_next, denoise=True):
trajectories=torch.zeros((len(seq)+1,*x.shape))
# set initial sample
trajectories[0,:,:,:,:] = x.clone()
n=1
with torch.no_grad():
# reverse time partition [T, 0]
for i, j in tqdm(zip(reversed(seq), reversed(seq_next)), total=len(seq)):
t = (torch.ones(x.shape[0]) * i)
t_next = (torch.ones(x.shape[0]) * j)
x, x_mean = sampler.update_fn(x, t, t_next)
if n==len(seq):
trajectories[n,:,:,:,:] = x_mean.clone()
else:
trajectories[n,:,:,:,:] = x.clone()
n+=1
# return inverse_scaler(x_mean if denoise else x)
return x_mean, trajectories
return ddim_sampling_fn