diff --git a/modules/img2img.py b/modules/img2img.py index bcc158dc..1b08c82f 100644 --- a/modules/img2img.py +++ b/modules/img2img.py @@ -76,9 +76,9 @@ def process_batch(p, input_dir, output_dir, inpaint_mask_dir, args): processed_image.save(os.path.join(output_dir, filename)) -def img2img(id_task: str, mode: int, prompt: str, negative_prompt: str, prompt_styles, init_img, sketch, init_img_with_mask, inpaint_color_sketch, inpaint_color_sketch_orig, init_img_inpaint, init_mask_inpaint, steps: int, sampler_index: int, mask_blur: int, mask_alpha: float, inpainting_fill: int, restore_faces: bool, tiling: bool, n_iter: int, batch_size: int, cfg_scale: float, image_cfg_scale: float, denoising_strength: 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, resize_mode: int, inpaint_full_res: bool, inpaint_full_res_padding: int, inpainting_mask_invert: int, img2img_batch_input_dir: str, img2img_batch_output_dir: str, img2img_batch_inpaint_mask_dir: str, override_settings_texts, *args): +def img2img(id_task: str, mode: int, prompt: str, negative_prompt: str, prompt_styles, init_img, sketch, init_img_with_mask, inpaint_color_sketch, inpaint_color_sketch_orig, init_img_inpaint, init_mask_inpaint, steps: int, sampler_index: int, mask_blur: int, mask_alpha: float, inpainting_fill: int, restore_faces: bool, tiling: bool, n_iter: int, batch_size: int, cfg_scale: float, image_cfg_scale: float, image_cfg_scale_max: float, denoising_strength: 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, resize_mode: int, inpaint_full_res: bool, inpaint_full_res_padding: int, inpainting_mask_invert: int, img2img_batch_input_dir: str, img2img_batch_output_dir: str, img2img_batch_inpaint_mask_dir: str, override_settings_texts, *args): override_settings = create_override_settings_dict(override_settings_texts) - + is_batch = mode == 5 if mode == 0: # img2img @@ -114,6 +114,12 @@ def img2img(id_task: str, mode: int, prompt: str, negative_prompt: str, prompt_s assert 0. <= denoising_strength <= 1., 'can only work with strength in [0.0, 1.0]' + if image_cfg_scale is None: + image_cfg_scale = 1 + else: + image_cfg_scale = float(image_cfg_scale_max) - image_cfg_scale + + p = StableDiffusionProcessingImg2Img( sd_model=shared.sd_model, outpath_samples=opts.outdir_samples or opts.outdir_img2img_samples, diff --git a/modules/processing.py b/modules/processing.py index e1b53ac0..5d6fdd23 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -268,7 +268,7 @@ class Processed: self.height = p.height self.sampler_name = p.sampler_name self.cfg_scale = p.cfg_scale - self.image_cfg_scale = getattr(p, 'image_cfg_scale', None) + self.image_cfg_scale = getattr(p, 'image_cfg_scale', 1) self.steps = p.steps self.batch_size = p.batch_size self.restore_faces = p.restore_faces @@ -446,7 +446,7 @@ def create_infotext(p, all_prompts, all_seeds, all_subseeds, comments=None, iter "Steps": p.steps, "Sampler": p.sampler_name, "CFG scale": p.cfg_scale, - "Image CFG scale": getattr(p, 'image_cfg_scale', None), + "Image CFG scale": getattr(p, 'image_cfg_scale', None) if shared.sd_model.cond_stage_key == "edit" else None, "Seed": all_seeds[index], "Face restoration": (opts.face_restoration_model if p.restore_faces else None), "Size": f"{p.width}x{p.height}", @@ -909,7 +909,7 @@ class StableDiffusionProcessingImg2Img(StableDiffusionProcessing): self.init_images = init_images self.resize_mode: int = resize_mode self.denoising_strength: float = denoising_strength - self.image_cfg_scale: float = image_cfg_scale if shared.sd_model.cond_stage_key == "edit" else None + self.image_cfg_scale: float = image_cfg_scale if shared.sd_model.cond_stage_key == "edit" else 1 self.init_latent = None self.image_mask = mask self.latent_mask = None diff --git a/modules/sd_samplers_kdiffusion.py b/modules/sd_samplers_kdiffusion.py index 4e5f293a..3bb9f052 100644 --- a/modules/sd_samplers_kdiffusion.py +++ b/modules/sd_samplers_kdiffusion.py @@ -65,7 +65,7 @@ class CFGDenoiser(torch.nn.Module): for i, conds in enumerate(conds_list): for cond_index, weight in conds: - denoised[i] += (weight * cond_scale) * (x_out[cond_index] - denoised_uncond[i]) * self.image_cfg_scale + denoised[i] += (weight * cond_scale) * (x_out[cond_index] - denoised_uncond[i]) * self.image_cfg_scale return denoised @@ -210,7 +210,7 @@ class KDiffusionSampler: self.model_wrap_cfg.mask = p.mask if hasattr(p, 'mask') else None self.model_wrap_cfg.nmask = p.nmask if hasattr(p, 'nmask') else None self.model_wrap_cfg.step = 0 - self.model_wrap_cfg.image_cfg_scale = getattr(p, 'image_cfg_scale', 1) + self.model_wrap_cfg.image_cfg_scale = p.image_cfg_scale if hasattr(p, 'image_cfg_scale') else 1 self.eta = p.eta if p.eta is not None else opts.eta_ancestral k_diffusion.sampling.torch = TorchHijack(self.sampler_noises if self.sampler_noises is not None else []) diff --git a/modules/ui.py b/modules/ui.py index f5df1ffe..ba97af26 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -768,6 +768,7 @@ def create_ui(): with FormRow(): cfg_scale = gr.Slider(minimum=1.0, maximum=30.0, step=0.5, label='CFG Scale', value=7.0, elem_id="img2img_cfg_scale") image_cfg_scale = gr.Slider(minimum=0, maximum=3.0, step=0.05, label='Image CFG Scale', value=1.5, elem_id="img2img_image_cfg_scale", visible=shared.sd_model and shared.sd_model.cond_stage_key == "edit") + image_cfg_scale_max = gr.Textbox(value=image_cfg_scale.maximum, visible=False) denoising_strength = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label='Denoising strength', value=0.75, elem_id="img2img_denoising_strength") elif category == "seed": @@ -864,6 +865,7 @@ def create_ui(): batch_size, cfg_scale, image_cfg_scale, + image_cfg_scale_max, denoising_strength, seed, subseed, subseed_strength, seed_resize_from_h, seed_resize_from_w, seed_checkbox,