From 3bd898b6ce94f34bcc7685672f0e4318d2d86b33 Mon Sep 17 00:00:00 2001 From: invincibledude <> Date: Sat, 21 Jan 2023 23:14:59 +0300 Subject: [PATCH 01/37] First test of different sampler for hi-res fix --- modules/processing.py | 7 ++++++- modules/txt2img.py | 4 +++- modules/ui.py | 5 +++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/modules/processing.py b/modules/processing.py index 6e6371a1..b3206f80 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -710,7 +710,7 @@ def old_hires_fix_first_pass_dimensions(width, height): class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing): sampler = None - def __init__(self, enable_hr: bool = False, denoising_strength: float = 0.75, firstphase_width: int = 0, firstphase_height: int = 0, hr_scale: float = 2.0, hr_upscaler: str = None, hr_second_pass_steps: int = 0, hr_resize_x: int = 0, hr_resize_y: int = 0, **kwargs): + def __init__(self, enable_hr: bool = False, denoising_strength: float = 0.75, firstphase_width: int = 0, firstphase_height: int = 0, hr_scale: float = 2.0, hr_upscaler: str = None, hr_second_pass_steps: int = 0, hr_resize_x: int = 0, hr_resize_y: int = 0, hr_sampler: int = 0, **kwargs): super().__init__(**kwargs) self.enable_hr = enable_hr self.denoising_strength = denoising_strength @@ -721,6 +721,7 @@ class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing): self.hr_resize_y = hr_resize_y self.hr_upscale_to_x = hr_resize_x self.hr_upscale_to_y = hr_resize_y + self.hr_sampler = hr_sampler if firstphase_width != 0 or firstphase_height != 0: self.hr_upscale_to_x = self.width @@ -862,6 +863,10 @@ class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing): shared.state.nextjob() img2img_sampler_name = self.sampler_name if self.sampler_name != 'PLMS' else 'DDIM' # PLMS does not support img2img so we just silently switch ot DDIM + + if self.hr_sampler != 0 and sd_samplers.samplers[self.hr_sampler].name != 'PLMS': + img2img_sampler_name = sd_samplers.samplers[self.hr_sampler].name + self.sampler = sd_samplers.create_sampler(img2img_sampler_name, self.sd_model) samples = samples[:, :, self.truncate_y//2:samples.shape[2]-(self.truncate_y+1)//2, self.truncate_x//2:samples.shape[3]-(self.truncate_x+1)//2] diff --git a/modules/txt2img.py b/modules/txt2img.py index e945fd69..21f48d12 100644 --- a/modules/txt2img.py +++ b/modules/txt2img.py @@ -8,7 +8,7 @@ import modules.processing as processing from modules.ui import plaintext_to_html -def txt2img(id_task: str, prompt: str, negative_prompt: str, prompt_styles, steps: int, sampler_index: int, restore_faces: bool, tiling: bool, n_iter: int, batch_size: int, cfg_scale: float, seed: int, subseed: int, subseed_strength: float, seed_resize_from_h: int, seed_resize_from_w: int, seed_enable_extras: bool, height: int, width: int, enable_hr: bool, denoising_strength: float, hr_scale: float, hr_upscaler: str, hr_second_pass_steps: int, hr_resize_x: int, hr_resize_y: int, *args): +def txt2img(id_task: str, prompt: str, negative_prompt: str, prompt_styles, steps: int, sampler_index: int, restore_faces: bool, tiling: bool, n_iter: int, batch_size: int, cfg_scale: float, seed: int, subseed: int, subseed_strength: float, seed_resize_from_h: int, seed_resize_from_w: int, seed_enable_extras: bool, height: int, width: int, enable_hr: bool, denoising_strength: float, hr_scale: float, hr_upscaler: str, hr_second_pass_steps: int, hr_resize_x: int, hr_resize_y: int, hr_sampler_index: int, *args): p = StableDiffusionProcessingTxt2Img( sd_model=shared.sd_model, outpath_samples=opts.outdir_samples or opts.outdir_txt2img_samples, @@ -38,6 +38,8 @@ def txt2img(id_task: str, prompt: str, negative_prompt: str, prompt_styles, step hr_second_pass_steps=hr_second_pass_steps, hr_resize_x=hr_resize_x, hr_resize_y=hr_resize_y, + hr_sampler=sd_samplers.samplers[hr_sampler_index].name + if hr_sampler_index != 0 else sd_samplers.samplers[sampler_index].name ) p.scripts = modules.scripts.scripts_txt2img diff --git a/modules/ui.py b/modules/ui.py index b3105901..6bbbc0f6 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -672,6 +672,9 @@ def create_ui(): hr_resize_x = gr.Slider(minimum=0, maximum=2048, step=8, label="Resize width to", value=0, elem_id="txt2img_hr_resize_x") hr_resize_y = gr.Slider(minimum=0, maximum=2048, step=8, label="Resize height to", value=0, elem_id="txt2img_hr_resize_y") + with FormRow(elem_id="txt2img_hires_fix_row3", variant="compact"): + hr_sampler_index = gr.Dropdown(label='Hires sampling method', elem_id=f"hr_sampler", choices=["---"] + [x.name for x in samplers], value="---", type="index") + elif category == "batch": if not opts.dimensions_and_batch_together: with FormRow(elem_id="txt2img_column_batch"): @@ -730,6 +733,7 @@ def create_ui(): hr_second_pass_steps, hr_resize_x, hr_resize_y, + hr_sampler_index, ] + custom_inputs, outputs=[ @@ -785,6 +789,7 @@ def create_ui(): (hr_second_pass_steps, "Hires steps"), (hr_resize_x, "Hires resize-1"), (hr_resize_y, "Hires resize-2"), + (hr_sampler_index, "Hires sampling method"), *modules.scripts.scripts_txt2img.infotext_fields ] parameters_copypaste.add_paste_fields("txt2img", None, txt2img_paste_fields) From 6c0566f937ba212e3faf1d30c337850543e85a12 Mon Sep 17 00:00:00 2001 From: invincibledude <> Date: Sat, 21 Jan 2023 23:25:36 +0300 Subject: [PATCH 02/37] Type mismatch fix --- modules/processing.py | 4 ++-- modules/txt2img.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/processing.py b/modules/processing.py index b3206f80..65673a9d 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -864,8 +864,8 @@ class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing): img2img_sampler_name = self.sampler_name if self.sampler_name != 'PLMS' else 'DDIM' # PLMS does not support img2img so we just silently switch ot DDIM - if self.hr_sampler != 0 and sd_samplers.samplers[self.hr_sampler].name != 'PLMS': - img2img_sampler_name = sd_samplers.samplers[self.hr_sampler].name + if self.hr_sampler != 0 and self.hr_sampler != 'PLMS': + img2img_sampler_name = self.hr_sampler self.sampler = sd_samplers.create_sampler(img2img_sampler_name, self.sd_model) diff --git a/modules/txt2img.py b/modules/txt2img.py index 21f48d12..3d16ac5b 100644 --- a/modules/txt2img.py +++ b/modules/txt2img.py @@ -39,7 +39,7 @@ def txt2img(id_task: str, prompt: str, negative_prompt: str, prompt_styles, step hr_resize_x=hr_resize_x, hr_resize_y=hr_resize_y, hr_sampler=sd_samplers.samplers[hr_sampler_index].name - if hr_sampler_index != 0 else sd_samplers.samplers[sampler_index].name + if hr_sampler_index != 0 else 0 ) p.scripts = modules.scripts.scripts_txt2img From 8bec3a2aa10d3c8ab15cca51b3b498e6ba350105 Mon Sep 17 00:00:00 2001 From: invincibledude <> Date: Sat, 21 Jan 2023 23:31:36 +0300 Subject: [PATCH 03/37] Index fix --- modules/txt2img.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/txt2img.py b/modules/txt2img.py index 3d16ac5b..8560b74f 100644 --- a/modules/txt2img.py +++ b/modules/txt2img.py @@ -38,7 +38,7 @@ def txt2img(id_task: str, prompt: str, negative_prompt: str, prompt_styles, step hr_second_pass_steps=hr_second_pass_steps, hr_resize_x=hr_resize_x, hr_resize_y=hr_resize_y, - hr_sampler=sd_samplers.samplers[hr_sampler_index].name + hr_sampler=sd_samplers.samplers[hr_sampler_index - 1].name if hr_sampler_index != 0 else 0 ) From 9e1f49c4e5b169eecbf075c49a304774dfdc6035 Mon Sep 17 00:00:00 2001 From: invincibledude <> Date: Sun, 22 Jan 2023 00:03:16 +0300 Subject: [PATCH 04/37] PLMS edge-case handling fix --- modules/processing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/processing.py b/modules/processing.py index 65673a9d..6e04b064 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -864,7 +864,7 @@ class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing): img2img_sampler_name = self.sampler_name if self.sampler_name != 'PLMS' else 'DDIM' # PLMS does not support img2img so we just silently switch ot DDIM - if self.hr_sampler != 0 and self.hr_sampler != 'PLMS': + if self.hr_sampler != 0 and sd_samplers.samplers[self.hr_sampler].name != 'PLMS': img2img_sampler_name = self.hr_sampler self.sampler = sd_samplers.create_sampler(img2img_sampler_name, self.sd_model) From 3ffe2e768b542c837927cfdf0310a2e0fdd48683 Mon Sep 17 00:00:00 2001 From: invincibledude <> Date: Sun, 22 Jan 2023 00:07:46 +0300 Subject: [PATCH 05/37] PLMS edge-case handling fix 2 --- modules/processing.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/modules/processing.py b/modules/processing.py index 6e04b064..83c65948 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -710,7 +710,7 @@ def old_hires_fix_first_pass_dimensions(width, height): class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing): sampler = None - def __init__(self, enable_hr: bool = False, denoising_strength: float = 0.75, firstphase_width: int = 0, firstphase_height: int = 0, hr_scale: float = 2.0, hr_upscaler: str = None, hr_second_pass_steps: int = 0, hr_resize_x: int = 0, hr_resize_y: int = 0, hr_sampler: int = 0, **kwargs): + def __init__(self, enable_hr: bool = False, denoising_strength: float = 0.75, firstphase_width: int = 0, firstphase_height: int = 0, hr_scale: float = 2.0, hr_upscaler: str = None, hr_second_pass_steps: int = 0, hr_resize_x: int = 0, hr_resize_y: int = 0, hr_sampler: int | str = 0, **kwargs): super().__init__(**kwargs) self.enable_hr = enable_hr self.denoising_strength = denoising_strength @@ -864,7 +864,11 @@ class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing): img2img_sampler_name = self.sampler_name if self.sampler_name != 'PLMS' else 'DDIM' # PLMS does not support img2img so we just silently switch ot DDIM - if self.hr_sampler != 0 and sd_samplers.samplers[self.hr_sampler].name != 'PLMS': + if self.hr_sampler == 0: + pass + elif self.hr_sampler == 'PLMS': + img2img_sampler_name = 'DDIM' + else: img2img_sampler_name = self.hr_sampler self.sampler = sd_samplers.create_sampler(img2img_sampler_name, self.sd_model) From 6cd7bf9f860dff3b61a50ec2d41915536cbcf448 Mon Sep 17 00:00:00 2001 From: invincibledude <> Date: Sun, 22 Jan 2023 00:08:58 +0300 Subject: [PATCH 06/37] PLMS edge-case handling fix 3 --- modules/processing.py | 4 ++-- modules/txt2img.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/processing.py b/modules/processing.py index 83c65948..6f6efe06 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -710,7 +710,7 @@ def old_hires_fix_first_pass_dimensions(width, height): class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing): sampler = None - def __init__(self, enable_hr: bool = False, denoising_strength: float = 0.75, firstphase_width: int = 0, firstphase_height: int = 0, hr_scale: float = 2.0, hr_upscaler: str = None, hr_second_pass_steps: int = 0, hr_resize_x: int = 0, hr_resize_y: int = 0, hr_sampler: int | str = 0, **kwargs): + def __init__(self, enable_hr: bool = False, denoising_strength: float = 0.75, firstphase_width: int = 0, firstphase_height: int = 0, hr_scale: float = 2.0, hr_upscaler: str = None, hr_second_pass_steps: int = 0, hr_resize_x: int = 0, hr_resize_y: int = 0, hr_sampler: str = '---', **kwargs): super().__init__(**kwargs) self.enable_hr = enable_hr self.denoising_strength = denoising_strength @@ -864,7 +864,7 @@ class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing): img2img_sampler_name = self.sampler_name if self.sampler_name != 'PLMS' else 'DDIM' # PLMS does not support img2img so we just silently switch ot DDIM - if self.hr_sampler == 0: + if self.hr_sampler == '---': pass elif self.hr_sampler == 'PLMS': img2img_sampler_name = 'DDIM' diff --git a/modules/txt2img.py b/modules/txt2img.py index 8560b74f..9c8ec621 100644 --- a/modules/txt2img.py +++ b/modules/txt2img.py @@ -39,7 +39,7 @@ def txt2img(id_task: str, prompt: str, negative_prompt: str, prompt_styles, step hr_resize_x=hr_resize_x, hr_resize_y=hr_resize_y, hr_sampler=sd_samplers.samplers[hr_sampler_index - 1].name - if hr_sampler_index != 0 else 0 + if hr_sampler_index != 0 else '---' ) p.scripts = modules.scripts.scripts_txt2img From 0f6862ef3041f3ee18e8236765b1c1958c84385b Mon Sep 17 00:00:00 2001 From: invincibledude <> Date: Sun, 22 Jan 2023 00:11:05 +0300 Subject: [PATCH 07/37] PLMS edge-case handling fix 5 --- modules/processing.py | 2 -- modules/txt2img.py | 2 +- modules/ui.py | 2 +- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/modules/processing.py b/modules/processing.py index 6f6efe06..a873498b 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -866,8 +866,6 @@ class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing): if self.hr_sampler == '---': pass - elif self.hr_sampler == 'PLMS': - img2img_sampler_name = 'DDIM' else: img2img_sampler_name = self.hr_sampler diff --git a/modules/txt2img.py b/modules/txt2img.py index 9c8ec621..c6ea11c2 100644 --- a/modules/txt2img.py +++ b/modules/txt2img.py @@ -38,7 +38,7 @@ def txt2img(id_task: str, prompt: str, negative_prompt: str, prompt_styles, step hr_second_pass_steps=hr_second_pass_steps, hr_resize_x=hr_resize_x, hr_resize_y=hr_resize_y, - hr_sampler=sd_samplers.samplers[hr_sampler_index - 1].name + hr_sampler=sd_samplers.samplers_for_img2img[hr_sampler_index - 1].name if hr_sampler_index != 0 else '---' ) diff --git a/modules/ui.py b/modules/ui.py index 6bbbc0f6..b408379f 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -673,7 +673,7 @@ def create_ui(): hr_resize_y = gr.Slider(minimum=0, maximum=2048, step=8, label="Resize height to", value=0, elem_id="txt2img_hr_resize_y") with FormRow(elem_id="txt2img_hires_fix_row3", variant="compact"): - hr_sampler_index = gr.Dropdown(label='Hires sampling method', elem_id=f"hr_sampler", choices=["---"] + [x.name for x in samplers], value="---", type="index") + hr_sampler_index = gr.Dropdown(label='Hires sampling method', elem_id=f"hr_sampler", choices=["---"] + [x.name for x in samplers_for_img2img], value="---", type="index") elif category == "batch": if not opts.dimensions_and_batch_together: From f7b38c484151a50a75788094d9cd357dd4841966 Mon Sep 17 00:00:00 2001 From: invincibledude <> Date: Sun, 22 Jan 2023 00:18:26 +0300 Subject: [PATCH 08/37] Style fix --- modules/txt2img.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/modules/txt2img.py b/modules/txt2img.py index c6ea11c2..2eb41f3d 100644 --- a/modules/txt2img.py +++ b/modules/txt2img.py @@ -38,8 +38,7 @@ def txt2img(id_task: str, prompt: str, negative_prompt: str, prompt_styles, step hr_second_pass_steps=hr_second_pass_steps, hr_resize_x=hr_resize_x, hr_resize_y=hr_resize_y, - hr_sampler=sd_samplers.samplers_for_img2img[hr_sampler_index - 1].name - if hr_sampler_index != 0 else '---' + hr_sampler=sd_samplers.samplers_for_img2img[hr_sampler_index - 1].name if hr_sampler_index != 0 else '---' ) p.scripts = modules.scripts.scripts_txt2img From 35b4104dafa97de913f563a8658a23b18a20859e Mon Sep 17 00:00:00 2001 From: InvincibleDude <81354513+InvincibleDude@users.noreply.github.com> Date: Sun, 22 Jan 2023 00:32:48 +0300 Subject: [PATCH 09/37] Change to run workflow --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9c0cd1ef..7c3375cf 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Stable Diffusion web UI +# Stable Diffusion web U A browser interface based on Gradio library for Stable Diffusion. ![](screenshot.png) From cd14e7e8fd6321658c132de5748c8bc9a34840af Mon Sep 17 00:00:00 2001 From: InvincibleDude <81354513+InvincibleDude@users.noreply.github.com> Date: Sun, 22 Jan 2023 00:33:21 +0300 Subject: [PATCH 10/37] Revert --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7c3375cf..9c0cd1ef 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Stable Diffusion web U +# Stable Diffusion web UI A browser interface based on Gradio library for Stable Diffusion. ![](screenshot.png) From 8114959e7e937361b719cd85bec246ffd258dca6 Mon Sep 17 00:00:00 2001 From: invincibledude <> Date: Sun, 22 Jan 2023 14:28:53 +0300 Subject: [PATCH 11/37] Hr separate prompt test --- modules/processing.py | 23 ++++++++++++++++++++++- modules/txt2img.py | 6 ++++-- modules/ui.py | 10 ++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/modules/processing.py b/modules/processing.py index a873498b..f407e175 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -516,6 +516,23 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: else: p.all_negative_prompts = p.batch_size * p.n_iter * [shared.prompt_styles.apply_negative_styles_to_prompt(p.negative_prompt, p.styles)] + if type(p) == StableDiffusionProcessingTxt2Img: + if p.hr_enabled and p.is_hr_pass: + if p.hr_prompt: + if type(p.prompt) == list: + p.all_prompts = [shared.prompt_styles.apply_styles_to_prompt(x, p.styles) for x in p.hr_prompt] + else: + p.all_prompts = p.batch_size * p.n_iter * [ + shared.prompt_styles.apply_styles_to_prompt(p.hr_prompt, p.styles)] + + if p.hr_negative_prompt: + if type(p.negative_prompt) == list: + p.all_negative_prompts = [shared.prompt_styles.apply_negative_styles_to_prompt(x, p.styles) for x in + p.hr_negative_prompt] + else: + p.all_negative_prompts = p.batch_size * p.n_iter * [ + shared.prompt_styles.apply_negative_styles_to_prompt(p.hr_negative_prompt, p.styles)] + if type(seed) == list: p.all_seeds = seed else: @@ -710,7 +727,7 @@ def old_hires_fix_first_pass_dimensions(width, height): class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing): sampler = None - def __init__(self, enable_hr: bool = False, denoising_strength: float = 0.75, firstphase_width: int = 0, firstphase_height: int = 0, hr_scale: float = 2.0, hr_upscaler: str = None, hr_second_pass_steps: int = 0, hr_resize_x: int = 0, hr_resize_y: int = 0, hr_sampler: str = '---', **kwargs): + def __init__(self, enable_hr: bool = False, denoising_strength: float = 0.75, firstphase_width: int = 0, firstphase_height: int = 0, hr_scale: float = 2.0, hr_upscaler: str = None, hr_second_pass_steps: int = 0, hr_resize_x: int = 0, hr_resize_y: int = 0, hr_sampler: str = '---', hr_prompt: str = '', hr_negative_prompt: str = '', **kwargs): super().__init__(**kwargs) self.enable_hr = enable_hr self.denoising_strength = denoising_strength @@ -722,6 +739,9 @@ class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing): self.hr_upscale_to_x = hr_resize_x self.hr_upscale_to_y = hr_resize_y self.hr_sampler = hr_sampler + self.hr_prompt = hr_prompt if hr_prompt != '' else self.prompt + self.hr_negative_prompt = hr_negative_prompt if hr_negative_prompt != '' else self.negative_prompt + self.is_hr_pass = False if firstphase_width != 0 or firstphase_height != 0: self.hr_upscale_to_x = self.width @@ -808,6 +828,7 @@ class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing): if not self.enable_hr: return samples + self.is_hr_pass = True target_width = self.hr_upscale_to_x target_height = self.hr_upscale_to_y diff --git a/modules/txt2img.py b/modules/txt2img.py index 2eb41f3d..c06f9f9d 100644 --- a/modules/txt2img.py +++ b/modules/txt2img.py @@ -8,7 +8,7 @@ import modules.processing as processing from modules.ui import plaintext_to_html -def txt2img(id_task: str, prompt: str, negative_prompt: str, prompt_styles, steps: int, sampler_index: int, restore_faces: bool, tiling: bool, n_iter: int, batch_size: int, cfg_scale: float, seed: int, subseed: int, subseed_strength: float, seed_resize_from_h: int, seed_resize_from_w: int, seed_enable_extras: bool, height: int, width: int, enable_hr: bool, denoising_strength: float, hr_scale: float, hr_upscaler: str, hr_second_pass_steps: int, hr_resize_x: int, hr_resize_y: int, hr_sampler_index: int, *args): +def txt2img(id_task: str, prompt: str, negative_prompt: str, prompt_styles, steps: int, sampler_index: int, restore_faces: bool, tiling: bool, n_iter: int, batch_size: int, cfg_scale: float, seed: int, subseed: int, subseed_strength: float, seed_resize_from_h: int, seed_resize_from_w: int, seed_enable_extras: bool, height: int, width: int, enable_hr: bool, denoising_strength: float, hr_scale: float, hr_upscaler: str, hr_second_pass_steps: int, hr_resize_x: int, hr_resize_y: int, hr_sampler_index: int, hr_prompt: str, hr_negative_prompt, *args): p = StableDiffusionProcessingTxt2Img( sd_model=shared.sd_model, outpath_samples=opts.outdir_samples or opts.outdir_txt2img_samples, @@ -38,7 +38,9 @@ def txt2img(id_task: str, prompt: str, negative_prompt: str, prompt_styles, step hr_second_pass_steps=hr_second_pass_steps, hr_resize_x=hr_resize_x, hr_resize_y=hr_resize_y, - hr_sampler=sd_samplers.samplers_for_img2img[hr_sampler_index - 1].name if hr_sampler_index != 0 else '---' + hr_sampler=sd_samplers.samplers_for_img2img[hr_sampler_index - 1].name if hr_sampler_index != 0 else '---', + hr_prompt=hr_prompt, + hr_negative_prompt=hr_negative_prompt ) p.scripts = modules.scripts.scripts_txt2img diff --git a/modules/ui.py b/modules/ui.py index b408379f..e0a9e40b 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -675,6 +675,14 @@ def create_ui(): with FormRow(elem_id="txt2img_hires_fix_row3", variant="compact"): hr_sampler_index = gr.Dropdown(label='Hires sampling method', elem_id=f"hr_sampler", choices=["---"] + [x.name for x in samplers_for_img2img], value="---", type="index") + with FormRow(elem_id="txt2img_hires_fix_row4", variant="compact"): + with gr.Column(scale=80): + with gr.Row(): + hr_prompt = gr.Textbox(label="Prompt", elem_id=f"hires_prompt", show_label=False, lines=3, placeholder="Prompt that will be used for hires fix pass") + with gr.Column(scale=80): + with gr.Row(): + hr_negative_prompt = gr.Textbox(label="Negative prompt", elem_id=f"hires_neg_prompt", show_label=False, lines=3, placeholder="Negative prompt that will be used for hires fix pass") + elif category == "batch": if not opts.dimensions_and_batch_together: with FormRow(elem_id="txt2img_column_batch"): @@ -734,6 +742,8 @@ def create_ui(): hr_resize_x, hr_resize_y, hr_sampler_index, + hr_prompt, + hr_negative_prompt, ] + custom_inputs, outputs=[ From b331ca784ad831de088ad3915b45da39c2a76fa8 Mon Sep 17 00:00:00 2001 From: invincibledude <> Date: Sun, 22 Jan 2023 14:35:34 +0300 Subject: [PATCH 12/37] Fix --- modules/processing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/processing.py b/modules/processing.py index f407e175..161999d5 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -517,7 +517,7 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: p.all_negative_prompts = p.batch_size * p.n_iter * [shared.prompt_styles.apply_negative_styles_to_prompt(p.negative_prompt, p.styles)] if type(p) == StableDiffusionProcessingTxt2Img: - if p.hr_enabled and p.is_hr_pass: + if p.enable_hr and p.is_hr_pass: if p.hr_prompt: if type(p.prompt) == list: p.all_prompts = [shared.prompt_styles.apply_styles_to_prompt(x, p.styles) for x in p.hr_prompt] From 81e0723d6559729f5b207ae04bc615318af5a11e Mon Sep 17 00:00:00 2001 From: invincibledude <> Date: Sun, 22 Jan 2023 14:41:41 +0300 Subject: [PATCH 13/37] Logging for debugging --- modules/processing.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/modules/processing.py b/modules/processing.py index 161999d5..eeab4b0c 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -518,12 +518,14 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: if type(p) == StableDiffusionProcessingTxt2Img: if p.enable_hr and p.is_hr_pass: + logging.info("Running hr pass with custom prompt") if p.hr_prompt: if type(p.prompt) == list: p.all_prompts = [shared.prompt_styles.apply_styles_to_prompt(x, p.styles) for x in p.hr_prompt] else: p.all_prompts = p.batch_size * p.n_iter * [ shared.prompt_styles.apply_styles_to_prompt(p.hr_prompt, p.styles)] + logging.info(p.all_prompts) if p.hr_negative_prompt: if type(p.negative_prompt) == list: @@ -532,6 +534,7 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: else: p.all_negative_prompts = p.batch_size * p.n_iter * [ shared.prompt_styles.apply_negative_styles_to_prompt(p.hr_negative_prompt, p.styles)] + logging.info(p.all_negative_prompts) if type(seed) == list: p.all_seeds = seed From f774a8d24ec57cf0b795fedb0c54f0304b43b4d9 Mon Sep 17 00:00:00 2001 From: invincibledude <> Date: Sun, 22 Jan 2023 14:52:01 +0300 Subject: [PATCH 14/37] Hr-fix separate prompt experimentation --- modules/processing.py | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/modules/processing.py b/modules/processing.py index eeab4b0c..1133619f 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -516,25 +516,25 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: else: p.all_negative_prompts = p.batch_size * p.n_iter * [shared.prompt_styles.apply_negative_styles_to_prompt(p.negative_prompt, p.styles)] - if type(p) == StableDiffusionProcessingTxt2Img: - if p.enable_hr and p.is_hr_pass: - logging.info("Running hr pass with custom prompt") - if p.hr_prompt: - if type(p.prompt) == list: - p.all_prompts = [shared.prompt_styles.apply_styles_to_prompt(x, p.styles) for x in p.hr_prompt] - else: - p.all_prompts = p.batch_size * p.n_iter * [ - shared.prompt_styles.apply_styles_to_prompt(p.hr_prompt, p.styles)] - logging.info(p.all_prompts) - - if p.hr_negative_prompt: - if type(p.negative_prompt) == list: - p.all_negative_prompts = [shared.prompt_styles.apply_negative_styles_to_prompt(x, p.styles) for x in - p.hr_negative_prompt] - else: - p.all_negative_prompts = p.batch_size * p.n_iter * [ - shared.prompt_styles.apply_negative_styles_to_prompt(p.hr_negative_prompt, p.styles)] - logging.info(p.all_negative_prompts) + # if type(p) == StableDiffusionProcessingTxt2Img: + # if p.enable_hr and p.is_hr_pass: + # logging.info("Running hr pass with custom prompt") + # if p.hr_prompt: + # if type(p.prompt) == list: + # p.all_prompts = [shared.prompt_styles.apply_styles_to_prompt(x, p.styles) for x in p.hr_prompt] + # else: + # p.all_prompts = p.batch_size * p.n_iter * [ + # shared.prompt_styles.apply_styles_to_prompt(p.hr_prompt, p.styles)] + # logging.info(p.all_prompts) + # + # if p.hr_negative_prompt: + # if type(p.negative_prompt) == list: + # p.all_negative_prompts = [shared.prompt_styles.apply_negative_styles_to_prompt(x, p.styles) for x in + # p.hr_negative_prompt] + # else: + # p.all_negative_prompts = p.batch_size * p.n_iter * [ + # shared.prompt_styles.apply_negative_styles_to_prompt(p.hr_negative_prompt, p.styles)] + # logging.info(p.all_negative_prompts) if type(seed) == list: p.all_seeds = seed @@ -744,7 +744,6 @@ class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing): self.hr_sampler = hr_sampler self.hr_prompt = hr_prompt if hr_prompt != '' else self.prompt self.hr_negative_prompt = hr_negative_prompt if hr_negative_prompt != '' else self.negative_prompt - self.is_hr_pass = False if firstphase_width != 0 or firstphase_height != 0: self.hr_upscale_to_x = self.width @@ -831,7 +830,9 @@ class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing): if not self.enable_hr: return samples - self.is_hr_pass = True + self.prompt = self.hr_prompt + self.negative_prompt = self.hr_negative_prompt + target_width = self.hr_upscale_to_x target_height = self.hr_upscale_to_y From a9f0e7d53611cf11331e2befd34f0351b47795ee Mon Sep 17 00:00:00 2001 From: invincibledude <> Date: Sun, 22 Jan 2023 15:12:00 +0300 Subject: [PATCH 15/37] hr conditioning --- modules/processing.py | 72 +++++++++++++++++++++++++++---------------- 1 file changed, 46 insertions(+), 26 deletions(-) diff --git a/modules/processing.py b/modules/processing.py index 1133619f..21886bb5 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -235,7 +235,7 @@ class StableDiffusionProcessing: def init(self, all_prompts, all_seeds, all_subseeds): pass - def sample(self, conditioning, unconditional_conditioning, seeds, subseeds, subseed_strength, prompts): + def sample(self, conditioning, unconditional_conditioning, hr_conditioning, hr_uconditional_conditioning, seeds, subseeds, subseed_strength, prompts): raise NotImplementedError() def close(self): @@ -516,25 +516,25 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: else: p.all_negative_prompts = p.batch_size * p.n_iter * [shared.prompt_styles.apply_negative_styles_to_prompt(p.negative_prompt, p.styles)] - # if type(p) == StableDiffusionProcessingTxt2Img: - # if p.enable_hr and p.is_hr_pass: - # logging.info("Running hr pass with custom prompt") - # if p.hr_prompt: - # if type(p.prompt) == list: - # p.all_prompts = [shared.prompt_styles.apply_styles_to_prompt(x, p.styles) for x in p.hr_prompt] - # else: - # p.all_prompts = p.batch_size * p.n_iter * [ - # shared.prompt_styles.apply_styles_to_prompt(p.hr_prompt, p.styles)] - # logging.info(p.all_prompts) - # - # if p.hr_negative_prompt: - # if type(p.negative_prompt) == list: - # p.all_negative_prompts = [shared.prompt_styles.apply_negative_styles_to_prompt(x, p.styles) for x in - # p.hr_negative_prompt] - # else: - # p.all_negative_prompts = p.batch_size * p.n_iter * [ - # shared.prompt_styles.apply_negative_styles_to_prompt(p.hr_negative_prompt, p.styles)] - # logging.info(p.all_negative_prompts) + if type(p) == StableDiffusionProcessingTxt2Img: + if p.enable_hr and p.is_hr_pass: + logging.info("Running hr pass with custom prompt") + if p.hr_prompt: + if type(p.prompt) == list: + p.all_hr_prompts = [shared.prompt_styles.apply_styles_to_prompt(x, p.styles) for x in p.hr_prompt] + else: + p.all_hr_prompts = p.batch_size * p.n_iter * [ + shared.prompt_styles.apply_styles_to_prompt(p.hr_prompt, p.styles)] + logging.info(p.all_prompts) + + if p.hr_negative_prompt: + if type(p.negative_prompt) == list: + p.all_hr_negative_prompts = [shared.prompt_styles.apply_negative_styles_to_prompt(x, p.styles) for x in + p.hr_negative_prompt] + else: + p.all_hr_negative_prompts = p.batch_size * p.n_iter * [ + shared.prompt_styles.apply_negative_styles_to_prompt(p.hr_negative_prompt, p.styles)] + logging.info(p.all_negative_prompts) if type(seed) == list: p.all_seeds = seed @@ -607,6 +607,12 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: prompts = p.all_prompts[n * p.batch_size:(n + 1) * p.batch_size] negative_prompts = p.all_negative_prompts[n * p.batch_size:(n + 1) * p.batch_size] + + if type(p) == StableDiffusionProcessingTxt2Img: + if p.enable_hr: + hr_prompts = p.all_hr_prompts[n * p.batch_size:(n + 1) * p.batch_size] + hr_negative_prompts = p.all_negative_prompts[n * p.batch_size:(n + 1) * p.batch_size] + seeds = p.all_seeds[n * p.batch_size:(n + 1) * p.batch_size] subseeds = p.all_subseeds[n * p.batch_size:(n + 1) * p.batch_size] @@ -620,6 +626,12 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: uc = get_conds_with_caching(prompt_parser.get_learned_conditioning, negative_prompts, p.steps, cached_uc) c = get_conds_with_caching(prompt_parser.get_multicond_learned_conditioning, prompts, p.steps, cached_c) + if type(p) == StableDiffusionProcessingTxt2Img: + if p.enable_hr: + hr_uc = get_conds_with_caching(prompt_parser.get_learned_conditioning, negative_prompts, p.steps, + cached_uc) + hr_c = get_conds_with_caching(prompt_parser.get_multicond_learned_conditioning, prompts, p.steps, + cached_c) if len(model_hijack.comments) > 0: for comment in model_hijack.comments: @@ -629,7 +641,16 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: shared.state.job = f"Batch {n+1} out of {p.n_iter}" with devices.autocast(): - samples_ddim = p.sample(conditioning=c, unconditional_conditioning=uc, seeds=seeds, subseeds=subseeds, subseed_strength=p.subseed_strength, prompts=prompts) + if type(p) == StableDiffusionProcessingTxt2Img: + if p.enable_hr: + samples_ddim = p.sample(conditioning=c, unconditional_conditioning=uc, hr_conditioning=hr_c, hr_uconditional_conditioning=hr_uc, seeds=seeds, subseeds=subseeds, subseed_strength=p.subseed_strength, prompts=prompts) + samples_ddim = p.sample(conditioning=c, unconditional_conditioning=uc, seeds=seeds, + subseeds=subseeds, + subseed_strength=p.subseed_strength, prompts=prompts) + else: + samples_ddim = p.sample(conditioning=c, unconditional_conditioning=uc, seeds=seeds, + subseeds=subseeds, + subseed_strength=p.subseed_strength, prompts=prompts) x_samples_ddim = [decode_first_stage(p.sd_model, samples_ddim[i:i+1].to(dtype=devices.dtype_vae))[0].cpu() for i in range(samples_ddim.size(0))] for x in x_samples_ddim: @@ -744,6 +765,8 @@ class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing): self.hr_sampler = hr_sampler self.hr_prompt = hr_prompt if hr_prompt != '' else self.prompt self.hr_negative_prompt = hr_negative_prompt if hr_negative_prompt != '' else self.negative_prompt + self.all_hr_prompts = None + self.all_hr_negative_prompts = None if firstphase_width != 0 or firstphase_height != 0: self.hr_upscale_to_x = self.width @@ -817,7 +840,7 @@ class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing): if self.hr_upscaler is not None: self.extra_generation_params["Hires upscaler"] = self.hr_upscaler - def sample(self, conditioning, unconditional_conditioning, seeds, subseeds, subseed_strength, prompts): + def sample(self, conditioning, unconditional_conditioning, hr_conditioning, hr_uconditional_conditioning, seeds, subseeds, subseed_strength, prompts): self.sampler = sd_samplers.create_sampler(self.sampler_name, self.sd_model) latent_scale_mode = shared.latent_upscale_modes.get(self.hr_upscaler, None) if self.hr_upscaler is not None else shared.latent_upscale_modes.get(shared.latent_upscale_default_mode, "nearest") @@ -830,9 +853,6 @@ class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing): if not self.enable_hr: return samples - self.prompt = self.hr_prompt - self.negative_prompt = self.hr_negative_prompt - target_width = self.hr_upscale_to_x target_height = self.hr_upscale_to_y @@ -904,7 +924,7 @@ class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing): x = None devices.torch_gc() - samples = self.sampler.sample_img2img(self, samples, noise, conditioning, unconditional_conditioning, steps=self.hr_second_pass_steps or self.steps, image_conditioning=image_conditioning) + samples = self.sampler.sample_img2img(self, samples, noise, hr_conditioning, hr_unconditional_conditioning, steps=self.hr_second_pass_steps or self.steps, image_conditioning=image_conditioning) return samples From 4e0cf7d4eda3d590df3e4b28654037d2dfb7ab94 Mon Sep 17 00:00:00 2001 From: invincibledude <> Date: Sun, 22 Jan 2023 15:15:08 +0300 Subject: [PATCH 16/37] hr conditioning --- modules/processing.py | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/modules/processing.py b/modules/processing.py index 21886bb5..6b49b4e3 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -517,24 +517,16 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: p.all_negative_prompts = p.batch_size * p.n_iter * [shared.prompt_styles.apply_negative_styles_to_prompt(p.negative_prompt, p.styles)] if type(p) == StableDiffusionProcessingTxt2Img: - if p.enable_hr and p.is_hr_pass: - logging.info("Running hr pass with custom prompt") - if p.hr_prompt: - if type(p.prompt) == list: - p.all_hr_prompts = [shared.prompt_styles.apply_styles_to_prompt(x, p.styles) for x in p.hr_prompt] - else: - p.all_hr_prompts = p.batch_size * p.n_iter * [ - shared.prompt_styles.apply_styles_to_prompt(p.hr_prompt, p.styles)] - logging.info(p.all_prompts) + if p.enable_hr: + if type(p.prompt) == list: + p.all_hr_prompts = [shared.prompt_styles.apply_styles_to_prompt(x, p.styles) for x in p.hr_prompt] + else: + p.all_hr_prompts = p.batch_size * p.n_iter * [shared.prompt_styles.apply_styles_to_prompt(p.hr_prompt, p.styles)] - if p.hr_negative_prompt: - if type(p.negative_prompt) == list: - p.all_hr_negative_prompts = [shared.prompt_styles.apply_negative_styles_to_prompt(x, p.styles) for x in - p.hr_negative_prompt] - else: - p.all_hr_negative_prompts = p.batch_size * p.n_iter * [ - shared.prompt_styles.apply_negative_styles_to_prompt(p.hr_negative_prompt, p.styles)] - logging.info(p.all_negative_prompts) + if type(p.negative_prompt) == list: + p.all_hr_negative_prompts = [shared.prompt_styles.apply_negative_styles_to_prompt(x, p.styles) for x in p.hr_negative_prompt] + else: + p.all_hr_negative_prompts = p.batch_size * p.n_iter * [shared.prompt_styles.apply_negative_styles_to_prompt(p.hr_negative_prompt, p.styles)] if type(seed) == list: p.all_seeds = seed @@ -628,9 +620,9 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: c = get_conds_with_caching(prompt_parser.get_multicond_learned_conditioning, prompts, p.steps, cached_c) if type(p) == StableDiffusionProcessingTxt2Img: if p.enable_hr: - hr_uc = get_conds_with_caching(prompt_parser.get_learned_conditioning, negative_prompts, p.steps, + hr_uc = get_conds_with_caching(prompt_parser.get_learned_conditioning, hr_negative_prompts, p.steps, cached_uc) - hr_c = get_conds_with_caching(prompt_parser.get_multicond_learned_conditioning, prompts, p.steps, + hr_c = get_conds_with_caching(prompt_parser.get_multicond_learned_conditioning, hr_prompts, p.steps, cached_c) if len(model_hijack.comments) > 0: @@ -840,7 +832,7 @@ class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing): if self.hr_upscaler is not None: self.extra_generation_params["Hires upscaler"] = self.hr_upscaler - def sample(self, conditioning, unconditional_conditioning, hr_conditioning, hr_uconditional_conditioning, seeds, subseeds, subseed_strength, prompts): + def sample(self, conditioning, unconditional_conditioning, hr_conditioning, hr_unconditional_conditioning, seeds, subseeds, subseed_strength, prompts): self.sampler = sd_samplers.create_sampler(self.sampler_name, self.sd_model) latent_scale_mode = shared.latent_upscale_modes.get(self.hr_upscaler, None) if self.hr_upscaler is not None else shared.latent_upscale_modes.get(shared.latent_upscale_default_mode, "nearest") From c5d4c87c02ce4c503de149db5ee3e87af4402cf0 Mon Sep 17 00:00:00 2001 From: invincibledude <> Date: Sun, 22 Jan 2023 15:17:43 +0300 Subject: [PATCH 17/37] hr conditioning --- modules/processing.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/modules/processing.py b/modules/processing.py index 6b49b4e3..a81ace6f 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -635,10 +635,11 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: with devices.autocast(): if type(p) == StableDiffusionProcessingTxt2Img: if p.enable_hr: - samples_ddim = p.sample(conditioning=c, unconditional_conditioning=uc, hr_conditioning=hr_c, hr_uconditional_conditioning=hr_uc, seeds=seeds, subseeds=subseeds, subseed_strength=p.subseed_strength, prompts=prompts) - samples_ddim = p.sample(conditioning=c, unconditional_conditioning=uc, seeds=seeds, - subseeds=subseeds, - subseed_strength=p.subseed_strength, prompts=prompts) + samples_ddim = p.sample(conditioning=c, unconditional_conditioning=uc, hr_conditioning=hr_c, hr_unconditional_conditioning=hr_uc, seeds=seeds, subseeds=subseeds, subseed_strength=p.subseed_strength, prompts=prompts) + else: + samples_ddim = p.sample(conditioning=c, unconditional_conditioning=uc, seeds=seeds, subseeds=subseeds, + subseed_strength=p.subseed_strength, prompts=prompts) + else: samples_ddim = p.sample(conditioning=c, unconditional_conditioning=uc, seeds=seeds, subseeds=subseeds, From 2ab2bce74d7b7affb80b55da9d1ffb219129c1ef Mon Sep 17 00:00:00 2001 From: invincibledude <> Date: Sun, 22 Jan 2023 15:28:38 +0300 Subject: [PATCH 18/37] hr conditioning --- modules/processing.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/modules/processing.py b/modules/processing.py index a81ace6f..bab1c101 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -235,7 +235,7 @@ class StableDiffusionProcessing: def init(self, all_prompts, all_seeds, all_subseeds): pass - def sample(self, conditioning, unconditional_conditioning, hr_conditioning, hr_uconditional_conditioning, seeds, subseeds, subseed_strength, prompts): + def sample(self, conditioning, unconditional_conditioning, hr_conditioning=None, hr_unconditional_conditioning=None, seeds, subseeds, subseed_strength, prompts): raise NotImplementedError() def close(self): @@ -517,7 +517,7 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: p.all_negative_prompts = p.batch_size * p.n_iter * [shared.prompt_styles.apply_negative_styles_to_prompt(p.negative_prompt, p.styles)] if type(p) == StableDiffusionProcessingTxt2Img: - if p.enable_hr: + if p.enable_hr and p.hr_prompt != '': if type(p.prompt) == list: p.all_hr_prompts = [shared.prompt_styles.apply_styles_to_prompt(x, p.styles) for x in p.hr_prompt] else: @@ -601,7 +601,7 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: negative_prompts = p.all_negative_prompts[n * p.batch_size:(n + 1) * p.batch_size] if type(p) == StableDiffusionProcessingTxt2Img: - if p.enable_hr: + if p.enable_hr and p.hr_prompt != '': hr_prompts = p.all_hr_prompts[n * p.batch_size:(n + 1) * p.batch_size] hr_negative_prompts = p.all_negative_prompts[n * p.batch_size:(n + 1) * p.batch_size] @@ -619,7 +619,7 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: uc = get_conds_with_caching(prompt_parser.get_learned_conditioning, negative_prompts, p.steps, cached_uc) c = get_conds_with_caching(prompt_parser.get_multicond_learned_conditioning, prompts, p.steps, cached_c) if type(p) == StableDiffusionProcessingTxt2Img: - if p.enable_hr: + if p.enable_hr and p.hr_prompt != '': hr_uc = get_conds_with_caching(prompt_parser.get_learned_conditioning, hr_negative_prompts, p.steps, cached_uc) hr_c = get_conds_with_caching(prompt_parser.get_multicond_learned_conditioning, hr_prompts, p.steps, @@ -635,7 +635,12 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: with devices.autocast(): if type(p) == StableDiffusionProcessingTxt2Img: if p.enable_hr: - samples_ddim = p.sample(conditioning=c, unconditional_conditioning=uc, hr_conditioning=hr_c, hr_unconditional_conditioning=hr_uc, seeds=seeds, subseeds=subseeds, subseed_strength=p.subseed_strength, prompts=prompts) + if p.hr_prompts != '': + samples_ddim = p.sample(conditioning=c, unconditional_conditioning=uc, hr_conditioning=hr_c, hr_unconditional_conditioning=hr_uc, seeds=seeds, subseeds=subseeds, subseed_strength=p.subseed_strength, prompts=prompts) + else: + samples_ddim = p.sample(conditioning=c, unconditional_conditioning=uc, hr_conditioning=c, + hr_unconditional_conditioning=uc, seeds=seeds, subseeds=subseeds, + subseed_strength=p.subseed_strength, prompts=prompts) else: samples_ddim = p.sample(conditioning=c, unconditional_conditioning=uc, seeds=seeds, subseeds=subseeds, subseed_strength=p.subseed_strength, prompts=prompts) @@ -756,8 +761,8 @@ class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing): self.hr_upscale_to_x = hr_resize_x self.hr_upscale_to_y = hr_resize_y self.hr_sampler = hr_sampler - self.hr_prompt = hr_prompt if hr_prompt != '' else self.prompt - self.hr_negative_prompt = hr_negative_prompt if hr_negative_prompt != '' else self.negative_prompt + self.hr_prompt = hr_prompt if hr_prompt != '' else '' + self.hr_negative_prompt = hr_negative_prompt if hr_negative_prompt != '' else '' self.all_hr_prompts = None self.all_hr_negative_prompts = None From 125d5c8d96eb374a7ba214dba51da9cf30da6564 Mon Sep 17 00:00:00 2001 From: invincibledude <> Date: Sun, 22 Jan 2023 15:31:11 +0300 Subject: [PATCH 19/37] hr conditioning --- modules/processing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/processing.py b/modules/processing.py index bab1c101..f0073d1a 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -235,7 +235,7 @@ class StableDiffusionProcessing: def init(self, all_prompts, all_seeds, all_subseeds): pass - def sample(self, conditioning, unconditional_conditioning, hr_conditioning=None, hr_unconditional_conditioning=None, seeds, subseeds, subseed_strength, prompts): + def sample(self, conditioning, unconditional_conditioning, seeds, subseeds, subseed_strength, prompts, hr_conditioning=None, hr_unconditional_conditioning=None): raise NotImplementedError() def close(self): @@ -838,7 +838,7 @@ class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing): if self.hr_upscaler is not None: self.extra_generation_params["Hires upscaler"] = self.hr_upscaler - def sample(self, conditioning, unconditional_conditioning, hr_conditioning, hr_unconditional_conditioning, seeds, subseeds, subseed_strength, prompts): + def sample(self, conditioning, unconditional_conditioning, seeds, subseeds, subseed_strength, prompts, hr_conditioning=None, hr_unconditional_conditioning=None): self.sampler = sd_samplers.create_sampler(self.sampler_name, self.sd_model) latent_scale_mode = shared.latent_upscale_modes.get(self.hr_upscaler, None) if self.hr_upscaler is not None else shared.latent_upscale_modes.get(shared.latent_upscale_default_mode, "nearest") From 34f6d667429ec3d4e0f09c6148fd37e66c460cb7 Mon Sep 17 00:00:00 2001 From: invincibledude <> Date: Sun, 22 Jan 2023 15:32:47 +0300 Subject: [PATCH 20/37] hr conditioning --- modules/processing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/processing.py b/modules/processing.py index f0073d1a..b2de8f92 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -635,7 +635,7 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: with devices.autocast(): if type(p) == StableDiffusionProcessingTxt2Img: if p.enable_hr: - if p.hr_prompts != '': + if p.hr_prompt != '': samples_ddim = p.sample(conditioning=c, unconditional_conditioning=uc, hr_conditioning=hr_c, hr_unconditional_conditioning=hr_uc, seeds=seeds, subseeds=subseeds, subseed_strength=p.subseed_strength, prompts=prompts) else: samples_ddim = p.sample(conditioning=c, unconditional_conditioning=uc, hr_conditioning=c, From b0ae92d605dfb65314b610589f84b643d535261e Mon Sep 17 00:00:00 2001 From: invincibledude <> Date: Sun, 22 Jan 2023 15:43:12 +0300 Subject: [PATCH 21/37] UI improvements --- modules/ui.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/ui.py b/modules/ui.py index e0a9e40b..1656e76d 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -678,10 +678,10 @@ def create_ui(): with FormRow(elem_id="txt2img_hires_fix_row4", variant="compact"): with gr.Column(scale=80): with gr.Row(): - hr_prompt = gr.Textbox(label="Prompt", elem_id=f"hires_prompt", show_label=False, lines=3, placeholder="Prompt that will be used for hires fix pass") + hr_prompt = gr.Textbox(label="Prompt", elem_id=f"hires_prompt", show_label=False, lines=3, placeholder="Prompt that will be used for hires fix pass (leave it blank to use the same prompt as in initial txt2img gen)") with gr.Column(scale=80): with gr.Row(): - hr_negative_prompt = gr.Textbox(label="Negative prompt", elem_id=f"hires_neg_prompt", show_label=False, lines=3, placeholder="Negative prompt that will be used for hires fix pass") + hr_negative_prompt = gr.Textbox(label="Negative prompt", elem_id=f"hires_neg_prompt", show_label=False, lines=3, placeholder="Negative prompt that will be used for hires fix pass (leave it blank to use the same prompt as in initial txt2img gen)") elif category == "batch": if not opts.dimensions_and_batch_together: From bbb1e35ea29f8cfc6a6db3b28b10bfbc99033c70 Mon Sep 17 00:00:00 2001 From: invincibledude <> Date: Sun, 22 Jan 2023 15:44:59 +0300 Subject: [PATCH 22/37] UI and PNG info improvements --- modules/processing.py | 3 +++ modules/ui.py | 2 ++ 2 files changed, 5 insertions(+) diff --git a/modules/processing.py b/modules/processing.py index b2de8f92..0cf4771e 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -789,6 +789,9 @@ class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing): if self.hr_resize_x == 0 and self.hr_resize_y == 0: self.extra_generation_params["Hires upscale"] = self.hr_scale + self.extra_generation_params["Hires sampler"] = self.hr_sampler + self.extra_generation_params["Hires prompt"] = self.hr_prompt + self.extra_generation_params["Hires negative prompt"] = self.hr_negative_prompt self.hr_upscale_to_x = int(self.width * self.hr_scale) self.hr_upscale_to_y = int(self.height * self.hr_scale) else: diff --git a/modules/ui.py b/modules/ui.py index 1656e76d..64180889 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -800,6 +800,8 @@ def create_ui(): (hr_resize_x, "Hires resize-1"), (hr_resize_y, "Hires resize-2"), (hr_sampler_index, "Hires sampling method"), + (hr_prompt, "Hires prompt"), + (hr_negative_prompt, "Hires negative prompt"), *modules.scripts.scripts_txt2img.infotext_fields ] parameters_copypaste.add_paste_fields("txt2img", None, txt2img_paste_fields) From a5c2b5ed8991e0e1ffea7b595ab13f93715dfa5c Mon Sep 17 00:00:00 2001 From: invincibledude <> Date: Sun, 22 Jan 2023 15:50:20 +0300 Subject: [PATCH 23/37] UI and PNG info improvements --- modules/processing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/processing.py b/modules/processing.py index 0cf4771e..8b4b2399 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -790,8 +790,8 @@ class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing): if self.hr_resize_x == 0 and self.hr_resize_y == 0: self.extra_generation_params["Hires upscale"] = self.hr_scale self.extra_generation_params["Hires sampler"] = self.hr_sampler - self.extra_generation_params["Hires prompt"] = self.hr_prompt - self.extra_generation_params["Hires negative prompt"] = self.hr_negative_prompt + self.extra_generation_params["Hires prompt"] = f"({self.hr_prompt})" + self.extra_generation_params["Hires negative prompt"] = f"({self.hr_negative_prompt})" self.hr_upscale_to_x = int(self.width * self.hr_scale) self.hr_upscale_to_y = int(self.height * self.hr_scale) else: From 2aaee73633ef7a6d12561859ca2d58d0e10cf297 Mon Sep 17 00:00:00 2001 From: invincibledude <> Date: Sun, 22 Jan 2023 16:00:35 +0300 Subject: [PATCH 24/37] Gen params paste improvement --- modules/generation_parameters_copypaste.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/modules/generation_parameters_copypaste.py b/modules/generation_parameters_copypaste.py index 46e12dc6..b8212abd 100644 --- a/modules/generation_parameters_copypaste.py +++ b/modules/generation_parameters_copypaste.py @@ -253,6 +253,9 @@ Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 965400086, Size: 512x512, Model done_with_prompt = True line = line[16:].strip() + if line.startswith("Hires prompt:"): + res["Hires prompt"] = line[1:][:-1] + if done_with_prompt: negative_prompt += ("" if negative_prompt == "" else "\n") + line else: From 1fa777c1d77556d116d39144c47715f6b1325538 Mon Sep 17 00:00:00 2001 From: invincibledude <> Date: Sun, 22 Jan 2023 16:03:42 +0300 Subject: [PATCH 25/37] Gen params paste improvement --- modules/generation_parameters_copypaste.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/modules/generation_parameters_copypaste.py b/modules/generation_parameters_copypaste.py index b8212abd..67e76e30 100644 --- a/modules/generation_parameters_copypaste.py +++ b/modules/generation_parameters_copypaste.py @@ -256,6 +256,9 @@ Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 965400086, Size: 512x512, Model if line.startswith("Hires prompt:"): res["Hires prompt"] = line[1:][:-1] + if line.startswith("Hires negative prompt:"): + res["Hires negative prompt"] = line[1:][:-1] + if done_with_prompt: negative_prompt += ("" if negative_prompt == "" else "\n") + line else: From d261bec1ec5a185b6e57f73e8589b07d5d5bc4e8 Mon Sep 17 00:00:00 2001 From: invincibledude <> Date: Sun, 22 Jan 2023 16:14:28 +0300 Subject: [PATCH 26/37] Gen params paste improvement --- modules/generation_parameters_copypaste.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/modules/generation_parameters_copypaste.py b/modules/generation_parameters_copypaste.py index 67e76e30..155ab350 100644 --- a/modules/generation_parameters_copypaste.py +++ b/modules/generation_parameters_copypaste.py @@ -252,13 +252,6 @@ Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 965400086, Size: 512x512, Model if line.startswith("Negative prompt:"): done_with_prompt = True line = line[16:].strip() - - if line.startswith("Hires prompt:"): - res["Hires prompt"] = line[1:][:-1] - - if line.startswith("Hires negative prompt:"): - res["Hires negative prompt"] = line[1:][:-1] - if done_with_prompt: negative_prompt += ("" if negative_prompt == "" else "\n") + line else: @@ -274,6 +267,10 @@ Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 965400086, Size: 512x512, Model res[k+"-2"] = m.group(2) else: res[k] = v + if k.startswith("Hires prompt:"): + res["Hires prompt"] = v[1:][:-1] + elif k.startswith("Hires negative prompt:"): + res["Hires negative prompt"] = v[1:][:-1] # Missing CLIP skip means it was set to 1 (the default) if "Clip skip" not in res: From fccc39834a61509a2fb60196229cbea20539aabb Mon Sep 17 00:00:00 2001 From: invincibledude <> Date: Sun, 22 Jan 2023 16:17:55 +0300 Subject: [PATCH 27/37] Gen params paste improvement --- modules/generation_parameters_copypaste.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/generation_parameters_copypaste.py b/modules/generation_parameters_copypaste.py index 155ab350..44c8273f 100644 --- a/modules/generation_parameters_copypaste.py +++ b/modules/generation_parameters_copypaste.py @@ -267,9 +267,10 @@ Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 965400086, Size: 512x512, Model res[k+"-2"] = m.group(2) else: res[k] = v - if k.startswith("Hires prompt:"): + + if k.startswith("Hires prompt"): res["Hires prompt"] = v[1:][:-1] - elif k.startswith("Hires negative prompt:"): + elif k.startswith("Hires negative prompt"): res["Hires negative prompt"] = v[1:][:-1] # Missing CLIP skip means it was set to 1 (the default) From 7f62300f7d496501a730d28f3aaaee885513998b Mon Sep 17 00:00:00 2001 From: invincibledude <> Date: Sun, 22 Jan 2023 16:29:08 +0300 Subject: [PATCH 28/37] Gen params paste improvement --- modules/processing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/processing.py b/modules/processing.py index 8b4b2399..c56717f6 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -790,8 +790,8 @@ class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing): if self.hr_resize_x == 0 and self.hr_resize_y == 0: self.extra_generation_params["Hires upscale"] = self.hr_scale self.extra_generation_params["Hires sampler"] = self.hr_sampler - self.extra_generation_params["Hires prompt"] = f"({self.hr_prompt})" - self.extra_generation_params["Hires negative prompt"] = f"({self.hr_negative_prompt})" + self.extra_generation_params["Hires prompt"] = f'"{self.hr_prompt}"' + self.extra_generation_params["Hires negative prompt"] = f'"{self.hr_negative_prompt}"' self.hr_upscale_to_x = int(self.width * self.hr_scale) self.hr_upscale_to_y = int(self.height * self.hr_scale) else: From 3bc8ee998db5f461b8011a72e6f167012ccb8bc1 Mon Sep 17 00:00:00 2001 From: invincibledude <> Date: Sun, 22 Jan 2023 16:35:42 +0300 Subject: [PATCH 29/37] Gen params paste improvement --- modules/generation_parameters_copypaste.py | 4 ++-- modules/processing.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/generation_parameters_copypaste.py b/modules/generation_parameters_copypaste.py index 44c8273f..98098cc8 100644 --- a/modules/generation_parameters_copypaste.py +++ b/modules/generation_parameters_copypaste.py @@ -269,9 +269,9 @@ Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 965400086, Size: 512x512, Model res[k] = v if k.startswith("Hires prompt"): - res["Hires prompt"] = v[1:][:-1] + res["Hires prompt"] = v[1:][:-1].replace(';', ',') elif k.startswith("Hires negative prompt"): - res["Hires negative prompt"] = v[1:][:-1] + res["Hires negative prompt"] = v[1:][:-1].replace(';', ',') # Missing CLIP skip means it was set to 1 (the default) if "Clip skip" not in res: diff --git a/modules/processing.py b/modules/processing.py index c56717f6..01c1b53c 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -790,8 +790,8 @@ class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing): if self.hr_resize_x == 0 and self.hr_resize_y == 0: self.extra_generation_params["Hires upscale"] = self.hr_scale self.extra_generation_params["Hires sampler"] = self.hr_sampler - self.extra_generation_params["Hires prompt"] = f'"{self.hr_prompt}"' - self.extra_generation_params["Hires negative prompt"] = f'"{self.hr_negative_prompt}"' + self.extra_generation_params["Hires prompt"] = f'({self.hr_prompt.replace(",", ";")})' + self.extra_generation_params["Hires negative prompt"] = f'({self.hr_negative_prompt.replace(",", ";")})' self.hr_upscale_to_x = int(self.width * self.hr_scale) self.hr_upscale_to_y = int(self.height * self.hr_scale) else: From c92ec3a9255a8364f4212d5e7434da1785752737 Mon Sep 17 00:00:00 2001 From: invincibledude <> Date: Sun, 29 Jan 2023 19:07:00 +0300 Subject: [PATCH 30/37] Extra networks loading fix --- modules/processing.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/modules/processing.py b/modules/processing.py index 605c541e..da890fb3 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -555,6 +555,10 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: model_hijack.embedding_db.load_textual_inversion_embeddings() _, extra_network_data = extra_networks.parse_prompts(p.all_prompts[0:1]) + if type(p) == StableDiffusionProcessingTxt2Img: + if p.enable_hr and p.hr_prompt != '': + _, hr_extra_network_data = extra_networks.parse_prompts(p.all_hr_prompts[0:1]) + if p.scripts is not None: p.scripts.process(p) @@ -594,7 +598,11 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: sd_vae_approx.model() if not p.disable_extra_networks: - extra_networks.activate(p, extra_network_data) + if type(p) == StableDiffusionProcessingTxt2Img: + if p.enable_hr and p.hr_prompt != '': + extra_networks.activate(p, extra_network_data + hr_extra_network_data) + else: + extra_networks.activate(p, extra_network_data) with open(os.path.join(paths.data_path, "params.txt"), "w", encoding="utf8") as file: processed = Processed(p, [], p.seed, "") @@ -745,7 +753,11 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: images.save_image(grid, p.outpath_grids, "grid", p.all_seeds[0], p.all_prompts[0], opts.grid_format, info=infotext(), short_filename=not opts.grid_extended_filename, p=p, grid=True) if not p.disable_extra_networks: - extra_networks.deactivate(p, extra_network_data) + if type(p) == StableDiffusionProcessingTxt2Img: + if p.enable_hr and p.hr_prompt != '': + extra_networks.deactivate(p, extra_network_data + hr_extra_network_data) + else: + extra_networks.deactivate(p, extra_network_data) devices.torch_gc() From 6127d2ff1bc5c3bc81d876e6dbc9a60f79aec898 Mon Sep 17 00:00:00 2001 From: invincibledude <> Date: Sun, 29 Jan 2023 19:13:27 +0300 Subject: [PATCH 31/37] Extra networks loading fix --- modules/processing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/processing.py b/modules/processing.py index da890fb3..a56915c5 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -600,7 +600,7 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: if not p.disable_extra_networks: if type(p) == StableDiffusionProcessingTxt2Img: if p.enable_hr and p.hr_prompt != '': - extra_networks.activate(p, extra_network_data + hr_extra_network_data) + extra_networks.activate(p, extra_network_data | hr_extra_network_data) else: extra_networks.activate(p, extra_network_data) @@ -755,7 +755,7 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: if not p.disable_extra_networks: if type(p) == StableDiffusionProcessingTxt2Img: if p.enable_hr and p.hr_prompt != '': - extra_networks.deactivate(p, extra_network_data + hr_extra_network_data) + extra_networks.deactivate(p, extra_network_data | hr_extra_network_data) else: extra_networks.deactivate(p, extra_network_data) From 9beeef6267ecbcb16f8b24e0092194f0f00142a6 Mon Sep 17 00:00:00 2001 From: invincibledude <> Date: Sun, 29 Jan 2023 19:16:17 +0300 Subject: [PATCH 32/37] Extra networks loading fix --- modules/processing.py | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/modules/processing.py b/modules/processing.py index a56915c5..98e83c69 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -558,6 +558,7 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: if type(p) == StableDiffusionProcessingTxt2Img: if p.enable_hr and p.hr_prompt != '': _, hr_extra_network_data = extra_networks.parse_prompts(p.all_hr_prompts[0:1]) + extra_network_data.update(hr_extra_network_data) if p.scripts is not None: @@ -598,11 +599,7 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: sd_vae_approx.model() if not p.disable_extra_networks: - if type(p) == StableDiffusionProcessingTxt2Img: - if p.enable_hr and p.hr_prompt != '': - extra_networks.activate(p, extra_network_data | hr_extra_network_data) - else: - extra_networks.activate(p, extra_network_data) + extra_networks.activate(p, extra_network_data) with open(os.path.join(paths.data_path, "params.txt"), "w", encoding="utf8") as file: processed = Processed(p, [], p.seed, "") @@ -753,11 +750,7 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: images.save_image(grid, p.outpath_grids, "grid", p.all_seeds[0], p.all_prompts[0], opts.grid_format, info=infotext(), short_filename=not opts.grid_extended_filename, p=p, grid=True) if not p.disable_extra_networks: - if type(p) == StableDiffusionProcessingTxt2Img: - if p.enable_hr and p.hr_prompt != '': - extra_networks.deactivate(p, extra_network_data | hr_extra_network_data) - else: - extra_networks.deactivate(p, extra_network_data) + extra_networks.deactivate(p, extra_network_data) devices.torch_gc() From 425eab34641e8e378db610d6a2b23f3ababe1058 Mon Sep 17 00:00:00 2001 From: invincibledude <> Date: Sun, 29 Jan 2023 19:26:31 +0300 Subject: [PATCH 33/37] Extra network in hr abomination fix --- modules/processing.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/processing.py b/modules/processing.py index 98e83c69..a2a91a5b 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -558,7 +558,8 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: if type(p) == StableDiffusionProcessingTxt2Img: if p.enable_hr and p.hr_prompt != '': _, hr_extra_network_data = extra_networks.parse_prompts(p.all_hr_prompts[0:1]) - extra_network_data.update(hr_extra_network_data) + if p.all_hr_prompts != p.all_prompts: + extra_network_data.update(hr_extra_network_data) if p.scripts is not None: From c3bd113a0b969732a324aff415c49ea9ad434707 Mon Sep 17 00:00:00 2001 From: InvincibleDude <81354513+InvincibleDude@users.noreply.github.com> Date: Sun, 5 Feb 2023 15:24:41 +0000 Subject: [PATCH 34/37] Image info fix --- modules/processing.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/modules/processing.py b/modules/processing.py index 61e6dfcd..43fac21c 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -808,6 +808,13 @@ class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing): def init(self, all_prompts, all_seeds, all_subseeds): if self.enable_hr: + if self.hr_sampler != '---': + self.extra_generation_params["Hires sampler"] = self.hr_sampler + + if self.hr_prompt != '': + self.extra_generation_params["Hires prompt"] = f'({self.hr_prompt.replace(",", ";")})' + self.extra_generation_params["Hires negative prompt"] = f'({self.hr_negative_prompt.replace(",", ";")})' + if opts.use_old_hires_fix_width_height and self.applied_old_hires_behavior_to != (self.width, self.height): self.hr_resize_x = self.width self.hr_resize_y = self.height @@ -819,9 +826,6 @@ class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing): if self.hr_resize_x == 0 and self.hr_resize_y == 0: self.extra_generation_params["Hires upscale"] = self.hr_scale - self.extra_generation_params["Hires sampler"] = self.hr_sampler - self.extra_generation_params["Hires prompt"] = f'({self.hr_prompt.replace(",", ";")})' - self.extra_generation_params["Hires negative prompt"] = f'({self.hr_negative_prompt.replace(",", ";")})' self.hr_upscale_to_x = int(self.width * self.hr_scale) self.hr_upscale_to_y = int(self.height * self.hr_scale) else: From 51f81efb02876d24c9e6d844e8c0cbd2384f6514 Mon Sep 17 00:00:00 2001 From: InvincibleDude <81354513+InvincibleDude@users.noreply.github.com> Date: Wed, 1 Mar 2023 21:30:20 +0300 Subject: [PATCH 35/37] Image processing changes Image processing changes --- modules/processing.py | 76 +++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/modules/processing.py b/modules/processing.py index 43fac21c..72dd3f6f 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -528,7 +528,9 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: p.all_negative_prompts = p.batch_size * p.n_iter * [shared.prompt_styles.apply_negative_styles_to_prompt(p.negative_prompt, p.styles)] if type(p) == StableDiffusionProcessingTxt2Img: - if p.enable_hr and p.hr_prompt != '': + if p.enable_hr and p.hr_prompt == '': + p.all_hr_prompts, p.all_hr_negative_prompts = p.all_prompts, p.all_negative_prompts + elif p.enable_hr and p.hr_prompt != '': if type(p.prompt) == list: p.all_hr_prompts = [shared.prompt_styles.apply_styles_to_prompt(x, p.styles) for x in p.hr_prompt] else: @@ -555,14 +557,6 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: if os.path.exists(cmd_opts.embeddings_dir) and not p.do_not_reload_embeddings: model_hijack.embedding_db.load_textual_inversion_embeddings() - _, extra_network_data = extra_networks.parse_prompts(p.all_prompts[0:1]) - if type(p) == StableDiffusionProcessingTxt2Img: - if p.enable_hr and p.hr_prompt != '': - _, hr_extra_network_data = extra_networks.parse_prompts(p.all_hr_prompts[0:1]) - if p.all_hr_prompts != p.all_prompts: - extra_network_data.update(hr_extra_network_data) - - if p.scripts is not None: p.scripts.process(p) @@ -600,13 +594,6 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: if shared.opts.live_previews_enable and opts.show_progress_type == "Approx NN": sd_vae_approx.model() - if not p.disable_extra_networks: - extra_networks.activate(p, extra_network_data) - - with open(os.path.join(paths.data_path, "params.txt"), "w", encoding="utf8") as file: - processed = Processed(p, [], p.seed, "") - file.write(processed.infotext(p, 0)) - if state.job_count == -1: state.job_count = p.n_iter @@ -623,9 +610,12 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: negative_prompts = p.all_negative_prompts[n * p.batch_size:(n + 1) * p.batch_size] if type(p) == StableDiffusionProcessingTxt2Img: - if p.enable_hr and p.hr_prompt != '': - hr_prompts = p.all_hr_prompts[n * p.batch_size:(n + 1) * p.batch_size] - hr_negative_prompts = p.all_negative_prompts[n * p.batch_size:(n + 1) * p.batch_size] + if p.enable_hr: + if p.hr_prompt == '': + hr_prompts, hr_negative_prompts = prompts, negative_prompts + else: + hr_prompts = p.all_hr_prompts[n * p.batch_size:(n + 1) * p.batch_size] + hr_negative_prompts = p.all_negative_prompts[n * p.batch_size:(n + 1) * p.batch_size] seeds = p.all_seeds[n * p.batch_size:(n + 1) * p.batch_size] subseeds = p.all_subseeds[n * p.batch_size:(n + 1) * p.batch_size] @@ -633,19 +623,40 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: if len(prompts) == 0: break - prompts, _ = extra_networks.parse_prompts(prompts) + prompts, extra_network_data = extra_networks.parse_prompts(prompts) + if type(p) == StableDiffusionProcessingTxt2Img: + if p.enable_hr and hr_prompts != prompts: + _, hr_extra_network_data = extra_networks.parse_prompts(hr_prompts) + extra_network_data.update(hr_extra_network_data) + + + + if not p.disable_extra_networks: + with devices.autocast(): + extra_networks.activate(p, extra_network_data) if p.scripts is not None: p.scripts.process_batch(p, batch_number=n, prompts=prompts, seeds=seeds, subseeds=subseeds) + # params.txt should be saved after scripts.process_batch, since the + # infotext could be modified by that callback + # Example: a wildcard processed by process_batch sets an extra model + # strength, which is saved as "Model Strength: 1.0" in the infotext + if n == 0: + with open(os.path.join(paths.data_path, "params.txt"), "w", encoding="utf8") as file: + processed = Processed(p, [], p.seed, "") + file.write(processed.infotext(p, 0)) + uc = get_conds_with_caching(prompt_parser.get_learned_conditioning, negative_prompts, p.steps, cached_uc) c = get_conds_with_caching(prompt_parser.get_multicond_learned_conditioning, prompts, p.steps, cached_c) + if type(p) == StableDiffusionProcessingTxt2Img: - if p.enable_hr and p.hr_prompt != '': - hr_uc = get_conds_with_caching(prompt_parser.get_learned_conditioning, hr_negative_prompts, p.steps, - cached_uc) - hr_c = get_conds_with_caching(prompt_parser.get_multicond_learned_conditioning, hr_prompts, p.steps, - cached_c) + if p.enable_hr: + if prompts != hr_prompts: + hr_uc = get_conds_with_caching(prompt_parser.get_learned_conditioning, hr_negative_prompts, p.steps, cached_uc) + hr_c = get_conds_with_caching(prompt_parser.get_multicond_learned_conditioning, hr_prompts, p.steps, cached_c) + else: + hr_uc, hr_c = uc, c if len(model_hijack.comments) > 0: for comment in model_hijack.comments: @@ -658,20 +669,9 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: with devices.without_autocast() if devices.unet_needs_upcast else devices.autocast(): if type(p) == StableDiffusionProcessingTxt2Img: if p.enable_hr: - if p.hr_prompt != '': - samples_ddim = p.sample(conditioning=c, unconditional_conditioning=uc, hr_conditioning=hr_c, hr_unconditional_conditioning=hr_uc, seeds=seeds, subseeds=subseeds, subseed_strength=p.subseed_strength, prompts=prompts) - else: - samples_ddim = p.sample(conditioning=c, unconditional_conditioning=uc, hr_conditioning=c, - hr_unconditional_conditioning=uc, seeds=seeds, subseeds=subseeds, - subseed_strength=p.subseed_strength, prompts=prompts) - else: - samples_ddim = p.sample(conditioning=c, unconditional_conditioning=uc, seeds=seeds, subseeds=subseeds, - subseed_strength=p.subseed_strength, prompts=prompts) - + samples_ddim = p.sample(conditioning=c, unconditional_conditioning=uc, hr_conditioning=hr_c, hr_unconditional_conditioning=hr_uc, seeds=seeds, subseeds=subseeds, subseed_strength=p.subseed_strength, prompts=prompts) else: - samples_ddim = p.sample(conditioning=c, unconditional_conditioning=uc, seeds=seeds, - subseeds=subseeds, - subseed_strength=p.subseed_strength, prompts=prompts) + samples_ddim = p.sample(conditioning=c, unconditional_conditioning=uc, seeds=seeds, subseeds=subseeds, subseed_strength=p.subseed_strength, prompts=prompts) x_samples_ddim = [decode_first_stage(p.sd_model, samples_ddim[i:i+1].to(dtype=devices.dtype_vae))[0].cpu() for i in range(samples_ddim.size(0))] for x in x_samples_ddim: From b9fdb9f701e576ce48458fdf756187c2d1725649 Mon Sep 17 00:00:00 2001 From: InvincibleDude <81354513+InvincibleDude@users.noreply.github.com> Date: Sat, 4 Mar 2023 18:09:05 +0000 Subject: [PATCH 36/37] Fix crash when hr is disabled --- modules/processing.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/processing.py b/modules/processing.py index 25479c06..f5cc2433 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -670,6 +670,8 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: if type(p) == StableDiffusionProcessingTxt2Img: if p.enable_hr: samples_ddim = p.sample(conditioning=c, unconditional_conditioning=uc, hr_conditioning=hr_c, hr_unconditional_conditioning=hr_uc, seeds=seeds, subseeds=subseeds, subseed_strength=p.subseed_strength, prompts=prompts) + else: + samples_ddim = p.sample(conditioning=c, unconditional_conditioning=uc, seeds=seeds, subseeds=subseeds, subseed_strength=p.subseed_strength, prompts=prompts) else: samples_ddim = p.sample(conditioning=c, unconditional_conditioning=uc, seeds=seeds, subseeds=subseeds, subseed_strength=p.subseed_strength, prompts=prompts) From f6e27378404631d951656388fc178b784fe1495b Mon Sep 17 00:00:00 2001 From: InvincibleDude <81354513+InvincibleDude@users.noreply.github.com> Date: Fri, 10 Mar 2023 12:13:55 +0000 Subject: [PATCH 37/37] Negative prompt fix --- modules/processing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/processing.py b/modules/processing.py index f5cc2433..694e2637 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -615,7 +615,7 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: hr_prompts, hr_negative_prompts = prompts, negative_prompts else: hr_prompts = p.all_hr_prompts[n * p.batch_size:(n + 1) * p.batch_size] - hr_negative_prompts = p.all_negative_prompts[n * p.batch_size:(n + 1) * p.batch_size] + hr_negative_prompts = p.all_hr_negative_prompts[n * p.batch_size:(n + 1) * p.batch_size] seeds = p.all_seeds[n * p.batch_size:(n + 1) * p.batch_size] subseeds = p.all_subseeds[n * p.batch_size:(n + 1) * p.batch_size]