|
import torch |
|
import PIL.Image |
|
import numpy as np |
|
|
|
from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_controlnet import * |
|
|
|
EXAMPLE_DOC_STRING = """ |
|
Examples: |
|
```py |
|
>>> # !pip install opencv-python transformers accelerate |
|
>>> from diffusers import StableDiffusionControlNetInpaintPipeline, ControlNetModel, UniPCMultistepScheduler |
|
>>> from diffusers.utils import load_image |
|
>>> import numpy as np |
|
>>> import torch |
|
>>> import cv2 |
|
>>> from PIL import Image |
|
>>> # download an image |
|
>>> image = load_image( |
|
... "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png" |
|
... ) |
|
>>> image = np.array(image) |
|
>>> mask_image = load_image( |
|
... "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png" |
|
... ) |
|
>>> mask_image = np.array(mask_image) |
|
>>> # get canny image |
|
>>> canny_image = cv2.Canny(image, 100, 200) |
|
>>> canny_image = canny_image[:, :, None] |
|
>>> canny_image = np.concatenate([canny_image, canny_image, canny_image], axis=2) |
|
>>> canny_image = Image.fromarray(canny_image) |
|
>>> # load control net and stable diffusion v1-5 |
|
>>> controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny", torch_dtype=torch.float16) |
|
>>> pipe = StableDiffusionControlNetInpaintPipeline.from_pretrained( |
|
... "runwayml/stable-diffusion-inpainting", controlnet=controlnet, torch_dtype=torch.float16 |
|
... ) |
|
>>> # speed up diffusion process with faster scheduler and memory optimization |
|
>>> pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config) |
|
>>> # remove following line if xformers is not installed |
|
>>> pipe.enable_xformers_memory_efficient_attention() |
|
>>> pipe.enable_model_cpu_offload() |
|
>>> # generate image |
|
>>> generator = torch.manual_seed(0) |
|
>>> image = pipe( |
|
... "futuristic-looking doggo", |
|
... num_inference_steps=20, |
|
... generator=generator, |
|
... image=image, |
|
... control_image=canny_image, |
|
... mask_image=mask_image |
|
... ).images[0] |
|
``` |
|
""" |
|
|
|
|
|
def prepare_mask_and_masked_image(image, mask): |
|
""" |
|
Prepares a pair (image, mask) to be consumed by the Stable Diffusion pipeline. This means that those inputs will be |
|
converted to ``torch.Tensor`` with shapes ``batch x channels x height x width`` where ``channels`` is ``3`` for the |
|
``image`` and ``1`` for the ``mask``. |
|
The ``image`` will be converted to ``torch.float32`` and normalized to be in ``[-1, 1]``. The ``mask`` will be |
|
binarized (``mask > 0.5``) and cast to ``torch.float32`` too. |
|
Args: |
|
image (Union[np.array, PIL.Image, torch.Tensor]): The image to inpaint. |
|
It can be a ``PIL.Image``, or a ``height x width x 3`` ``np.array`` or a ``channels x height x width`` |
|
``torch.Tensor`` or a ``batch x channels x height x width`` ``torch.Tensor``. |
|
mask (_type_): The mask to apply to the image, i.e. regions to inpaint. |
|
It can be a ``PIL.Image``, or a ``height x width`` ``np.array`` or a ``1 x height x width`` |
|
``torch.Tensor`` or a ``batch x 1 x height x width`` ``torch.Tensor``. |
|
Raises: |
|
ValueError: ``torch.Tensor`` images should be in the ``[-1, 1]`` range. ValueError: ``torch.Tensor`` mask |
|
should be in the ``[0, 1]`` range. ValueError: ``mask`` and ``image`` should have the same spatial dimensions. |
|
TypeError: ``mask`` is a ``torch.Tensor`` but ``image`` is not |
|
(ot the other way around). |
|
Returns: |
|
tuple[torch.Tensor]: The pair (mask, masked_image) as ``torch.Tensor`` with 4 |
|
dimensions: ``batch x channels x height x width``. |
|
""" |
|
if isinstance(image, torch.Tensor): |
|
if not isinstance(mask, torch.Tensor): |
|
raise TypeError(f"`image` is a torch.Tensor but `mask` (type: {type(mask)} is not") |
|
|
|
|
|
if image.ndim == 3: |
|
assert image.shape[0] == 3, "Image outside a batch should be of shape (3, H, W)" |
|
image = image.unsqueeze(0) |
|
|
|
|
|
if mask.ndim == 2: |
|
mask = mask.unsqueeze(0).unsqueeze(0) |
|
|
|
|
|
if mask.ndim == 3: |
|
|
|
if mask.shape[0] == 1: |
|
mask = mask.unsqueeze(0) |
|
|
|
|
|
else: |
|
mask = mask.unsqueeze(1) |
|
|
|
assert image.ndim == 4 and mask.ndim == 4, "Image and Mask must have 4 dimensions" |
|
assert image.shape[-2:] == mask.shape[-2:], "Image and Mask must have the same spatial dimensions" |
|
assert image.shape[0] == mask.shape[0], "Image and Mask must have the same batch size" |
|
|
|
|
|
if image.min() < -1 or image.max() > 1: |
|
raise ValueError("Image should be in [-1, 1] range") |
|
|
|
|
|
if mask.min() < 0 or mask.max() > 1: |
|
raise ValueError("Mask should be in [0, 1] range") |
|
|
|
|
|
mask[mask < 0.5] = 0 |
|
mask[mask >= 0.5] = 1 |
|
|
|
|
|
image = image.to(dtype=torch.float32) |
|
elif isinstance(mask, torch.Tensor): |
|
raise TypeError(f"`mask` is a torch.Tensor but `image` (type: {type(image)} is not") |
|
else: |
|
|
|
if isinstance(image, (PIL.Image.Image, np.ndarray)): |
|
image = [image] |
|
|
|
if isinstance(image, list) and isinstance(image[0], PIL.Image.Image): |
|
image = [np.array(i.convert("RGB"))[None, :] for i in image] |
|
image = np.concatenate(image, axis=0) |
|
elif isinstance(image, list) and isinstance(image[0], np.ndarray): |
|
image = np.concatenate([i[None, :] for i in image], axis=0) |
|
|
|
image = image.transpose(0, 3, 1, 2) |
|
image = torch.from_numpy(image).to(dtype=torch.float32) / 127.5 - 1.0 |
|
|
|
|
|
if isinstance(mask, (PIL.Image.Image, np.ndarray)): |
|
mask = [mask] |
|
|
|
if isinstance(mask, list) and isinstance(mask[0], PIL.Image.Image): |
|
mask = np.concatenate([np.array(m.convert("L"))[None, None, :] for m in mask], axis=0) |
|
mask = mask.astype(np.float32) / 255.0 |
|
elif isinstance(mask, list) and isinstance(mask[0], np.ndarray): |
|
mask = np.concatenate([m[None, None, :] for m in mask], axis=0) |
|
|
|
mask[mask < 0.5] = 0 |
|
mask[mask >= 0.5] = 1 |
|
mask = torch.from_numpy(mask) |
|
|
|
masked_image = image * (mask < 0.5) |
|
|
|
return mask, masked_image |
|
|
|
class StableDiffusionControlNetInpaintPipeline(StableDiffusionControlNetPipeline): |
|
r""" |
|
Pipeline for text-guided image inpainting using Stable Diffusion with ControlNet guidance. |
|
This model inherits from [`StableDiffusionControlNetPipeline`]. Check the superclass documentation for the generic methods the |
|
library implements for all the pipelines (such as downloading or saving, running on a particular device, etc.) |
|
Args: |
|
vae ([`AutoencoderKL`]): |
|
Variational Auto-Encoder (VAE) Model to encode and decode images to and from latent representations. |
|
text_encoder ([`CLIPTextModel`]): |
|
Frozen text-encoder. Stable Diffusion uses the text portion of |
|
[CLIP](https://huggingface.co/docs/transformers/model_doc/clip#transformers.CLIPTextModel), specifically |
|
the [clip-vit-large-patch14](https://huggingface.co/openai/clip-vit-large-patch14) variant. |
|
tokenizer (`CLIPTokenizer`): |
|
Tokenizer of class |
|
[CLIPTokenizer](https://huggingface.co/docs/transformers/v4.21.0/en/model_doc/clip#transformers.CLIPTokenizer). |
|
unet ([`UNet2DConditionModel`]): Conditional U-Net architecture to denoise the encoded image latents. |
|
controlnet ([`ControlNetModel`]): |
|
Provides additional conditioning to the unet during the denoising process |
|
scheduler ([`SchedulerMixin`]): |
|
A scheduler to be used in combination with `unet` to denoise the encoded image latents. Can be one of |
|
[`DDIMScheduler`], [`LMSDiscreteScheduler`], or [`PNDMScheduler`]. |
|
safety_checker ([`StableDiffusionSafetyChecker`]): |
|
Classification module that estimates whether generated images could be considered offensive or harmful. |
|
Please, refer to the [model card](https://huggingface.co/runwayml/stable-diffusion-v1-5) for details. |
|
feature_extractor ([`CLIPFeatureExtractor`]): |
|
Model that extracts features from generated images to be used as inputs for the `safety_checker`. |
|
""" |
|
|
|
def prepare_mask_latents( |
|
self, mask, masked_image, batch_size, height, width, dtype, device, generator, do_classifier_free_guidance |
|
): |
|
|
|
|
|
|
|
mask = torch.nn.functional.interpolate( |
|
mask, size=(height // self.vae_scale_factor, width // self.vae_scale_factor) |
|
) |
|
mask = mask.to(device=device, dtype=dtype) |
|
|
|
masked_image = masked_image.to(device=device, dtype=dtype) |
|
|
|
|
|
if isinstance(generator, list): |
|
masked_image_latents = [ |
|
self.vae.encode(masked_image[i : i + 1]).latent_dist.sample(generator=generator[i]) |
|
for i in range(batch_size) |
|
] |
|
masked_image_latents = torch.cat(masked_image_latents, dim=0) |
|
else: |
|
masked_image_latents = self.vae.encode(masked_image).latent_dist.sample(generator=generator) |
|
masked_image_latents = self.vae.config.scaling_factor * masked_image_latents |
|
|
|
|
|
if mask.shape[0] < batch_size: |
|
if not batch_size % mask.shape[0] == 0: |
|
raise ValueError( |
|
"The passed mask and the required batch size don't match. Masks are supposed to be duplicated to" |
|
f" a total batch size of {batch_size}, but {mask.shape[0]} masks were passed. Make sure the number" |
|
" of masks that you pass is divisible by the total requested batch size." |
|
) |
|
mask = mask.repeat(batch_size // mask.shape[0], 1, 1, 1) |
|
if masked_image_latents.shape[0] < batch_size: |
|
if not batch_size % masked_image_latents.shape[0] == 0: |
|
raise ValueError( |
|
"The passed images and the required batch size don't match. Images are supposed to be duplicated" |
|
f" to a total batch size of {batch_size}, but {masked_image_latents.shape[0]} images were passed." |
|
" Make sure the number of images that you pass is divisible by the total requested batch size." |
|
) |
|
masked_image_latents = masked_image_latents.repeat(batch_size // masked_image_latents.shape[0], 1, 1, 1) |
|
|
|
mask = torch.cat([mask] * 2) if do_classifier_free_guidance else mask |
|
masked_image_latents = ( |
|
torch.cat([masked_image_latents] * 2) if do_classifier_free_guidance else masked_image_latents |
|
) |
|
|
|
|
|
masked_image_latents = masked_image_latents.to(device=device, dtype=dtype) |
|
return mask, masked_image_latents |
|
|
|
@torch.no_grad() |
|
@replace_example_docstring(EXAMPLE_DOC_STRING) |
|
def __call__( |
|
self, |
|
prompt: Union[str, List[str]] = None, |
|
image: Union[torch.FloatTensor, PIL.Image.Image] = None, |
|
control_image: Union[torch.FloatTensor, PIL.Image.Image, List[torch.FloatTensor], List[PIL.Image.Image]] = None, |
|
mask_image: Union[torch.FloatTensor, PIL.Image.Image] = None, |
|
height: Optional[int] = None, |
|
width: Optional[int] = None, |
|
num_inference_steps: int = 50, |
|
guidance_scale: float = 7.5, |
|
negative_prompt: Optional[Union[str, List[str]]] = None, |
|
num_images_per_prompt: Optional[int] = 1, |
|
eta: float = 0.0, |
|
generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None, |
|
latents: Optional[torch.FloatTensor] = None, |
|
prompt_embeds: Optional[torch.FloatTensor] = None, |
|
negative_prompt_embeds: Optional[torch.FloatTensor] = None, |
|
output_type: Optional[str] = "pil", |
|
return_dict: bool = True, |
|
callback: Optional[Callable[[int, int, torch.FloatTensor], None]] = None, |
|
callback_steps: int = 1, |
|
cross_attention_kwargs: Optional[Dict[str, Any]] = None, |
|
controlnet_conditioning_scale: float = 1.0, |
|
): |
|
r""" |
|
Function invoked when calling the pipeline for generation. |
|
Args: |
|
prompt (`str` or `List[str]`, *optional*): |
|
The prompt or prompts to guide the image generation. If not defined, one has to pass `prompt_embeds`. |
|
instead. |
|
image (`PIL.Image.Image`): |
|
`Image`, or tensor representing an image batch which will be inpainted, *i.e.* parts of the image will |
|
be masked out with `mask_image` and repainted according to `prompt`. |
|
control_image (`torch.FloatTensor`, `PIL.Image.Image`, `List[torch.FloatTensor]` or `List[PIL.Image.Image]`): |
|
The ControlNet input condition. ControlNet uses this input condition to generate guidance to Unet. If |
|
the type is specified as `Torch.FloatTensor`, it is passed to ControlNet as is. PIL.Image.Image` can |
|
also be accepted as an image. The control image is automatically resized to fit the output image. |
|
mask_image (`PIL.Image.Image`): |
|
`Image`, or tensor representing an image batch, to mask `image`. White pixels in the mask will be |
|
repainted, while black pixels will be preserved. If `mask_image` is a PIL image, it will be converted |
|
to a single channel (luminance) before use. If it's a tensor, it should contain one color channel (L) |
|
instead of 3, so the expected shape would be `(B, H, W, 1)`. |
|
height (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor): |
|
The height in pixels of the generated image. |
|
width (`int`, *optional*, defaults to self.unet.config.sample_size * self.vae_scale_factor): |
|
The width in pixels of the generated image. |
|
num_inference_steps (`int`, *optional*, defaults to 50): |
|
The number of denoising steps. More denoising steps usually lead to a higher quality image at the |
|
expense of slower inference. |
|
guidance_scale (`float`, *optional*, defaults to 7.5): |
|
Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://arxiv.org/abs/2207.12598). |
|
`guidance_scale` is defined as `w` of equation 2. of [Imagen |
|
Paper](https://arxiv.org/pdf/2205.11487.pdf). Guidance scale is enabled by setting `guidance_scale > |
|
1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`, |
|
usually at the expense of lower image quality. |
|
negative_prompt (`str` or `List[str]`, *optional*): |
|
The prompt or prompts not to guide the image generation. If not defined, one has to pass |
|
`negative_prompt_embeds`. instead. If not defined, one has to pass `negative_prompt_embeds`. instead. |
|
Ignored when not using guidance (i.e., ignored if `guidance_scale` is less than `1`). |
|
num_images_per_prompt (`int`, *optional*, defaults to 1): |
|
The number of images to generate per prompt. |
|
eta (`float`, *optional*, defaults to 0.0): |
|
Corresponds to parameter eta (η) in the DDIM paper: https://arxiv.org/abs/2010.02502. Only applies to |
|
[`schedulers.DDIMScheduler`], will be ignored for others. |
|
generator (`torch.Generator` or `List[torch.Generator]`, *optional*): |
|
One or a list of [torch generator(s)](https://pytorch.org/docs/stable/generated/torch.Generator.html) |
|
to make generation deterministic. |
|
latents (`torch.FloatTensor`, *optional*): |
|
Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image |
|
generation. Can be used to tweak the same generation with different prompts. If not provided, a latents |
|
tensor will ge generated by sampling using the supplied random `generator`. |
|
prompt_embeds (`torch.FloatTensor`, *optional*): |
|
Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not |
|
provided, text embeddings will be generated from `prompt` input argument. |
|
negative_prompt_embeds (`torch.FloatTensor`, *optional*): |
|
Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt |
|
weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input |
|
argument. |
|
output_type (`str`, *optional*, defaults to `"pil"`): |
|
The output format of the generate image. Choose between |
|
[PIL](https://pillow.readthedocs.io/en/stable/): `PIL.Image.Image` or `np.array`. |
|
return_dict (`bool`, *optional*, defaults to `True`): |
|
Whether or not to return a [`~pipelines.stable_diffusion.StableDiffusionPipelineOutput`] instead of a |
|
plain tuple. |
|
callback (`Callable`, *optional*): |
|
A function that will be called every `callback_steps` steps during inference. The function will be |
|
called with the following arguments: `callback(step: int, timestep: int, latents: torch.FloatTensor)`. |
|
callback_steps (`int`, *optional*, defaults to 1): |
|
The frequency at which the `callback` function will be called. If not specified, the callback will be |
|
called at every step. |
|
cross_attention_kwargs (`dict`, *optional*): |
|
A kwargs dictionary that if specified is passed along to the `AttnProcessor` as defined under |
|
`self.processor` in |
|
[diffusers.cross_attention](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/cross_attention.py). |
|
controlnet_conditioning_scale (`float`, *optional*, defaults to 1.0): |
|
The outputs of the controlnet are multiplied by `controlnet_conditioning_scale` before they are added |
|
to the residual in the original unet. |
|
Examples: |
|
Returns: |
|
[`~pipelines.stable_diffusion.StableDiffusionPipelineOutput`] or `tuple`: |
|
[`~pipelines.stable_diffusion.StableDiffusionPipelineOutput`] if `return_dict` is True, otherwise a `tuple. |
|
When returning a tuple, the first element is a list with the generated images, and the second element is a |
|
list of `bool`s denoting whether the corresponding generated image likely represents "not-safe-for-work" |
|
(nsfw) content, according to the `safety_checker`. |
|
""" |
|
|
|
height, width = self._default_height_width(height, width, control_image) |
|
|
|
|
|
self.check_inputs( |
|
prompt, control_image, height, width, callback_steps, negative_prompt, prompt_embeds, negative_prompt_embeds |
|
) |
|
|
|
|
|
if prompt is not None and isinstance(prompt, str): |
|
batch_size = 1 |
|
elif prompt is not None and isinstance(prompt, list): |
|
batch_size = len(prompt) |
|
else: |
|
batch_size = prompt_embeds.shape[0] |
|
|
|
device = self._execution_device |
|
|
|
|
|
|
|
do_classifier_free_guidance = guidance_scale > 1.0 |
|
|
|
|
|
prompt_embeds = self._encode_prompt( |
|
prompt, |
|
device, |
|
num_images_per_prompt, |
|
do_classifier_free_guidance, |
|
negative_prompt, |
|
prompt_embeds=prompt_embeds, |
|
negative_prompt_embeds=negative_prompt_embeds, |
|
) |
|
|
|
|
|
control_image = self.prepare_image( |
|
control_image, |
|
width, |
|
height, |
|
batch_size * num_images_per_prompt, |
|
num_images_per_prompt, |
|
device, |
|
self.controlnet.dtype, |
|
) |
|
|
|
if do_classifier_free_guidance: |
|
control_image = torch.cat([control_image] * 2) |
|
|
|
|
|
self.scheduler.set_timesteps(num_inference_steps, device=device) |
|
timesteps = self.scheduler.timesteps |
|
|
|
|
|
num_channels_latents = self.controlnet.in_channels |
|
latents = self.prepare_latents( |
|
batch_size * num_images_per_prompt, |
|
num_channels_latents, |
|
height, |
|
width, |
|
prompt_embeds.dtype, |
|
device, |
|
generator, |
|
latents, |
|
) |
|
|
|
|
|
mask, masked_image = prepare_mask_and_masked_image(image, mask_image) |
|
mask, masked_image_latents = self.prepare_mask_latents( |
|
mask, |
|
masked_image, |
|
batch_size * num_images_per_prompt, |
|
height, |
|
width, |
|
prompt_embeds.dtype, |
|
device, |
|
generator, |
|
do_classifier_free_guidance, |
|
) |
|
|
|
|
|
extra_step_kwargs = self.prepare_extra_step_kwargs(generator, eta) |
|
|
|
|
|
num_warmup_steps = len(timesteps) - num_inference_steps * self.scheduler.order |
|
with self.progress_bar(total=num_inference_steps) as progress_bar: |
|
for i, t in enumerate(timesteps): |
|
|
|
latent_model_input = torch.cat([latents] * 2) if do_classifier_free_guidance else latents |
|
latent_model_input = self.scheduler.scale_model_input(latent_model_input, t) |
|
|
|
down_block_res_samples, mid_block_res_sample = self.controlnet( |
|
latent_model_input, |
|
t, |
|
encoder_hidden_states=prompt_embeds, |
|
controlnet_cond=control_image, |
|
return_dict=False, |
|
) |
|
|
|
down_block_res_samples = [ |
|
down_block_res_sample * controlnet_conditioning_scale |
|
for down_block_res_sample in down_block_res_samples |
|
] |
|
mid_block_res_sample *= controlnet_conditioning_scale |
|
|
|
|
|
latent_model_input = torch.cat([latent_model_input, mask, masked_image_latents], dim=1) |
|
noise_pred = self.unet( |
|
latent_model_input, |
|
t, |
|
encoder_hidden_states=prompt_embeds, |
|
cross_attention_kwargs=cross_attention_kwargs, |
|
down_block_additional_residuals=down_block_res_samples, |
|
mid_block_additional_residual=mid_block_res_sample, |
|
).sample |
|
|
|
|
|
if do_classifier_free_guidance: |
|
noise_pred_uncond, noise_pred_text = noise_pred.chunk(2) |
|
noise_pred = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond) |
|
|
|
|
|
latents = self.scheduler.step(noise_pred, t, latents, **extra_step_kwargs).prev_sample |
|
|
|
|
|
if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0): |
|
progress_bar.update() |
|
if callback is not None and i % callback_steps == 0: |
|
callback(i, t, latents) |
|
|
|
|
|
|
|
if hasattr(self, "final_offload_hook") and self.final_offload_hook is not None: |
|
self.unet.to("cpu") |
|
self.controlnet.to("cpu") |
|
torch.cuda.empty_cache() |
|
|
|
if output_type == "latent": |
|
image = latents |
|
has_nsfw_concept = None |
|
elif output_type == "pil": |
|
|
|
image = self.decode_latents(latents) |
|
|
|
|
|
image, has_nsfw_concept = self.run_safety_checker(image, device, prompt_embeds.dtype) |
|
|
|
|
|
image = self.numpy_to_pil(image) |
|
else: |
|
|
|
image = self.decode_latents(latents) |
|
|
|
|
|
image, has_nsfw_concept = self.run_safety_checker(image, device, prompt_embeds.dtype) |
|
|
|
|
|
if hasattr(self, "final_offload_hook") and self.final_offload_hook is not None: |
|
self.final_offload_hook.offload() |
|
|
|
if not return_dict: |
|
return (image, has_nsfw_concept) |
|
|
|
return StableDiffusionPipelineOutput(images=image, nsfw_content_detected=has_nsfw_concept) |