Diffusers documentation

VQModel

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v0.43.0).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

VQModel

VQ-VAE 模型由 Aaron van den Oord、Oriol Vinyals 和 Koray Kavukcuoglu 在 Neural Discrete Representation Learning 中提出。该模型在 🤗 Diffusers 中用于将潜在表示解码为图像。与 AutoencoderKL 不同,VQModel 在量化潜在空间中工作。

该论文的摘要如下:

在无监督条件下学习有用的表示仍然是机器学习领域的一项关键挑战。本文提出了一种简单而强大的生成模型,用于学习此类离散表示。我们的模型——向量量化变分自编码器(VQ-VAE)——与 VAE 在两个关键方面有所不同:编码器网络输出的是离散码而非连续码;并且先验分布是通过学习获得的,而非静态的。为了学习离散潜在表示,我们引入了向量量化(VQ)的思想。使用 VQ 方法使模型能够规避“后验坍缩”问题——即当潜在变量与强大的自回归解码器配对时,潜在变量被忽略的现象——该问题通常在 VAE 框架中观察到。将这些表示与自回归先验配对后,该模型能够生成高质量的图像、视频和语音,并实现高质量的说话人转换以及音素的无监督学习,从而进一步证明了所学习表示的实用性。

VQModel

类 diffusers.VQModel

< >

( 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 = 3sample_size: int = 32num_vq_embeddings: int = 256norm_num_groups: int = 32vq_embed_dim: int | None = Nonescaling_factor: float = 0.18215norm_type: str = 'group'mid_block_add_attention = Truelookup_from_codebook = Falseforce_upcast = False )

参数

  • 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,)) — 块输出通道的元组。
  • layers_per_block (int,可选,默认为 1) — 每个块中的层数。
  • act_fn (str,可选,默认为 "silu") — 要使用的激活函数。
  • latent_channels (int,可选,默认为 3) — 潜在空间中的通道数。
  • sample_size (int,可选,默认为 32) — 采样输入大小。
  • num_vq_embeddings (int,可选,默认为 256) — VQ-VAE 中码本向量的数量。
  • norm_num_groups (int,可选,默认为 32) — 归一化层的组数。
  • vq_embed_dim (int, 可选) — VQ-VAE 中码本向量的隐藏维度。
  • 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 节。
  • norm_type (str,可选,默认为 "group") — 要使用的归一化层类型。可以是 "group" 或 "spatial" 之一。

用于解码潜在表示的 VQ-VAE 模型。

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

forward

< >

( sample: Tensorreturn_dict: bool = True ) → VQEncoderOutput or tuple

参数

返回值

VQEncoderOutput 或 tuple

If return_dict is True, a VQEncoderOutput is returned, otherwise a plain tuple is returned.

VQModel 的前向方法。

VQEncoderOutput

class diffusers.models.autoencoders.vq_model.VQEncoderOutput

< >

( latents: Tensor )

参数

  • latents(形状为 (batch_size, num_channels, height, width) 的 torch.Tensor)— 模型最后一层输出的编码样本。

VQModel 编码方法的输出。

Update on GitHub