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 編碼方法的輸出。