From 16e7ac09df1cef85781494a1234a7aa3c51fb4a9 Mon Sep 17 00:00:00 2001 From: DepFA <35278260+dfaker@users.noreply.github.com> Date: Tue, 27 Sep 2022 02:26:13 +0100 Subject: [PATCH 1/5] job_timestamp initialization change --- modules/shared.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/shared.py b/modules/shared.py index c32da110..bd030fe8 100644 --- a/modules/shared.py +++ b/modules/shared.py @@ -66,7 +66,7 @@ class State: job = "" job_no = 0 job_count = 0 - job_timestamp = 0 + job_timestamp = '0' sampling_step = 0 sampling_steps = 0 current_latent = None @@ -80,6 +80,7 @@ class State: self.job_no += 1 self.sampling_step = 0 self.current_image_sampling_step = 0 + def get_job_timestamp(self): return datetime.datetime.now().strftime("%Y%m%d%H%M%S") From c74becca23d17eeeee3774b6629674a010ae91bd Mon Sep 17 00:00:00 2001 From: Alexandre Simard Date: Mon, 26 Sep 2022 17:18:57 -0400 Subject: [PATCH 2/5] Solve issue #962 Fix by @MrAcademy --- .gitignore | 3 ++- javascript/ui.js | 5 ++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 9d78853a..fa1ab43e 100644 --- a/.gitignore +++ b/.gitignore @@ -19,4 +19,5 @@ __pycache__ /webui-user.sh /interrogate /user.css -/.idea \ No newline at end of file +/.idea +/SwinIR diff --git a/javascript/ui.js b/javascript/ui.js index 076e9436..7db4db48 100644 --- a/javascript/ui.js +++ b/javascript/ui.js @@ -1,9 +1,8 @@ // various functions for interation with ui.py not large enough to warrant putting them in separate files function selected_gallery_index(){ - var gr = gradioApp() - var buttons = gradioApp().querySelectorAll(".gallery-item") - var button = gr.querySelector(".gallery-item.\\!ring-2") + var buttons = gradioApp().querySelectorAll('[style="display: block;"].tabitem .gallery-item') + var button = gradioApp().querySelector('[style="display: block;"].tabitem .gallery-item.\\!ring-2') var result = -1 buttons.forEach(function(v, i){ if(v==button) { result = i } }) From 258a2d4f064c2c3c0d63c7cf8966d2260fea3f33 Mon Sep 17 00:00:00 2001 From: Martin Cairns Date: Mon, 26 Sep 2022 21:13:23 +0100 Subject: [PATCH 3/5] Add option to img2imgalt.py to use sigma adjustment instead of original method for #736 --- scripts/img2imgalt.py | 68 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 6 deletions(-) diff --git a/scripts/img2imgalt.py b/scripts/img2imgalt.py index 7b4ba244..0ef137f7 100644 --- a/scripts/img2imgalt.py +++ b/scripts/img2imgalt.py @@ -59,7 +59,55 @@ def find_noise_for_image(p, cond, uncond, cfg_scale, steps): return x / x.std() -Cached = namedtuple("Cached", ["noise", "cfg_scale", "steps", "latent", "original_prompt", "original_negative_prompt"]) +Cached = namedtuple("Cached", ["noise", "cfg_scale", "steps", "latent", "original_prompt", "original_negative_prompt", "sigma_adjustment"]) + + +# Based on changes suggested by briansemrau in https://github.com/AUTOMATIC1111/stable-diffusion-webui/issues/736 +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) + sigmas = dnw.get_sigmas(steps).flip(0) + + shared.state.sampling_steps = steps + + for i in trange(1, len(sigmas)): + shared.state.sampling_step += 1 + + x_in = torch.cat([x] * 2) + sigma_in = torch.cat([sigmas[i - 1] * s_in] * 2) + cond_in = torch.cat([uncond, cond]) + + c_out, c_in = [K.utils.append_dims(k, x_in.ndim) for k in dnw.get_scalings(sigma_in)] + + if i == 1: + t = dnw.sigma_to_t(torch.cat([sigmas[i] * s_in] * 2)) + else: + t = dnw.sigma_to_t(sigma_in) + + eps = shared.sd_model.apply_model(x_in * c_in, t, cond=cond_in) + denoised_uncond, denoised_cond = (x_in + eps * c_out).chunk(2) + + denoised = denoised_uncond + (denoised_cond - denoised_uncond) * cfg_scale + + if i == 1: + d = (x - denoised) / (2 * sigmas[i]) + else: + d = (x - denoised) / sigmas[i - 1] + + dt = sigmas[i] - sigmas[i - 1] + x = x + d * dt + + sd_samplers.store_latent(x) + + # This shouldn't be necessary, but solved some VRAM issues + del x_in, sigma_in, cond_in, c_out, c_in, t, + del eps, denoised_uncond, denoised_cond, denoised, d, dt + + shared.state.nextjob() + + return x / sigmas[-1] class Script(scripts.Script): @@ -78,9 +126,10 @@ class Script(scripts.Script): cfg = gr.Slider(label="Decode CFG scale", minimum=0.0, maximum=15.0, step=0.1, value=1.0) st = gr.Slider(label="Decode steps", minimum=1, maximum=150, step=1, value=50) randomness = gr.Slider(label="Randomness", minimum=0.0, maximum=1.0, step=0.01, value=0.0) - return [original_prompt, original_negative_prompt, cfg, st, randomness] + sigma_adjustment = gr.Checkbox(label="Sigma adjustment for finding noise for image", value=False) + return [original_prompt, original_negative_prompt, cfg, st, randomness, sigma_adjustment] - def run(self, p, original_prompt, original_negative_prompt, cfg, st, randomness): + def run(self, p, original_prompt, original_negative_prompt, cfg, st, randomness, sigma_adjustment): p.batch_size = 1 p.batch_count = 1 @@ -88,7 +137,10 @@ class Script(scripts.Script): def sample_extra(conditioning, unconditional_conditioning, seeds, subseeds, subseed_strength): lat = (p.init_latent.cpu().numpy() * 10).astype(int) - same_params = self.cache is not None and self.cache.cfg_scale == cfg and self.cache.steps == st and self.cache.original_prompt == original_prompt and self.cache.original_negative_prompt == original_negative_prompt + same_params = self.cache is not None and self.cache.cfg_scale == cfg and self.cache.steps == st \ + and self.cache.original_prompt == original_prompt \ + and self.cache.original_negative_prompt == original_negative_prompt \ + and self.cache.sigma_adjustment == sigma_adjustment same_everything = same_params and self.cache.latent.shape == lat.shape and np.abs(self.cache.latent-lat).sum() < 100 if same_everything: @@ -97,8 +149,11 @@ class Script(scripts.Script): shared.state.job_count += 1 cond = p.sd_model.get_learned_conditioning(p.batch_size * [original_prompt]) uncond = p.sd_model.get_learned_conditioning(p.batch_size * [original_negative_prompt]) - rec_noise = find_noise_for_image(p, cond, uncond, cfg, st) - self.cache = Cached(rec_noise, cfg, st, lat, original_prompt, original_negative_prompt) + if sigma_adjustment: + rec_noise = find_noise_for_image_sigma_adjustment(p, cond, uncond, cfg, st) + else: + rec_noise = find_noise_for_image(p, cond, uncond, cfg, st) + self.cache = Cached(rec_noise, cfg, st, lat, original_prompt, original_negative_prompt, sigma_adjustment) rand_noise = processing.create_random_tensors(p.init_latent.shape[1:], [p.seed + x + 1 for x in range(p.init_latent.shape[0])]) @@ -121,6 +176,7 @@ class Script(scripts.Script): p.extra_generation_params["Decode CFG scale"] = cfg p.extra_generation_params["Decode steps"] = st p.extra_generation_params["Randomness"] = randomness + p.extra_generation_params["Sigma Adjustment"] = sigma_adjustment processed = processing.process_images(p) From 8dcac9ac81db587cdac59d01db85c01579156392 Mon Sep 17 00:00:00 2001 From: Daniel <113887727+mortstraze@users.noreply.github.com> Date: Mon, 26 Sep 2022 19:30:18 +1000 Subject: [PATCH 4/5] Grids saving to subdirectory when setting unchecked Fixed grids saving to subdirectory when the setting is unchecked but "save images to subdirectory" was checked. --- modules/processing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/processing.py b/modules/processing.py index 0246e094..3abf3181 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -406,7 +406,7 @@ def process_images(p: StableDiffusionProcessing) -> Processed: index_of_first_image = 1 if opts.grid_save: - images.save_image(grid, p.outpath_grids, "grid", all_seeds[0], all_prompts[0], opts.grid_format, info=infotext(), short_filename=not opts.grid_extended_filename, p=p) + images.save_image(grid, p.outpath_grids, "grid", all_seeds[0], all_prompts[0], opts.grid_format, info=infotext(), short_filename=not opts.grid_extended_filename, p=p, grid=True) devices.torch_gc() return Processed(p, output_images, all_seeds[0], infotext(), subseed=all_subseeds[0], all_prompts=all_prompts, all_seeds=all_seeds, all_subseeds=all_subseeds, index_of_first_image=index_of_first_image) From c0b1177a3203091ca43f2d08f24dd821f1237612 Mon Sep 17 00:00:00 2001 From: Connum Date: Mon, 26 Sep 2022 18:12:55 +0200 Subject: [PATCH 5/5] refactored image paste handling to fill unset images successively, then replace last existing image (fixes #981) --- javascript/dragdrop.js | 24 +++++++++++++++--------- script.js | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/javascript/dragdrop.js b/javascript/dragdrop.js index c01f66e2..5aac57f7 100644 --- a/javascript/dragdrop.js +++ b/javascript/dragdrop.js @@ -68,13 +68,19 @@ window.addEventListener('paste', e => { if ( ! isValidImageList( files ) ) { return; } - [...gradioApp().querySelectorAll('input[type=file][accept="image/x-png,image/gif,image/jpeg"]')] - .filter(input => !input.matches('.\\!hidden input[type=file]')) - .forEach(input => { - input.files = files; - input.dispatchEvent(new Event('change')) - }); - [...gradioApp().querySelectorAll('[data-testid="image"]')] - .filter(imgWrap => !imgWrap.closest('.\\!hidden')) - .forEach(imgWrap => dropReplaceImage( imgWrap, files )); + + const visibleImageFields = [...gradioApp().querySelectorAll('[data-testid="image"]')] + .filter(el => uiElementIsVisible(el)); + if ( ! visibleImageFields.length ) { + return; + } + + const firstFreeImageField = visibleImageFields + .filter(el => el.querySelector('input[type=file]'))?.[0]; + + dropReplaceImage( + firstFreeImageField ? + firstFreeImageField : + visibleImageFields[visibleImageFields.length - 1] + , files ); }); diff --git a/script.js b/script.js index 7f26e23b..cf989605 100644 --- a/script.js +++ b/script.js @@ -39,3 +39,24 @@ document.addEventListener("DOMContentLoaded", function() { }); mutationObserver.observe( gradioApp(), { childList:true, subtree:true }) }); + +/** + * checks that a UI element is not in another hidden element or tab content + */ +function uiElementIsVisible(el) { + let isVisible = !el.closest('.\\!hidden'); + if ( ! isVisible ) { + return false; + } + + while( isVisible = el.closest('.tabitem')?.style.display !== 'none' ) { + if ( ! isVisible ) { + return false; + } else if ( el.parentElement ) { + el = el.parentElement + } else { + break; + } + } + return isVisible; +} \ No newline at end of file