Diffusers 文档

Cosmos

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

Cosmos

Cosmos 世界基础模型平台,助力物理 AI,由 NVIDIA 提供。

物理 AI 需要首先进行数字化训练。它需要自身的数字孪生,即策略模型,以及世界的数字孪生,即世界模型。在本文中,我们提出了 Cosmos 世界基础模型平台,以帮助开发者为他们的物理 AI 设置构建定制化的世界模型。我们将世界基础模型定位为一种通用世界模型,可以通过微调用于下游应用,从而成为定制化的世界模型。我们的平台涵盖了视频筛选流程、预训练的世界基础模型、预训练世界基础模型的后训练示例以及视频tokenizer。为了帮助物理 AI 构建者解决我们社会最关键的问题,我们将我们的平台开源,并将我们的模型以许可协议开放权重,可从https://github.com/NVIDIA/Cosmos获取。

请务必查阅调度器指南,了解如何在调度器速度和质量之间进行权衡,并查看跨管道重用组件部分,了解如何高效地将相同组件加载到多个管道中。

CosmosTextToWorldPipeline

class diffusers.CosmosTextToWorldPipeline

< >

( text_encoder: T5EncoderModel tokenizer: T5TokenizerFast transformer: CosmosTransformer3DModel vae: AutoencoderKLCosmos scheduler: EDMEulerScheduler safety_checker: CosmosSafetyChecker = None )

参数

  • text_encoder (T5EncoderModel) — 冻结的文本编码器。Cosmos 使用 T5;具体是 t5-11b 变体。
  • tokenizer (T5TokenizerFast) — T5Tokenizer 类的分词器。
  • transformer (CosmosTransformer3DModel) — 用于对编码图像潜空间进行去噪的条件 Transformer。
  • scheduler (FlowMatchEulerDiscreteScheduler) — 与 transformer 结合使用的调度器,用于对编码图像潜空间进行去噪。
  • vae (AutoencoderKLCosmos) — 变分自编码器(VAE)模型,用于将视频编码和解码为潜在表示。

使用 Cosmos Predict1 进行文本到世界生成的管道。

此模型继承自 DiffusionPipeline。有关所有管道通用的方法(下载、保存、在特定设备上运行等)的更多信息,请查看超类文档。

__call__

< >

( prompt: typing.Union[str, typing.List[str]] = None negative_prompt: typing.Union[str, typing.List[str], NoneType] = None height: int = 704 width: int = 1280 num_frames: int = 121 num_inference_steps: int = 36 guidance_scale: float = 7.0 fps: int = 30 num_videos_per_prompt: 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 callback_on_step_end: typing.Union[typing.Callable[[int, int, typing.Dict], NoneType], diffusers.callbacks.PipelineCallback, diffusers.callbacks.MultiPipelineCallbacks, NoneType] = None callback_on_step_end_tensor_inputs: typing.List[str] = ['latents'] max_sequence_length: int = 512 ) ~CosmosPipelineOutputtuple

