From fcdd95b652b8f503528d3e3526590e3c7ab2f3dc Mon Sep 17 00:00:00 2001 From: Sean McLellan Date: Fri, 26 Aug 2022 00:39:57 -0400 Subject: [PATCH 1/4] Refactor so that behavior is consolidated at top level --- ldm/simplet2i.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/ldm/simplet2i.py b/ldm/simplet2i.py index 0de3a33237..6ae45b36bf 100644 --- a/ldm/simplet2i.py +++ b/ldm/simplet2i.py @@ -282,6 +282,11 @@ The vast majority of these arguments default to reasonable values. seed_everything(seed) iter_images = next(images_iterator) for image in iter_images: + try: + if gfpgan_strength > 0: + image = self._run_gfpgan(image, gfpgan_strength) + except Exception as e: + print(f"Error running GFPGAN - Your image was not enhanced.\n{e}") results.append([image, seed]) if image_callback is not None: image_callback(image,seed) @@ -305,7 +310,6 @@ The vast majority of these arguments default to reasonable values. batch_size, steps,cfg_scale,ddim_eta, skip_normalize, - gfpgan_strength, width,height): """ An infinite iterator of images from the prompt. @@ -325,7 +329,7 @@ The vast majority of these arguments default to reasonable values. unconditional_guidance_scale=cfg_scale, unconditional_conditioning=uc, eta=ddim_eta) - yield self._samples_to_images(samples, gfpgan_strength=gfpgan_strength) + yield self._samples_to_images(samples) @torch.no_grad() def _img2img(self, @@ -334,7 +338,6 @@ The vast majority of these arguments default to reasonable values. batch_size, steps,cfg_scale,ddim_eta, skip_normalize, - gfpgan_strength, init_img,strength): """ An infinite iterator of images from the prompt and the initial image @@ -365,7 +368,7 @@ The vast majority of these arguments default to reasonable values. # decode it samples = sampler.decode(z_enc, c, t_enc, unconditional_guidance_scale=cfg_scale, unconditional_conditioning=uc,) - yield self._samples_to_images(samples, gfpgan_strength) + yield self._samples_to_images(samples) # TODO: does this actually need to run every loop? does anything in it vary by random seed? def _get_uc_and_c(self, prompt, batch_size, skip_normalize): @@ -389,18 +392,13 @@ The vast majority of these arguments default to reasonable values. c = self.model.get_learned_conditioning(batch_size * [prompt]) return (uc, c) - def _samples_to_images(self, samples, gfpgan_strength=0): + def _samples_to_images(self, samples): x_samples = self.model.decode_first_stage(samples) x_samples = torch.clamp((x_samples + 1.0) / 2.0, min=0.0, max=1.0) images = list() for x_sample in x_samples: x_sample = 255. * rearrange(x_sample.cpu().numpy(), 'c h w -> h w c') image = Image.fromarray(x_sample.astype(np.uint8)) - try: - if gfpgan_strength > 0: - image = self._run_gfpgan(image, gfpgan_strength) - except Exception as e: - print(f"Error running GFPGAN - Your image was not enhanced.\n{e}") images.append(image) return images From 4f1664ec4f7b95addaf8f531ebe0c2bd68f0f516 Mon Sep 17 00:00:00 2001 From: Sean McLellan Date: Fri, 26 Aug 2022 00:41:41 -0400 Subject: [PATCH 2/4] remove params --- ldm/simplet2i.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/ldm/simplet2i.py b/ldm/simplet2i.py index 6ae45b36bf..66fdbb48b4 100644 --- a/ldm/simplet2i.py +++ b/ldm/simplet2i.py @@ -266,7 +266,6 @@ The vast majority of these arguments default to reasonable values. batch_size=batch_size, steps=steps,cfg_scale=cfg_scale,ddim_eta=ddim_eta, skip_normalize=skip_normalize, - gfpgan_strength=gfpgan_strength, init_img=init_img,strength=strength) else: images_iterator = self._txt2img(prompt, @@ -274,7 +273,6 @@ The vast majority of these arguments default to reasonable values. batch_size=batch_size, steps=steps,cfg_scale=cfg_scale,ddim_eta=ddim_eta, skip_normalize=skip_normalize, - gfpgan_strength=gfpgan_strength, width=width,height=height) with scope(self.device.type), self.model.ema_scope(): From f1ffb5b51b2d2adb48799d367e285d573b108792 Mon Sep 17 00:00:00 2001 From: Sean McLellan Date: Fri, 26 Aug 2022 00:45:19 -0400 Subject: [PATCH 3/4] Fix blend if the target image has been upscaled --- ldm/simplet2i.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ldm/simplet2i.py b/ldm/simplet2i.py index 66fdbb48b4..1b7e34a7dc 100644 --- a/ldm/simplet2i.py +++ b/ldm/simplet2i.py @@ -529,6 +529,9 @@ The vast majority of these arguments default to reasonable values. res = Image.fromarray(restored_img) if strength < 1.0: + # Resize the image to the new image if the sizes have changed + if restored_img.size != image.size: + res = res.resize(image.size) res = Image.blend(image, res, strength) return res From 407d70a98789d0f14ccfaf4aea8a0ed156af96d8 Mon Sep 17 00:00:00 2001 From: Sean McLellan Date: Fri, 26 Aug 2022 00:49:12 -0400 Subject: [PATCH 4/4] Fix backwards logic --- ldm/simplet2i.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ldm/simplet2i.py b/ldm/simplet2i.py index 1b7e34a7dc..f1f88bba5e 100644 --- a/ldm/simplet2i.py +++ b/ldm/simplet2i.py @@ -531,7 +531,7 @@ The vast majority of these arguments default to reasonable values. if strength < 1.0: # Resize the image to the new image if the sizes have changed if restored_img.size != image.size: - res = res.resize(image.size) + image = image.resize(res.size) res = Image.blend(image, res, strength) return res