diffusers icon indicating copy to clipboard operation
diffusers copied to clipboard

Image results of Diffusers are quite different, how can I get same image as WebUI with diffusers

Open yibolu96 opened this issue 2 years ago • 9 comments

Describe the bug

I found that the Image results of diffusers and Webui are quite different. Don't know what I did wrong @yiyixuxu @sayakpaul @DN6

WebUI 00003-123

Diffusers: res_0

Reproduction

Here is the webui setup: image

Here is my diffusers script:

import torch
import time
import GPUtil
from diffusers import StableDiffusionXLPipeline, EulerDiscreteScheduler

model_path = "sd_xl_base"

start = time.perf_counter()
model = StableDiffusionXLPipeline.from_pretrained(model_path, safety_checker=None).to(
    torch_device="cuda", torch_dtype=torch.float16)
print(f"model load time: {time.perf_counter() - start}")

model.scheduler = EulerDiscreteScheduler.from_pretrained(
    model_path, subfolder="scheduler")

prompt = "a girl"
negative_prompt = ""
height, width = 1024, 1024
steps = 20
guidance_scale = 7.0
generator = torch.Generator("cuda").manual_seed(123)
num_images = 1

start = time.perf_counter()
images = model(prompt=prompt, height=height, width=width, num_inference_steps=steps,
               guidance_scale=guidance_scale, negative_prompt=negative_prompt, num_images_per_prompt=num_images,
               generator=generator).images
print("image gen cost: ", time.perf_counter() - start)

for i, image in enumerate(images):
    image.save(f"res_2_{i}.png")

I also tried StableDiffusionXLKDiffusionPipeline

import torch
import time
from diffusers import StableDiffusionXLKDiffusionPipeline

model_path = "sd_xl_base_1.0_0.9vae.safetensors"

start = time.perf_counter()
model = StableDiffusionXLKDiffusionPipeline.from_single_file(model_path, safety_checker=None).to(
    torch_device="cuda", torch_dtype=torch.float16)
print(f"model load time: {time.perf_counter() - start}")

model.set_scheduler("sample_euler")

prompt = "a girl"
negative_prompt = ""
height, width = 1024, 1024
steps = 20
guidance_scale = 7.0
generator = torch.Generator("cuda").manual_seed(123)
num_images = 1
start = time.perf_counter()


images = model(prompt=prompt, height=height, width=width, num_inference_steps=steps,
               guidance_scale=guidance_scale, negative_prompt=negative_prompt, num_images_per_prompt=num_images,
               generator=generator).images
print("image gen cost: ", time.perf_counter() - start)
for i, image in enumerate(images):
    image.save(f"res_{i}.png")

But still, the generated image is not the same as webui's. res_2_0

Logs

No response

System Info

  • diffusers version: 0.26.3
  • Python version: 3.10.9
  • PyTorch version (GPU?): 2.0.1+cu117 (True)
  • Huggingface_hub version: 0.21.3
  • Transformers version: 4.30.2
  • Accelerate version: 0.21.0
  • xFormers version: 0.0.21
  • Using GPU in script?: A10

Who can help?

No response

yibolu96 avatar Mar 22 '24 07:03 yibolu96

First of all, you should use the latest diffusers version.

Also, what is sd_xl_base? Is it stabilityai/stable-diffusion-xl-base-1.0 and with that you're using the VAE from SDXL 0.9?

Furthermore, IIUC you're using the refiner in the WebUI as well as the HighRes fix which are not being done in diffusers.

This is a little vague of a comparison IMO. Maybe @asomoza has more inputs on this?

Cc: @yiyixuxu

sayakpaul avatar Mar 22 '24 07:03 sayakpaul

Hi, @sayakpaul, thanks for the fast reply! Btw, this issue is more like a "Help Needed Issue", but I didn't find this option when creating this issue.

And, the sd_xl_base is just the diffusers format of sd_xl_base_1.0_0.9vae.safetensors which is the model here "https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/blob/main/sd_xl_base_1.0_0.9vae.safetensors". Sorry that I didn't give it a good name.