参数

  • prompt (strList[str], 可选) — 用于引导图像生成的提示词或提示词列表。如果未定义,则必须传入 prompt_embeds
  • height (int, 默认为 720) — 生成图像的高度(像素)。
  • width (int, 默认为 1280) — 生成图像的宽度(像素)。
  • num_frames (int, 默认为 121) — 生成视频的帧数。
  • num_inference_steps (int, 默认为 36) — 去噪步数。更多的去噪步数通常会带来更高质量的图像,但推理速度会变慢。
  • guidance_scale (float, 默认为 7.0) — Classifier-Free Diffusion Guidance 中定义的引导比例。guidance_scale 定义为 Imagen Paper 中公式 2 的 w。通过设置 guidance_scale > 1 启用引导比例。
  • fps (int, 默认为 30) — 生成视频的每秒帧数。
  • num_videos_per_prompt (int, 可选, 默认为 1) — 每个提示要生成的图像数量。
  • generator (torch.GeneratorList[torch.Generator], 可选) — 用于使生成具有确定性的 torch.Generator
  • latents (torch.Tensor, 可选) — 从高斯分布中采样的预生成噪声潜空间,用作图像生成的输入。可用于通过不同的提示调整相同的生成。如果未提供,则使用提供的随机 generator 进行采样生成潜空间张量。
  • prompt_embeds (torch.Tensor, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如提示词权重。如果未提供,文本嵌入将从 prompt 输入参数生成。
  • negative_prompt_embeds (torch.FloatTensor, 可选) — 预生成的负面文本嵌入。对于 PixArt-Sigma,此负面提示应为 ""。如果未提供,负面提示嵌入将从 negative_prompt 输入参数生成。
  • output_type (str, 可选, 默认为 "pil") — 生成图像的输出格式。在 PIL.Imagenp.array 之间选择。
  • return_dict (bool, 可选, 默认为 True) — 是否返回 CosmosPipelineOutput 而不是普通元组。
  • callback_on_step_end (Callable, PipelineCallback, MultiPipelineCallbacks, 可选) — 在推理过程中,每个去噪步骤结束时调用的函数或 PipelineCallbackMultiPipelineCallbacks 的子类,具有以下参数: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 属性中列出的变量。

返回

~CosmosPipelineOutputtuple

如果 return_dictTrue,则返回 CosmosPipelineOutput,否则返回一个 tuple,其中第一个元素是生成的图像列表,第二个元素是布尔值列表,指示相应的生成的图像是否包含“不适合工作”(nsfw)内容。

用于生成的管道的调用函数。

示例

>>> import torch
>>> from diffusers import CosmosTextToWorldPipeline
>>> from diffusers.utils import export_to_video

>>> model_id = "nvidia/Cosmos-1.0-Diffusion-7B-Text2World"
>>> pipe = CosmosTextToWorldPipeline.from_pretrained(model_id, torch_dtype=torch.bfloat16)
>>> pipe.to("cuda")

>>> prompt = "A sleek, humanoid robot stands in a vast warehouse filled with neatly stacked cardboard boxes on industrial shelves. The robot's metallic body gleams under the bright, even lighting, highlighting its futuristic design and intricate joints. A glowing blue light emanates from its chest, adding a touch of advanced technology. The background is dominated by rows of boxes, suggesting a highly organized storage system. The floor is lined with wooden pallets, enhancing the industrial setting. The camera remains static, capturing the robot's poised stance amidst the orderly environment, with a shallow depth of field that keeps the focus on the robot while subtly blurring the background for a cinematic effect."

>>> output = pipe(prompt=prompt).frames[0]
>>> export_to_video(output, "output.mp4", fps=30)

encode_prompt

< >

( prompt: typing.Union[str, typing.List[str]] negative_prompt: typing.Union[str, typing.List[str], NoneType] = None do_classifier_free_guidance: bool = True num_videos_per_prompt: int = 1 prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None max_sequence_length: int = 512 device: typing.Optional[torch.device] = None dtype: typing.Optional[torch.dtype] = None )

参数

  • prompt (str or List[str], optional) — 要编码的提示。
  • negative_prompt (strList[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_embeds 将从 negative_prompt 输入参数生成。
  • device — (torch.device, 可选): torch 设备
  • dtype — (torch.dtype, 可选): torch 数据类型

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

CosmosVideoToWorldPipeline

class diffusers.CosmosVideoToWorldPipeline

< >

( text_encoder: T5EncoderModel tokenizer: T5TokenizerFast transformer: CosmosTransformer3DModel vae: AutoencoderKLCosmos scheduler: EDMEulerScheduler safety_checker: CosmosSafetyChecker = None )

参数

  • text_encoder (T5EncoderModel) — 冻结的文本编码器。Cosmos 使用 T5;特别是 t5-11b 变体。
  • tokenizer (T5TokenizerFast) — T5Tokenizer 类的分词器。
  • transformer (CosmosTransformer3DModel) — 用于对编码图像潜伏进行去噪的条件变换器。
  • scheduler (FlowMatchEulerDiscreteScheduler) — 与 transformer 结合使用的调度器,用于对编码图像潜伏进行去噪。
  • vae (AutoencoderKLCosmos) — 变分自动编码器 (VAE) 模型,用于将视频编码和解码为潜在表示。

使用 Cosmos Predict-1 进行图像到世界和视频到世界生成的流水线。

此模型继承自 DiffusionPipeline。有关所有管道通用的方法(下载、保存、在特定设备上运行等)的更多信息,请查看超类文档。

__call__

< >

( image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, typing.List[PIL.Image.Image], typing.List[numpy.ndarray], typing.List[torch.Tensor]] = None video: typing.List[typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, typing.List[PIL.Image.Image], typing.List[numpy.ndarray], typing.List[torch.Tensor]]] = None prompt: typing.Union[str, typing.List[str]] = None negative_prompt: typing.Union[str, typing.List[str], NoneType] = None height: int = 704 width: int = 1280 num_frames: int = 121 num_inference_steps: int = 36 guidance_scale: float = 7.0 input_frames_guidance: bool = False augment_sigma: float = 0.001 fps: int = 30 num_videos_per_prompt: 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 callback_on_step_end: typing.Union[typing.Callable[[int, int, typing.Dict], NoneType], diffusers.callbacks.PipelineCallback, diffusers.callbacks.MultiPipelineCallbacks, NoneType] = None callback_on_step_end_tensor_inputs: typing.List[str] = ['latents'] max_sequence_length: int = 512 ) ~CosmosPipelineOutput or tuple

参数

  • prompt (strList[str], 可选) — 引导图像生成的提示。如果未定义,则必须传递 prompt_embeds
  • height (int, 默认为 720) — 生成图像的高度(像素)。
  • width (int, 默认为 1280) — 生成图像的宽度(像素)。
  • num_frames (int, 默认为 121) — 生成视频的帧数。
  • num_inference_steps (int, 默认为 36) — 去噪步数。更多去噪步数通常会带来更高质量的图像,但推理速度会变慢。
  • guidance_scale (float, 默认为 7.0) — Classifier-Free Diffusion Guidance 中定义的指导比例。guidance_scale 定义为 Imagen Paper 中公式 2 的 w。通过设置 guidance_scale > 1 启用指导比例。
  • fps (int, 默认为 30) — 生成视频的每秒帧数。
  • num_videos_per_prompt (int, 可选, 默认为 1) — 每个提示生成的图像数量。
  • generator (torch.GeneratorList[torch.Generator], 可选) — 用于使生成具有确定性的 torch.Generator
  • latents (torch.Tensor, 可选) — 从高斯分布中采样的预生成的噪声潜伏,用作图像生成的输入。可用于使用不同的提示调整相同的生成。如果未提供,潜伏张量将通过使用提供的随机 generator 采样生成。
  • prompt_embeds (torch.Tensor, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如提示权重。如果未提供,文本嵌入将从 prompt 输入参数生成。
  • negative_prompt_embeds (torch.FloatTensor, 可选) — 预生成的负文本嵌入。可用于轻松调整文本输入,例如提示权重。如果未提供,negative_prompt_embeds 将从 negative_prompt 输入参数生成。
  • output_type (str, 可选, 默认为 "pil") — 生成图像的输出格式。选择 PIL.Imagenp.array
  • return_dict (bool, 可选, 默认为 True) — 是否返回 CosmosPipelineOutput 而不是普通元组。
  • callback_on_step_end (Callable, PipelineCallback, MultiPipelineCallbacks, 可选) — 在推理过程中,每个去噪步骤结束时调用的函数或 PipelineCallbackMultiPipelineCallbacks 的子类,具有以下参数: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 属性中列出的变量。

返回

~CosmosPipelineOutputtuple

如果 return_dictTrue,则返回 CosmosPipelineOutput,否则返回一个 tuple,其中第一个元素是生成的图像列表,第二个元素是布尔值列表,指示相应的生成的图像是否包含“不适合工作”(nsfw)内容。

用于生成的管道的调用函数。

示例

图像条件

>>> import torch
>>> from diffusers import CosmosVideoToWorldPipeline
>>> from diffusers.utils import export_to_video, load_image

>>> model_id = "nvidia/Cosmos-1.0-Diffusion-7B-Video2World"
>>> pipe = CosmosVideoToWorldPipeline.from_pretrained(model_id, torch_dtype=torch.bfloat16)
>>> pipe.to("cuda")

>>> prompt = "The video depicts a long, straight highway stretching into the distance, flanked by metal guardrails. The road is divided into multiple lanes, with a few vehicles visible in the far distance. The surrounding landscape features dry, grassy fields on one side and rolling hills on the other. The sky is mostly clear with a few scattered clouds, suggesting a bright, sunny day."
>>> image = load_image(
...     "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/cosmos/cosmos-video2world-input.jpg"
... )

>>> video = pipe(image=image, prompt=prompt).frames[0]
>>> export_to_video(video, "output.mp4", fps=30)

视频条件

>>> import torch
>>> from diffusers import CosmosVideoToWorldPipeline
>>> from diffusers.utils import export_to_video, load_video

>>> model_id = "nvidia/Cosmos-1.0-Diffusion-7B-Video2World"
>>> pipe = CosmosVideoToWorldPipeline.from_pretrained(model_id, torch_dtype=torch.bfloat16)
>>> pipe.transformer = torch.compile(pipe.transformer)
>>> pipe.to("cuda")

>>> prompt = "The video depicts a winding mountain road covered in snow, with a single vehicle traveling along it. The road is flanked by steep, rocky cliffs and sparse vegetation. The landscape is characterized by rugged terrain and a river visible in the distance. The scene captures the solitude and beauty of a winter drive through a mountainous region."
>>> video = load_video(
...     "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/cosmos/cosmos-video2world-input-vid.mp4"
... )[
...     :21
... ]  # This example uses only the first 21 frames

>>> video = pipe(video=video, prompt=prompt).frames[0]
>>> export_to_video(video, "output.mp4", fps=30)

encode_prompt

< >

( prompt: typing.Union[str, typing.List[str]] negative_prompt: typing.Union[str, typing.List[str], NoneType] = None do_classifier_free_guidance: bool = True num_videos_per_prompt: int = 1 prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None max_sequence_length: int = 512 device: typing.Optional[torch.device] = None dtype: typing.Optional[torch.dtype] = None )

参数

  • prompt (strList[str], 可选) — 要编码的提示
  • negative_prompt (strList[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_embeds 将从 negative_prompt 输入参数生成。
  • device — (torch.device, 可选): torch 设备
  • dtype — (torch.dtype, 可选): torch 数据类型

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

Cosmos2TextToImagePipeline

class diffusers.Cosmos2TextToImagePipeline

< >

( text_encoder: T5EncoderModel tokenizer: T5TokenizerFast transformer: CosmosTransformer3DModel vae: AutoencoderKLWan scheduler: FlowMatchEulerDiscreteScheduler safety_checker: CosmosSafetyChecker = None )

参数

  • text_encoder (T5EncoderModel) — 冻结的文本编码器。Cosmos 使用 T5;特别是 t5-11b 变体。
  • tokenizer (T5TokenizerFast) — T5Tokenizer 类的分词器。
  • transformer (CosmosTransformer3DModel) — 用于对编码图像潜伏进行去噪的条件变换器。
  • scheduler (FlowMatchEulerDiscreteScheduler) — 与 transformer 结合使用的调度器,用于对编码图像潜伏进行去噪。
  • vae (AutoencoderKLWan) — 变分自动编码器 (VAE) 模型,用于将视频编码和解码为潜在表示。

使用 Cosmos Predict2 进行文本到图像生成的流水线。

此模型继承自 DiffusionPipeline。有关所有管道通用的方法(下载、保存、在特定设备上运行等)的更多信息,请查看超类文档。

__call__

< >

( prompt: typing.Union[str, typing.List[str]] = None negative_prompt: typing.Union[str, typing.List[str], NoneType] = None height: int = 768 width: int = 1360 num_inference_steps: int = 35 guidance_scale: float = 7.0 num_images_per_prompt: 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 callback_on_step_end: typing.Union[typing.Callable[[int, int, typing.Dict], NoneType], diffusers.callbacks.PipelineCallback, diffusers.callbacks.MultiPipelineCallbacks, NoneType] = None callback_on_step_end_tensor_inputs: typing.List[str] = ['latents'] max_sequence_length: int = 512 ) ~CosmosImagePipelineOutput or tuple

参数

  • prompt (strList[str], 可选) — 引导图像生成的提示。如果未定义,则必须传递 prompt_embeds
  • height (int, 默认为 768) — 生成图像的高度(像素)。
  • width (int, 默认为 1360) — 生成图像的宽度(像素)。
  • num_inference_steps (int, 默认为 35) — 去噪步骤的数量。去噪步骤越多,通常会生成更高质量的图像,但推理速度会变慢。
  • guidance_scale (float, 默认为 7.0) — 在 Classifier-Free Diffusion Guidance 中定义的指导比例。guidance_scaleImagen Paper 的公式 2 中定义为 w。通过设置 guidance_scale > 1 启用指导比例。
  • num_images_per_prompt (int, 可选, 默认为 1) — 每个提示生成的图像数量。
  • generator (torch.GeneratorList[torch.Generator], 可选) — 一个 torch.Generator 用于使生成过程确定化。
  • latents (torch.Tensor, 可选) — 从高斯分布采样的预生成噪声潜在变量,用作图像生成的输入。可用于通过不同的提示调整相同的生成。如果未提供,则使用提供的随机 generator 进行采样以生成潜在张量。
  • prompt_embeds (torch.Tensor, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如提示权重。如果未提供,则将从 prompt 输入参数生成文本嵌入。
  • negative_prompt_embeds (torch.FloatTensor, 可选) — 预生成的负面文本嵌入。对于 PixArt-Sigma,此负面提示应为“”。如果未提供,negative_prompt_embeds 将从 negative_prompt 输入参数生成。
  • output_type (str, 可选, 默认为 "pil") — 生成图像的输出格式。在 PIL.Imagenp.array 之间选择。
  • return_dict (bool, 可选, 默认为 True) — 是否返回 CosmosImagePipelineOutput 而不是普通元组。
  • callback_on_step_end (Callable, PipelineCallback, MultiPipelineCallbacks, 可选) — 在推理过程中,每个去噪步骤结束时调用的函数或 PipelineCallbackMultiPipelineCallbacks 的子类。参数如下: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 属性中列出的变量。

返回

~CosmosImagePipelineOutputtuple

如果 return_dictTrue,则返回 CosmosImagePipelineOutput,否则返回一个 tuple,其中第一个元素是生成的图像列表,第二个元素是指示相应生成的图像是否包含“不适合工作”(nsfw)内容的 bool 列表。

用于生成的管道的调用函数。

示例

>>> import torch
>>> from diffusers import Cosmos2TextToImagePipeline

>>> # Available checkpoints: nvidia/Cosmos-Predict2-2B-Text2Image, nvidia/Cosmos-Predict2-14B-Text2Image
>>> model_id = "nvidia/Cosmos-Predict2-2B-Text2Image"
>>> pipe = Cosmos2TextToImagePipeline.from_pretrained(model_id, torch_dtype=torch.bfloat16)
>>> pipe.to("cuda")

>>> prompt = "A close-up shot captures a vibrant yellow scrubber vigorously working on a grimy plate, its bristles moving in circular motions to lift stubborn grease and food residue. The dish, once covered in remnants of a hearty meal, gradually reveals its original glossy surface. Suds form and bubble around the scrubber, creating a satisfying visual of cleanliness in progress. The sound of scrubbing fills the air, accompanied by the gentle clinking of the dish against the sink. As the scrubber continues its task, the dish transforms, gleaming under the bright kitchen lights, symbolizing the triumph of cleanliness over mess."
>>> negative_prompt = "The video captures a series of frames showing ugly scenes, static with no motion, motion blur, over-saturation, shaky footage, low resolution, grainy texture, pixelated images, poorly lit areas, underexposed and overexposed scenes, poor color balance, washed out colors, choppy sequences, jerky movements, low frame rate, artifacting, color banding, unnatural transitions, outdated special effects, fake elements, unconvincing visuals, poorly edited content, jump cuts, visual noise, and flickering. Overall, the video is of poor quality."

>>> output = pipe(
...     prompt=prompt, negative_prompt=negative_prompt, generator=torch.Generator().manual_seed(1)
... ).images[0]
>>> output.save("output.png")

encode_prompt

< >

( prompt: typing.Union[str, typing.List[str]] negative_prompt: typing.Union[str, typing.List[str], NoneType] = None do_classifier_free_guidance: bool = True num_images_per_prompt: int = 1 prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None max_sequence_length: int = 512 device: typing.Optional[torch.device] = None dtype: typing.Optional[torch.dtype] = None )

参数

  • prompt (strList[str], 可选) — 要编码的提示。
  • negative_prompt (strList[str], 可选) — 不引导图像生成的提示。如果未定义,则必须传递 negative_prompt_embeds。当不使用引导时(即 guidance_scale 小于 1 时),此参数将被忽略。
  • do_classifier_free_guidance (bool, 可选, 默认为 True) — 是否使用分类器自由引导。
  • num_images_per_prompt (int, 可选, 默认为 1) — 每个提示应生成的视频数量。用于放置结果嵌入的 torch 设备。
  • prompt_embeds (torch.Tensor, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如提示权重。如果未提供,则将从 prompt 输入参数生成文本嵌入。
  • negative_prompt_embeds (torch.Tensor, 可选) — 预生成的负面文本嵌入。可用于轻松调整文本输入,例如提示权重。如果未提供,negative_prompt_embeds 将从 negative_prompt 输入参数生成。
  • device — (torch.device, 可选): 用于放置结果嵌入的 torch 设备。
  • dtype — (torch.dtype, 可选): torch 数据类型。

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

Cosmos2VideoToWorldPipeline

class diffusers.Cosmos2VideoToWorldPipeline

< >

( text_encoder: T5EncoderModel tokenizer: T5TokenizerFast transformer: CosmosTransformer3DModel vae: AutoencoderKLWan scheduler: FlowMatchEulerDiscreteScheduler safety_checker: CosmosSafetyChecker = None )

参数

  • text_encoder (T5EncoderModel) — 冻结的文本编码器。Cosmos 使用 T5;具体是 t5-11b 变体。
  • tokenizer (T5TokenizerFast) — T5Tokenizer 类的分词器。
  • transformer (CosmosTransformer3DModel) — 用于去噪编码图像潜在变量的条件 Transformer。
  • scheduler (FlowMatchEulerDiscreteScheduler) — 与 transformer 结合使用的调度器,用于去噪编码图像潜在变量。
  • vae (AutoencoderKLWan) — 变分自编码器(VAE)模型,用于将视频编码和解码为潜在表示。

使用 Cosmos Predict2 进行视频到世界生成的管道。

此模型继承自 DiffusionPipeline。有关所有管道通用的方法(下载、保存、在特定设备上运行等)的更多信息,请查看超类文档。

__call__

< >

( image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, typing.List[PIL.Image.Image], typing.List[numpy.ndarray], typing.List[torch.Tensor]] = None video: typing.List[typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, typing.List[PIL.Image.Image], typing.List[numpy.ndarray], typing.List[torch.Tensor]]] = None prompt: typing.Union[str, typing.List[str]] = None negative_prompt: typing.Union[str, typing.List[str], NoneType] = None height: int = 704 width: int = 1280 num_frames: int = 93 num_inference_steps: int = 35 guidance_scale: float = 7.0 fps: int = 16 num_videos_per_prompt: 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 callback_on_step_end: typing.Union[typing.Callable[[int, int, typing.Dict], NoneType], diffusers.callbacks.PipelineCallback, diffusers.callbacks.MultiPipelineCallbacks, NoneType] = None callback_on_step_end_tensor_inputs: typing.List[str] = ['latents'] max_sequence_length: int = 512 sigma_conditioning: float = 0.0001 ) ~CosmosPipelineOutputtuple

参数

  • image (PIL.Image.Image, np.ndarray, torch.Tensor, 可选) — 用作视频生成条件输入的图像。
  • video (List[PIL.Image.Image], np.ndarray, torch.Tensor, 可选) — 用作视频生成条件输入的视频。
  • prompt (strList[str], 可选) — 引导图像生成的提示。如果未定义,则必须传递 prompt_embeds
  • height (int, 默认为 704) — 生成图像的高度(像素)。
  • width (int, 默认为 1280) — 生成图像的宽度(像素)。
  • num_frames (int, 默认为 93) — 生成视频的帧数。
  • num_inference_steps (int, 默认为 35) — 去噪步骤的数量。去噪步骤越多,通常会生成更高质量的图像,但推理速度会变慢。
  • guidance_scale (float, 默认为 7.0) — 在 Classifier-Free Diffusion Guidance 中定义的指导比例。guidance_scaleImagen Paper 的公式 2 中定义为 w。通过设置 guidance_scale > 1 启用指导比例。
  • fps (int, 默认为 16) — 生成视频的每秒帧数。
  • num_videos_per_prompt (int, 可选, 默认为 1) — 每个提示生成的图像数量。
  • generator (torch.GeneratorList[torch.Generator], 可选) — 一个 torch.Generator 用于使生成过程确定化。
  • latents (torch.Tensor, 可选) — 从高斯分布采样的预生成噪声潜在变量,用作图像生成的输入。可用于通过不同的提示调整相同的生成。如果未提供,则使用提供的随机 generator 进行采样以生成潜在张量。
  • prompt_embeds (torch.Tensor, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如提示权重。如果未提供,则将从 prompt 输入参数生成文本嵌入。
  • negative_prompt_embeds (torch.FloatTensor, 可选) — 预生成的负面文本嵌入。对于 PixArt-Sigma,此负面提示应为“”。如果未提供,negative_prompt_embeds 将从 negative_prompt 输入参数生成。
  • output_type (str, 可选, 默认为 "pil") — 生成图像的输出格式。在 PIL.Imagenp.array 之间选择。
  • return_dict (bool, 可选, 默认为 True) — 是否返回 CosmosPipelineOutput 而不是普通元组。
  • callback_on_step_end (Callable, PipelineCallback, MultiPipelineCallbacks, 可选) — 在推理过程中,每个去噪步骤结束时调用的函数或 PipelineCallbackMultiPipelineCallbacks 的子类。参数如下: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, 默认为 512) — 提示词中的最大 token 数。如果提示词超过此长度,则将被截断。如果提示词短于此长度,则将进行填充。
  • sigma_conditioning (float, 默认为 0.0001) — 用于缩放条件潜变量的 sigma 值。理想情况下,它不应更改或应设置为接近零的小值。

返回

~CosmosPipelineOutputtuple

如果 return_dictTrue,则返回 CosmosPipelineOutput,否则返回一个 tuple,其中第一个元素是生成的图像列表,第二个元素是布尔值列表,指示相应的生成的图像是否包含“不适合工作”(nsfw)内容。

用于生成的管道的调用函数。

示例

>>> import torch
>>> from diffusers import Cosmos2VideoToWorldPipeline
>>> from diffusers.utils import export_to_video, load_image

>>> # Available checkpoints: nvidia/Cosmos-Predict2-2B-Video2World, nvidia/Cosmos-Predict2-14B-Video2World
>>> model_id = "nvidia/Cosmos-Predict2-2B-Video2World"
>>> pipe = Cosmos2VideoToWorldPipeline.from_pretrained(model_id, torch_dtype=torch.bfloat16)
>>> pipe.to("cuda")

>>> prompt = "A close-up shot captures a vibrant yellow scrubber vigorously working on a grimy plate, its bristles moving in circular motions to lift stubborn grease and food residue. The dish, once covered in remnants of a hearty meal, gradually reveals its original glossy surface. Suds form and bubble around the scrubber, creating a satisfying visual of cleanliness in progress. The sound of scrubbing fills the air, accompanied by the gentle clinking of the dish against the sink. As the scrubber continues its task, the dish transforms, gleaming under the bright kitchen lights, symbolizing the triumph of cleanliness over mess."
>>> negative_prompt = "The video captures a series of frames showing ugly scenes, static with no motion, motion blur, over-saturation, shaky footage, low resolution, grainy texture, pixelated images, poorly lit areas, underexposed and overexposed scenes, poor color balance, washed out colors, choppy sequences, jerky movements, low frame rate, artifacting, color banding, unnatural transitions, outdated special effects, fake elements, unconvincing visuals, poorly edited content, jump cuts, visual noise, and flickering. Overall, the video is of poor quality."
>>> image = load_image(
...     "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/yellow-scrubber.png"
... )

>>> video = pipe(
...     image=image, prompt=prompt, negative_prompt=negative_prompt, generator=torch.Generator().manual_seed(1)
... ).frames[0]
>>> export_to_video(video, "output.mp4", fps=16)

encode_prompt

< >

( prompt: typing.Union[str, typing.List[str]] negative_prompt: typing.Union[str, typing.List[str], NoneType] = None do_classifier_free_guidance: bool = True num_videos_per_prompt: int = 1 prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None max_sequence_length: int = 512 device: typing.Optional[torch.device] = None dtype: typing.Optional[torch.dtype] = None )

参数

  • prompt (strList[str], 可选) — 待编码的提示词
  • negative_prompt (strList[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 输入参数生成。
  • device — (torch.device, 可选): torch 设备
  • dtype — (torch.dtype, 可选): torch 数据类型

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

CosmosPipelineOutput

class diffusers.pipelines.cosmos.pipeline_output.CosmosPipelineOutput

< >

( frames: Tensor )

参数

  • frames (torch.Tensor, np.ndarray, 或 List[List[PIL.Image.Image]]) — 视频输出列表 - 可以是长度为 batch_size 的嵌套列表,每个子列表包含长度为 num_frames 的去噪 PIL 图像序列。也可以是形状为 (batch_size, num_frames, channels, height, width) 的 NumPy 数组或 Torch 张量。

Cosmos 任意到世界/视频管道的输出类。

CosmosImagePipelineOutput

class diffusers.pipelines.cosmos.pipeline_output.CosmosImagePipelineOutput

< >

( images: typing.Union[typing.List[PIL.Image.Image], numpy.ndarray] )

参数

  • images (List[PIL.Image.Image]np.ndarray) — 去噪的 PIL 图像列表,长度为 batch_size,或形状为 (batch_size, height, width, num_channels) 的 numpy 数组。PIL 图像或 numpy 数组表示扩散管道的去噪图像。

Cosmos 任意到图像管道的输出类。

< > 在 GitHub 上更新