Stable Diffusion XL简介

Stable Diffusion XL的是一个文生图模型,是原来Stable Diffusion的升级版。相比旧版的Stable Diffusion模型,Stable Diffusion XL主要的不同有三点:

  1. 有一个精化模型(下图的Refiner),通过image-to-image的方式来提高视觉保真度。
  2. 使用了两个text encoder,OpenCLIP ViT-bigG和CLIP ViT-L。
  3. 增加了图片大小和长宽比作为输入条件。

SDXL与以前SD结构的不同如下图:

代码示例

加载基础和精化两个模型,并生成图片:

from diffusers import DiffusionPipeline
import torch

base = DiffusionPipeline.from_pretrained(r"D:\hg_models\stabilityai\stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True).to("cuda")
refiner = DiffusionPipeline.from_pretrained(r"D:\hg_models\stabilityai\stable-diffusion-xl-refiner-1.0", text_encoder_2=base.text_encoder_2, vae=base.vae, torch_dtype=torch.float16, variant="fp16", use_safetensors=True).to("cuda")

n_steps = 40
high_noise_frac = 0.8
prompt = "A girl with purple hair, a yellow headband, and red eyes"
generator = torch.Generator(device='cuda').manual_seed(100)
image = base(
    prompt=prompt,
    generator=generator,
    num_inference_steps=n_steps,
    denoising_end=high_noise_frac,
    output_type="latent",
).images
image = refiner(
    prompt=prompt,
    generator=generator,
    num_inference_steps=n_steps,
    denoising_start=high_noise_frac,
    image=image,
).images[0]

n_steps定义总步数,high_noise_frac定义基础模型跑的步数所占的比例。SDXL 基础模型在 0-999 的时间步上进行训练,而SDXL 精化模型则在 0-199 的低噪声时间步上根据基本模型进行微调,因此我们在前 800 个时间步(高噪声)上使用基本模型,而在后 200 个时间步(低噪声)上使用精化模型。因此,high_noise_frac 被设为 0.8,这样所有 200-999 步(去噪时间步的前 80%)都由基本模型执行,而 0-199 步(去噪时间步的后 20%)则由细化模型执行。

因为总步数是采样的40步,实际上,base模型跑了32步,refiner跑了8步。

只使用基础模型也是可以出图的。如果只使用基础模型跑全部的40步,则生成的图片如下明显质量降低。

n_steps = 40
high_noise_frac = 0.8
prompt = "A girl with purple hair, a yellow headband, and red eyes"
generator = torch.Generator(device='cuda').manual_seed(100)
image = base(
    prompt=prompt,
    generator=generator,
    num_inference_steps=n_steps,
    # denoising_end=high_noise_frac,
    # output_type="latent",
).images[0]
# image = refiner(
#     prompt=prompt,
#     generator=generator,
#     num_inference_steps=n_steps,
#     denoising_start=high_noise_frac,
#     image=image,
# ).images[0]

如果将original_size设置的比较小(128, 128),则会生成一个模糊的图片,类似把原来(128, 128)的图片放大的效果。

n_steps = 40
prompt = "A girl with purple hair, a yellow headband, and red eyes"
image = base(
    prompt=prompt,
    generator=torch.Generator(device='cuda').manual_seed(100),
    num_inference_steps=n_steps,
    original_size=(128, 128),
).images[0]

如果将crops_coords_top_left设置为(0, 512),则会生成一个偏左的图片,类似把原来图crop截取过。

prompt = "A girl with purple hair, a yellow headband, and red eyes"
image = base(
    prompt=prompt,
    generator=torch.Generator(device='cuda').manual_seed(100),
    num_inference_steps=40,
    crops_coords_top_left=(0, 512),
).images[0]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值