| 69 | |
| 70 | |
| 71 | class DiffusionFlow(nn.Module): |
| 72 | def __init__( |
| 73 | self, |
| 74 | net_cfg: dict, |
| 75 | timesteps: int = 1000, |
| 76 | beta_schedule: str = 'linear', |
| 77 | loss_type: str = 'l2', |
| 78 | parameterization: str = 'v', |
| 79 | linear_start: float = 0.00085, |
| 80 | linear_end: float = 0.0120, |
| 81 | ddim_steps: int = 10, # 10 timesteps default for FlowModel |
| 82 | ): |
| 83 | super().__init__() |
| 84 | self.net = instantiate_from_config(net_cfg) |
| 85 | |
| 86 | self.diffusion_cfg = dict( |
| 87 | timesteps=timesteps, |
| 88 | beta_schedule=beta_schedule, |
| 89 | zero_terminal_snr=False, |
| 90 | loss_type=loss_type, |
| 91 | parameterization=parameterization, |
| 92 | linear_start=linear_start, |
| 93 | linear_end=linear_end, |
| 94 | cosine_s=8e-3, |
| 95 | original_elbo_weight=0., |
| 96 | v_posterior=0., |
| 97 | l_simple_weight=1.0 |
| 98 | ) |
| 99 | self.diffusion = GaussianDiffusion(**self.diffusion_cfg) |
| 100 | |
| 101 | self.ddim_steps = ddim_steps |
| 102 | self.ddim_sampler = DDIMSampler(self.diffusion) |
| 103 | |
| 104 | def forward(self, x: torch.Tensor, t: torch.Tensor, **kwargs): |
| 105 | return self.net(x, t, **kwargs) |
| 106 | |
| 107 | def training_losses(self, x1: torch.Tensor, x0: torch.Tensor = None, **cond_kwargs): |
| 108 | loss, _ = self.diffusion.training_losses( |
| 109 | model=self.net, |
| 110 | x_start=x1, |
| 111 | model_kwargs=cond_kwargs, |
| 112 | x_noise=x0, |
| 113 | ) |
| 114 | return loss |
| 115 | |
| 116 | def generate(self, x: torch.Tensor, sample_kwargs=None, reverse=False, return_intermediates=False, **kwargs): |
| 117 | """ |
| 118 | Args: |
| 119 | x: source minibatch (bs, *dim) |
| 120 | sample_kwargs: dict, additional sampling arguments for the solver |
| 121 | progress: bool, whether to show a progress bar |
| 122 | clip_denoised: bool, whether to clip the denoised images to [-1, 1] |
| 123 | use_ddpm: bool, whether to use DDPM sampling instead of DDIM |
| 124 | intermediate_key: str, key to use for intermediate outputs |
| 125 | (DDIM: 'x_inter', 'pred_x0' | DDPM: 'sample' or 'pred_xstart') |
| 126 | intermediate_freq: int, frequency of intermediate outputs |
| 127 | __ DDIM only __: |
| 128 | num_steps: int, number of DDIM steps to take |
nothing calls this directly
no outgoing calls
no test coverage detected