From 05ec128ca94fa8e72502a91a6db3dfae04917a08 Mon Sep 17 00:00:00 2001 From: "Alex \"mcmonkey\" Goodwin" Date: Mon, 20 Mar 2023 15:42:36 -0700 Subject: [PATCH 1/2] fix img2img alt for SD v2.x --- scripts/img2imgalt.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/scripts/img2imgalt.py b/scripts/img2imgalt.py index 2572443f9..0b7ca6330 100644 --- a/scripts/img2imgalt.py +++ b/scripts/img2imgalt.py @@ -22,7 +22,12 @@ def find_noise_for_image(p, cond, uncond, cfg_scale, steps): x = p.init_latent s_in = x.new_ones([x.shape[0]]) - dnw = K.external.CompVisDenoiser(shared.sd_model) + if shared.sd_model.parameterization == "v": + dnw = K.external.CompVisVDenoiser(shared.sd_model) + skip = 1 + else: + dnw = K.external.CompVisDenoiser(shared.sd_model) + skip = 0 sigmas = dnw.get_sigmas(steps).flip(0) shared.state.sampling_steps = steps @@ -37,7 +42,7 @@ def find_noise_for_image(p, cond, uncond, cfg_scale, steps): image_conditioning = torch.cat([p.image_conditioning] * 2) cond_in = {"c_concat": [image_conditioning], "c_crossattn": [cond_in]} - c_out, c_in = [K.utils.append_dims(k, x_in.ndim) for k in dnw.get_scalings(sigma_in)] + c_out, c_in = [K.utils.append_dims(k, x_in.ndim) for k in dnw.get_scalings(sigma_in)[skip:]] t = dnw.sigma_to_t(sigma_in) eps = shared.sd_model.apply_model(x_in * c_in, t, cond=cond_in) @@ -69,7 +74,12 @@ def find_noise_for_image_sigma_adjustment(p, cond, uncond, cfg_scale, steps): x = p.init_latent s_in = x.new_ones([x.shape[0]]) - dnw = K.external.CompVisDenoiser(shared.sd_model) + if shared.sd_model.parameterization == "v": + dnw = K.external.CompVisVDenoiser(shared.sd_model) + skip = 1 + else: + dnw = K.external.CompVisDenoiser(shared.sd_model) + skip = 0 sigmas = dnw.get_sigmas(steps).flip(0) shared.state.sampling_steps = steps @@ -84,7 +94,7 @@ def find_noise_for_image_sigma_adjustment(p, cond, uncond, cfg_scale, steps): image_conditioning = torch.cat([p.image_conditioning] * 2) cond_in = {"c_concat": [image_conditioning], "c_crossattn": [cond_in]} - c_out, c_in = [K.utils.append_dims(k, x_in.ndim) for k in dnw.get_scalings(sigma_in)] + c_out, c_in = [K.utils.append_dims(k, x_in.ndim) for k in dnw.get_scalings(sigma_in)[skip:]] if i == 1: t = dnw.sigma_to_t(torch.cat([sigmas[i] * s_in] * 2)) From c9c692c4d96a58d80b037d1fd3f3bf8bd27cc4c0 Mon Sep 17 00:00:00 2001 From: "Alex \"mcmonkey\" Goodwin" Date: Mon, 20 Mar 2023 15:43:01 -0700 Subject: [PATCH 2/2] cleanup the img2img alt file a bit --- scripts/img2imgalt.py | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/scripts/img2imgalt.py b/scripts/img2imgalt.py index 0b7ca6330..bb00fb3f1 100644 --- a/scripts/img2imgalt.py +++ b/scripts/img2imgalt.py @@ -6,18 +6,11 @@ from tqdm import trange import modules.scripts as scripts import gradio as gr -from modules import processing, shared, sd_samplers, prompt_parser, sd_samplers_common -from modules.processing import Processed -from modules.shared import opts, cmd_opts, state +from modules import processing, shared, sd_samplers, sd_samplers_common import torch import k_diffusion as K -from PIL import Image -from torch import autocast -from einops import rearrange, repeat - - def find_noise_for_image(p, cond, uncond, cfg_scale, steps): x = p.init_latent @@ -135,7 +128,7 @@ class Script(scripts.Script): def show(self, is_img2img): return is_img2img - def ui(self, is_img2img): + def ui(self, is_img2img): info = gr.Markdown(''' * `CFG Scale` should be 2 or lower. ''') @@ -223,4 +216,3 @@ class Script(scripts.Script): processed = processing.process_images(p) return processed -