Diffusers 文档
VQModel
并获得增强的文档体验
开始使用
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
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
缩放回原始比例。有关更多详细信息,请参阅 High-Resolution Image Synthesis with Latent Diffusion Models 论文的 4.3.2 节和 D.1 节。 - norm_type (
str
, 可选, 默认为"group"
) — 要使用的归一化层类型。可以是"group"
或"spatial"
之一。
一个用于解码潜在表示的 VQ-VAE 模型。
此模型继承自 ModelMixin。有关所有模型实现的通用方法(如下载或保存),请参阅超类文档。
forward
< 源代码 >( 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 的前向传播方法。
VQEncoderOutput
class diffusers.models.autoencoders.vq_model.VQEncoderOutput
< 源代码 >( latents: Tensor )
VQModel 编码方法的输出。