Utilities for training and sampling diffusion models. Ported directly from here: https://github.com/hojonathanho/diffusion/blob/1e0dceb3b3495bbe19116a5e1b3596cd0706c543/diffusion_tf/diffusion_utils_2.py#L42 :param betas: a 1-D array of betas for each diffusion timestep from T to 1
| 125 | |
| 126 | |
| 127 | class GaussianDiffusion: |
| 128 | """ |
| 129 | Utilities for training and sampling diffusion models. |
| 130 | |
| 131 | Ported directly from here: |
| 132 | https://github.com/hojonathanho/diffusion/blob/1e0dceb3b3495bbe19116a5e1b3596cd0706c543/diffusion_tf/diffusion_utils_2.py#L42 |
| 133 | |
| 134 | :param betas: a 1-D array of betas for each diffusion timestep from T to 1. |
| 135 | :param model_mean_type: a string determining what the model outputs. |
| 136 | :param model_var_type: a string determining how variance is output. |
| 137 | :param loss_type: a string determining the loss function to use. |
| 138 | :param discretized_t0: if True, use discrete gaussian loss for t=0. Only |
| 139 | makes sense for images. |
| 140 | :param channel_scales: a multiplier to apply to x_start in training_losses |
| 141 | and sampling functions. |
| 142 | """ |
| 143 | |
| 144 | def __init__( |
| 145 | self, |
| 146 | *, |
| 147 | betas: Sequence[float], |
| 148 | model_mean_type: str, |
| 149 | model_var_type: str, |
| 150 | loss_type: str, |
| 151 | discretized_t0: bool = False, |
| 152 | channel_scales: Optional[np.ndarray] = None, |
| 153 | channel_biases: Optional[np.ndarray] = None, |
| 154 | ): |
| 155 | self.model_mean_type = model_mean_type |
| 156 | self.model_var_type = model_var_type |
| 157 | self.loss_type = loss_type |
| 158 | self.discretized_t0 = discretized_t0 |
| 159 | self.channel_scales = channel_scales |
| 160 | self.channel_biases = channel_biases |
| 161 | |
| 162 | # Use float64 for accuracy. |
| 163 | betas = np.array(betas, dtype=np.float64) |
| 164 | self.betas = betas |
| 165 | assert len(betas.shape) == 1, "betas must be 1-D" |
| 166 | assert (betas > 0).all() and (betas <= 1).all() |
| 167 | |
| 168 | self.num_timesteps = int(betas.shape[0]) |
| 169 | |
| 170 | alphas = 1.0 - betas |
| 171 | self.alphas_cumprod = np.cumprod(alphas, axis=0) |
| 172 | self.alphas_cumprod_prev = np.append(1.0, self.alphas_cumprod[:-1]) |
| 173 | self.alphas_cumprod_next = np.append(self.alphas_cumprod[1:], 0.0) |
| 174 | assert self.alphas_cumprod_prev.shape == (self.num_timesteps,) |
| 175 | |
| 176 | # calculations for diffusion q(x_t | x_{t-1}) and others |
| 177 | self.sqrt_alphas_cumprod = np.sqrt(self.alphas_cumprod) |
| 178 | self.sqrt_one_minus_alphas_cumprod = np.sqrt(1.0 - self.alphas_cumprod) |
| 179 | self.log_one_minus_alphas_cumprod = np.log(1.0 - self.alphas_cumprod) |
| 180 | self.sqrt_recip_alphas_cumprod = np.sqrt(1.0 / self.alphas_cumprod) |
| 181 | self.sqrt_recipm1_alphas_cumprod = np.sqrt(1.0 / self.alphas_cumprod - 1) |
| 182 | |
| 183 | # calculations for posterior q(x_{t-1} | x_t, x_0) |
| 184 | self.posterior_variance = ( |
no outgoing calls
no test coverage detected