Diffusers documentation
AutoencoderKLPH3Image
AutoencoderKLPH3Image
Pruna AI 的 P-H3-Image 图像 VAE。它使用了 MiniMax-H3 的 视频 VAE 的编码器和潜在空间,并采用 ViT 解码器将单个潜在帧解码为图像。
它遵循 AutoencoderKLMiniMaxH3 的约定:
- 图像以单帧视频的形式传入,形状为
(batch_size, channels, 1, height, width),空间压缩率为 16 倍。 - 潜变量按通道使用
latents_mean/latents_std进行归一化,而不是使用scaling_factor。 - 像素是经过 ImageNet 归一化的 RGB 值,取值范围为
[0, 1]:encode期望输入为(pixel - imagenet_mean) / imagenet_std,而decode返回的也是该空间中的值。 - 空间平铺默认开启,使用 256 像素的平铺块,且至少包含 64 像素的重叠区域。
import torch
from diffusers import AutoencoderKLPH3Image
vae = AutoencoderKLPH3Image.from_pretrained("PrunaAI/P-H3-Image", subfolder="vae", dtype=torch.bfloat16).to(
"cuda"
) # or "mps", "xpu", "cpu"无论模型以何种 dtype 加载,编码器和解码器的输出投影层均保持为 float32。
AutoencoderKLPH3Image
class diffusers.AutoencoderKLPH3Image
< source >( in_channels: int = 3out_channels: int = 3latent_channels: int = 24block_out_channels: tuple = (128, 256, 256, 512, 512, 1024)layers_per_block: int = 2spatial_downsample_factors: tuple = (2, 2, 2, 2, 1, 1)temporal_downsample_factors: tuple = (1, 2, 2, 1, 1, 1)norm_num_groups: int = 32norm_eps: float = 1e-06spatial_padding_mode: str = 'reflect'decoder_num_layers: int = 36decoder_num_attention_heads: int = 32decoder_attention_head_dim: int = 64decoder_num_register_tokens: int = 4decoder_ffn_mult: int = 4decoder_rope_theta: float = 100.0decoder_rope_dim_ratio: float = 0.75decoder_norm_eps: float = 1e-05latents_mean: tuple = (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)latents_std: tuple = (1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0) )
P-H3-Image 的图像 VAE:MiniMax-H3 的 VAE 编码器与潜在空间,配备一个 PH3ImageViTDecoder,用于解码单个潜在帧。
图像以单帧视频的形式传入,形状为 (batch_size, channels, 1, height, width)。与 AutoencoderKLMiniMaxH3 相同,像素为在 [0, 1] 范围内经过 ImageNet 归一化的 RGB 值(均值 (0.485, 0.456, 0.406),标准差 (0.229, 0.224, 0.225)),并且默认启用空间分块。
encode
< source >( x: Tensorreturn_dict: bool = True )
参数
- x (
torch.Tensor) — 作为单帧视频输入的图片批次,形状为(batch_size, in_channels, 1, height, width)。 - return_dict (
bool,可选,默认为True) — 是否返回 AutoencoderKLOutput 而不是普通的元组。
将一批图像编码为潜在表示。
decode
< source >( z: Tensorreturn_dict: bool = True ) → DecoderOutput or tuple
参数
- z (
torch.Tensor) — 输入的潜变量批次,形状为(batch_size, latent_channels, 1, height, width)。 - return_dict (
bool,可选,默认为True) — 是否返回 DecoderOutput 而不是普通元组。
返回值
DecoderOutput 或 tuple
解码后的图像,形状为 (batch_size, out_channels, 1, height, width)。
解码一批潜在图像。
enable_tiling
< source >( tile_sample_min_height: int | None = Nonetile_sample_min_width: int | None = Nonetile_sample_min_overlap_height: int | None = Nonetile_sample_min_overlap_width: int | None = None )
启用分块 VAE 编码/解码。启用此选项后,VAE 会将帧分割为多个块,分别对每个块进行编码或解码,并将重叠部分线性混合以重新组合。这可以降低内存需求,从而支持处理更大的帧。
forward
< source >( sample: Tensorsample_posterior: bool = Falsegenerator: typing.Optional[torch.Generator] = Nonereturn_dict: bool = True ) → DecoderOutput or tuple
参数
- sample (
torch.Tensor) — 作为单帧视频输入的图像批次,形状为(batch_size, in_channels, 1, height, width)。 - sample_posterior (
bool,可选,默认为False) — 是否对后验分布进行采样,而不是取其众数。 - generator (
torch.Generator,可选) — 当sample_posterior=True时使用的生成器。 - return_dict (
bool,可选,默认为True) — 是否返回 DecoderOutput 而不是普通元组。
返回值
DecoderOutput 或 tuple
往返处理后的图像,形状为 (batch_size, out_channels, 1, height, width)。
对一批图像进行编码,然后解码。