Diffusers 文档

I2VGen-XL

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

I2VGen-XL

I2VGen-XL: High-Quality Image-to-Video Synthesis via Cascaded Diffusion Models by Shiwei Zhang, Jiayu Wang, Yingya Zhang, Kang Zhao, Hangjie Yuan, Zhiwu Qin, Xiang Wang, Deli Zhao, and Jingren Zhou.

该论文的摘要是

视频合成技术受益于扩散模型的快速发展,近年来取得了显著的进步。然而,它在语义准确性、清晰度和时空连续性方面仍然面临挑战。这些挑战主要源于对齐良好的文本-视频数据稀缺以及视频固有的复杂结构,使得模型难以同时确保语义和质量的卓越性。在本报告中,我们提出了一种级联的 I2VGen-XL 方法,该方法通过解耦这两个因素来增强模型性能,并通过利用静态图像作为关键指导形式来确保输入数据的一致性。I2VGen-XL 由两个阶段组成:i) 基础阶段,通过使用两个分层编码器来保证连贯的语义并保留来自输入图像的内容;ii) 细化阶段,通过结合额外的简短文本来增强视频的细节,并将分辨率提高到 1280×720。为了提高多样性,我们收集了约 3500 万个单镜头文本-视频对和 60 亿个文本-图像对来优化模型。通过这种方式,I2VGen-XL 可以同时提高生成视频的语义准确性、细节连续性和清晰度。通过广泛的实验,我们研究了 I2VGen-XL 的潜在原理,并将其与当前顶级方法进行了比较,这可以证明其在不同数据上的有效性。源代码和模型将在 this https URL 公开提供。

原始代码库可以在这里找到。模型检查点可以在这里找到。

请务必查看 调度器 指南,了解如何探索调度器速度和质量之间的权衡,并查看 跨管道重用组件 部分,了解如何有效地将相同组件加载到多个管道中。此外,要了解更多关于减少此管道内存使用量的信息,请参阅 此处 的“[减少内存使用量]”部分。

使用 I2VGenXL 的示例输出

库。
library

注释

  • I2VGenXL 始终使用 clip_skip 值为 1。这意味着它利用了 CLIP 文本编码器的倒数第二层表示。
  • 它可以生成质量通常与 Stable Video Diffusion (SVD) 相媲美的视频。
  • 与 SVD 不同,它还接受文本提示作为输入。
  • 它可以生成更高分辨率的视频。
  • 当使用 DDIMScheduler(此管道的默认调度器)时,少于 50 步的推理会导致不良结果。
  • 此实现是 I2VGenXL 的单阶段变体。I2VGen-XL 论文中的主要图示显示了一个两阶段变体,但是,单阶段变体效果良好。有关更多详细信息,请参阅 此讨论

I2VGenXLPipeline

class diffusers.I2VGenXLPipeline

< >

( vae: AutoencoderKL text_encoder: CLIPTextModel tokenizer: CLIPTokenizer image_encoder: CLIPVisionModelWithProjection feature_extractor: CLIPImageProcessor unet: I2VGenXLUNet scheduler: DDIMScheduler )

参数

  • vae (AutoencoderKL) — 变分自动编码器 (VAE) 模型,用于将图像编码和解码为潜在表示。
  • text_encoder (CLIPTextModel) — 冻结的文本编码器 (clip-vit-large-patch14)。
  • tokenizer (CLIPTokenizer) — 用于标记文本的 CLIPTokenizer
  • unet (I2VGenXLUNet) — 用于对编码的视频潜在空间进行去噪的 I2VGenXLUNet
  • scheduler (DDIMScheduler) — 一种调度器,与 unet 结合使用,以对编码的图像潜在空间进行去噪。

用于图像到视频生成的管道,如 I2VGenXL 中提出的那样。

此模型继承自 DiffusionPipeline。查看超类文档以获取为所有管道实现的通用方法(下载、保存、在特定设备上运行等)。

__call__

< >

