From 69d3a95b12d54fbdd727e1b3f2b5b7059e2a7340 Mon Sep 17 00:00:00 2001 From: Vladimir Mandic Date: Thu, 10 Oct 2024 06:41:58 -0400 Subject: [PATCH] fix adetailer Signed-off-by: Vladimir Mandic --- modules/processing.py | 23 ++++++---- modules/processing_class.py | 84 ++++++++++++++++++++++++++++++++++++- modules/shared.py | 8 ++-- 3 files changed, 101 insertions(+), 14 deletions(-) diff --git a/modules/processing.py b/modules/processing.py index 6078ad10c..4f24d4a8e 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -354,21 +354,24 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: else: sample = validate_sample(sample) image = Image.fromarray(sample) - sample = face_restoration.restore_faces(sample, p) + if p.restore_faces: + p.ops.append('restore') + if not p.do_not_save_samples and shared.opts.save_images_before_detailer: + info = create_infotext(p, p.prompts, p.seeds, p.subseeds, index=i) + images.save_image(Image.fromarray(sample), path=p.outpath_samples, basename="", seed=p.seeds[i], prompt=p.prompts[i], extension=shared.opts.samples_format, info=info, p=p, suffix="-before-restore") + sample = face_restoration.restore_faces(sample, p) + if sample is not None: + image = Image.fromarray(sample) if p.detailer: + p.ops.append('detailer') if not p.do_not_save_samples and shared.opts.save_images_before_detailer: info = create_infotext(p, p.prompts, p.seeds, p.subseeds, index=i) images.save_image(Image.fromarray(sample), path=p.outpath_samples, basename="", seed=p.seeds[i], prompt=p.prompts[i], extension=shared.opts.samples_format, info=info, p=p, suffix="-before-detailer") - p.ops.append('detailer') sample = detailer.detail(sample, p) if sample is not None: image = Image.fromarray(sample) - if p.scripts is not None and isinstance(p.scripts, scripts.ScriptRunner): - pp = scripts.PostprocessImageArgs(image) - p.scripts.postprocess_image(p, pp) - if pp.image is not None: - image = pp.image if p.color_corrections is not None and i < len(p.color_corrections): + p.ops.append('color') if not p.do_not_save_samples and shared.opts.save_images_before_color_correction: orig = p.color_corrections p.color_corrections = None @@ -376,8 +379,12 @@ def process_images_inner(p: StableDiffusionProcessing) -> Processed: image_without_cc = apply_overlay(image, p.paste_to, i, p.overlay_images) info = create_infotext(p, p.prompts, p.seeds, p.subseeds, index=i) images.save_image(image_without_cc, path=p.outpath_samples, basename="", seed=p.seeds[i], prompt=p.prompts[i], extension=shared.opts.samples_format, info=info, p=p, suffix="-before-color-correct") - p.ops.append('color') image = apply_color_correction(p.color_corrections[i], image) + if p.scripts is not None and isinstance(p.scripts, scripts.ScriptRunner): + pp = scripts.PostprocessImageArgs(image) + p.scripts.postprocess_image(p, pp) + if pp.image is not None: + image = pp.image if shared.opts.mask_apply_overlay: image = apply_overlay(image, p.paste_to, i, p.overlay_images) diff --git a/modules/processing_class.py b/modules/processing_class.py index 3eb35aa31..ccb426605 100644 --- a/modules/processing_class.py +++ b/modules/processing_class.py @@ -20,7 +20,68 @@ class StableDiffusionProcessing: """ The first set of paramaters: sd_models -> do_not_reload_embeddings represent the minimum required to create a StableDiffusionProcessing """ - def __init__(self, sd_model=None, outpath_samples=None, outpath_grids=None, prompt: str = "", styles: List[str] = None, seed: int = -1, subseed: int = -1, subseed_strength: float = 0, seed_resize_from_h: int = -1, seed_resize_from_w: int = -1, seed_enable_extras: bool = True, sampler_name: str = None, hr_sampler_name: str = None, batch_size: int = 1, n_iter: int = 1, steps: int = 50, cfg_scale: float = 7.0, image_cfg_scale: float = None, clip_skip: int = 1, width: int = 512, height: int = 512, full_quality: bool = True, detailer: bool = False, tiling: bool = False, hidiffusion: bool = False, do_not_save_samples: bool = False, do_not_save_grid: bool = False, extra_generation_params: Dict[Any, Any] = None, overlay_images: Any = None, negative_prompt: str = None, eta: float = None, do_not_reload_embeddings: bool = False, denoising_strength: float = 0, diffusers_guidance_rescale: float = 0.7, pag_scale: float = 0.0, pag_adaptive: float = 0.5, cfg_end: float = 1, resize_mode: int = 0, resize_name: str = 'None', resize_context: str = 'None', scale_by: float = 0, selected_scale_tab: int = 0, hdr_mode: int = 0, hdr_brightness: float = 0, hdr_color: float = 0, hdr_sharpen: float = 0, hdr_clamp: bool = False, hdr_boundary: float = 4.0, hdr_threshold: float = 0.95, hdr_maximize: bool = False, hdr_max_center: float = 0.6, hdr_max_boundry: float = 1.0, hdr_color_picker: str = None, hdr_tint_ratio: float = 0, override_settings: Dict[str, Any] = None, override_settings_restore_afterwards: bool = True, sampler_index: int = None, script_args: list = None): # pylint: disable=unused-argument + def __init__(self, + sd_model=None, + outpath_samples=None, + outpath_grids=None, + prompt: str = "", + styles: List[str] = None, + seed: int = -1, + subseed: int = -1, + subseed_strength: float = 0, + seed_resize_from_h: int = -1, + seed_resize_from_w: int = -1, + seed_enable_extras: bool = True, + sampler_name: str = None, + hr_sampler_name: str = None, + batch_size: int = 1, + n_iter: int = 1, + steps: int = 50, + cfg_scale: float = 7.0, + image_cfg_scale: float = None, + clip_skip: int = 1, + width: int = 512, + height: int = 512, + full_quality: bool = True, + detailer: bool = False, + restore_faces: bool = False, + tiling: bool = False, + hidiffusion: bool = False, + do_not_save_samples: bool = False, + do_not_save_grid: bool = False, + extra_generation_params: Dict[Any, Any] = None, + overlay_images: Any = None, + negative_prompt: str = None, + eta: float = None, + do_not_reload_embeddings: bool = False, + denoising_strength: float = 0, + diffusers_guidance_rescale: float = 0.7, + pag_scale: float = 0.0, + pag_adaptive: float = 0.5, + cfg_end: float = 1, + resize_mode: int = 0, + resize_name: str = 'None', + resize_context: str = 'None', + scale_by: float = 0, + selected_scale_tab: int = 0, + hdr_mode: int = 0, + hdr_brightness: float = 0, + hdr_color: float = 0, + hdr_sharpen: float = 0, + hdr_clamp: bool = False, + hdr_boundary: float = 4.0, + hdr_threshold: float = 0.95, + hdr_maximize: bool = False, + hdr_max_center: float = 0.6, + hdr_max_boundry: float = 1.0, + hdr_color_picker: str = None, + hdr_tint_ratio: float = 0, + override_settings: Dict[str, Any] = None, + override_settings_restore_afterwards: bool = True, + sampler_index: int = None, + script_args: list = None + ): # pylint: disable=unused-argument + self.state: str = '' self.skip = [] self.outpath_samples: str = outpath_samples @@ -51,6 +112,7 @@ class StableDiffusionProcessing: self.height: int = height self.full_quality: bool = full_quality self.detailer: bool = detailer + self.restore_faces: bool = restore_faces self.tiling: bool = tiling self.hidiffusion: bool = hidiffusion self.do_not_save_samples: bool = do_not_save_samples @@ -191,7 +253,25 @@ class StableDiffusionProcessing: class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing): - 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_force: bool = False, hr_resize_mode: int = 0, hr_resize_context: str = 'None', hr_upscaler: str = None, hr_second_pass_steps: int = 0, hr_resize_x: int = 0, hr_resize_y: int = 0, refiner_steps: int = 5, refiner_start: float = 0, refiner_prompt: str = '', refiner_negative: 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_force: bool = False, + hr_resize_mode: int = 0, + hr_resize_context: str = 'None', + hr_upscaler: str = None, + hr_second_pass_steps: int = 0, + hr_resize_x: int = 0, + hr_resize_y: int = 0, + refiner_steps: int = 5, + refiner_start: float = 0, + refiner_prompt: str = '', + refiner_negative: str = '', + **kwargs + ): super().__init__(**kwargs) self.reprocess = {} diff --git a/modules/shared.py b/modules/shared.py index b972a05b2..2249d1db6 100644 --- a/modules/shared.py +++ b/modules/shared.py @@ -474,9 +474,9 @@ options_templates.update(options_section(('cuda', "Compute Settings"), { "cuda_compile_errors": OptionInfo(True, "Model compile suppress errors"), "deep_cache_interval": OptionInfo(3, "DeepCache cache interval", gr.Slider, {"minimum": 1, "maximum": 10, "step": 1}), - "quant_sep": OptionInfo("

