huggingface / huggingface/diffusers
Running `diffusers/stable-diffusion-xl-1.0-inpainting-0.1` with `StableDiffusionXLInpaintPipeline` introduces weird noise when strength is set to 1.0 during inference
- Dominant language
- Python
- Stars
- 34.5k
- Forks
- 7.3k
- Avg merge
- 3d 3h
- Merged PRs (30d)
- 91
Description
### Describe the bug
I am currently comparing the inpainting generated results between the [diffusers/stable-diffusion-xl-1.0-inpainting-0.1](https://huggingface.co/diffusers/stable-diffusion-xl-1.0-inpainting-0.1) model and the [stabilityai/stable-diffusion-2-inpainting](https://huggingface.co/stabilityai/stable-diffusion-2-inpainting) model, and I noticed that the `strength` parameter in the `__call__()` function in `StableDiffusionInpaintPipeline` defaults to 1.0 whereas the `strength` parameter in the `__call__()` function in `StableDiffusionXLInpaintPipeline` default to 0.9999.
What I want to achieve is that I want to use `strength=1.0` in the `StableDiffusionXLInpaintPipeline` pipeline because otherwise the original content of the image has a much larger impact than the prompt does on the generated result. For example, if the original image has a blue car, and my prompt describes a pink car, using `strength=0.9999` or even `strength=0.99999999` would still show a blue car in the generated result. And the only way that I can effectively avoid this behavior is by setting `strength=1.0` when using the `StableDiffusionXLInpaintPipeline` pipeline. However, using `strength=1.0` in the `StableDiffusionXLInpaintPipeline` pipeline introduces a lot of noises in the generated image, and I have tried increasing the number of inference steps but it does not help with removing the noises.
For example, the following are the original image (with white pixels added in the margin to better illustrate the weird noises) and the original image's corresponding mask as well as the inpainted results of the two pipelines. And the result from the `StableDiffusionXLInpaintPipeline` pipeline has a lot of noises.
P.S. I also read something that sounds similar in https://github.com/huggingface/diffusers/issues/4392, but am not sure if the noises that I am seeing here is the same thing as what's discussed in that issue. Plus I would like to know how I can resolve the weird noises issue when using `StableDiffusionXLInpaintPipeline` with the [diffusers/stable-diffusion-xl-1.0-inpainting-0.1](https://huggingface.co/diffusers/stable-diffusion-xl-1.0-inpainting-0.1) model with `strength=1.0`.
**Original Image:**

**Original Image's Corresponding Mask:**

**Inpainted Result (`StableDiffusionInpaintPipeline` with `strength=1.0` and the prompt `"Furry lion sitting on a bench, high quality, 4k"`)**

**Inpainted Result (`StableDiffusionXLInpaintPipeline` with `strength=1.0` and the prompt `"Furry lion sitting on a bench, high quality, 4k"`)**

### Reproduction
The following is the code I used to generate the inpainted result for both the `StableDiffusionInpaintPipeline` (with the [stabilityai/stable-diffusion-2-inpainting](https://huggingface.co/stabilityai/stable-diffusion-2-inpainting) model) and the `StableDiffusionXLInpaintPipeline` (with the [diffusers/stable-diffusion-xl-1.0-inpainting-0.1](https://huggingface.co/diffusers/stable-diffusion-xl-1.0-inpainting-0.1) model). You can change the boolean value on the line `USE_SDXL_INPAINT = True # <=== Change this` to generate inpainted result of the respective pipeline/model. I have also pasted the original image and its corresponding mask image that I used in the "Describe the bug" section above.
Code:
```
import os
import numpy as np
import torch
from PIL import Image
from diffusers import StableDiffusionInpaintPipeline, StableDiffusionXLInpaintPipeline
os.chdir(os.path.dirname(os.path.abspath(__file__)))
USE_SDXL_INPAINT = True # <=== Change this
def main():
image_pil = Image.open("./dog.png")
mask_pil = Image.open("./dog_mask.png")
if USE_SDXL_INPAINT:
image_pil = image_pil.resize((1024, 1024))
mask_pil = mask_pil.resize((1024, 1024))
image_np = np.array(image_pil)
mask_np = np.array(mask_pil)
image_torch = torch.from_numpy(np.expand_dims(np.transpose(image_np / 255, (2, 0, 1)), 0).astype(np.float16)).cuda()
print("image_torch.size():", image_torch.size())
print("image_torch.dtype:", image_torch.dtype)
mask_torch = torch.from_numpy(np.expand_dims(np.transpose(np.expand_dims(mask_np[:, :, 0] / 255, -1), (2, 0, 1)), 0).astype(np.float16)).cuda()
print("mask_torch.size():", mask_torch.size())
print("mask_torch.dtype:", mask_torch.dtype)
if USE_SDXL_INPAINT:
pipe = StableDiffusionXLInpaintPipeline.from_pretrained("../stable-diffusion-xl-1.0-inpainting-0.1", torch_dtype=torch.float16, use_safetensors=True).to("cuda")
else:
pipe = StableDiffusionInpaintPipeline.from_pretrained("../stable-diffusion-2-inpainting", torch_dtype=torch.float16).to("cuda")
pipe.enable_xformers_memory_efficient_attention()
results = pipe(
prompt="Furry lion sitting on a bench, high quality, 4k",
image=image_torch,
mask_image=mask_torch,
strength=1.0,
num_inference_steps=50,
generator=torch.Generator("cuda").manual_seed(123),
output_type="np"
)
gen_image = results.images[0]
gen_image_pil = Image.fromarray((gen_image * 255).round().astype(np.uint8).clip(0, 255))
if USE_SDXL_INPAINT:
gen_image_pil.save("./sd_xl_gen_image.png")
else:
gen_image_pil.save("./sd_2_gen_image.png")
if __name__ == "__main__":
main()
```
### Logs
_No response_
### System Info
System: Windows
GPU: RTX 3090
**`diffusers-cli env` output**
- `diffusers` version: 0.27.2
- Platform: Windows-10
- Python version: 3.11.3
- PyTorch version (GPU?): 2.2.2+cu121 (True)
- Huggingface_hub version: 0.22.2
- Transformers version: 4.39.3
- Accelerate version: 0.29.2
- xFormers version: 0.0.25.post1
- Using GPU in script?:
- Using distributed or parallel set-up in script?:
### Who can help?
@yiyixuxu @sayakpaul
Contributor guide
Assessment
This issue has not been assessed yet.