| 26 | |
| 27 | |
| 28 | class MyNullInversion: |
| 29 | |
| 30 | def prev_step(self, model_output: Union[torch.FloatTensor, np.ndarray], timestep: int, |
| 31 | sample: Union[torch.FloatTensor, np.ndarray]): |
| 32 | prev_timestep = timestep - self.scheduler.config.num_train_timesteps // self.scheduler.num_inference_steps |
| 33 | alpha_prod_t = self.scheduler.alphas_cumprod[timestep] |
| 34 | alpha_prod_t_prev = self.scheduler.alphas_cumprod[ |
| 35 | prev_timestep] if prev_timestep >= 0 else self.scheduler.final_alpha_cumprod |
| 36 | beta_prod_t = 1 - alpha_prod_t |
| 37 | pred_original_sample = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5 |
| 38 | pred_sample_direction = (1 - alpha_prod_t_prev) ** 0.5 * model_output |
| 39 | prev_sample = alpha_prod_t_prev ** 0.5 * pred_original_sample + pred_sample_direction |
| 40 | return prev_sample |
| 41 | |
| 42 | def next_step(self, model_output: Union[torch.FloatTensor, np.ndarray], timestep: int, |
| 43 | sample: Union[torch.FloatTensor, np.ndarray]): |
| 44 | timestep, next_timestep = min( |
| 45 | timestep - self.scheduler.config.num_train_timesteps // self.scheduler.num_inference_steps, 999), timestep |
| 46 | alpha_prod_t = self.scheduler.alphas_cumprod[timestep] if timestep >= 0 else self.scheduler.final_alpha_cumprod |
| 47 | alpha_prod_t_next = self.scheduler.alphas_cumprod[next_timestep] |
| 48 | beta_prod_t = 1 - alpha_prod_t |
| 49 | next_original_sample = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5 |
| 50 | next_sample_direction = (1 - alpha_prod_t_next) ** 0.5 * model_output |
| 51 | next_sample = alpha_prod_t_next ** 0.5 * next_original_sample + next_sample_direction |
| 52 | return next_sample |
| 53 | |
| 54 | def get_noise_pred_single(self, latents, t, context, normal_infer=False): |
| 55 | noise_pred = self.model.unet(latents, t, encoder_hidden_states=context, normal_infer=False)["sample"] |
| 56 | return noise_pred |
| 57 | |
| 58 | def get_noise_pred(self, latents, t, is_forward=True, context=None, normal_infer=False): |
| 59 | latents_input = torch.cat([latents] * 2) |
| 60 | if context is None: |
| 61 | context = self.context |
| 62 | guidance_scale = 1 if is_forward else 7.5 |
| 63 | noise_pred = self.model.unet(latents_input, t, encoder_hidden_states=context, normal_infer=False)["sample"] |
| 64 | noise_pred_uncond, noise_prediction_text = noise_pred.chunk(2) |
| 65 | noise_pred = noise_pred_uncond + guidance_scale * (noise_prediction_text - noise_pred_uncond) |
| 66 | if is_forward: |
| 67 | latents = self.next_step(noise_pred, t, latents) |
| 68 | else: |
| 69 | latents = self.prev_step(noise_pred, t, latents) |
| 70 | return latents |
| 71 | |
| 72 | @torch.no_grad() |
| 73 | def latent2image(self, latents, return_type='np'): |
| 74 | latents = 1 / 0.18215 * latents.detach() |
| 75 | image = self.model.vae.decode(latents)['sample'] |
| 76 | if return_type == 'np': |
| 77 | image = (image / 2 + 0.5).clamp(0, 1) |
| 78 | image = image.cpu().permute(0, 2, 3, 1).numpy()[0] |
| 79 | image = (image * 255).astype(np.uint8) |
| 80 | return image |
| 81 | |
| 82 | @torch.no_grad() |
| 83 | def image2latent(self, image): |
| 84 | with torch.no_grad(): |
| 85 | if type(image) is Image: |
nothing calls this directly
no outgoing calls
no test coverage detected