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

Đang mở
#8,450 15 bình luận 0 reaction 0 người được giao Xem trên GitHub
bug stale
Ngôn ngữ chính
Python
Star
34.5k
Fork
7.3k
Merge trung bình
3 ngày 3 giờ
Pull request đã merge (30 ngày)
91

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Hướng nghiên cứu

Bắt đầu bằng cách chạy reproduction được cung cấp với StableDiffusionXLInpaintPipeline và so sánh cách xử lý strength trong __call__ với StableDiffusionInpaintPipeline. Theo dõi đường đi của strength=1.0 và xác định tại sao nó tạo ra nhiễu đối với mô hình inpainting SDXL được nêu tên; được xem là hoàn thành khi hành vi này đã được giải thích và pipeline không còn tạo ra nhiễu được báo cáo mà không làm suy yếu việc thay thế dựa trên prompt.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python, pytorch
Lĩnh vực
machine-learning
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.