Besides, IIUC I didn't use the refiner and HighRes fix in WebUI.

I just tried the latest diffusers 0.27.2, but I don't see a big difference comparing to the result above. Do I miss something? res_2_0

yibolu96 avatar Mar 22 '24 11:03 yibolu96

There has to be something wrong in your code. "https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/blob/main/sd_xl_base_1.0_0.9vae.safetensors" cannot be loaded with from_pretrained().

sayakpaul avatar Mar 22 '24 11:03 sayakpaul

There has to be something wrong in your code. "https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/blob/main/sd_xl_base_1.0_0.9vae.safetensors" cannot be loaded with from_pretrained().

Yes, I used this script down here to convert the model in diffusers format.

pipe = StableDiffusionXLPipeline.from_single_file("sd_xl_base_1.0_0.9vae.safetensors")
pipe.save_pretrained("sd_xl_base")

yibolu96 avatar Mar 22 '24 11:03 yibolu96

I think what @yibolu96 is trying to say is that with the same seed, model and scheduler the generated image is not the exact same image as the one in automatic1111.

If this is what you're asking, that's not an error, diffusers doesn't use the original implementation for Stable Diffusion and even with that, the results probably wouldn't be the same, even if you use Forge or SD.Next which are forks from automatic1111 they won't give you the exact same image.

If this is not the case, I don't see that the image with diffusers is worse, just a black and white variation of it.

asomoza avatar Mar 22 '24 11:03 asomoza

I think what @yibolu96 is trying to say is that with the same seed, model and scheduler the generated image is not the exact same image as the one in automatic1111.

If this is what you're asking, that's not an error, diffusers doesn't use the original implementation for Stable Diffusion and even with that, the results probably wouldn't be the same, even if you use Forge or SD.Next which are forks from automatic1111 they won't give you the exact same image.

If this is not the case, I don't see that the image with diffusers is worse, just a black and white variation of it.

@asomoza Thanks, that's exactly what I was trying to say.

But I don't see a huge difference between diffusers' and the original's implementations on SDXL. And I did get the same images with SD1.5 before. Maybe there are some special options of the pipeline to change in the config or something that I can do to make this happen with SDXL?

yibolu96 avatar Mar 22 '24 12:03 yibolu96

I am getting this image:

base_1 0_0 9vae

At least the image is not black and white.

I don't know maybe the precision is a problem? The VAE attached to the checkpoint is known to have numerical problems.

from diffusers import StableDiffusionXLPipeline
import torch


pipeline = StableDiffusionXLPipeline.from_single_file("https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/blob/main/sd_xl_base_1.0_0.9vae.safetensors").to("cuda")

image = pipeline(
	prompt="a girl", 
	num_inference_steps=20,
	guidance_scale=7.0, 
	negative_prompt="", 
	generator=torch.manual_seed(123)
).images[0]
image.save("base_1.0_0.9vae.png")

sayakpaul avatar Mar 22 '24 12:03 sayakpaul

It depends on a lot of factors. The most important one here is that your prompt is too simple, with that you're giving the model too much freedom to alter the generation.

Also as @sayakpaul mentioned, the precision plays a role, also the torch.type (I use bfloat and I never get the same results than the other people).

Getting the exact same result is a really hard to get coincidence, back when I used automatic1111 I couldn't even get the same results of the people that posted the images on civitai. I don't know what did you do with SD 1.5, I would like a lot to see those examples to test them.

If you don't care about the method to get it, you can use a combination of IP Adapter with controlnet but that is like cheating.

If I use the exact same model with the exact same parameters I get this in ComfyUI which is closer to the diffusers result:

ComfyUI_00297_

asomoza avatar Mar 22 '24 13:03 asomoza

I think precision definitely is part of the problem here. Closing the Issue now, and thanks for your help.

yibolu96 avatar Mar 22 '24 14:03 yibolu96