Diffusers 文档
Cosmos
并获得增强的文档体验
开始使用
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 ) → ~CosmosPipelineOutput
或 tuple
参数
- prompt (
str
或List[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.Generator
或List[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.Image
或np.array
之间选择。 - return_dict (
bool
, 可选, 默认为True
) — 是否返回CosmosPipelineOutput
而不是普通元组。 - callback_on_step_end (
Callable
,PipelineCallback
,MultiPipelineCallbacks
, 可选) — 在推理过程中,每个去噪步骤结束时调用的函数或PipelineCallback
或MultiPipelineCallbacks
的子类,具有以下参数: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
属性中列出的变量。
返回
~CosmosPipelineOutput
或 tuple
如果 return_dict
为 True
,则返回 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
orList[str]
, optional) — 要编码的提示。 - 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_embeds
将从negative_prompt
输入参数生成。 - device — (
torch.device
, 可选): torch 设备 - dtype — (
torch.dtype
, 可选): torch 数据类型
将提示编码为文本编码器隐藏状态。
CosmosVideoToWorldPipeline
class diffusers.CosmosVideoToWorldPipeline
< source >( 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__
< source >( 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 (
str
或List[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.Generator
或List[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.Image
或np.array
。 - return_dict (
bool
, 可选, 默认为True
) — 是否返回CosmosPipelineOutput
而不是普通元组。 - callback_on_step_end (
Callable
,PipelineCallback
,MultiPipelineCallbacks
, 可选) — 在推理过程中,每个去噪步骤结束时调用的函数或PipelineCallback
或MultiPipelineCallbacks
的子类,具有以下参数: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
属性中列出的变量。
返回
~CosmosPipelineOutput
或 tuple
如果 return_dict
为 True
,则返回 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
< source >( 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
或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_embeds
将从negative_prompt
输入参数生成。 - device — (
torch.device
, 可选): torch 设备 - dtype — (
torch.dtype
, 可选): torch 数据类型
将提示编码为文本编码器隐藏状态。
Cosmos2TextToImagePipeline
class diffusers.Cosmos2TextToImagePipeline
< source >( 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__
< source >( 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 (
str
或List[str]
, 可选) — 引导图像生成的提示。如果未定义,则必须传递prompt_embeds
。 - height (
int
, 默认为768
) — 生成图像的高度(像素)。 - width (
int
, 默认为1360
) — 生成图像的宽度(像素)。 - num_inference_steps (
int
, 默认为35
) — 去噪步骤的数量。去噪步骤越多,通常会生成更高质量的图像,但推理速度会变慢。 - guidance_scale (
float
, 默认为7.0
) — 在 Classifier-Free Diffusion Guidance 中定义的指导比例。guidance_scale
在 Imagen Paper 的公式 2 中定义为w
。通过设置guidance_scale > 1
启用指导比例。 - num_images_per_prompt (
int
, 可选, 默认为 1) — 每个提示生成的图像数量。 - generator (
torch.Generator
或List[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.Image
或np.array
之间选择。 - return_dict (
bool
, 可选, 默认为True
) — 是否返回CosmosImagePipelineOutput
而不是普通元组。 - callback_on_step_end (
Callable
,PipelineCallback
,MultiPipelineCallbacks
, 可选) — 在推理过程中,每个去噪步骤结束时调用的函数或PipelineCallback
或MultiPipelineCallbacks
的子类。参数如下: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
属性中列出的变量。
返回
~CosmosImagePipelineOutput
或 tuple
如果 return_dict
为 True
,则返回 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 (
str
或List[str]
, 可选) — 要编码的提示。 - negative_prompt (
str
或List[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 ) → ~CosmosPipelineOutput
或 tuple
参数
- image (
PIL.Image.Image
,np.ndarray
,torch.Tensor
, 可选) — 用作视频生成条件输入的图像。 - video (
List[PIL.Image.Image]
,np.ndarray
,torch.Tensor
, 可选) — 用作视频生成条件输入的视频。 - prompt (
str
或List[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_scale
在 Imagen Paper 的公式 2 中定义为w
。通过设置guidance_scale > 1
启用指导比例。 - fps (
int
, 默认为16
) — 生成视频的每秒帧数。 - num_videos_per_prompt (
int
, 可选, 默认为 1) — 每个提示生成的图像数量。 - generator (
torch.Generator
或List[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.Image
或np.array
之间选择。 - return_dict (
bool
, 可选, 默认为True
) — 是否返回CosmosPipelineOutput
而不是普通元组。 - callback_on_step_end (
Callable
,PipelineCallback
,MultiPipelineCallbacks
, 可选) — 在推理过程中,每个去噪步骤结束时调用的函数或PipelineCallback
或MultiPipelineCallbacks
的子类。参数如下: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 值。理想情况下,它不应更改或应设置为接近零的小值。
返回
~CosmosPipelineOutput
或 tuple
如果 return_dict
为 True
,则返回 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
< source >( 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
或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
输入参数生成。 - device — (
torch.device
, 可选): torch 设备 - dtype — (
torch.dtype
, 可选): torch 数据类型
将提示编码为文本编码器隐藏状态。
CosmosPipelineOutput
class diffusers.pipelines.cosmos.pipeline_output.CosmosPipelineOutput
< source >( frames: Tensor )
Cosmos 任意到世界/视频管道的输出类。
CosmosImagePipelineOutput
class diffusers.pipelines.cosmos.pipeline_output.CosmosImagePipelineOutput
< source >( images: typing.Union[typing.List[PIL.Image.Image], numpy.ndarray] )
Cosmos 任意到图像管道的输出类。