From 7a701506a4f1344d1d5125800da512407c5a03f5 Mon Sep 17 00:00:00 2001 From: Lincoln Stein Date: Fri, 7 Oct 2022 14:43:59 -0400 Subject: [PATCH] restore ability of ksamplers to process -v variation options - supersedes PR #977 - works with both img2img and txt2img --- ldm/dream/generator/img2img.py | 1 + ldm/models/diffusion/ksampler.py | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/ldm/dream/generator/img2img.py b/ldm/dream/generator/img2img.py index 09750b3748..27425a2b6b 100644 --- a/ldm/dream/generator/img2img.py +++ b/ldm/dream/generator/img2img.py @@ -49,6 +49,7 @@ class Img2Img(Generator): img_callback = step_callback, unconditional_guidance_scale=cfg_scale, unconditional_conditioning=uc, + init_latent = self.init_latent, # changes how noising is performed in ksampler ) return self.sample_to_image(samples) diff --git a/ldm/models/diffusion/ksampler.py b/ldm/models/diffusion/ksampler.py index 65fbf6fe68..6b78f8a5b0 100644 --- a/ldm/models/diffusion/ksampler.py +++ b/ldm/models/diffusion/ksampler.py @@ -174,9 +174,14 @@ class KSampler(Sampler): # sigmas are set up in make_schedule - we take the last steps items total_steps = len(self.sigmas) sigmas = self.sigmas[-S-1:] - + + # x_T is variation noise. When an init image is provided (in x0) we need to add + # more randomness to the starting image. if x_T is not None: - x = x_T + torch.randn([batch_size, *shape], device=self.device) * sigmas[0] + if x0 is not None: + x = x_T + torch.randn_like(x0, device=self.device) * sigmas[0] + else: + x = x_T * sigmas[0] else: x = torch.randn([batch_size, *shape], device=self.device) * sigmas[0]