Utilities for training and sampling diffusion models. Ported directly from here, and then adapted over time to further experimentation. https://github.com/hojonathanho/diffusion/blob/1e0dceb3b3495bbe19116a5e1b3596cd0706c543/diffusion_tf/diffusion_utils_2.py#L42 :param betas: a 1-D
| 310 | |
| 311 | |
| 312 | class GaussianDiffusion: |
| 313 | """ |
| 314 | Utilities for training and sampling diffusion models. |
| 315 | |
| 316 | Ported directly from here, and then adapted over time to further experimentation. |
| 317 | https://github.com/hojonathanho/diffusion/blob/1e0dceb3b3495bbe19116a5e1b3596cd0706c543/diffusion_tf/diffusion_utils_2.py#L42 |
| 318 | |
| 319 | :param betas: a 1-D numpy array of betas for each diffusion timestep, |
| 320 | starting at T and going to 1. |
| 321 | :param model_mean_type: a ModelMeanType determining what the model outputs. |
| 322 | :param model_var_type: a ModelVarType determining how variance is output. |
| 323 | :param loss_type: a LossType determining the loss function to use. |
| 324 | :param rescale_timesteps: if True, pass floating point timesteps into the |
| 325 | model so that they are always scaled like in the |
| 326 | original paper (0 to 1000). |
| 327 | """ |
| 328 | |
| 329 | def __init__( |
| 330 | self, |
| 331 | *, |
| 332 | betas, |
| 333 | model_mean_type, |
| 334 | model_var_type, |
| 335 | loss_type, |
| 336 | rescale_timesteps=False, |
| 337 | ): |
| 338 | self.model_mean_type = model_mean_type |
| 339 | self.model_var_type = model_var_type |
| 340 | self.loss_type = loss_type |
| 341 | self.rescale_timesteps = rescale_timesteps |
| 342 | |
| 343 | # Use float64 for accuracy. |
| 344 | betas = np.array(betas, dtype=np.float64) |
| 345 | self.betas = betas |
| 346 | assert len(betas.shape) == 1, "betas must be 1-D" |
| 347 | assert (betas > 0).all() and (betas <= 1).all() |
| 348 | |
| 349 | self.num_timesteps = int(betas.shape[0]) |
| 350 | |
| 351 | alphas = 1.0 - betas |
| 352 | self.alphas_cumprod = np.cumprod(alphas, axis=0) |
| 353 | self.alphas_cumprod_prev = np.append(1.0, self.alphas_cumprod[:-1]) |
| 354 | self.alphas_cumprod_next = np.append(self.alphas_cumprod[1:], 0.0) |
| 355 | assert self.alphas_cumprod_prev.shape == (self.num_timesteps,) |
| 356 | |
| 357 | # calculations for diffusion q(x_t | x_{t-1}) and others |
| 358 | self.sqrt_alphas_cumprod = np.sqrt(self.alphas_cumprod) |
| 359 | self.sqrt_one_minus_alphas_cumprod = np.sqrt(1.0 - self.alphas_cumprod) |
| 360 | self.log_one_minus_alphas_cumprod = np.log(1.0 - self.alphas_cumprod) |
| 361 | self.sqrt_recip_alphas_cumprod = np.sqrt(1.0 / self.alphas_cumprod) |
| 362 | self.sqrt_recipm1_alphas_cumprod = np.sqrt(1.0 / self.alphas_cumprod - 1) |
| 363 | |
| 364 | # calculations for posterior q(x_{t-1} | x_t, x_0) |
| 365 | self.posterior_variance = ( |
| 366 | betas * (1.0 - self.alphas_cumprod_prev) / (1.0 - self.alphas_cumprod) |
| 367 | ) |
| 368 | # log calculation clipped because the posterior variance is 0 at the |
| 369 | # beginning of the diffusion chain. |