Skip to content

Commit a551ddf

Browse files
authored
[docs] mask_blur and padding_mask_crop (#6498)
new inpaint features
1 parent 1d57892 commit a551ddf

File tree

1 file changed

+68
-5
lines changed

1 file changed

+68
-5
lines changed

docs/source/en/using-diffusers/inpaint.md

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,42 @@ Throughout this guide, the mask image is provided in all of the code examples fo
7777
Upload a base image to inpaint on and use the sketch tool to draw a mask. Once you're done, click **Run** to generate and download the mask image.
7878

7979
<iframe
80-
src="https://stevhliu-inpaint-mask-maker.hf.space"
81-
frameborder="0"
82-
width="850"
83-
height="450"
80+
src="https://stevhliu-inpaint-mask-maker.hf.space"
81+
frameborder="0"
82+
width="850"
83+
height="450"
8484
></iframe>
8585
86+
### Mask blur
87+
88+
The [`~VaeImageProcessor.blur`] method provides an option for how to blend the original image and inpaint area. The amount of blur is determined by the `blur_factor` parameter. Increasing the `blur_factor` increases the amount of blur applied to the mask edges, softening the transition between the original image and inpaint area. A low or zero `blur_factor` preserves the sharper edges of the mask.
89+
90+
To use this, create a blurred mask with the image processor.
91+
92+
```py
93+
import torch
94+
from diffusers import AutoPipelineForInpainting
95+
from diffusers.utils import load_image
96+
from PIL import Image
97+
98+
pipeline = AutoPipelineForInpainting.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16).to('cuda')
99+
100+
mask = load_image("https://huggingface.co/datasets/YiYiXu/testing-images/resolve/main/seashore_mask.png")
101+
blurred_mask = pipeline.mask_processor.blur(mask, blur_factor=33)
102+
blurred_mask
103+
```
104+
105+
<div class="flex gap-4">
106+
<div>
107+
<img class="rounded-xl" src="https://huggingface.co/datasets/YiYiXu/testing-images/resolve/main/seashore_mask.png"/>
108+
<figcaption class="mt-2 text-center text-sm text-gray-500">mask with no blur</figcaption>
109+
</div>
110+
<div>
111+
<img class="rounded-xl" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/mask_blurred.png"/>
112+
<figcaption class="mt-2 text-center text-sm text-gray-500">mask with blur applied</figcaption>
113+
</div>
114+
</div>
115+
86116
## Popular models
87117

88118
[Stable Diffusion Inpainting](https://huggingface.co/runwayml/stable-diffusion-inpainting), [Stable Diffusion XL (SDXL) Inpainting](https://huggingface.co/diffusers/stable-diffusion-xl-1.0-inpainting-0.1), and [Kandinsky 2.2 Inpainting](https://huggingface.co/kandinsky-community/kandinsky-2-2-decoder-inpaint) are among the most popular models for inpainting. SDXL typically produces higher resolution images than Stable Diffusion v1.5, and Kandinsky 2.2 is also capable of generating high-quality images.
@@ -318,7 +348,7 @@ make_image_grid([init_image, image], rows=1, cols=2)
318348

319349
The trade-off of using a non-inpaint specific checkpoint is the overall image quality may be lower, but it generally tends to preserve the mask area (that is why you can see the mask outline). The inpaint specific checkpoints are intentionally trained to generate higher quality inpainted images, and that includes creating a more natural transition between the masked and unmasked areas. As a result, these checkpoints are more likely to change your unmasked area.
320350

321-
If preserving the unmasked area is important for your task, you can use the `apply_overlay` method of [`VaeImageProcessor`] to force the unmasked area of an image to remain the same at the expense of some more unnatural transitions between the masked and unmasked areas.
351+
If preserving the unmasked area is important for your task, you can use the [`VaeImageProcessor.apply_overlay`] method to force the unmasked area of an image to remain the same at the expense of some more unnatural transitions between the masked and unmasked areas.
322352

323353
```py
324354
import PIL
@@ -475,6 +505,39 @@ make_image_grid([init_image, mask_image, image], rows=1, cols=3)
475505
</figure>
476506
</div>
477507

508+
### Padding mask crop
509+
510+
A method for increasing the inpainting image quality is to use the [`padding_mask_crop`](https://huggingface.co/docs/diffusers/v0.25.0/en/api/pipelines/stable_diffusion/inpaint#diffusers.StableDiffusionInpaintPipeline.__call__.padding_mask_crop) parameter. When enabled, this option crops the masked area with some user-specified padding and it'll also crop the same area from the original image. Both the image and mask are upscaled to a higher resolution for inpainting, and then overlaid on the original image. This is a quick and easy way to improve image quality without using a separate pipeline like [`StableDiffusionUpscalePipeline`].
511+
512+
Add the `padding_mask_crop` parameter to the pipeline call and set it to the desired padding value.
513+
514+
```py
515+
import torch
516+
from diffusers import AutoPipelineForInpainting
517+
from diffusers.utils import load_image
518+
from PIL import Image
519+
520+
generator = torch.Generator(device='cuda').manual_seed(0)
521+
pipeline = AutoPipelineForInpainting.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16).to('cuda')
522+
523+
base = load_image("https://huggingface.co/datasets/YiYiXu/testing-images/resolve/main/seashore.png")
524+
mask = load_image("https://huggingface.co/datasets/YiYiXu/testing-images/resolve/main/seashore_mask.png")
525+
526+
image = pipeline("boat", image=base, mask_image=mask, strength=0.75, generator=generator, padding_mask_crop=32).images[0]
527+
image
528+
```
529+
530+
<div class="flex gap-4">
531+
<div>
532+
<img class="rounded-xl" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/baseline_inpaint.png"/>
533+
<figcaption class="mt-2 text-center text-sm text-gray-500">default inpaint image</figcaption>
534+
</div>
535+
<div>
536+
<img class="rounded-xl" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/padding_mask_crop_inpaint.png"/>
537+
<figcaption class="mt-2 text-center text-sm text-gray-500">inpaint image with `padding_mask_crop` enabled</figcaption>
538+
</div>
539+
</div>
540+
478541
## Chained inpainting pipelines
479542

480543
[`AutoPipelineForInpainting`] can be chained with other 🤗 Diffusers pipelines to edit their outputs. This is often useful for improving the output quality from your other diffusion pipelines, and if you're using multiple pipelines, it can be more memory-efficient to chain them together to keep the outputs in latent space and reuse the same pipeline components.

0 commit comments

Comments
 (0)