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

Abierto
#8,450 15 comentarios 0 reacciones 0 asignados Ver en GitHub
bug stale
Lenguaje dominante
Python
Estrellas
34.5k
Forks
7.3k
Merge medio
3 d 3 h
PR fusionados (30 d)
91

Descripción

### 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:**
![dog](https://github.com/huggingface/diffusers/assets/28615340/e7bb8217-5585-4846-80dd-19fe65c05f77)
**Original Image's Corresponding Mask:**
![dog_mask](https://github.com/huggingface/diffusers/assets/28615340/8300f8bb-1f2f-4296-b126-45ac4fedc24c)
**Inpainted Result (`StableDiffusionInpaintPipeline` with `strength=1.0` and the prompt `"Furry lion sitting on a bench, high quality, 4k"`)**
![sd_2_gen_image](https://github.com/huggingface/diffusers/assets/28615340/0c5df504-f41a-49c1-96fc-0708be54f526)
**Inpainted Result (`StableDiffusionXLInpaintPipeline` with `strength=1.0` and the prompt `"Furry lion sitting on a bench, high quality, 4k"`)**
![sd_xl_gen_image](https://github.com/huggingface/diffusers/assets/28615340/63ef433f-efba-4f3c-96ea-fb949fdeea5b)

### 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

Guía de contribución

Abrir la guía de contribución

Línea de trabajo

Start by running the supplied reproduction with StableDiffusionXLInpaintPipeline and compare its __call__ strength handling with StableDiffusionInpaintPipeline. Trace the strength=1.0 path and determine why it produces noise for the named SDXL inpainting model; done means the behavior is explained and the pipeline no longer produces the reported noise without weakening prompt-driven replacement.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python, pytorch
Área
machine-learning
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
25/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.