( prompt: typing.Union[str, typing.List[str]] = None image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, typing.List[PIL.Image.Image], typing.List[numpy.ndarray], typing.List[torch.Tensor]] = None height: typing.Optional[int] = 704 width: typing.Optional[int] = 1280 target_fps: typing.Optional[int] = 16 num_frames: int = 16 num_inference_steps: int = 50 guidance_scale: float = 9.0 negative_prompt: typing.Union[str, typing.List[str], NoneType] = None eta: float = 0.0 num_videos_per_prompt: typing.Optional[int] = 1 decode_chunk_size: typing.Optional[int] = 1 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.Tensor] = None prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None output_type: typing.Optional[str] = 'pil' return_dict: bool = True cross_attention_kwargs: typing.Optional[typing.Dict[str, typing.Any]] = None clip_skip: typing.Optional[int] = 1 ) pipelines.i2vgen_xl.pipeline_i2vgen_xl.I2VGenXLPipelineOutputtuple

参数

  • prompt (strList[str], 可选) — 用于引导图像生成的提示或提示列表。如果未定义,则需要传递 prompt_embeds
  • image (PIL.Image.ImageList[PIL.Image.Image]torch.Tensor) — 用于引导图像生成的图像或图像列表。如果提供张量,则需要与 CLIPImageProcessor 兼容。
  • height (int, 可选, 默认为 self.unet.config.sample_size * self.vae_scale_factor) — 生成图像的高度像素值。
  • width (int, 可选, 默认为 self.unet.config.sample_size * self.vae_scale_factor) — 生成图像的宽度像素值。
  • target_fps (int, 可选) — 帧率。生成图像导出为视频后的帧速率。这也用作生成过程中的“微条件”。
  • num_frames (int, 可选) — 要生成的视频帧数。
  • num_inference_steps (int, 可选) — 去噪步骤的数量。
  • guidance_scale (float, 可选, 默认为 7.5) — 较高的 guidance scale 值会鼓励模型生成与文本 prompt 紧密相关的图像,但会以降低图像质量为代价。当 guidance_scale > 1 时,guidance scale 生效。
  • negative_prompt (strList[str], 可选) — 用于引导图像生成中不应包含的内容的提示或提示列表。如果未定义,则需要改为传递 negative_prompt_embeds。不使用 guidance 时(guidance_scale < 1),将被忽略。
  • eta (float, 可选) — 对应于 DDIM 论文中的参数 eta (η)。仅适用于 DDIMScheduler,在其他调度器中将被忽略。
  • num_videos_per_prompt (int, 可选) — 每个提示要生成的图像数量。
  • decode_chunk_size (int, 可选) — 一次解码的帧数。较高的 chunk size 值会提高帧之间的时间一致性,但也会增加内存消耗。默认情况下,解码器将一次解码所有帧以获得最佳质量。减少 decode_chunk_size 可减少内存使用量。
  • generator (torch.GeneratorList[torch.Generator], 可选) — 用于使生成具有确定性的 torch.Generator
  • latents (torch.Tensor, 可选) — 预生成的、从高斯分布中采样的噪声潜在空间,用作图像生成的输入。可用于使用不同的提示调整相同的生成。如果未提供,则通过使用提供的随机 generator 进行采样来生成潜在空间张量。
  • prompt_embeds (torch.Tensor, optional) — 预生成的文本嵌入 (text embeddings)。 可以用于轻松调整文本输入(提示词权重)。如果未提供,则会从 prompt 输入参数生成文本嵌入。
  • negative_prompt_embeds (torch.Tensor, optional) — 预生成的负面文本嵌入。 可以用于轻松调整文本输入(提示词权重)。如果未提供,则会从 negative_prompt 输入参数生成 negative_prompt_embeds
  • output_type (str, optional, defaults to "pil") — 生成图像的输出格式。 在 PIL.Imagenp.array 之间选择。
  • return_dict (bool, optional, defaults to True) — 是否返回 StableDiffusionPipelineOutput 而不是普通的元组。
  • cross_attention_kwargs (dict, optional) — 一个 kwargs 字典,如果指定,则会传递给 self.processor 中定义的 AttentionProcessor
  • clip_skip (int, optional) — 在计算提示词嵌入时,从 CLIP 模型中跳过的层数。值为 1 表示将使用预倒数第二层的输出计算提示词嵌入。

