CogVideoX
CogVideoX:带有专家 Transformer 的文本到视频扩散模型 来自清华大学 & ZhipuAI,作者:Zhuoyi Yang、Jiayan Teng、Wendi Zheng、Ming Ding、Shiyu Huang、Jiazheng Xu、Yuanming Yang、Wenyi Hong、Xiaohan Zhang、Guanyu Feng、Da Yin、Xiaotao Gu、Yuxuan Zhang、Weihan Wang、Yean Cheng、Ting Liu、Bin Xu、Yuxiao Dong、Jie Tang。
论文摘要如下
我们介绍了 CogVideoX,这是一个大型扩散 Transformer 模型,旨在根据文本提示生成视频。为了有效地建模视频数据,我们建议利用 3D 变分自编码器 (VAE) 来压缩空间和时间维度上的视频。为了提高文本-视频对齐,我们提出了一个专家 Transformer,其中包含专家自适应 LayerNorm,以促进两种模态之间的深度融合。通过采用渐进式训练技术,CogVideoX 擅长生成连贯的、持续时间长的视频,这些视频以显着的运动为特征。此外,我们开发了一种有效的文本-视频数据处理流程,其中包括各种数据预处理策略和视频字幕方法。它显着帮助提高了 CogVideoX 的性能,提高了生成质量和语义对齐。结果表明,CogVideoX 在多个机器指标和人工评估中都表现出最先进的性能。 CogVideoX-2B 的模型权重可在 https://github.com/THUDM/CogVideo 上公开获得。
此管道由 zRzRzRzRzRzRzR 贡献。原始代码库可以在此处找到。原始权重可以在 hf.co/THUDM 下找到。
有两个模型可用于文本到视频和视频到视频 CogVideoX 管道
THUDM/CogVideoX-2b
:运行此模型的推荐 dtype 为fp16
。THUDM/CogVideoX-5b
:运行此模型的推荐 dtype 为bf16
。
有一个模型可用于图像到视频 CogVideoX 管道
THUDM/CogVideoX-5b-I2V
:运行此模型的推荐 dtype 为bf16
。
推理
使用 torch.compile
以减少推理延迟。
首先,加载管道
import torch
from diffusers import CogVideoXPipeline, CogVideoXImageToVideoPipeline
from diffusers.utils import export_to_video,load_image
pipe = CogVideoXPipeline.from_pretrained("THUDM/CogVideoX-5b").to("cuda") # or "THUDM/CogVideoX-2b"
如果您正在使用图像到视频管道,请按如下方式加载它
pipe = CogVideoXImageToVideoPipeline.from_pretrained("THUDM/CogVideoX-5b-I2V").to("cuda")
然后将管道 transformer
组件的内存布局更改为 torch.channels_last
pipe.transformer.to(memory_format=torch.channels_last)
编译组件并运行推理
pipe.transformer = torch.compile(pipeline.transformer, mode="max-autotune", fullgraph=True)
# CogVideoX works well with long and well-described prompts
prompt = "A panda, dressed in a small, red jacket and a tiny hat, sits on a wooden stool in a serene bamboo forest. The panda's fluffy paws strum a miniature acoustic guitar, producing soft, melodic tunes. Nearby, a few other pandas gather, watching curiously and some clapping in rhythm. Sunlight filters through the tall bamboo, casting a gentle glow on the scene. The panda's face is expressive, showing concentration and joy as it plays. The background includes a small, flowing stream and vibrant green foliage, enhancing the peaceful and magical atmosphere of this unique musical performance."
video = pipe(prompt=prompt, guidance_scale=6, num_inference_steps=50).frames[0]
在 80GB A100 机器上的 T2V 基准测试 结果如下
Without torch.compile(): Average inference time: 96.89 seconds.
With torch.compile(): Average inference time: 76.27 seconds.
内存优化
CogVideoX-2b 需要约 19 GB 的 GPU 内存来解码 49 帧(8 FPS 下 6 秒视频),输出分辨率为 720x480(宽 x 高),这使得它无法在消费级 GPU 或免费层 T4 Colab 上运行。以下内存优化可用于减少内存占用。对于复制,您可以参考此脚本。
pipe.enable_model_cpu_offload()
:- 在不启用 CPU 卸载的情况下,内存使用量为
33 GB
- 启用 CPU 卸载后,内存使用量为
19 GB
- 在不启用 CPU 卸载的情况下,内存使用量为
pipe.vae.enable_tiling()
:- 启用 CPU 卸载和 tiling 后,内存使用量为
11 GB
- 启用 CPU 卸载和 tiling 后,内存使用量为
pipe.vae.enable_slicing()
CogVideoXPipeline
class diffusers.CogVideoXPipeline
< 源码 >( tokenizer: T5Tokenizer text_encoder: T5EncoderModel vae: AutoencoderKLCogVideoX transformer: CogVideoXTransformer3DModel scheduler: Union )
参数
- vae (AutoencoderKL) — 变分自编码器 (VAE) 模型,用于将视频编码和解码为潜在表示形式。
- text_encoder (
T5EncoderModel
) — 冻结的文本编码器。CogVideoX 使用 T5;特别是 t5-v1_1-xxl 变体。 - tokenizer (
T5Tokenizer
) — T5Tokenizer 类的分词器。 - transformer (CogVideoXTransformer3DModel) — 文本条件
CogVideoXTransformer3DModel
,用于对编码后的视频潜在空间进行去噪。 - scheduler (SchedulerMixin) — 一个调度器,与
transformer
结合使用,以对编码后的视频潜在空间进行去噪。
使用 CogVideoX 进行文本到视频生成的 Pipeline。
此模型继承自 DiffusionPipeline。有关库为所有 pipeline 实现的通用方法(例如下载或保存、在特定设备上运行等),请查看超类文档。
__call__
< 源码 >( prompt: Union = None negative_prompt: Union = None height: int = 480 width: int = 720 num_frames: int = 49 num_inference_steps: int = 50 timesteps: Optional = None guidance_scale: float = 6 use_dynamic_cfg: bool = False num_videos_per_prompt: int = 1 eta: float = 0.0 generator: Union = None latents: Optional = None prompt_embeds: Optional = None negative_prompt_embeds: Optional = None output_type: str = 'pil' return_dict: bool = True callback_on_step_end: Union = None callback_on_step_end_tensor_inputs: List = ['latents'] max_sequence_length: int = 226 ) → CogVideoXPipelineOutput 或 tuple
参数
- prompt (
str
或List[str]
, 可选) — 引导图像生成的提示或提示列表。如果未定义,则必须传递prompt_embeds
。 - negative_prompt (
str
或List[str]
, 可选) — 不引导图像生成的提示或提示列表。如果未定义,则必须传递negative_prompt_embeds
。当不使用 guidance 时(即,如果guidance_scale
小于1
),则忽略。 - height (
int
, 可选, 默认为 self.unet.config.sample_size * self.vae_scale_factor) — 生成图像的高度像素。为了获得最佳效果,默认设置为 1024。 - width (
int
, 可选, 默认为 self.unet.config.sample_size * self.vae_scale_factor) — 生成图像的宽度像素。为了获得最佳效果,默认设置为 1024。 - num_frames (
int
, 默认为48
) — 要生成的帧数。必须能被 self.vae_scale_factor_temporal 整除。生成的视频将包含 1 个额外的帧,因为 CogVideoX 使用 (num_seconds * fps + 1) 帧进行条件设置,其中 num_seconds 为 6,fps 为 4。但是,由于视频可以以任何 fps 保存,因此唯一需要满足的条件是上面提到的可除性。 - num_inference_steps (
int
, 可选, 默认为 50) — 去噪步骤的数量。更多的去噪步骤通常会带来更高质量的图像,但代价是推理速度较慢。 - timesteps (
List[int]
, 可选) — 自定义时间步长,用于支持在其set_timesteps
方法中使用timesteps
参数的调度器进行去噪过程。如果未定义,将使用传递num_inference_steps
时的默认行为。 必须按降序排列。 - guidance_scale (
float
, 可选, 默认为 7.0) — Classifier-Free Diffusion Guidance 中定义的 Guidance scale。guidance_scale
定义为 Imagen Paper 的公式 2 中的w
。通过设置guidance_scale > 1
启用 Guidance scale。更高的 guidance scale 鼓励生成与文本prompt
紧密相关的图像,但通常以降低图像质量为代价。 - num_videos_per_prompt (
int
, 可选, 默认为 1) — 每个 prompt 要生成的视频数量。 - generator (
torch.Generator
或List[torch.Generator]
, 可选) — 一个或一组 torch generator(s),用于使生成具有确定性。 - latents (
torch.FloatTensor
, 可选) — 预生成的噪声潜在空间,从高斯分布中采样,用作图像生成的输入。可用于通过不同的 prompt 调整相同的生成。如果未提供,将使用提供的随机generator
采样生成潜在张量。 - prompt_embeds (
torch.FloatTensor
, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如 prompt 权重。如果未提供,将从prompt
输入参数生成文本嵌入。 - negative_prompt_embeds (
torch.FloatTensor
, 可选) — 预生成的负面文本嵌入。可用于轻松调整文本输入,例如 prompt 权重。如果未提供,将从negative_prompt
输入参数生成 negative_prompt_embeds。 - output_type (
str
, 可选, 默认为"pil"
) — 生成图像的输出格式。在 PIL:PIL.Image.Image
或np.array
之间选择。 - return_dict (
bool
, 可选, 默认为True
) — 是否返回~pipelines.stable_diffusion_xl.StableDiffusionXLPipelineOutput
而不是纯元组。 - callback_on_step_end (
Callable
, 可选) — 在推理过程中每个去噪步骤结束时调用的函数。该函数被调用时带有以下参数:callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)
。callback_kwargs
将包含由callback_on_step_end_tensor_inputs
指定的所有张量的列表。 - callback_on_step_end_tensor_inputs (
List
, 可选) —callback_on_step_end
函数的张量输入列表。列表中指定的张量将作为callback_kwargs
参数传递。您将只能包含在您的 pipeline 类的._callback_tensor_inputs
属性中列出的变量。 - max_sequence_length (
int
, 默认为226
) — 编码提示中的最大序列长度。必须与self.transformer.config.max_text_seq_length
一致,否则可能导致不良结果。
返回
CogVideoXPipelineOutput 或 tuple
CogVideoXPipelineOutput 如果 return_dict
为 True,否则为 tuple
。当返回元组时,第一个元素是包含生成图像的列表。
调用 pipeline 进行生成时调用的函数。
示例
>>> import torch
>>> from diffusers import CogVideoXPipeline
>>> from diffusers.utils import export_to_video
>>> # Models: "THUDM/CogVideoX-2b" or "THUDM/CogVideoX-5b"
>>> pipe = CogVideoXPipeline.from_pretrained("THUDM/CogVideoX-2b", torch_dtype=torch.float16).to("cuda")
>>> prompt = (
... "A panda, dressed in a small, red jacket and a tiny hat, sits on a wooden stool in a serene bamboo forest. "
... "The panda's fluffy paws strum a miniature acoustic guitar, producing soft, melodic tunes. Nearby, a few other "
... "pandas gather, watching curiously and some clapping in rhythm. Sunlight filters through the tall bamboo, "
... "casting a gentle glow on the scene. The panda's face is expressive, showing concentration and joy as it plays. "
... "The background includes a small, flowing stream and vibrant green foliage, enhancing the peaceful and magical "
... "atmosphere of this unique musical performance."
... )
>>> video = pipe(prompt=prompt, guidance_scale=6, num_inference_steps=50).frames[0]
>>> export_to_video(video, "output.mp4", fps=8)
encode_prompt
< source >( prompt: Union negative_prompt: Union = None do_classifier_free_guidance: bool = True num_videos_per_prompt: int = 1 prompt_embeds: Optional = None negative_prompt_embeds: Optional = None max_sequence_length: int = 226 device: Optional = None dtype: Optional = None )
参数
- prompt (
str
或List[str]
, 可选) — 要编码的提示 - negative_prompt (
str
或List[str]
, 可选) — 不用于引导图像生成的提示或提示列表。如果未定义,则必须传递negative_prompt_embeds
代替。当不使用引导时忽略(即,如果guidance_scale
小于1
则忽略)。 - do_classifier_free_guidance (
bool
, 可选, 默认为True
) — 是否使用无分类器引导。 - num_videos_per_prompt (
int
, 可选, 默认为 1) — 每个提示应生成的视频数量。用于放置结果嵌入的 torch 设备 - prompt_embeds (
torch.Tensor
, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如 提示权重。如果未提供,则将从prompt
输入参数生成文本嵌入。 - negative_prompt_embeds (
torch.Tensor
, 可选) — 预生成的负面文本嵌入。可用于轻松调整文本输入,例如 提示权重。如果未提供,则将从negative_prompt
输入参数生成 negative_prompt_embeds。device — (torch.device
, 可选): torch 设备 dtype — (torch.dtype
, 可选): torch dtype
将提示编码为文本编码器隐藏状态。
启用融合 QKV 投影。
如果启用,则禁用 QKV 投影融合。
CogVideoXImageToVideoPipeline
class diffusers.CogVideoXImageToVideoPipeline
< source >( tokenizer: T5Tokenizer text_encoder: T5EncoderModel vae: AutoencoderKLCogVideoX transformer: CogVideoXTransformer3DModel scheduler: Union )
参数
- vae (AutoencoderKL) — 变分自动编码器 (VAE) 模型,用于将视频编码和解码为潜在表示形式以及从潜在表示形式解码视频。
- text_encoder (
T5EncoderModel
) — 冻结的文本编码器。CogVideoX 使用 T5;特别是 t5-v1_1-xxl 变体。 - tokenizer (
T5Tokenizer
) — 类 T5Tokenizer 的分词器。 - transformer (CogVideoXTransformer3DModel) — 文本条件
CogVideoXTransformer3DModel
,用于对编码的视频潜在空间进行去噪。 - scheduler (SchedulerMixin) — 调度器,与
transformer
结合使用,以对编码的视频潜在空间进行去噪。
使用 CogVideoX 的图像到视频生成管线。
此模型继承自 DiffusionPipeline。有关库为所有 pipeline 实现的通用方法(例如下载或保存、在特定设备上运行等),请查看超类文档。
__call__
< source >( image: Union prompt: Union = None negative_prompt: Union = None height: int = 480 width: int = 720 num_frames: int = 49 num_inference_steps: int = 50 timesteps: Optional = None guidance_scale: float = 6 use_dynamic_cfg: bool = False num_videos_per_prompt: int = 1 eta: float = 0.0 generator: Union = None latents: Optional = None prompt_embeds: Optional = None negative_prompt_embeds: Optional = None output_type: str = 'pil' return_dict: bool = True callback_on_step_end: Union = None callback_on_step_end_tensor_inputs: List = ['latents'] max_sequence_length: int = 226 ) → CogVideoXPipelineOutput 或 tuple
参数
- image (
PipelineImageInput
) — 用于条件生成的输入视频。必须是图像、图像列表或torch.Tensor
。 - prompt (
str
或List[str]
, 可选) — 用于引导图像生成的提示或提示列表。如果未定义,则必须传递prompt_embeds
代替。 - negative_prompt (
str
或List[str]
, 可选) — 不用于引导图像生成的提示或提示列表。如果未定义,则必须传递negative_prompt_embeds
代替。当不使用引导时忽略(即,如果guidance_scale
小于1
则忽略)。 - height (
int
, 可选, 默认为 self.unet.config.sample_size * self.vae_scale_factor) — 生成图像的高度像素。 默认设置为 1024 以获得最佳效果。 - width (
int
, 可选, 默认为 self.unet.config.sample_size * self.vae_scale_factor) — 生成图像的宽度像素。 默认设置为 1024 以获得最佳效果。 - num_frames (
int
, 默认为48
) — 要生成的帧数。必须能被 self.vae_scale_factor_temporal 整除。生成的视频将包含 1 个额外的帧,因为 CogVideoX 使用 (num_seconds * fps + 1) 帧进行条件控制,其中 num_seconds 为 6,fps 为 4。但是,由于视频可以以任何 fps 保存,因此唯一需要满足的条件是上面提到的可除性。 - num_inference_steps (
int
, 可选, 默认为 50) — 去噪步骤的数量。更多的去噪步骤通常会带来更高质量的图像,但会牺牲推理速度。 - timesteps (
List[int]
, 可选) — 用于去噪过程的自定义时间步长,适用于在其set_timesteps
方法中支持timesteps
参数的调度器。如果未定义,将使用传递num_inference_steps
时的默认行为。必须以降序排列。 - guidance_scale (
float
, 可选, 默认为 7.0) — Guidance scale,定义在 Classifier-Free Diffusion Guidance 中。guidance_scale
定义为 Imagen Paper 的公式 2 中的w
。通过设置guidance_scale > 1
启用 Guidance scale。更高的 guidance scale 鼓励生成与文本prompt
紧密相关的图像,但通常会牺牲较低的图像质量。 - num_videos_per_prompt (
int
, 可选, 默认为 1) — 每个 prompt 要生成的视频数量。 - generator (
torch.Generator
或List[torch.Generator]
, 可选) — 一个或一组 torch 生成器,用于使生成具有确定性。 - latents (
torch.FloatTensor
, 可选) — 预生成的噪声潜变量,从高斯分布中采样,用作图像生成的输入。可用于使用不同的 prompt 微调相同的生成。如果未提供,则将使用提供的随机generator
采样生成潜变量张量。 - prompt_embeds (
torch.FloatTensor
, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如 prompt 权重。如果未提供,将从prompt
输入参数生成文本嵌入。 - negative_prompt_embeds (
torch.FloatTensor
, 可选) — 预生成的负面文本嵌入。可用于轻松调整文本输入,例如 prompt 权重。如果未提供,negative_prompt_embeds 将从negative_prompt
输入参数生成。 - output_type (
str
, 可选, 默认为"pil"
) — 生成图像的输出格式。在 PIL:PIL.Image.Image
或np.array
之间选择。 - return_dict (
bool
, 可选, 默认为True
) — 是否返回~pipelines.stable_diffusion_xl.StableDiffusionXLPipelineOutput
而不是纯元组。 - callback_on_step_end (
Callable
, 可选) — 在推理期间每个去噪步骤结束时调用的函数。该函数使用以下参数调用:callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)
。callback_kwargs
将包含由callback_on_step_end_tensor_inputs
指定的所有张量的列表。 - callback_on_step_end_tensor_inputs (
List
, 可选) —callback_on_step_end
函数的张量输入列表。列表中指定的张量将作为callback_kwargs
参数传递。您将只能包含管道类的._callback_tensor_inputs
属性中列出的变量。 - max_sequence_length (
int
, 默认为226
) — 编码 prompt 中的最大序列长度。必须与self.transformer.config.max_text_seq_length
一致,否则可能导致不良结果。
返回
CogVideoXPipelineOutput 或 tuple
CogVideoXPipelineOutput 如果 return_dict
为 True,否则为 tuple
。当返回元组时,第一个元素是包含生成图像的列表。
调用 pipeline 进行生成时调用的函数。
示例
>>> import torch
>>> from diffusers import CogVideoXImageToVideoPipeline
>>> from diffusers.utils import export_to_video, load_image
>>> pipe = CogVideoXImageToVideoPipeline.from_pretrained("THUDM/CogVideoX-5b-I2V", torch_dtype=torch.bfloat16)
>>> pipe.to("cuda")
>>> prompt = "An astronaut hatching from an egg, on the surface of the moon, the darkness and depth of space realised in the background. High quality, ultrarealistic detail and breath-taking movie-like camera shot."
>>> image = load_image(
... "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/astronaut.jpg"
... )
>>> video = pipe(image, prompt, use_dynamic_cfg=True)
>>> export_to_video(video.frames[0], "output.mp4", fps=8)
encode_prompt
< source >( prompt: Union negative_prompt: Union = None do_classifier_free_guidance: bool = True num_videos_per_prompt: int = 1 prompt_embeds: Optional = None negative_prompt_embeds: Optional = None max_sequence_length: int = 226 device: Optional = None dtype: Optional = None )
参数
- prompt (
str
或List[str]
, 可选) — 要编码的 prompt - negative_prompt (
str
或List[str]
, 可选) — 不引导图像生成的 prompt 或 prompts。如果未定义,则必须传递negative_prompt_embeds
。当不使用 guidance 时忽略(即,如果guidance_scale
小于1
则忽略)。 - do_classifier_free_guidance (
bool
, 可选, 默认为True
) — 是否使用无分类器 guidance。 - num_videos_per_prompt (
int
, 可选, 默认为 1) — 每个 prompt 应生成的视频数量。用于放置结果嵌入的 torch 设备 - prompt_embeds (
torch.Tensor
, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如 prompt 权重。如果未提供,将从prompt
输入参数生成文本嵌入。 - negative_prompt_embeds (
torch.Tensor
, 可选) — 预生成的负面文本嵌入。可用于轻松调整文本输入,例如 prompt 权重。如果未提供,negative_prompt_embeds 将从negative_prompt
输入参数生成。device — (torch.device
, 可选): torch 设备 dtype — (torch.dtype
, 可选): torch dtype
将提示编码为文本编码器隐藏状态。
启用融合 QKV 投影。
如果启用,则禁用 QKV 投影融合。
CogVideoXVideoToVideoPipeline
class diffusers.CogVideoXVideoToVideoPipeline
< source >( tokenizer: T5Tokenizer text_encoder: T5EncoderModel vae: AutoencoderKLCogVideoX transformer: CogVideoXTransformer3DModel scheduler: Union )
参数
- vae (AutoencoderKL) — 变分自动编码器 (VAE) 模型,用于将视频编码和解码为潜在表示形式。
- text_encoder (
T5EncoderModel
) — 冻结的文本编码器。CogVideoX 使用 T5; 特别是 t5-v1_1-xxl 变体。 - tokenizer (
T5Tokenizer
) — T5Tokenizer 类的分词器。 - transformer (CogVideoXTransformer3DModel) — 一个文本条件化的
CogVideoXTransformer3DModel
,用于对编码后的视频潜在变量进行去噪。 - scheduler (SchedulerMixin) — 一个调度器,与
transformer
结合使用,以对编码后的视频潜在变量进行去噪。
使用 CogVideoX 进行视频到视频生成的管线。
此模型继承自 DiffusionPipeline。有关库为所有 pipeline 实现的通用方法(例如下载或保存、在特定设备上运行等),请查看超类文档。
__call__
< source >( video: List = None prompt: Union = None negative_prompt: Union = None height: int = 480 width: int = 720 num_inference_steps: int = 50 timesteps: Optional = None strength: float = 0.8 guidance_scale: float = 6 use_dynamic_cfg: bool = False num_videos_per_prompt: int = 1 eta: float = 0.0 generator: Union = None latents: Optional = None prompt_embeds: Optional = None negative_prompt_embeds: Optional = None output_type: str = 'pil' return_dict: bool = True callback_on_step_end: Union = None callback_on_step_end_tensor_inputs: List = ['latents'] max_sequence_length: int = 226 ) → CogVideoXPipelineOutput or tuple
参数
- video (
List[PIL.Image.Image]
) — 作为生成条件输入的视频。 必须是视频的图像/帧列表。 - prompt (
str
或List[str]
, 可选) — 用于引导图像生成的提示或提示列表。 如果未定义,则必须改为传递prompt_embeds
。 - negative_prompt (
str
或List[str]
, 可选) — 不用于引导图像生成的提示或提示列表。 如果未定义,则必须改为传递negative_prompt_embeds
。 当不使用 guidance 时忽略(即,如果guidance_scale
小于1
则忽略)。 - height (
int
, 可选, 默认为 self.unet.config.sample_size * self.vae_scale_factor) — 生成图像的像素高度。 默认设置为 1024 以获得最佳效果。 - width (
int
, 可选, 默认为 self.unet.config.sample_size * self.vae_scale_factor) — 生成图像的像素宽度。 默认设置为 1024 以获得最佳效果。 - num_inference_steps (
int
, 可选, 默认为 50) — 去噪步骤的数量。 更多的去噪步骤通常会以较慢的推理速度为代价,从而获得更高质量的图像。 - timesteps (
List[int]
, 可选) — 用于去噪过程的自定义时间步长,用于调度器,这些调度器在其set_timesteps
方法中支持timesteps
参数。 如果未定义,则将使用传递num_inference_steps
时的默认行为。 必须按降序排列。 - strength (
float
, 可选, 默认为 0.8) — 更高的 strength 会导致原始视频和生成视频之间产生更多差异。 - guidance_scale (
float
, 可选, 默认为 7.0) — Guidance scale,如 Classifier-Free Diffusion Guidance 中定义。guidance_scale
定义为 Imagen Paper 中公式 2 的 w。 通过设置guidance_scale > 1
启用 Guidance scale。 更高的 Guidance scale 鼓励生成与文本prompt
紧密相关的图像,通常以较低的图像质量为代价。 - num_videos_per_prompt (
int
, 可选, 默认为 1) — 每个 prompt 要生成的视频数量。 - generator (
torch.Generator
或List[torch.Generator]
, 可选) — 用于使生成具有确定性的一个或多个 torch 生成器。 - latents (
torch.FloatTensor
, 可选) — 预生成的噪声潜在变量,从高斯分布中采样,用作图像生成的输入。 可用于使用不同的 prompt 调整相同的生成。 如果未提供,则将通过使用提供的随机generator
进行采样来生成潜在变量张量。 - prompt_embeds (
torch.FloatTensor
, 可选) — 预生成的文本嵌入。 可用于轻松调整文本输入,例如 prompt 权重。 如果未提供,则将从prompt
输入参数生成文本嵌入。 - negative_prompt_embeds (
torch.FloatTensor
, 可选) — 预生成的负文本嵌入。 可用于轻松调整文本输入,例如 prompt 权重。 如果未提供,则将从negative_prompt
输入参数生成negative_prompt_embeds
。 - output_type (
str
, 可选, 默认为"pil"
) — 生成图像的输出格式。 在 PIL:PIL.Image.Image
或np.array
之间选择。 - return_dict (
bool
, 可选, 默认为True
) — 是否返回~pipelines.stable_diffusion_xl.StableDiffusionXLPipelineOutput
而不是普通元组。 - callback_on_step_end (
Callable
, 可选) — 在推理过程中每个去噪步骤结束时调用的函数。 该函数使用以下参数调用:callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)
。callback_kwargs
将包含callback_on_step_end_tensor_inputs
指定的所有张量的列表。 - callback_on_step_end_tensor_inputs (
List
, 可选) — 用于callback_on_step_end
函数的张量输入列表。列表中指定的张量将作为callback_kwargs
参数传递。您只能包含管道类的._callback_tensor_inputs
属性中列出的变量。 - max_sequence_length (
int
, 默认为226
) — 编码提示中的最大序列长度。必须与self.transformer.config.max_text_seq_length
一致,否则可能导致不良结果。
返回
CogVideoXPipelineOutput 或 tuple
CogVideoXPipelineOutput 如果 return_dict
为 True,否则为 tuple
。当返回元组时,第一个元素是包含生成图像的列表。
调用 pipeline 进行生成时调用的函数。
示例
>>> import torch
>>> from diffusers import CogVideoXDPMScheduler, CogVideoXVideoToVideoPipeline
>>> from diffusers.utils import export_to_video, load_video
>>> # Models: "THUDM/CogVideoX-2b" or "THUDM/CogVideoX-5b"
>>> pipe = CogVideoXVideoToVideoPipeline.from_pretrained("THUDM/CogVideoX-5b", torch_dtype=torch.bfloat16)
>>> pipe.to("cuda")
>>> pipe.scheduler = CogVideoXDPMScheduler.from_config(pipe.scheduler.config)
>>> input_video = load_video(
... "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/hiker.mp4"
... )
>>> prompt = (
... "An astronaut stands triumphantly at the peak of a towering mountain. Panorama of rugged peaks and "
... "valleys. Very futuristic vibe and animated aesthetic. Highlights of purple and golden colors in "
... "the scene. The sky is looks like an animated/cartoonish dream of galaxies, nebulae, stars, planets, "
... "moons, but the remainder of the scene is mostly realistic."
... )
>>> video = pipe(
... video=input_video, prompt=prompt, strength=0.8, guidance_scale=6, num_inference_steps=50
... ).frames[0]
>>> export_to_video(video, "output.mp4", fps=8)
encode_prompt
< source >( prompt: Union negative_prompt: Union = None do_classifier_free_guidance: bool = True num_videos_per_prompt: int = 1 prompt_embeds: Optional = None negative_prompt_embeds: Optional = None max_sequence_length: int = 226 device: Optional = None dtype: Optional = None )
参数
- prompt (
str
或List[str]
, 可选) — 要编码的提示 - negative_prompt (
str
或List[str]
, 可选) — 不用于引导图像生成的提示或提示列表。如果未定义,则必须传递negative_prompt_embeds
。当不使用引导时忽略(即,如果guidance_scale
小于1
则忽略)。 - do_classifier_free_guidance (
bool
, 可选, 默认为True
) — 是否使用无分类器引导。 - num_videos_per_prompt (
int
, 可选, 默认为 1) — 每个提示应生成的视频数量。 用于放置结果嵌入的 torch 设备 - prompt_embeds (
torch.Tensor
, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如 提示权重。如果未提供,则将从prompt
输入参数生成文本嵌入。 - negative_prompt_embeds (
torch.Tensor
, 可选) — 预生成的负文本嵌入。可用于轻松调整文本输入,例如 提示权重。如果未提供,则将从negative_prompt
输入参数生成 negative_prompt_embeds。 device — (torch.device
, 可选): torch 设备 dtype — (torch.dtype
, 可选): torch dtype
将提示编码为文本编码器隐藏状态。
启用融合 QKV 投影。
如果启用,则禁用 QKV 投影融合。
CogVideoXPipelineOutput
class diffusers.pipelines.cogvideo.pipeline_output.CogVideoXPipelineOutput
< source >( frames: Tensor )
CogVideo 管道的输出类。