Model Quantization

", "", gr.HTML), - "quant_shuffle_weights": OptionInfo(False, "Shuffle the weights between GPU and CPU when quantizing"), - "diffusers_quantization": OptionInfo(False, "Dynamic quantization with TorchAO"), + "quant_sep": OptionInfo("

Model Quantization

", "", gr.HTML, {"visible": native}), + "quant_shuffle_weights": OptionInfo(False, "Shuffle the weights between GPU and CPU when quantizing", gr.Checkbox, {"visible": native}), + "diffusers_quantization": OptionInfo(False, "Dynamic quantization with TorchAO", gr.Checkbox, {"visible": native}), "nncf_compress_weights": OptionInfo([], "Compress Model weights with NNCF INT8", gr.CheckboxGroup, {"choices": ["Model", "VAE", "Text Encoder", "ControlNet"], "visible": native}), "optimum_quanto_weights": OptionInfo([], "Quantize Model weights with Optimum Quanto", gr.CheckboxGroup, {"choices": ["Model", "VAE", "Text Encoder", "ControlNet"], "visible": native}), "optimum_quanto_weights_type": OptionInfo("qint8", "Weights type for Optimum Quanto", gr.Radio, {"choices": ['qint8', 'qfloat8_e4m3fn', 'qfloat8_e5m2', 'qint4', 'qint2'], "visible": native}), @@ -543,7 +543,7 @@ options_templates.update(options_section(('advanced', "Inference Settings"), { "batch_frame_mode": OptionInfo(False, "Parallel process images in batch"), "inference_other_sep": OptionInfo("

Other

", "", gr.HTML), "inference_mode": OptionInfo("no-grad", "Torch inference mode", gr.Radio, {"choices": ["no-grad", "inference-mode", "none"]}), - "sd_vae_sliced_encode": OptionInfo(False, "VAE sliced encode"), + "sd_vae_sliced_encode": OptionInfo(False, "VAE sliced encode", gr.Checkbox, {"visible": not native}), })) options_templates.update(options_section(('diffusers', "Diffusers Settings"), {