Diffusers 文档
VQ模型
并获得增强的文档体验
开始使用
VQModel
VQ-VAE 模型在 Neural Discrete Representation Learning 一文中由 Aaron van den Oord、Oriol Vinyals 和 Koray Kavukcuoglu 提出。该模型在 🤗 Diffusers 中用于将潜在表示解码为图像。与 AutoencoderKL 不同,VQModel 在量化的潜在空间中工作。
该论文的摘要如下:
在没有监督的情况下学习有用的表示仍然是机器学习中的一个关键挑战。在本文中,我们提出了一种简单而强大的生成模型,该模型学习这种离散表示。我们的模型,向量量化-变分自编码器 (VQ-VAE),与 VAE 在两个关键方面有所不同:编码器网络输出离散代码,而不是连续代码;先验是学习的,而不是静态的。为了学习离散潜在表示,我们融入了来自向量量化 (VQ) 的思想。使用 VQ 方法允许模型规避“后验崩溃”的问题——当潜在变量与强大的自回归解码器配对时,潜在变量会被忽略——这通常在 VAE 框架中观察到。将这些表示与自回归先验配对,该模型可以生成高质量的图像、视频和语音,以及进行高质量的说话人转换和音素的无监督学习,从而进一步证明了学习到的表示的效用。
VQModel
class diffusers.VQModel
< 源代码 >( in_channels: int = 3 out_channels: int = 3 down_block_types: typing.Tuple[str, ...] = ('DownEncoderBlock2D',) up_block_types: typing.Tuple[str, ...] = ('UpDecoderBlock2D',) block_out_channels: typing.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 norm_num_groups: int = 32 vq_embed_dim: typing.Optional[int] = None scaling_factor: float = 0.18215 norm_type: str = 'group' mid_block_add_attention = True lookup_from_codebook = False force_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
缩放回原始尺度。有关更多详细信息,请参阅 使用潜在扩散模型进行高分辨率图像合成 论文的 4.3.2 和 D.1 节。 - norm_type (
str
, 可选, 默认为"group"
) — 要使用的归一化层类型。可以是"group"
或"spatial"
之一。
用于解码潜在表示的 VQ-VAE 模型。
此模型继承自 ModelMixin。查看超类文档以了解为其所有模型实现的通用方法(例如下载或保存)。
forward
< source >( sample: Tensor return_dict: bool = True ) → VQEncoderOutput 或 tuple
参数
- sample (
torch.Tensor
) — 输入样本。 - return_dict (
bool
, 可选, 默认为True
) — 是否返回 models.autoencoders.vq_model.VQEncoderOutput 而不是普通元组。
返回值
VQEncoderOutput 或 tuple
如果 return_dict 为 True,则返回 VQEncoderOutput,否则返回普通 tuple
。
VQModel 的 forward 方法。
VQEncoderOutput
VQModel 编码方法的输出。