Diffusers 文件

AsymmetricAutoencoderKL

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

AsymmetricAutoencoderKL

改進的KL損失的更大變分自編碼器(VAE)模型,用於修復任務:設計更好的StableDiffusion非對稱VQGAN,作者:Zixin Zhu, Xuelu Feng, Dongdong Chen, Jianmin Bao, Le Wang, Yinpeng Chen, Lu Yuan, Gang Hua。

論文摘要如下:

StableDiffusion是一款革命性的文字到影像生成器,正在影像生成和編輯領域引起轟動。與在畫素空間中學習擴散模型的傳統方法不同,StableDiffusion透過VQGAN在潛在空間中學習擴散模型,從而確保了效率和質量。它不僅支援影像生成任務,還支援真實影像的影像編輯,如影像修復和區域性編輯。然而,我們觀察到StableDiffusion中使用的普通VQGAN會導致顯著的資訊損失,即使在未編輯的影像區域也會導致失真偽影。為此,我們提出了一種具有兩種簡單設計的新的非對稱VQGAN。首先,除了來自編碼器的輸入,解碼器還包含一個條件分支,該分支結合了來自任務特定先驗的資訊,例如影像修復中的未遮罩影像區域。其次,解碼器比編碼器重得多,允許更詳細的恢復,同時只稍微增加了總推理成本。我們非對稱VQGAN的訓練成本低廉,我們只需重新訓練一個新的非對稱解碼器,同時保持普通VQGAN編碼器和StableDiffusion不變。我們的非對稱VQGAN可以廣泛應用於基於StableDiffusion的影像修復和區域性編輯方法。大量的實驗表明,它可以顯著提高影像修復和編輯效能,同時保持原始的文字到影像能力。程式碼可在https://github.com/buxiangzhiren/Asymmetric_VQGAN獲取

評估結果可在原始論文的第4.1節中找到。

可用檢查點

用法示例

from diffusers import AsymmetricAutoencoderKL, StableDiffusionInpaintPipeline
from diffusers.utils import load_image, make_image_grid


prompt = "a photo of a person with beard"
img_url = "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/repaint/celeba_hq_256.png"
mask_url = "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/repaint/mask_256.png"

original_image = load_image(img_url).resize((512, 512))
mask_image = load_image(mask_url).resize((512, 512))

pipe = StableDiffusionInpaintPipeline.from_pretrained("runwayml/stable-diffusion-inpainting")
pipe.vae = AsymmetricAutoencoderKL.from_pretrained("cross-attention/asymmetric-autoencoder-kl-x-1-5")
pipe.to("cuda")

image = pipe(prompt=prompt, image=original_image, mask_image=mask_image).images[0]
make_image_grid([original_image, mask_image, image], rows=1, cols=3)

AsymmetricAutoencoderKL

class diffusers.AsymmetricAutoencoderKL

< >

( in_channels: int = 3 out_channels: int = 3 down_block_types: typing.Tuple[str, ...] = ('DownEncoderBlock2D',) down_block_out_channels: typing.Tuple[int, ...] = (64,) layers_per_down_block: int = 1 up_block_types: typing.Tuple[str, ...] = ('UpDecoderBlock2D',) up_block_out_channels: typing.Tuple[int, ...] = (64,) layers_per_up_block: int = 1 act_fn: str = 'silu' latent_channels: int = 4 norm_num_groups: int = 32 sample_size: int = 32 scaling_factor: float = 0.18215 )

引數

  • in_channels (int, 可選, 預設為3) — 輸入影像的通道數。
  • out_channels (int, 可選, 預設為3) — 輸出通道數。
  • down_block_types (Tuple[str], 可選, 預設為("DownEncoderBlock2D",)) — 下采樣塊型別元組。
  • down_block_out_channels (Tuple[int], 可選, 預設為(64,)) — 下采樣塊輸出通道元組。
  • layers_per_down_block (int, 可選, 預設為1) — 下采樣塊的層數。
  • up_block_types (Tuple[str], 可選, 預設為("UpDecoderBlock2D",)) — 上取樣塊型別元組。
  • up_block_out_channels (Tuple[int], 可選, 預設為(64,)) — 上取樣塊輸出通道元組。
  • layers_per_up_block (int, 可選, 預設為1) — 上取樣塊的層數。
  • act_fn (str, 可選, 預設為"silu") — 要使用的啟用函式。
  • latent_channels (int, 可選, 預設為4) — 潛在空間中的通道數。
  • sample_size (int, 可選, 預設為32) — 樣本輸入大小。
  • norm_num_groups (int, 可選, 預設為32) — ResNet塊中第一個歸一化層使用的組數。
  • scaling_factor (float, 可選, 預設為0.18215) — 使用訓練集的第一批次計算出的訓練潛在空間的元件標準差。這用於在訓練擴散模型時將潛在空間縮放為單位方差。潛在空間在傳遞給擴散模型之前,透過公式z = z * scaling_factor進行縮放。在解碼時,潛在空間透過公式z = 1 / scaling_factor * z縮放回原始比例。有關更多詳細資訊,請參閱高解析度影像合成與潛在擴散模型論文的4.3.2節和D.1節。

為StableDiffusion設計更好的非對稱VQGAN https://huggingface.co/papers/2306.04632。一個使用KL損失將影像編碼為潛在空間,並將潛在表示解碼為影像的VAE模型。

此模型繼承自 ModelMixin。有關所有模型實現的通用方法(如下載或儲存),請參閱超類文件。

前向傳播

< >

( sample: Tensor mask: typing.Optional[torch.Tensor] = None sample_posterior: bool = False return_dict: bool = True generator: typing.Optional[torch._C.Generator] = None )

引數

  • sample (torch.Tensor) — 輸入樣本。
  • mask (torch.Tensor, 可選, 預設為None) — 可選的影像修復遮罩。
  • sample_posterior (bool, 可選, 預設為False) — 是否從後驗分佈中取樣。
  • return_dict (bool, 可選, 預設為True) — 是否返回DecoderOutput而不是普通元組。

AutoencoderKLOutput

class diffusers.models.modeling_outputs.AutoencoderKLOutput

< >

( latent_dist: DiagonalGaussianDistribution )

引數

  • latent_dist (DiagonalGaussianDistribution) — Encoder的編碼輸出,表示為DiagonalGaussianDistribution的均值和對數方差。DiagonalGaussianDistribution允許從分佈中取樣潛在變數。

AutoencoderKL 編碼方法的輸出。

DecoderOutput

class diffusers.models.autoencoders.vae.DecoderOutput

< >

( sample: Tensor commit_loss: typing.Optional[torch.FloatTensor] = None )

引數

  • sample (形狀為(batch_size, num_channels, height, width)torch.Tensor) — 模型最後一層解碼的輸出樣本。

解碼方法的輸出。

< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.