Diffusers 文档
DiT
并获得增强的文档体验
开始
DiT
Scalable Diffusion Models with Transformers (DiT) 作者是 William Peebles 和 Saining Xie。
论文摘要如下
我们探索了一种基于 transformer 架构的新型扩散模型。我们训练图像的潜在扩散模型,将常用的 U-Net 主干替换为在潜在 patch 上运行的 transformer。我们通过 Gflops 衡量的正向传播复杂度的视角分析了我们的 Diffusion Transformers (DiTs) 的可扩展性。我们发现,Gflops 较高的 DiTs(通过增加 transformer 的深度/宽度或输入 token 的数量)始终具有较低的 FID。除了具有良好的可扩展性之外,我们最大的 DiT-XL/2 模型在类条件 ImageNet 512x512 和 256x256 基准测试中优于所有先前的扩散模型,在后者上实现了 2.27 的最先进的 FID。
原始代码库可以在以下位置找到:facebookresearch/dit。
请务必查看 Schedulers 指南,了解如何探索 scheduler 速度和质量之间的权衡,并查看“跨 pipelines 重用组件”部分,了解如何有效地将相同组件加载到多个 pipelines 中。
DiTPipeline
class diffusers.DiTPipeline
< source >( transformer: DiTTransformer2DModel vae: AutoencoderKL scheduler: KarrasDiffusionSchedulers id2label: typing.Optional[typing.Dict[int, str]] = None )
参数
- transformer (DiTTransformer2DModel) — 一个类条件
DiTTransformer2DModel
,用于去噪编码的图像潜在表示。 - vae (AutoencoderKL) — 用于将图像编码和解码为潜在表示和从潜在表示解码图像的变分自动编码器 (VAE) 模型。
- scheduler (DDIMScheduler) — 调度器,与
transformer
结合使用,以对编码后的图像潜在空间进行去噪。
基于 Transformer 主干而不是 UNet 的图像生成 Pipeline。
此模型继承自 DiffusionPipeline。查看超类文档以获取为所有 Pipeline 实现的通用方法(下载、保存、在特定设备上运行等)。
__call__
< source >( class_labels: typing.List[int] guidance_scale: float = 4.0 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None num_inference_steps: int = 50 output_type: typing.Optional[str] = 'pil' return_dict: bool = True ) → ImagePipelineOutput 或 tuple
参数
- class_labels (List[int]) — 要生成的图像的 ImageNet 类标签列表。
- guidance_scale (
float
, 可选,默认为 4.0) — 较高的 guidance scale 值会鼓励模型生成与文本prompt
紧密相关的图像,但会牺牲较低的图像质量。当guidance_scale > 1
时,guidance scale 启用。 - generator (
torch.Generator
, 可选) — 用于使生成具有确定性的torch.Generator
。 - num_inference_steps (
int
, 可选,默认为 250) — 去噪步骤的数量。 更多的去噪步骤通常会带来更高质量的图像,但会牺牲较慢的推理速度。 - output_type (
str
, 可选,默认为"pil"
) — 生成图像的输出格式。 在PIL.Image
或np.array
之间选择。 - return_dict (
bool
, 可选,默认为True
) — 是否返回 ImagePipelineOutput 而不是普通元组。
返回值
ImagePipelineOutput 或 tuple
如果 return_dict
为 True
,则返回 ImagePipelineOutput,否则返回 tuple
,其中第一个元素是包含生成图像的列表
用于生成的 Pipeline 的调用函数。
示例
>>> from diffusers import DiTPipeline, DPMSolverMultistepScheduler
>>> import torch
>>> pipe = DiTPipeline.from_pretrained("facebook/DiT-XL-2-256", torch_dtype=torch.float16)
>>> pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
>>> pipe = pipe.to("cuda")
>>> # pick words from Imagenet class labels
>>> pipe.labels # to print all available words
>>> # pick words that exist in ImageNet
>>> words = ["white shark", "umbrella"]
>>> class_ids = pipe.get_label_ids(words)
>>> generator = torch.manual_seed(33)
>>> output = pipe(class_labels=class_ids, num_inference_steps=25, generator=generator)
>>> image = output.images[0] # label 'white shark'
get_label_ids
< source >( label: typing.Union[str, typing.List[str]] ) → int
的 list
将来自 ImageNet 的标签字符串映射到相应的类 ID。
ImagePipelineOutput
class diffusers.ImagePipelineOutput
< source >( images: typing.Union[typing.List[PIL.Image.Image], numpy.ndarray] )
图像 Pipeline 的输出类。