Diffusers 文档

Tiny AutoEncoder

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

微型自动编码器

用于 Stable Diffusion (TAESD) 的微型自动编码器由 Ollin Boer Bohan 在 madebyollin/taesd 中引入。它是 Stable Diffusion 的 VAE 的一个微型精炼版本,可以几乎即时地解码 StableDiffusionPipelineStableDiffusionXLPipeline 中的潜在变量。

与 Stable Diffusion v-2.1 配合使用

import torch
from diffusers import DiffusionPipeline, AutoencoderTiny

pipe = DiffusionPipeline.from_pretrained(
    "stabilityai/stable-diffusion-2-1-base", torch_dtype=torch.float16
)
pipe.vae = AutoencoderTiny.from_pretrained("madebyollin/taesd", torch_dtype=torch.float16)
pipe = pipe.to("cuda")

prompt = "slice of delicious New York-style berry cheesecake"
image = pipe(prompt, num_inference_steps=25).images[0]
image

与 Stable Diffusion XL 1.0 配合使用

import torch
from diffusers import DiffusionPipeline, AutoencoderTiny

pipe = DiffusionPipeline.from_pretrained(
    "stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16
)
pipe.vae = AutoencoderTiny.from_pretrained("madebyollin/taesdxl", torch_dtype=torch.float16)
pipe = pipe.to("cuda")

prompt = "slice of delicious New York-style berry cheesecake"
image = pipe(prompt, num_inference_steps=25).images[0]
image

AutoencoderTiny

diffusers.AutoencoderTiny

< >

( in_channels: int = 3 out_channels: int = 3 encoder_block_out_channels: typing.Tuple[int, ...] = (64, 64, 64, 64) decoder_block_out_channels: typing.Tuple[int, ...] = (64, 64, 64, 64) act_fn: str = 'relu' upsample_fn: str = 'nearest' latent_channels: int = 4 upsampling_scaling_factor: int = 2 num_encoder_blocks: typing.Tuple[int, ...] = (1, 3, 3, 3) num_decoder_blocks: typing.Tuple[int, ...] = (3, 3, 3, 1) latent_magnitude: int = 3 latent_shift: float = 0.5 force_upcast: bool = False scaling_factor: float = 1.0 shift_factor: float = 0.0 )

参数

  • in_channels (int, 可选, 默认为 3) — 输入图像中的通道数。
  • out_channels (int, 可选, 默认为 3) — 输出中的通道数。
  • encoder_block_out_channels (Tuple[int], 可选, 默认为 (64, 64, 64, 64)) — 表示每个编码器块输出通道数的整数元组。元组的长度应与编码器块的数量相等。
  • decoder_block_out_channels (Tuple[int], 可选, 默认为 (64, 64, 64, 64)) — 表示每个解码器块输出通道数的整数元组。元组的长度应与解码器块的数量相等。
  • act_fn (str, 可选, 默认为 "relu") — 模型中使用的激活函数。
  • latent_channels (int, 可选, 默认为 4) — 潜在表示中的通道数。潜在空间充当输入图像的压缩表示。
  • upsampling_scaling_factor (int, 可选, 默认为 2) — 解码器中上采样的缩放因子。它决定了上采样过程中输出图像的大小。
  • num_encoder_blocks (Tuple[int], 可选, 默认为 (1, 3, 3, 3)) — 表示编码过程中每个阶段的编码器块数量的整数元组。元组的长度应与编码器中的阶段数量相等。每个阶段的编码器块数量不同。
  • num_decoder_blocks (Tuple[int], 可选, 默认为 (3, 3, 3, 1)) — 表示解码过程中每个阶段的解码器块数量的整数元组。元组的长度应与解码器中的阶段数量相等。每个阶段的解码器块数量不同。
  • latent_magnitude (float, 可选, 默认为 3.0) — 潜在表示的幅度。此参数用于缩放潜在表示值,以控制信息保留的程度。
  • latent_shift (float, 可选, 默认为 0.5) — 应用于潜在表示的偏移。此参数控制潜在空间的中心。
  • scaling_factor (float, 可选, 默认为 1.0) — 使用训练集的第一批计算出的训练潜在空间的组件式标准差。这用于在训练扩散模型时将潜在空间缩放为单位方差。在传递给扩散模型之前,潜在变量会按照公式 z = z * scaling_factor 进行缩放。解码时,潜在变量会按照公式 z = 1 / scaling_factor * z 缩放回原始比例。有关更多详细信息,请参阅 使用潜在扩散模型进行高分辨率图像合成 论文的第 4.3.2 和 D.1 节。然而,此自动编码器未使用此类缩放因子,因此默认值为 1.0。
  • force_upcast (bool, 可选, 默认为 False) — 如果启用,它将强制 VAE 以 float32 运行,以用于高图像分辨率的管道,例如 SD-XL。VAE 可以进行微调/训练以降低范围而不会损失太多精度,在这种情况下,force_upcast 可以设置为 False(请参阅此支持 fp16 的 自动编码器)。

一个微型精炼 VAE 模型,用于将图像编码为潜在变量并将潜在表示解码为图像。

AutoencoderTinyTAESD 原始实现的包装器。

此模型继承自 ModelMixin。有关为其所有模型实现的通用方法(例如下载或保存),请查阅超类文档。

禁用切片

< >

( )

禁用切片 VAE 解码。如果之前启用了 enable_slicing,此方法将恢复一步计算解码。

禁用平铺

< >

( )

禁用平铺 VAE 解码。如果之前启用了 enable_tiling,此方法将恢复一步计算解码。

启用切片

< >

( )

启用切片 VAE 解码。启用此选项后,VAE 会将输入张量分片,分步计算解码。这有助于节省一些内存并允许更大的批次大小。

启用平铺

< >

( use_tiling: bool = True )

启用平铺 VAE 解码。启用此选项后,VAE 将把输入张量分割成瓦片,分多步计算编码和解码。这对于节省大量内存和处理更大的图像非常有用。

前向

< >

( sample: Tensor return_dict: bool = True )

参数

  • sample (torch.Tensor) — 输入样本。
  • return_dict (bool, 可选, 默认为 True) — 是否返回 DecoderOutput 而不是普通元组。

缩放潜在变量

< >

( x: Tensor )

原始潜在变量 -> [0, 1]

取消缩放潜在变量

< >

( x: Tensor )

[0, 1] -> 原始潜在变量

AutoencoderTinyOutput

diffusers.models.autoencoders.autoencoder_tiny.AutoencoderTinyOutput

< >

( latents: Tensor )

参数

  • latents (torch.Tensor) — Encoder 的编码输出。

AutoencoderTiny 编码方法的输出。

< > 在 GitHub 上更新