Diffusers documentation
AsymmetricAutoencoderKL
AsymmetricAutoencoderKL
用于图像修复任务的改进型大型变分自编码器(VAE)模型,采用 KL 损失:Designing a Better Asymmetric VQGAN for StableDiffusion,作者为 Zixin Zhu、Xuelu Feng、Dongdong Chen、Jianmin Bao、Le Wang、Yinpeng Chen、Lu Yuan、Gang Hua。
该论文的摘要如下:
StableDiffusion 是一款革命性的文本生成图像模型,正在图像生成与编辑领域引起广泛关注。与在像素空间学习扩散模型的传统方法不同,StableDiffusion 通过 VQGAN 在潜在空间学习扩散模型,从而兼顾效率与质量。它不仅支持图像生成任务,还能对真实图像进行编辑,例如图像修复(inpainting)和局部编辑。然而,我们观察到 StableDiffusion 中使用的原始 VQGAN 会导致显著的信息损失,即使在未编辑的图像区域也会出现失真伪影。为此,我们提出了一种新的非对称 VQGAN,包含两个简单的设计。首先,除了来自编码器的输入外,解码器还包含一个条件分支,用于融合任务特定的先验信息,例如修复任务中未掩码的图像区域。其次,解码器比编码器重得多,从而能够恢复更多细节,同时仅略微增加总推理成本。我们的非对称 VQGAN 训练成本较低,只需重新训练一个新的非对称解码器,而保持原始 VQGAN 编码器和 StableDiffusion 不变。我们的非对称 VQGAN 可广泛应用于基于 StableDiffusion 的修复和局部编辑方法。大量实验表明,它能在保持原有文本生成图像能力的同时,显著提升修复和编辑性能。代码可在 https://github.com/buxiangzhiren/Asymmetric_VQGAN 获取。
评估结果可在原论文的 4.1 节中找到。
可用的检查点
- https://huggingface.co/cross-attention/asymmetric-autoencoder-kl-x-1-5
- https://huggingface.co/cross-attention/asymmetric-autoencoder-kl-x-2
使用示例
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("stable-diffusion-v1-5/stable-diffusion-inpainting")
pipe.vae = AsymmetricAutoencoderKL.from_pretrained("cross-attention/asymmetric-autoencoder-kl-x-1-5")
pipe.to("cuda") # or "mps", "xpu", "cpu"
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
< source >( in_channels: int = 3out_channels: int = 3down_block_types: tuple[str, ...] = ('DownEncoderBlock2D',)down_block_out_channels: tuple[int, ...] = (64,)layers_per_down_block: int = 1up_block_types: tuple[str, ...] = ('UpDecoderBlock2D',)up_block_out_channels: tuple[int, ...] = (64,)layers_per_up_block: int = 1act_fn: str = 'silu'latent_channels: int = 4norm_num_groups: int = 32sample_size: int = 32scaling_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缩放回原始尺度。更多细节请参阅 High-Resolution Image Synthesis with Latent Diffusion Models 论文的 4.3.2 节和 D.1 节。
为 StableDiffusion 设计更优的非对称 VQGAN https://huggingface.co/papers/2306.04632。一个使用 KL 损失的 VAE 模型,用于将图像编码为潜在表示,并将潜在表示解码为图像。
该模型继承自 ModelMixin。请查阅其父类文档,了解为所有模型实现的通用方法(例如下载或保存)。
forward
< source >( sample: Tensormask: typing.Optional[torch.Tensor] = Nonesample_posterior: bool = Falsereturn_dict: bool = Truegenerator: typing.Optional[torch.Generator] = None ) → DecoderOutput or tuple
参数
- sample (
torch.Tensor) — 输入样本。 - mask (
torch.Tensor,可选,默认为None) — 可选的修复掩码。 - sample_posterior (
bool,可选,默认为False) — 是否从后验分布中采样。 - return_dict (
bool,可选,默认为True) — 是否返回DecoderOutput而不是普通元组。 - generator (
torch.Generator,可选) — 用于使采样过程确定性的torch.Generator。
返回值
DecoderOutput 或 tuple
如果 return_dict 为 True,则返回 DecoderOutput,否则返回普通的 tuple。
AutoencoderKLOutput
类 diffusers.models.modeling_outputs.AutoencoderKLOutput
< source >( latent_dist: DiagonalGaussianDistribution )
AutoencoderKL 编码方法的输出。
DecoderOutput
class diffusers.models.autoencoders.vae.DecoderOutput
< source >( sample: Tensorcommit_loss: typing.Optional[torch.FloatTensor] = None )
解码方法的输出。