算力平台:
Stable Diffusion 2
Stable Diffusion 2 是一个基于文本到图像的 潜在扩散 模型,建立在原始 Stable Diffusion 的工作基础上,由 Stability AI 和 LAION 的 Robin Rombach 和 Katherine Crowson 领导开发。
Stable Diffusion 2.0 版本包括使用全新文本编码器(OpenCLIP)训练的强大的文本到图像模型,该编码器由 LAION 开发,并得到 Stability AI 的支持,与早期的 V1 版本相比,大大提高了生成图像的质量。此版本中的文本到图像模型可以生成默认分辨率为 512x512 像素和 768x768 像素的图像。这些模型在 Stability AI 的 DeepFloyd 团队创建的 LAION-5B 数据集 的美学子集上进行训练,然后使用 LAION 的 NSFW 过滤器 进一步过滤以去除成人内容。
有关 Stable Diffusion 2 的工作原理及其与原始 Stable Diffusion 的区别的更多详细信息,请参阅官方的 公告文章。
Stable Diffusion 2 的架构与原始的 Stable Diffusion 模型 基本相同,因此请查看其 API 文档以了解如何使用 Stable Diffusion 2。我们推荐使用 [DPMSolverMultistepScheduler
],因为它在速度和质量之间提供了合理的权衡,并且可以仅用 20 步运行。
Stable Diffusion 2 可用于文本到图像、图像修复、超分辨率和深度到图像等任务:
Task | Repository |
---|---|
text-to-image (512x512) | stabilityai/stable-diffusion-2-base |
text-to-image (768x768) | stabilityai/stable-diffusion-2 |
inpainting | stabilityai/stable-diffusion-2-inpainting |
super-resolution | stable-diffusion-x4-upscaler |
depth-to-image | stabilityai/stable-diffusion-2-depth |
以下是使用 Stable Diffusion 2 进行每项任务的一些示例:
文本到图像
py
from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler
import torch
repo_id = "stabilityai/stable-diffusion-2-base"
pipe = DiffusionPipeline.from_pretrained(repo_id, torch_dtype=torch.float16, variant="fp16")
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
pipe = pipe.to("cuda")
prompt = "High quality photo of an astronaut riding a horse in space"
image = pipe(prompt, num_inference_steps=25).images[0]
image
图像修复
py
import torch
from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler
from diffusers.utils import load_image, make_image_grid
img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png"
mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png"
init_image = load_image(img_url).resize((512, 512))
mask_image = load_image(mask_url).resize((512, 512))
repo_id = "stabilityai/stable-diffusion-2-inpainting"
pipe = DiffusionPipeline.from_pretrained(repo_id, torch_dtype=torch.float16, variant="fp16")
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
pipe = pipe.to("cuda")
prompt = "Face of a yellow cat, high resolution, sitting on a park bench"
image = pipe(prompt=prompt, image=init_image, mask_image=mask_image, num_inference_steps=25).images[0]
make_image_grid([init_image, mask_image, image], rows=1, cols=3)
超分辨率
py
from diffusers import StableDiffusionUpscalePipeline
from diffusers.utils import load_image, make_image_grid
import torch
# load model and scheduler
model_id = "stabilityai/stable-diffusion-x4-upscaler"
pipeline = StableDiffusionUpscalePipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipeline = pipeline.to("cuda")
# let's download an image
url = "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale/low_res_cat.png"
low_res_img = load_image(url)
low_res_img = low_res_img.resize((128, 128))
prompt = "a white cat"
upscaled_image = pipeline(prompt=prompt, image=low_res_img).images[0]
make_image_grid([low_res_img.resize((512, 512)), upscaled_image.resize((512, 512))], rows=1, cols=2)
深度到图像
py
import torch
from diffusers import StableDiffusionDepth2ImgPipeline
from diffusers.utils import load_image, make_image_grid
pipe = StableDiffusionDepth2ImgPipeline.from_pretrained(
"stabilityai/stable-diffusion-2-depth",
torch_dtype=torch.float16,
).to("cuda")
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
init_image = load_image(url)
prompt = "two tigers"
negative_prompt = "bad, deformed, ugly, bad anotomy"
image = pipe(prompt=prompt, image=init_image, negative_prompt=negative_prompt, strength=0.7).images[0]
make_image_grid([init_image, image], rows=1, cols=2)