返回

pipelines.i2vgen_xl.pipeline_i2vgen_xl.I2VGenXLPipelineOutputtuple

如果 return_dictTrue,则返回 pipelines.i2vgen_xl.pipeline_i2vgen_xl.I2VGenXLPipelineOutput,否则返回一个 tuple,其中第一个元素是包含生成帧的列表。

用于使用 I2VGenXLPipeline 进行图像到视频生成的 pipeline 的调用函数。

示例

>>> import torch
>>> from diffusers import I2VGenXLPipeline
>>> from diffusers.utils import export_to_gif, load_image

>>> pipeline = I2VGenXLPipeline.from_pretrained(
...     "ali-vilab/i2vgen-xl", torch_dtype=torch.float16, variant="fp16"
... )
>>> pipeline.enable_model_cpu_offload()

>>> image_url = (
...     "https://huggingface.co/datasets/diffusers/docs-images/resolve/main/i2vgen_xl_images/img_0009.png"
... )
>>> image = load_image(image_url).convert("RGB")

>>> prompt = "Papers were floating in the air on a table in the library"
>>> negative_prompt = "Distorted, discontinuous, Ugly, blurry, low resolution, motionless, static, disfigured, disconnected limbs, Ugly faces, incomplete arms"
>>> generator = torch.manual_seed(8888)

>>> frames = pipeline(
...     prompt=prompt,
...     image=image,
...     num_inference_steps=50,
...     negative_prompt=negative_prompt,
...     guidance_scale=9.0,
...     generator=generator,
... ).frames[0]
>>> video_path = export_to_gif(frames, "i2v.gif")

encode_prompt

< >

( prompt device num_videos_per_prompt negative_prompt = None prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None clip_skip: typing.Optional[int] = None )

参数

  • prompt (str or List[str], optional) — 要编码的提示词
  • device — (torch.device): torch 设备
  • num_videos_per_prompt (int) — 每个提示词应生成的图像数量
  • do_classifier_free_guidance (bool) — 是否使用无分类器引导(classifier free guidance)
  • negative_prompt (str or List[str], optional) — 不用于引导图像生成的提示词或提示词列表。 如果未定义,则必须传递 negative_prompt_embeds。 当不使用引导时忽略(即,如果 guidance_scale 小于 1 则忽略)。
  • prompt_embeds (torch.Tensor, optional) — 预生成的文本嵌入。 可以用于轻松调整文本输入,例如 提示词权重。 如果未提供,则将从 prompt 输入参数生成文本嵌入。
  • negative_prompt_embeds (torch.Tensor, optional) — 预生成的负面文本嵌入。 可以用于轻松调整文本输入,例如 提示词权重。 如果未提供,则将从 negative_prompt 输入参数生成 negative_prompt_embeds。
  • clip_skip (int, optional) — 在计算提示词嵌入时,从 CLIP 模型中跳过的层数。值为 1 表示将使用预倒数第二层的输出计算提示词嵌入。

将提示词编码为文本编码器隐藏状态。

I2VGenXLPipelineOutput

class diffusers.pipelines.i2vgen_xl.pipeline_i2vgen_xl.I2VGenXLPipelineOutput

< >

( frames: typing.Union[torch.Tensor, numpy.ndarray, typing.List[typing.List[PIL.Image.Image]]] )

参数

  • frames (torch.Tensor, np.ndarray, or List[List[PIL.Image.Image]]) — 视频输出列表 - 它可以是长度为 batch_size 的嵌套列表,每个子列表包含去噪的

图像到视频 pipeline 的输出类。

长度为 num_frames 的 PIL 图像序列。 它也可以是形状为 (batch_size, num_frames, channels, height, width) 的 NumPy 数组或 Torch 张量

< > 在 GitHub 上更新