Skip to content

Commit

Permalink
Merge pull request AUTOMATIC1111#8772 from mcmonkey4eva/img2img-alt-s…
Browse files Browse the repository at this point in the history
…d2-fix

Fix img2img-alternative-test script for SD v2.x
  • Loading branch information
AUTOMATIC1111 authored Mar 25, 2023
2 parents b7c14ed + c9c692c commit 983d48a
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions scripts/img2imgalt.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,21 @@
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

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
Expand All @@ -37,7 +35,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)
Expand Down Expand Up @@ -69,7 +67,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
Expand All @@ -84,7 +87,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))
Expand Down Expand Up @@ -125,7 +128,7 @@ def title(self):
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.
''')
Expand Down Expand Up @@ -213,4 +216,3 @@ def sample_extra(conditioning, unconditional_conditioning, seeds, subseeds, subs
processed = processing.process_images(p)

return processed

0 comments on commit 983d48a

Please sign in to comment.