Python
added mask strength to flux 2
DeepBeepMeep828d696aee19
diff --git a/README.md b/README.mdindex ed43515..864fe7c 100644--- a/README.md+++ b/README.md@@ -45,7 +45,7 @@ The only snag is that it is a 60B parameters for the *Transformer* part and 40BBehold the WanGP Miracle ! Flux 2 wil work with only 8 GB of VRAM if you are happy with 8 bits quantization (no need for lower quality 4bits). With 9GB of VRAM you can run the model at full power. You will need at least 64 GB of RAM. If not maybe Memory Profile 5 will be your friend.-*With WanGP v9.74*, **Flux 2 Control Net** hidden power has also been unleashed from the vanilla model. You can now enjoy Flux 2 *Inpainting* and *Pose transfer*. This can be combined with *Image Refs* to do *Face Swapping* as it is targetted only to specific areas using *Masking*.+*With WanGP v9.74*, **Flux 2 Control Net** hidden power has also been unleashed from the vanilla model. You can now enjoy Flux 2 *Inpainting* and *Pose transfer*. This can be combined with *Image Refs* to get the best *Identity Preservation* / *Face Swapping* an Image Model can offer: just target the effect to a specific area using a *Mask* and set a *Masking Strength* of 0.3-0.4 for a perfect blending- **Z-Image**: a small model, very fast (8 steps), very low VRAM (optimized even more in WanGP for fun, just in case you want to generate 16 images at a time) that produces outstanding Image quality. Not yet the Flux 2 level, and no Image editing yet but a very good trade-off.diff --git a/models/flux/flux_handler.py b/models/flux/flux_handler.pyindex 9e9cd0f..9e7b173 100644--- a/models/flux/flux_handler.py+++ b/models/flux/flux_handler.py@@ -117,6 +117,8 @@ class family_handler():"visible": False}+ extra_model_def["mask_strength_always_enabled"] = True+extra_model_def["guide_preprocessing"] = {"selection": ["", "PV", "MV"],}@@ -318,6 +320,7 @@ class family_handler():ui_defaults.update({"embedded_guidance_scale": 4.0,"remove_background_images_ref" : 0,+ "mask_strength": 0.3,})diff --git a/models/flux/sampling.py b/models/flux/sampling.pyindex 901fce3..26c3d8d 100644--- a/models/flux/sampling.py+++ b/models/flux/sampling.py@@ -357,7 +357,7 @@ def denoise('pipeline': pipeline,'callback': callback,"img_len": img.shape[1],- "siglip_embedding": siglip_embedding,+ "siglip_embedding": siglip_embedding,"siglip_embedding_ids": siglip_embedding_ids,}@@ -371,8 +371,8 @@ def denoise(if original_image_latents is None: original_image_latents= img_cond_seq.clone()randn = torch.randn_like(original_image_latents)if denoising_strength < 1.:- first_step = int(len(timesteps) * (1. - denoising_strength))- masked_steps = math.ceil(len(timesteps) * masking_strength)+ first_step = int(len(timesteps[:-1]) * (1. - denoising_strength))+ masked_steps = math.ceil(len(timesteps[:-1]) * masking_strength)if not morph:latent_noise_factor = timesteps[first_step]latents = original_image_latents * (1.0 - latent_noise_factor) + randn * latent_noise_factordiff --git a/wgp.py b/wgp.pyindex a59c3d4..2a3e379 100644--- a/wgp.py+++ b/wgp.py@@ -754,11 +754,14 @@ def process_prompt_and_add_tasks(state, current_gallery_tab, model_choice):if "G" in video_prompt_type:if denoising_strength < 1.:gr.Info(f"With Denoising Strength {denoising_strength:.1f}, Denoising will start at Step no {int(round(num_inference_steps * (1. - denoising_strength),4))} ")- masking_duration = math.ceil(num_inference_steps * masking_strength)+ else:+ denoising_strength = 1.0++ if "G" in video_prompt_type or model_def.get("mask_strength_always_enabled", False):if "A" in video_prompt_type and "U" not in video_prompt_type and masking_strength < 1.:+ masking_duration = math.ceil(num_inference_steps * masking_strength)gr.Info(f"With Masking Strength {masking_strength:.1f}, Masking will last {masking_duration}{' Step' if masking_duration==1 else ' Steps'}")else:- denoising_strength = 1.0masking_strength = 1.0if len(keep_frames_video_guide) > 0 and model_type in ["ltxv_13B"]:gr.Info("Keep Frames for Control Video is not supported with LTX Video")@@ -3606,7 +3609,7 @@ def select_audio(state, audio_files_paths, audio_file_selected):video_guide_processes = "PEDSLCMU"-all_guide_processes = video_guide_processes + "VBH"+all_guide_processes = video_guide_processes + "VGBH"process_map_outside_mask = { "Y" : "depth", "W": "scribble", "X": "inpaint", "Z": "flow"}process_map_video_guide = { "P": "pose", "D" : "depth", "S": "scribble", "E": "canny", "L": "flow", "C": "gray", "M": "inpaint", "U": "identity"}@@ -3892,7 +3895,7 @@ def select_video(state, current_gallery_tab, input_file_list, file_selected, audif "G" in video_video_prompt_type and "V" in video_video_prompt_type:values += [configs.get("denoising_strength",1)]labels += ["Denoising Strength"]- if "G" in video_video_prompt_type and "A" in video_video_prompt_type and "U" not in video_video_prompt_type:+ if ("G" in video_video_prompt_type or model_def.get("mask_strength_always_enabled", False)) and "A" in video_video_prompt_type and "U" not in video_video_prompt_type:values += [configs.get("masking_strength",1)]labels += ["Masking Strength"]@@ -6844,8 +6847,12 @@ def prepare_inputs_dict(target, inputs, model_type = None, model_filename = Nonepop += ["MMAudio_setting", "MMAudio_prompt", "MMAudio_neg_prompt"]video_prompt_type = inputs["video_prompt_type"]- if not "G" in video_prompt_type:- pop += ["denoising_strength", "masking_strength"]+ if "G" not in video_prompt_type:+ pop += ["denoising_strength"]++ if "G" not in video_prompt_type and not model_def.get("mask_strength_always_enabled", False):+ pop += ["masking_strength"]+if not (server_config.get("enhancer_enabled", 0) > 0 and server_config.get("enhancer_mode", 0) == 0):pop += ["prompt_enhancer"]@@ -7833,8 +7840,8 @@ def refresh_video_prompt_type_video_guide(state, filter_type, video_prompt_type,if len(custom_trigger) == 0 or custom_trigger in video_prompt_type:custom_options = Truecustom_checkbox = custom_video_selection.get("type","") == "checkbox"-- return video_prompt_type, gr.update(visible = visible and not image_outputs), image_guide, gr.update(visible = keep_frames_video_guide_visible), gr.update(visible = visible and "G" in video_prompt_type), gr.update(visible = mask_visible and "G" in video_prompt_type), gr.update(visible= (visible or "F" in video_prompt_type or "K" in video_prompt_type) and any_outpainting), gr.update(visible= visible and mask_selector_visible and not "U" in video_prompt_type ) , gr.update(visible= mask_visible and not image_outputs), image_mask, image_mask_guide, gr.update(visible= mask_visible), gr.update(visible = ref_images_visible ), gr.update(visible= custom_options and not custom_checkbox ), gr.update(visible= custom_options and custom_checkbox )+ mask_strength_always_enabled = model_def.get("mask_strength_always_enabled", False)+ return video_prompt_type, gr.update(visible = visible and not image_outputs), image_guide, gr.update(visible = keep_frames_video_guide_visible), gr.update(visible = visible and "G" in video_prompt_type), gr.update(visible = mask_visible and( mask_strength_always_enabled or "G" in video_prompt_type)), gr.update(visible= (visible or "F" in video_prompt_type or "K" in video_prompt_type) and any_outpainting), gr.update(visible= visible and mask_selector_visible and not "U" in video_prompt_type ) , gr.update(visible= mask_visible and not image_outputs), image_mask, image_mask_guide, gr.update(visible= mask_visible), gr.update(visible = ref_images_visible ), gr.update(visible= custom_options and not custom_checkbox ), gr.update(visible= custom_options and custom_checkbox )def refresh_video_prompt_type_video_custom_dropbox(state, video_prompt_type, video_prompt_type_video_custom_dropbox):model_type = state["model_type"]@@ -8590,7 +8597,8 @@ def generate_video_tab(update_form = False, state_dict = None, ui_defaults = Non# image_mask = gr.Image(label= "Image Mask Area (for Inpainting, white = Control Area, black = Unchanged)", type ="pil", visible= image_mode_value==1 and "V" in video_prompt_type_value and "A" in video_prompt_type_value and not "U" in video_prompt_type_value , height = gallery_height, value= ui_defaults.get("image_mask", None))image_mask = gr.Image(label= "Image Mask Area (for Inpainting, white = Control Area, black = Unchanged)", type ="pil", visible= False, height = gallery_height, value= ui_defaults.get("image_mask", None))video_mask = gr.Video(label= "Video Mask Area (for Inpainting, white = Control Area, black = Unchanged)", visible= (not image_outputs) and "V" in video_prompt_type_value and "A" in video_prompt_type_value and not "U" in video_prompt_type_value , height = gallery_height, value= ui_defaults.get("video_mask", None))- masking_strength = gr.Slider(0, 1, value= ui_defaults.get("masking_strength", 1), step=0.01, label=f"Masking Strength (the Lower the More Freedom for Unmasked Area", visible = "G" in video_prompt_type_value and "V" in video_prompt_type_value and "A" in video_prompt_type_value and not "U" in video_prompt_type_value , show_reset_button= False)+ mask_strength_always_enabled = model_def.get("mask_strength_always_enabled", False)+ masking_strength = gr.Slider(0, 1, value= ui_defaults.get("masking_strength", 1), step=0.01, label=f"Masking Strength (the Lower the More Freedom for Unmasked Area", visible = (mask_strength_always_enabled or "G" in video_prompt_type_value) and "V" in video_prompt_type_value and "A" in video_prompt_type_value and not "U" in video_prompt_type_value , show_reset_button= False)mask_expand = gr.Slider(-10, 50, value=ui_defaults.get("mask_expand", 0), step=1, label="Expand / Shrink Mask Area", visible= "V" in video_prompt_type_value and "A" in video_prompt_type_value and not "U" in video_prompt_type_value, show_reset_button= False )image_refs_single_image_mode = model_def.get("one_image_ref_needed", False)