|
| 1 | +from typing import Literal, Optional |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import torch |
| 5 | +import torchvision.transforms as T |
| 6 | +from PIL import Image, ImageFilter |
| 7 | +from torchvision.transforms.functional import resize as tv_resize |
| 8 | + |
| 9 | +from invokeai.app.invocations.baseinvocation import BaseInvocation, BaseInvocationOutput, invocation, invocation_output |
| 10 | +from invokeai.app.invocations.denoise_latents import DEFAULT_PRECISION |
| 11 | +from invokeai.app.invocations.fields import ( |
| 12 | + DenoiseMaskField, |
| 13 | + FieldDescriptions, |
| 14 | + ImageField, |
| 15 | + Input, |
| 16 | + InputField, |
| 17 | + OutputField, |
| 18 | +) |
| 19 | +from invokeai.app.invocations.image_to_latents import ImageToLatentsInvocation |
| 20 | +from invokeai.app.invocations.model import UNetField, VAEField |
| 21 | +from invokeai.app.services.shared.invocation_context import InvocationContext |
| 22 | +from invokeai.backend.model_manager import LoadedModel |
| 23 | +from invokeai.backend.model_manager.config import MainConfigBase, ModelVariantType |
| 24 | +from invokeai.backend.stable_diffusion.diffusers_pipeline import image_resized_to_grid_as_tensor |
| 25 | + |
| 26 | + |
| 27 | +@invocation_output("gradient_mask_output") |
| 28 | +class GradientMaskOutput(BaseInvocationOutput): |
| 29 | + """Outputs a denoise mask and an image representing the total gradient of the mask.""" |
| 30 | + |
| 31 | + denoise_mask: DenoiseMaskField = OutputField(description="Mask for denoise model run") |
| 32 | + expanded_mask_area: ImageField = OutputField( |
| 33 | + description="Image representing the total gradient area of the mask. For paste-back purposes." |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +@invocation( |
| 38 | + "create_gradient_mask", |
| 39 | + title="Create Gradient Mask", |
| 40 | + tags=["mask", "denoise"], |
| 41 | + category="latents", |
| 42 | + version="1.1.0", |
| 43 | +) |
| 44 | +class CreateGradientMaskInvocation(BaseInvocation): |
| 45 | + """Creates mask for denoising model run.""" |
| 46 | + |
| 47 | + mask: ImageField = InputField(default=None, description="Image which will be masked", ui_order=1) |
| 48 | + edge_radius: int = InputField( |
| 49 | + default=16, ge=0, description="How far to blur/expand the edges of the mask", ui_order=2 |
| 50 | + ) |
| 51 | + coherence_mode: Literal["Gaussian Blur", "Box Blur", "Staged"] = InputField(default="Gaussian Blur", ui_order=3) |
| 52 | + minimum_denoise: float = InputField( |
| 53 | + default=0.0, ge=0, le=1, description="Minimum denoise level for the coherence region", ui_order=4 |
| 54 | + ) |
| 55 | + image: Optional[ImageField] = InputField( |
| 56 | + default=None, |
| 57 | + description="OPTIONAL: Only connect for specialized Inpainting models, masked_latents will be generated from the image with the VAE", |
| 58 | + title="[OPTIONAL] Image", |
| 59 | + ui_order=6, |
| 60 | + ) |
| 61 | + unet: Optional[UNetField] = InputField( |
| 62 | + description="OPTIONAL: If the Unet is a specialized Inpainting model, masked_latents will be generated from the image with the VAE", |
| 63 | + default=None, |
| 64 | + input=Input.Connection, |
| 65 | + title="[OPTIONAL] UNet", |
| 66 | + ui_order=5, |
| 67 | + ) |
| 68 | + vae: Optional[VAEField] = InputField( |
| 69 | + default=None, |
| 70 | + description="OPTIONAL: Only connect for specialized Inpainting models, masked_latents will be generated from the image with the VAE", |
| 71 | + title="[OPTIONAL] VAE", |
| 72 | + input=Input.Connection, |
| 73 | + ui_order=7, |
| 74 | + ) |
| 75 | + tiled: bool = InputField(default=False, description=FieldDescriptions.tiled, ui_order=8) |
| 76 | + fp32: bool = InputField( |
| 77 | + default=DEFAULT_PRECISION == "float32", |
| 78 | + description=FieldDescriptions.fp32, |
| 79 | + ui_order=9, |
| 80 | + ) |
| 81 | + |
| 82 | + @torch.no_grad() |
| 83 | + def invoke(self, context: InvocationContext) -> GradientMaskOutput: |
| 84 | + mask_image = context.images.get_pil(self.mask.image_name, mode="L") |
| 85 | + if self.edge_radius > 0: |
| 86 | + if self.coherence_mode == "Box Blur": |
| 87 | + blur_mask = mask_image.filter(ImageFilter.BoxBlur(self.edge_radius)) |
| 88 | + else: # Gaussian Blur OR Staged |
| 89 | + # Gaussian Blur uses standard deviation. 1/2 radius is a good approximation |
| 90 | + blur_mask = mask_image.filter(ImageFilter.GaussianBlur(self.edge_radius / 2)) |
| 91 | + |
| 92 | + blur_tensor: torch.Tensor = image_resized_to_grid_as_tensor(blur_mask, normalize=False) |
| 93 | + |
| 94 | + # redistribute blur so that the original edges are 0 and blur outwards to 1 |
| 95 | + blur_tensor = (blur_tensor - 0.5) * 2 |
| 96 | + |
| 97 | + threshold = 1 - self.minimum_denoise |
| 98 | + |
| 99 | + if self.coherence_mode == "Staged": |
| 100 | + # wherever the blur_tensor is less than fully masked, convert it to threshold |
| 101 | + blur_tensor = torch.where((blur_tensor < 1) & (blur_tensor > 0), threshold, blur_tensor) |
| 102 | + else: |
| 103 | + # wherever the blur_tensor is above threshold but less than 1, drop it to threshold |
| 104 | + blur_tensor = torch.where((blur_tensor > threshold) & (blur_tensor < 1), threshold, blur_tensor) |
| 105 | + |
| 106 | + else: |
| 107 | + blur_tensor: torch.Tensor = image_resized_to_grid_as_tensor(mask_image, normalize=False) |
| 108 | + |
| 109 | + mask_name = context.tensors.save(tensor=blur_tensor.unsqueeze(1)) |
| 110 | + |
| 111 | + # compute a [0, 1] mask from the blur_tensor |
| 112 | + expanded_mask = torch.where((blur_tensor < 1), 0, 1) |
| 113 | + expanded_mask_image = Image.fromarray((expanded_mask.squeeze(0).numpy() * 255).astype(np.uint8), mode="L") |
| 114 | + expanded_image_dto = context.images.save(expanded_mask_image) |
| 115 | + |
| 116 | + masked_latents_name = None |
| 117 | + if self.unet is not None and self.vae is not None and self.image is not None: |
| 118 | + # all three fields must be present at the same time |
| 119 | + main_model_config = context.models.get_config(self.unet.unet.key) |
| 120 | + assert isinstance(main_model_config, MainConfigBase) |
| 121 | + if main_model_config.variant is ModelVariantType.Inpaint: |
| 122 | + mask = blur_tensor |
| 123 | + vae_info: LoadedModel = context.models.load(self.vae.vae) |
| 124 | + image = context.images.get_pil(self.image.image_name) |
| 125 | + image_tensor = image_resized_to_grid_as_tensor(image.convert("RGB")) |
| 126 | + if image_tensor.dim() == 3: |
| 127 | + image_tensor = image_tensor.unsqueeze(0) |
| 128 | + img_mask = tv_resize(mask, image_tensor.shape[-2:], T.InterpolationMode.BILINEAR, antialias=False) |
| 129 | + masked_image = image_tensor * torch.where(img_mask < 0.5, 0.0, 1.0) |
| 130 | + masked_latents = ImageToLatentsInvocation.vae_encode( |
| 131 | + vae_info, self.fp32, self.tiled, masked_image.clone() |
| 132 | + ) |
| 133 | + masked_latents_name = context.tensors.save(tensor=masked_latents) |
| 134 | + |
| 135 | + return GradientMaskOutput( |
| 136 | + denoise_mask=DenoiseMaskField(mask_name=mask_name, masked_latents_name=masked_latents_name, gradient=True), |
| 137 | + expanded_mask_area=ImageField(image_name=expanded_image_dto.image_name), |
| 138 | + ) |
0 commit comments