Diffusers documentation
AutoencoderKL
AutoencoderKL
Diederik P. Kingma 和 Max Welling 在 Auto-Encoding Variational Bayes 中引入了带有 KL 损失的变分自编码器(VAE)模型。在 🤗 Diffusers 中,该模型用于将图像编码为潜在表示,并将潜在表示解码为图像。
该论文的摘要如下:
在存在具有不可解后验分布的连续隐变量以及大规模数据集的情况下,我们如何对有向概率模型执行高效的推理和学习?我们提出了一种随机变分推断和学习算法,该算法可扩展至大规模数据集,并且在某些温和的可微性条件下,即使在不可解的情况下也能正常工作。我们的贡献有两方面。首先,我们表明,对变分下界进行重参数化可以得到一个下界估计器,该估计器可以直接使用标准的随机梯度方法进行优化。其次,我们表明,对于每个数据点具有连续隐变量的独立同分布(i.i.d.)数据集,通过使用所提出的下界估计器,将近似推断模型(也称为识别模型)拟合到不可解的后验分布上,可以使后验推断变得特别高效。理论上的优势在实验结果中得到了体现。
从原始格式加载
默认情况下,AutoencoderKL 应使用 from_pretrained() 加载,但也可以使用 FromOriginalModelMixin.from_single_file 从原始格式加载,方法如下:
from diffusers import AutoencoderKL
url = "https://huggingface.co/stabilityai/sd-vae-ft-mse-original/blob/main/vae-ft-mse-840000-ema-pruned.safetensors" # can also be a local file
model = AutoencoderKL.from_single_file(url)AutoencoderKL
类 diffusers.AutoencoderKL
< source >( in_channels: int = 3out_channels: int = 3down_block_types: tuple[str] = ('DownEncoderBlock2D',)up_block_types: tuple[str] = ('UpDecoderBlock2D',)block_out_channels: tuple[int] = (64,)layers_per_block: int = 1act_fn: str = 'silu'latent_channels: int = 4norm_num_groups: int = 32sample_size: int = 32scaling_factor: float = 0.18215shift_factor: float | None = Nonelatents_mean: tuple[float] | None = Nonelatents_std: tuple[float] | None = Noneforce_upcast: bool = Trueuse_quant_conv: bool = Trueuse_post_quant_conv: bool = Truemid_block_add_attention: bool = True )
参数
- in_channels(int,可选,默认为 3)— 输入图像的通道数。
- out_channels (int, 可选,默认为 3) — 输出中的通道数。
- down_block_types (
tuple[str],可选,默认为("DownEncoderBlock2D",)) — 下采样块类型的元组。 - up_block_types (
tuple[str],可选,默认为("UpDecoderBlock2D",)) — 上采样块类型的元组。 - block_out_channels (
tuple[int],可选,默认为(64,)) — 块输出通道的元组。 - act_fn (
str,可选,默认为"silu") — 要使用的激活函数。 - latent_channels (
int,可选,默认为 4) — 潜在空间中的通道数。 - sample_size (
int,可选,默认为32) — 采样输入大小。 - scaling_factor (
float, 可选,默认为 0.18215) — 使用训练集的第一个批次计算得出的训练后潜在空间的逐分量标准差。在训练扩散模型时,用于将潜在空间缩放至单位方差。潜在变量在传入扩散模型之前,会按公式z = z * scaling_factor进行缩放。解码时,潜在变量会按公式z = 1 / scaling_factor * z缩放回原始尺度。更多细节请参阅 High-Resolution Image Synthesis with Latent Diffusion Models 论文的 4.3.2 节和 D.1 节。 - force_upcast (
bool,可选,默认为True) — 如果启用,将强制 VAE 以 float32 运行,适用于高分辨率图像流水线(例如 SD-XL)。VAE 可以微调/训练至更低的数值范围,而不会损失过多精度,此时可将force_upcast设置为False- 参见:https://huggingface.co/madebyollin/sdxl-vae-fp16-fix - mid_block_add_attention (
bool,可选,默认为True) — 如果启用,Encoder 和 Decoder 的 mid_block 将包含注意力块。如果设置为 false,mid_block 将仅包含 resnet 块
一个带有 KL 损失的 VAE 模型,用于将图像编码为潜在表示,并将潜在表示解码为图像。
该模型继承自 ModelMixin。请查阅其父类文档,了解为所有模型实现的通用方法(例如下载或保存)。
decode
< source >( z: FloatTensorreturn_dict: bool = Truegenerator = None ) → ~models.vae.DecoderOutput or tuple
解码一批图像。
encode
< source >( x: Tensorreturn_dict: bool = True )
将一批图像编码为潜在表示。
forward
< source >( sample: Tensorsample_posterior: bool = Falsereturn_dict: bool = Truegenerator: typing.Optional[torch.Generator] = None ) → ~models.vae.DecoderOutput or tuple
参数
- sample (
torch.Tensor) — 输入样本。 - sample_posterior (
bool,可选,默认为False) — 是否从后验分布中采样。 - return_dict (
bool,可选,默认为True) — 是否返回DecoderOutput而不是普通元组。 - generator (
torch.Generator,可选) — 用于使采样过程确定性的torch.Generator。
返回值
~models.vae.DecoderOutput 或 tuple
如果 return_dict 为 True,则返回 ~models.vae.DecoderOutput,否则返回普通的 tuple。
启用融合的 QKV 投影。对于自注意力模块,所有投影矩阵(即 query、key、value)均会被融合。对于交叉注意力模块,key 和 value 投影矩阵会被融合。
> 此 API 处于 🧪 实验阶段。
禁用自定义注意力处理器并设置默认的注意力实现。
tiled_decode
< source >( z: Tensorreturn_dict: bool = True ) → ~models.vae.DecoderOutput or tuple
使用分块解码器解码一批图像。
tiled_encode
< source >( x: Tensorreturn_dict: bool = True ) → ~models.autoencoder_kl.AutoencoderKLOutput or tuple
使用分块编码器对一批图像进行编码。
启用此选项后,VAE 会将输入张量拆分为多个图块,分步计算编码。这有助于保持内存占用恒定,而不受图像尺寸影响。由于每个图块使用不同的编码器,分块编码的最终结果与非分块编码不同。为避免分块伪影,图块之间会重叠并混合,以形成平滑的输出。输出中可能仍会出现图块大小的变化,但应明显减弱。
AutoencoderKLOutput
类 diffusers.models.modeling_outputs.AutoencoderKLOutput
< source >( latent_dist: DiagonalGaussianDistribution )
AutoencoderKL 编码方法的输出。
DecoderOutput
class diffusers.models.autoencoders.vae.DecoderOutput
< source >( sample: Tensorcommit_loss: typing.Optional[torch.FloatTensor] = None )
解码方法的输出。