Diffusers 文件

LTX-影片

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

LoRA MPS

LTX-影片

LTX-Video 是一種擴散 Transformer,旨在實現從文字和影像快速即時生成高解析度影片。LTX-Video 的主要特點是 Video-VAE。Video-VAE 具有更高的畫素到潛在空間壓縮比(1:192),這使得影片資料處理更高效,生成速度更快。為了支援並防止在生成過程中丟失更精細的細節,Video-VAE 解碼器執行潛在空間到畫素的轉換**和**最後的去噪步驟。

您可以在 Lightricks 組織下找到所有原始的 LTX-Video 檢查點。

點選右側邊欄的 LTX-Video 模型,檢視更多其他影片生成任務的示例。

以下示例演示瞭如何生成針對記憶體或推理速度進行最佳化的影片。

記憶體
推理速度

有關各種記憶體節省技術的更多詳細資訊,請參閱減少記憶體使用指南。

下面的 LTX-Video 模型需要約 10GB 的視訊記憶體。

import torch
from diffusers import LTXPipeline, AutoModel
from diffusers.hooks import apply_group_offloading
from diffusers.utils import export_to_video

# fp8 layerwise weight-casting
transformer = AutoModel.from_pretrained(
    "Lightricks/LTX-Video",
    subfolder="transformer",
    torch_dtype=torch.bfloat16
)
transformer.enable_layerwise_casting(
    storage_dtype=torch.float8_e4m3fn, compute_dtype=torch.bfloat16
)

pipeline = LTXPipeline.from_pretrained("Lightricks/LTX-Video", transformer=transformer, torch_dtype=torch.bfloat16)

# group-offloading
onload_device = torch.device("cuda")
offload_device = torch.device("cpu")
pipeline.transformer.enable_group_offload(onload_device=onload_device, offload_device=offload_device, offload_type="leaf_level", use_stream=True)
apply_group_offloading(pipeline.text_encoder, onload_device=onload_device, offload_type="block_level", num_blocks_per_group=2)
apply_group_offloading(pipeline.vae, onload_device=onload_device, offload_type="leaf_level")

prompt = """
A woman with long brown hair and light skin smiles at another woman with long blonde hair.
The woman with brown hair wears a black jacket and has a small, barely noticeable mole on her right cheek.
The camera angle is a close-up, focused on the woman with brown hair's face. The lighting is warm and 
natural, likely from the setting sun, casting a soft glow on the scene. The scene appears to be real-life footage
"""
negative_prompt = "worst quality, inconsistent motion, blurry, jittery, distorted"

video = pipeline(
    prompt=prompt,
    negative_prompt=negative_prompt,
    width=768,
    height=512,
    num_frames=161,
    decode_timestep=0.03,
    decode_noise_scale=0.025,
    num_inference_steps=50,
).frames[0]
export_to_video(video, "output.mp4", fps=24)

注意事項

  • 請參考 LTX-Video 倉庫中推薦的生成設定。

    • Transformer、VAE 和文字編碼器推薦的資料型別是 torch.bfloat16。VAE 和文字編碼器也可以是 torch.float32torch.float16
    • 對於 LTX-Video 的引導蒸餾變體,將 guidance_scale 設定為 1.0。對於任何其他模型,guidance_scale 應設定得更高,例如 5.0,以獲得良好的生成質量。
    • 對於時間步感知 VAE 變體(LTX-Video 0.9.1 及以上版本),將 decode_timestep 設定為 0.05,將 image_cond_noise_scale 設定為 0.025
    • 對於支援在多個條件影像和影片之間進行插值的變體(LTX-Video 0.9.5 及以上版本),請使用相似的影像和影片以獲得最佳結果。偏離條件輸入可能會導致生成的影片中出現 abrupt transitionts。
  • LTX-Video 0.9.7 包含一個空間潛在上取樣器和一個 13B 引數的 Transformer。在推理過程中,首先快速生成低解析度影片,然後進行上取樣和精細化。

    顯示示例程式碼
    import torch
    from diffusers import LTXConditionPipeline, LTXLatentUpsamplePipeline
    from diffusers.pipelines.ltx.pipeline_ltx_condition import LTXVideoCondition
    from diffusers.utils import export_to_video, load_video
    
    pipeline = LTXConditionPipeline.from_pretrained("Lightricks/LTX-Video-0.9.7-dev", torch_dtype=torch.bfloat16)
    pipeline_upsample = LTXLatentUpsamplePipeline.from_pretrained("Lightricks/ltxv-spatial-upscaler-0.9.7", vae=pipeline.vae, torch_dtype=torch.bfloat16)
    pipeline.to("cuda")
    pipe_upsample.to("cuda")
    pipeline.vae.enable_tiling()
    
    def round_to_nearest_resolution_acceptable_by_vae(height, width):
        height = height - (height % pipeline.vae_temporal_compression_ratio)
        width = width - (width % pipeline.vae_temporal_compression_ratio)
        return height, width
    
    video = load_video(
        "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/cosmos/cosmos-video2world-input-vid.mp4"
    )[:21]  # only use the first 21 frames as conditioning
    condition1 = LTXVideoCondition(video=video, frame_index=0)
    
    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.
    """
    negative_prompt = "worst quality, inconsistent motion, blurry, jittery, distorted"
    expected_height, expected_width = 768, 1152
    downscale_factor = 2 / 3
    num_frames = 161
    
    # 1. Generate video at smaller resolution
    # Text-only conditioning is also supported without the need to pass `conditions`
    downscaled_height, downscaled_width = int(expected_height * downscale_factor), int(expected_width * downscale_factor)
    downscaled_height, downscaled_width = round_to_nearest_resolution_acceptable_by_vae(downscaled_height, downscaled_width)
    latents = pipeline(
        conditions=[condition1],
        prompt=prompt,
        negative_prompt=negative_prompt,
        width=downscaled_width,
        height=downscaled_height,
        num_frames=num_frames,
        num_inference_steps=30,
        decode_timestep=0.05,
        decode_noise_scale=0.025,
        image_cond_noise_scale=0.0,
        guidance_scale=5.0,
        guidance_rescale=0.7,
        generator=torch.Generator().manual_seed(0),
        output_type="latent",
    ).frames
    
    # 2. Upscale generated video using latent upsampler with fewer inference steps
    # The available latent upsampler upscales the height/width by 2x
    upscaled_height, upscaled_width = downscaled_height * 2, downscaled_width * 2
    upscaled_latents = pipe_upsample(
        latents=latents,
        output_type="latent"
    ).frames
    
    # 3. Denoise the upscaled video with few steps to improve texture (optional, but recommended)
    video = pipeline(
        conditions=[condition1],
        prompt=prompt,
        negative_prompt=negative_prompt,
        width=upscaled_width,
        height=upscaled_height,
        num_frames=num_frames,
        denoise_strength=0.4,  # Effectively, 4 inference steps out of 10
        num_inference_steps=10,
        latents=upscaled_latents,
        decode_timestep=0.05,
        decode_noise_scale=0.025,
        image_cond_noise_scale=0.0,
        guidance_scale=5.0,
        guidance_rescale=0.7,
        generator=torch.Generator().manual_seed(0),
        output_type="pil",
    ).frames[0]
    
    # 4. Downscale the video to the expected resolution
    video = [frame.resize((expected_width, expected_height)) for frame in video]
    
    export_to_video(video, "output.mp4", fps=24)
  • LTX-Video 0.9.7 蒸餾模型經過引導和時間步蒸餾,以加速生成。它需要將 guidance_scale 設定為 1.0,並且 num_inference_steps 應設定在 410 之間以獲得良好的生成質量。您還應使用以下自定義時間步以獲得最佳結果。

    • 為上取樣做準備的基礎模型推理:[1000, 993, 987, 981, 975, 909, 725, 0.03]
    • 上取樣:[1000, 909, 725, 421, 0]
    顯示示例程式碼
    import torch
    from diffusers import LTXConditionPipeline, LTXLatentUpsamplePipeline
    from diffusers.pipelines.ltx.pipeline_ltx_condition import LTXVideoCondition
    from diffusers.utils import export_to_video, load_video
    
    pipeline = LTXConditionPipeline.from_pretrained("Lightricks/LTX-Video-0.9.7-distilled", torch_dtype=torch.bfloat16)
    pipe_upsample = LTXLatentUpsamplePipeline.from_pretrained("Lightricks/ltxv-spatial-upscaler-0.9.7", vae=pipeline.vae, torch_dtype=torch.bfloat16)
    pipeline.to("cuda")
    pipe_upsample.to("cuda")
    pipeline.vae.enable_tiling()
    
    def round_to_nearest_resolution_acceptable_by_vae(height, width):
        height = height - (height % pipeline.vae_temporal_compression_ratio)
        width = width - (width % pipeline.vae_temporal_compression_ratio)
        return height, width
    
    prompt = """
    artistic anatomical 3d render, utlra quality, human half full male body with transparent 
    skin revealing structure instead of organs, muscular, intricate creative patterns, 
    monochromatic with backlighting, lightning mesh, scientific concept art, blending biology 
    with botany, surreal and ethereal quality, unreal engine 5, ray tracing, ultra realistic, 
    16K UHD, rich details. camera zooms out in a rotating fashion
    """
    negative_prompt = "worst quality, inconsistent motion, blurry, jittery, distorted"
    expected_height, expected_width = 768, 1152
    downscale_factor = 2 / 3
    num_frames = 161
    
    # 1. Generate video at smaller resolution
    downscaled_height, downscaled_width = int(expected_height * downscale_factor), int(expected_width * downscale_factor)
    downscaled_height, downscaled_width = round_to_nearest_resolution_acceptable_by_vae(downscaled_height, downscaled_width)
    latents = pipeline(
        prompt=prompt,
        negative_prompt=negative_prompt,
        width=downscaled_width,
        height=downscaled_height,
        num_frames=num_frames,
        timesteps=[1000, 993, 987, 981, 975, 909, 725, 0.03],
        decode_timestep=0.05,
        decode_noise_scale=0.025,
        image_cond_noise_scale=0.0,
        guidance_scale=1.0,
        guidance_rescale=0.7,
        generator=torch.Generator().manual_seed(0),
        output_type="latent",
    ).frames
    
    # 2. Upscale generated video using latent upsampler with fewer inference steps
    # The available latent upsampler upscales the height/width by 2x
    upscaled_height, upscaled_width = downscaled_height * 2, downscaled_width * 2
    upscaled_latents = pipe_upsample(
        latents=latents,
        adain_factor=1.0,
        output_type="latent"
    ).frames
    
    # 3. Denoise the upscaled video with few steps to improve texture (optional, but recommended)
    video = pipeline(
        prompt=prompt,
        negative_prompt=negative_prompt,
        width=upscaled_width,
        height=upscaled_height,
        num_frames=num_frames,
        denoise_strength=0.999,  # Effectively, 4 inference steps out of 5
        timesteps=[1000, 909, 725, 421, 0],
        latents=upscaled_latents,
        decode_timestep=0.05,
        decode_noise_scale=0.025,
        image_cond_noise_scale=0.0,
        guidance_scale=1.0,
        guidance_rescale=0.7,
        generator=torch.Generator().manual_seed(0),
        output_type="pil",
    ).frames[0]
    
    # 4. Downscale the video to the expected resolution
    video = [frame.resize((expected_width, expected_height)) for frame in video]
    
    export_to_video(video, "output.mp4", fps=24)
  • LTX-Video 支援使用 load_lora_weights() 載入 LoRA。

    顯示示例程式碼
    import torch
    from diffusers import LTXConditionPipeline
    from diffusers.utils import export_to_video, load_image
    
    pipeline = LTXConditionPipeline.from_pretrained(
        "Lightricks/LTX-Video-0.9.5", torch_dtype=torch.bfloat16
    )
    
    pipeline.load_lora_weights("Lightricks/LTX-Video-Cakeify-LoRA", adapter_name="cakeify")
    pipeline.set_adapters("cakeify")
    
    # use "CAKEIFY" to trigger the LoRA
    prompt = "CAKEIFY a person using a knife to cut a cake shaped like a Pikachu plushie"
    image = load_image("https://huggingface.co/Lightricks/LTX-Video-Cakeify-LoRA/resolve/main/assets/images/pikachu.png")
    
    video = pipeline(
        prompt=prompt,
        image=image,
        width=576,
        height=576,
        num_frames=161,
        decode_timestep=0.03,
        decode_noise_scale=0.025,
        num_inference_steps=50,
    ).frames[0]
    export_to_video(video, "output.mp4", fps=26)
  • LTX-Video 支援從單個檔案載入,例如 GGUF 檢查點,使用 loaders.FromOriginalModelMixin.from_single_file()loaders.FromSingleFileMixin.from_single_file()

    顯示示例程式碼
    import torch
    from diffusers.utils import export_to_video
    from diffusers import LTXPipeline, AutoModel, GGUFQuantizationConfig
    
    transformer = AutoModel.from_single_file(
        "https://huggingface.co/city96/LTX-Video-gguf/blob/main/ltx-video-2b-v0.9-Q3_K_S.gguf"
        quantization_config=GGUFQuantizationConfig(compute_dtype=torch.bfloat16),
        torch_dtype=torch.bfloat16
    )
    pipeline = LTXPipeline.from_pretrained(
        "Lightricks/LTX-Video",
        transformer=transformer,
        torch_dtype=torch.bfloat16
    )

LTXPipeline

diffusers.LTXPipeline

< >

( 排程器: FlowMatchEulerDiscreteScheduler vae: AutoencoderKLLTXVideo 文字編碼器: T5EncoderModel 分詞器: T5TokenizerFast Transformer: LTXVideoTransformer3DModel )

引數

用於文字到影片生成的流水線。

參考資料:https://github.com/Lightricks/LTX-Video

__call__

< >

( 提示詞: typing.Union[str, typing.List[str]] = None 負向提示詞: typing.Union[str, typing.List[str], NoneType] = None 高度: int = 512 寬度: int = 704 幀數: int = 161 幀率: int = 25 推理步數: int = 50 時間步: typing.List[int] = None 引導比例: float = 3 引導重縮放: float = 0.0 每個提示的影片數量: typing.Optional[int] = 1 生成器: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None 潛在向量: typing.Optional[torch.Tensor] = None 提示詞嵌入: typing.Optional[torch.Tensor] = None 提示詞注意力掩碼: typing.Optional[torch.Tensor] = None 負向提示詞嵌入: typing.Optional[torch.Tensor] = None 負向提示詞注意力掩碼: typing.Optional[torch.Tensor] = None 解碼時間步: typing.Union[float, typing.List[float]] = 0.0 解碼噪聲比例: typing.Union[float, typing.List[float], NoneType] = None 輸出型別: typing.Optional[str] = 'pil' 返回字典: bool = True 注意力引數: typing.Optional[typing.Dict[str, typing.Any]] = None 步結束回撥: typing.Optional[typing.Callable[[int, int, typing.Dict], NoneType]] = None 步結束張量輸入回撥: typing.List[str] = ['latents'] 最大序列長度: int = 128 ) ~pipelines.ltx.LTXPipelineOutput元組

引數

  • prompt (strList[str], 可選) — 用於引導影像生成的提示詞或提示詞列表。如果未定義,則必須傳入 prompt_embeds
  • height (int, 預設為 512) — 生成影像的高度(畫素)。為獲得最佳效果,預設設定為 480。
  • width (int, 預設為 704) — 生成影像的寬度(畫素)。為獲得最佳效果,預設設定為 848。
  • num_frames (int, 預設為 161) — 要生成的影片幀數
  • num_inference_steps (int, 可選, 預設為 50) — 去噪步數。更多去噪步數通常會帶來更高質量的影像,但推理速度會變慢。
  • timesteps (List[int], 可選) — 用於去噪過程的自定義時間步,適用於支援在其 set_timesteps 方法中帶有 timesteps 引數的排程器。如果未定義,將使用傳入 num_inference_steps 時的預設行為。必須按降序排列。
  • guidance_scale (float, 預設為 3 ) — Classifier-Free Diffusion Guidance 中定義的引導比例。guidance_scale 定義為 Imagen Paper 中公式 2 的 w。透過設定 guidance_scale > 1 啟用引導比例。更高的引導比例鼓勵生成與文字 prompt 緊密相關的影像,通常以犧牲較低影像質量為代價。
  • guidance_rescale (float, 可選, 預設為 0.0) — 常見的擴散噪聲排程和樣本步驟存在缺陷 中提出的引導重縮放因子。guidance_scale 定義為 常見的擴散噪聲排程和樣本步驟存在缺陷 中公式 16 的 φ。引導重縮放因子應修復使用零終端信噪比時的過曝問題。
  • num_videos_per_prompt (int, 可選, 預設為 1) — 每個提示詞生成的影片數量。
  • 生成器 (torch.GeneratorList[torch.Generator], 可選) — 一個或多個 torch 生成器,用於使生成具有確定性。
  • 潛在變數 (torch.Tensor, 可選) — 預生成的帶噪聲的潛在變數,從高斯分佈中取樣,用作影像生成的輸入。可用於使用不同提示調整相同的生成。如果未提供,將使用提供的隨機 generator 取樣生成一個潛在變數張量。
  • 提示詞嵌入 (torch.Tensor, 可選) — 預生成的文字嵌入。可用於輕鬆調整文字輸入,例如提示詞權重。如果未提供,文字嵌入將從 prompt 輸入引數生成。
  • 提示詞注意力掩碼 (torch.Tensor, 可選) — 預生成的文字嵌入注意力掩碼。
  • 負提示詞嵌入 (torch.FloatTensor, 可選) — 預生成的負文字嵌入。對於 PixArt-Sigma,此負提示詞應為""。如果未提供,將從 negative_prompt 輸入引數生成 negative_prompt_embeds。
  • 負提示詞注意力掩碼 (torch.FloatTensor, 可選) — 預生成的負文字嵌入注意力掩碼。
  • 解碼時間步長 (float, 預設為 0.0) — 生成影片的解碼時間步長。
  • 解碼噪聲比例 (float, 預設為 None) — 在解碼時間步長處,隨機噪聲與去噪潛在變數之間的插值因子。
  • 輸出型別 (str, 可選, 預設為 "pil") — 生成影像的輸出格式。選擇 PILPIL.Image.Imagenp.array
  • 返回字典 (bool, 可選, 預設為 True) — 是否返回 ~pipelines.ltx.LTXPipelineOutput 而不是普通元組。
  • 注意力 kwargs (dict, 可選) — 一個 kwargs 字典,如果指定,則傳遞給 diffusers.models.attention_processorself.processor 下定義的 AttentionProcessor
  • 每步結束回撥函式 (Callable, 可選) — 在推理過程中,每個去噪步驟結束時呼叫的函式。該函式以以下引數呼叫:callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)callback_kwargs 將包含 callback_on_step_end_tensor_inputs 指定的所有張量列表。
  • 每步結束回撥張量輸入 (List, 可選) — callback_on_step_end 函式的張量輸入列表。列表中指定的張量將作為 callback_kwargs 引數傳遞。您只能包含管道類 ._callback_tensor_inputs 屬性中列出的變數。
  • 最大序列長度 (int 預設為 128) — 與 prompt 一起使用的最大序列長度。

返回

~pipelines.ltx.LTXPipelineOutputtuple

如果 return_dictTrue,則返回 ~pipelines.ltx.LTXPipelineOutput,否則返回一個 tuple,其中第一個元素是生成的影像列表。

呼叫管道進行生成時呼叫的函式。

示例

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

>>> pipe = LTXPipeline.from_pretrained("Lightricks/LTX-Video", torch_dtype=torch.bfloat16)
>>> pipe.to("cuda")

>>> prompt = "A woman with long brown hair and light skin smiles at another woman with long blonde hair. The woman with brown hair wears a black jacket and has a small, barely noticeable mole on her right cheek. The camera angle is a close-up, focused on the woman with brown hair's face. The lighting is warm and natural, likely from the setting sun, casting a soft glow on the scene. The scene appears to be real-life footage"
>>> negative_prompt = "worst quality, inconsistent motion, blurry, jittery, distorted"

>>> video = pipe(
...     prompt=prompt,
...     negative_prompt=negative_prompt,
...     width=704,
...     height=480,
...     num_frames=161,
...     num_inference_steps=50,
... ).frames[0]
>>> export_to_video(video, "output.mp4", fps=24)

編碼提示

< >

( 提示: typing.Union[str, typing.List[str]] 負提示: typing.Union[str, typing.List[str], NoneType] = None do_classifier_free_guidance: bool = True 每個提示的影片數量: int = 1 提示詞嵌入: typing.Optional[torch.Tensor] = None 負提示詞嵌入: typing.Optional[torch.Tensor] = None 提示詞注意力掩碼: typing.Optional[torch.Tensor] = None 負提示詞注意力掩碼: typing.Optional[torch.Tensor] = None max_sequence_length: int = 128 裝置: typing.Optional[torch.device] = None 資料型別: typing.Optional[torch.dtype] = None )

引數

  • 提示 (strList[str], 可選) — 待編碼的提示。
  • 負提示 (strList[str], 可選) — 不用於引導影像生成的提示。如果未定義,則必須傳遞 negative_prompt_embeds。當不使用引導時(即,如果 guidance_scale 小於 1 時),將忽略此引數。
  • 是否使用分類器自由引導 (bool, 可選, 預設為 True) — 是否使用分類器自由引導。
  • 每個提示詞的影片數量 (int, 可選, 預設為 1) — 每個提示詞應生成的影片數量。要放置結果嵌入的 torch 裝置。
  • 提示詞嵌入 (torch.Tensor, 可選) — 預生成的文字嵌入。可用於輕鬆調整文字輸入,例如提示詞權重。如果未提供,文字嵌入將從 prompt 輸入引數生成。
  • 負提示詞嵌入 (torch.Tensor, 可選) — 預生成的負文字嵌入。可用於輕鬆調整文字輸入,例如提示詞權重。如果未提供,將從 negative_prompt 輸入引數生成 negative_prompt_embeds。
  • 裝置 — (torch.device, 可選): torch 裝置
  • 資料型別 — (torch.dtype, 可選): torch 資料型別

將提示編碼為文字編碼器隱藏狀態。

LTXImageToVideoPipeline

diffusers.LTXImageToVideoPipeline

< >

( 排程器: FlowMatchEulerDiscreteScheduler vae: AutoencoderKLLTXVideo 文字編碼器: T5EncoderModel 分詞器: T5TokenizerFast Transformer: LTXVideoTransformer3DModel )

引數

影像到影片生成管道。

參考資料:https://github.com/Lightricks/LTX-Video

__call__

< >

( 影像: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, typing.List[PIL.Image.Image], typing.List[numpy.ndarray], typing.List[torch.Tensor]] = None 提示: typing.Union[str, typing.List[str]] = None 負提示: typing.Union[str, typing.List[str], NoneType] = None 高度: int = 512 寬度: int = 704 幀數: int = 161 幀率: int = 25 推理步數: int = 50 時間步長: typing.List[int] = None 引導比例: float = 3 引導重縮放: float = 0.0 每個提示的影片數量: typing.Optional[int] = 1 生成器: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None 潛在變數: typing.Optional[torch.Tensor] = None 提示詞嵌入: typing.Optional[torch.Tensor] = None 提示詞注意力掩碼: typing.Optional[torch.Tensor] = None 負提示詞嵌入: typing.Optional[torch.Tensor] = None 負提示詞注意力掩碼: typing.Optional[torch.Tensor] = None 解碼時間步長: typing.Union[float, typing.List[float]] = 0.0 解碼噪聲比例: typing.Union[float, typing.List[float], NoneType] = None 輸出型別: typing.Optional[str] = 'pil' 返回字典: bool = True 注意力 kwargs: typing.Optional[typing.Dict[str, typing.Any]] = None 每步結束回撥函式: typing.Optional[typing.Callable[[int, int, typing.Dict], NoneType]] = None 每步結束回撥張量輸入: typing.List[str] = ['latents'] 最大序列長度: int = 128 ) ~pipelines.ltx.LTXPipelineOutputtuple

引數

  • 影像 (PipelineImageInput) — 用於條件生成的輸入影像。必須是影像、影像列表或 torch.Tensor
  • 提示 (strList[str], 可選) — 用於引導影像生成的提示。如果未定義,則必須傳遞 prompt_embeds
  • 高度 (int, 預設為 512) — 生成影像的高度(畫素)。為獲得最佳效果,此值預設為 480。
  • 寬度 (int, 預設為 704) — 生成影像的寬度(畫素)。為獲得最佳效果,此值預設為 848。
  • 幀數 (int, 預設為 161) — 要生成的影片幀數。
  • 推理步數 (int, 可選, 預設為 50) — 去噪步數。更多的去噪步數通常會帶來更高的影像質量,但會犧牲推理速度。
  • 時間步長 (List[int], 可選) — 用於去噪過程的自定義時間步長,適用於在其 set_timesteps 方法中支援 timesteps 引數的排程器。如果未定義,將使用傳遞 num_inference_steps 時的預設行為。必須按降序排列。
  • 引導比例 (float, 預設為 3) — Classifier-Free Diffusion Guidance 中定義的引導比例。guidance_scale 定義為 Imagen Paper 方程 2 中的 w。透過設定 guidance_scale > 1 啟用引導比例。更高的引導比例鼓勵生成與文字 prompt 密切相關的影像,通常以犧牲影像質量為代價。
  • 引導重縮放 (float, 可選, 預設為 0.0) — Common Diffusion Noise Schedules and Sample Steps are Flawed 中提出的引導重縮放因子。guidance_scale 定義為 Common Diffusion Noise Schedules and Sample Steps are Flawed 方程 16 中的 φ。引導重縮放因子應在使用零終端信噪比時修復過度曝光。
  • 每個提示的影片數量 (int, 可選, 預設為 1) — 每個提示要生成的影片數量。
  • 生成器 (torch.GeneratorList[torch.Generator], 可選) — 一個或多個 torch 生成器,用於使生成具有確定性。
  • 潛在變數 (torch.Tensor, 可選) — 預生成的帶噪聲的潛在變數,從高斯分佈中取樣,用作影像生成的輸入。可用於使用不同提示調整相同的生成。如果未提供,將使用提供的隨機 generator 取樣生成一個潛在變數張量。
  • 提示詞嵌入 (torch.Tensor, 可選) — 預生成的文字嵌入。可用於輕鬆調整文字輸入,例如提示詞權重。如果未提供,文字嵌入將從 prompt 輸入引數生成。
  • 提示詞注意力掩碼 (torch.Tensor, 可選) — 預生成的文字嵌入注意力掩碼。
  • 負提示詞嵌入 (torch.FloatTensor, 可選) — 預生成的負文字嵌入。對於 PixArt-Sigma,此負提示詞應為""。如果未提供,將從 negative_prompt 輸入引數生成 negative_prompt_embeds。
  • 負提示詞注意力掩碼 (torch.FloatTensor, 可選) — 預生成的負文字嵌入注意力掩碼。
  • 解碼時間步長 (float, 預設為 0.0) — 生成影片的解碼時間步長。
  • 解碼噪聲比例 (float, 預設為 None) — 在解碼時間步長處,隨機噪聲與去噪潛在變數之間的插值因子。
  • 輸出型別 (str, 可選, 預設為 "pil") — 生成影像的輸出格式。選擇 PILPIL.Image.Imagenp.array
  • 返回字典 (bool, 可選, 預設為 True) — 是否返回 ~pipelines.ltx.LTXPipelineOutput 而不是普通元組。
  • 注意力 kwargs (dict, 可選) — 一個 kwargs 字典,如果指定,則傳遞給 diffusers.models.attention_processorself.processor 下定義的 AttentionProcessor
  • 每步結束回撥函式 (Callable, 可選) — 在推理過程中,每個去噪步驟結束時呼叫的函式。該函式以以下引數呼叫:callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)callback_kwargs 將包含 callback_on_step_end_tensor_inputs 指定的所有張量列表。
  • 每步結束回撥張量輸入 (List, 可選) — callback_on_step_end 函式的張量輸入列表。列表中指定的張量將作為 callback_kwargs 引數傳遞。您只能包含管道類 ._callback_tensor_inputs 屬性中列出的變數。
  • 最大序列長度 (int 預設為 128) — 與 prompt 一起使用的最大序列長度。

返回

~pipelines.ltx.LTXPipelineOutputtuple

如果 return_dictTrue,則返回 ~pipelines.ltx.LTXPipelineOutput,否則返回一個 tuple,其中第一個元素是生成的影像列表。

呼叫管道進行生成時呼叫的函式。

示例

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

>>> pipe = LTXImageToVideoPipeline.from_pretrained("Lightricks/LTX-Video", torch_dtype=torch.bfloat16)
>>> pipe.to("cuda")

>>> image = load_image(
...     "https://huggingface.co/datasets/a-r-r-o-w/tiny-meme-dataset-captioned/resolve/main/images/8.png"
... )
>>> prompt = "A young girl stands calmly in the foreground, looking directly at the camera, as a house fire rages in the background. Flames engulf the structure, with smoke billowing into the air. Firefighters in protective gear rush to the scene, a fire truck labeled '38' visible behind them. The girl's neutral expression contrasts sharply with the chaos of the fire, creating a poignant and emotionally charged scene."
>>> negative_prompt = "worst quality, inconsistent motion, blurry, jittery, distorted"

>>> video = pipe(
...     image=image,
...     prompt=prompt,
...     negative_prompt=negative_prompt,
...     width=704,
...     height=480,
...     num_frames=161,
...     num_inference_steps=50,
... ).frames[0]
>>> export_to_video(video, "output.mp4", fps=24)

編碼提示

< >

( 提示: typing.Union[str, typing.List[str]] 負提示: typing.Union[str, typing.List[str], NoneType] = None do_classifier_free_guidance: bool = True 每個提示的影片數量: int = 1 提示詞嵌入: typing.Optional[torch.Tensor] = None 負提示詞嵌入: typing.Optional[torch.Tensor] = None 提示詞注意力掩碼: typing.Optional[torch.Tensor] = None 負提示詞注意力掩碼: typing.Optional[torch.Tensor] = None max_sequence_length: int = 128 裝置: typing.Optional[torch.device] = None 資料型別: 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 輸入引數生成 negative_prompt_embeds。
  • device — (torch.device, 可選): torch 裝置
  • dtype — (torch.dtype, 可選): torch dtype

將提示編碼為文字編碼器隱藏狀態。

LTXConditionPipeline

class diffusers.LTXConditionPipeline

< >

( 排程器: FlowMatchEulerDiscreteScheduler vae: AutoencoderKLLTXVideo 文字編碼器: T5EncoderModel 分詞器: T5TokenizerFast Transformer: LTXVideoTransformer3DModel )

引數

用於文字/影像/影片到影片生成的管道。

參考資料:https://github.com/Lightricks/LTX-Video

__call__

< >

( conditions: typing.Union[diffusers.pipelines.ltx.pipeline_ltx_condition.LTXVideoCondition, typing.List[diffusers.pipelines.ltx.pipeline_ltx_condition.LTXVideoCondition]] = None image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, typing.List[PIL.Image.Image], typing.List[numpy.ndarray], typing.List[torch.Tensor], 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 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 frame_index: typing.Union[int, typing.List[int]] = 0 strength: typing.Union[float, typing.List[float]] = 1.0 denoise_strength: float = 1.0 prompt: typing.Union[str, typing.List[str]] = None negative_prompt: typing.Union[str, typing.List[str], NoneType] = None height: int = 512 width: int = 704 num_frames: int = 161 frame_rate: int = 25 num_inference_steps: int = 50 timesteps: typing.List[int] = None guidance_scale: float = 3 guidance_rescale: float = 0.0 image_cond_noise_scale: float = 0.15 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 prompt_attention_mask: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_attention_mask: typing.Optional[torch.Tensor] = None decode_timestep: typing.Union[float, typing.List[float]] = 0.0 decode_noise_scale: typing.Union[float, typing.List[float], NoneType] = None output_type: typing.Optional[str] = 'pil' return_dict: bool = True attention_kwargs: typing.Optional[typing.Dict[str, typing.Any]] = None callback_on_step_end: typing.Optional[typing.Callable[[int, int, typing.Dict], NoneType]] = None callback_on_step_end_tensor_inputs: typing.List[str] = ['latents'] max_sequence_length: int = 256 ) ~pipelines.ltx.LTXPipelineOutputtuple

引數

  • conditions (List[LTXVideoCondition], *可選*) — 影片生成的幀條件項列表。如果未提供,將使用 imagevideoframe_indexstrength 建立條件。
  • image (PipelineImageInputList[PipelineImageInput], 可選) — 用於條件影片生成的影像。如果未提供,則必須傳入 videoconditions
  • video (List[PipelineImageInput], 可選) — 用於條件影片生成的影片。如果未提供,則必須傳入 imageconditions
  • frame_index (intList[int], 可選) — 影像或影片將條件影響影片生成的幀索引或幀索引列表。如果未提供,則必須傳入 conditions
  • strength (floatList[float], 可選) — 條件作用的強度。如果未提供,則必須傳入 conditions
  • denoise_strength (float, 預設為 1.0) — 新增到潛在表示中的噪聲強度,用於編輯。強度越高,新增到潛在表示中的噪聲越多,導致原始影片和生成影片之間的差異越大。這對於影片到影片編輯很有用。
  • prompt (strList[str], 可選) — 用於引導影像生成的提示詞。如果未定義,則必須傳入 prompt_embeds
  • height (int, 預設為 512) — 生成影像的高度(畫素)。為了獲得最佳結果,預設設定為 480。
  • width (int, 預設為 704) — 生成影像的寬度(畫素)。為了獲得最佳結果,預設設定為 848。
  • num_frames (int, 預設為 161) — 要生成的影片幀數
  • num_inference_steps (int, 可選, 預設為 50) — 去噪步數。更多的去噪步數通常會產生更高質量的影像,但會以較慢的推理速度為代價。
  • timesteps (List[int], 可選) — 用於去噪過程的自定義時間步,適用於支援在其 set_timesteps 方法中使用 timesteps 引數的排程器。如果未定義,將使用傳入 num_inference_steps 時的預設行為。必須按降序排列。
  • guidance_scale (float, 預設為 3 ) — 根據 Classifier-Free Diffusion Guidance 定義的指導尺度。guidance_scale 定義為 Imagen Paper 中公式 2 的 w。透過設定 guidance_scale > 1 啟用指導尺度。較高的指導尺度鼓勵生成與文字 prompt 密切相關的影像,通常以犧牲影像質量為代價。
  • guidance_rescale (float, 可選, 預設為 0.0) — Common Diffusion Noise Schedules and Sample Steps are Flawed 中提出的指導重縮放因子。guidance_scale 定義為 Common Diffusion Noise Schedules and Sample Steps are Flawed 中公式 16 的 φ。指導重縮放因子應解決在使用零終端信噪比時過度曝光的問題。
  • num_videos_per_prompt (int, 可選, 預設為 1) — 每個提示詞生成的影片數量。
  • generator (torch.GeneratorList[torch.Generator], 可選) — 一個或多個 torch generator(s),用於使生成具有確定性。
  • latents (torch.Tensor, 可選) — 預生成的帶噪聲潛在表示,從高斯分佈中取樣,用作影像生成的輸入。可用於使用不同的提示詞調整相同的生成。如果未提供,將使用提供的隨機 generator 取樣生成一個潛在張量。
  • prompt_embeds (torch.Tensor, 可選) — 預先生成的文字嵌入。可用於輕鬆調整文字輸入,例如提示詞權重。如果未提供,將根據 prompt 輸入引數生成文字嵌入。
  • prompt_attention_mask (torch.Tensor, 可選) — 文字嵌入的預生成注意力掩碼。
  • negative_prompt_embeds (torch.FloatTensor, 可選) — 預先生成的負面文字嵌入。對於 PixArt-Sigma,此負面提示應為 ""。如果未提供,將根據 negative_prompt 輸入引數生成 negative_prompt_embeds。
  • negative_prompt_attention_mask (torch.FloatTensor, 可選) — 負面文字嵌入的預生成注意力掩碼。
  • decode_timestep (float, 預設為 0.0) — 生成影片的解碼時間步。
  • decode_noise_scale (float, 預設為 None) — 在解碼時間步長時,隨機噪聲和去噪潛在表示之間的插值因子。
  • output_type (str, 可選, 預設為 "pil") — 生成影像的輸出格式。選擇 PIL: PIL.Image.Imagenp.array
  • return_dict (bool, 可選, 預設為 True) — 是否返回 ~pipelines.ltx.LTXPipelineOutput 而不是普通元組。
  • attention_kwargs (dict, 可選) — 一個 kwargs 字典,如果指定,則傳遞給 diffusers.models.attention_processorself.processor 下定義的 AttentionProcessor
  • 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 預設為 128 ) — 用於 prompt 的最大序列長度。

返回

~pipelines.ltx.LTXPipelineOutputtuple

如果 return_dictTrue,則返回 ~pipelines.ltx.LTXPipelineOutput,否則返回一個 tuple,其中第一個元素是生成的影像列表。

呼叫管道進行生成時呼叫的函式。

示例

>>> import torch
>>> from diffusers.pipelines.ltx.pipeline_ltx_condition import LTXConditionPipeline, LTXVideoCondition
>>> from diffusers.utils import export_to_video, load_video, load_image

>>> pipe = LTXConditionPipeline.from_pretrained("Lightricks/LTX-Video-0.9.5", torch_dtype=torch.bfloat16)
>>> pipe.to("cuda")

>>> # Load input image and video
>>> video = load_video(
...     "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/cosmos/cosmos-video2world-input-vid.mp4"
... )
>>> image = load_image(
...     "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/cosmos/cosmos-video2world-input.jpg"
... )

>>> # Create conditioning objects
>>> condition1 = LTXVideoCondition(
...     image=image,
...     frame_index=0,
... )
>>> condition2 = LTXVideoCondition(
...     video=video,
...     frame_index=80,
... )

>>> 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. And then the camera switch to 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."
>>> negative_prompt = "worst quality, inconsistent motion, blurry, jittery, distorted"

>>> # Generate video
>>> generator = torch.Generator("cuda").manual_seed(0)
>>> # Text-only conditioning is also supported without the need to pass `conditions`
>>> video = pipe(
...     conditions=[condition1, condition2],
...     prompt=prompt,
...     negative_prompt=negative_prompt,
...     width=768,
...     height=512,
...     num_frames=161,
...     num_inference_steps=40,
...     generator=generator,
... ).frames[0]

>>> export_to_video(video, "output.mp4", fps=24)

add_noise_to_image_conditioning_latents

< >

( t: float init_latents: Tensor latents: Tensor noise_scale: float conditioning_mask: Tensor generator eps = 1e-06 )

為硬條件潛在表示新增時間步相關的噪聲。這有助於運動的連續性,特別是在單個幀上進行條件化時。

編碼提示

< >

( 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 prompt_attention_mask: typing.Optional[torch.Tensor] = None negative_prompt_attention_mask: typing.Optional[torch.Tensor] = None max_sequence_length: int = 256 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資料型別

將提示編碼為文字編碼器隱藏狀態。

trim_conditioning_sequence

< >

( start_frame: int sequence_num_frames: int target_num_frames: int ) int

引數

  • start_frame (int) — 序列中第一幀的目標幀數。
  • sequence_num_frames (int) — 序列中的幀數。
  • target_num_frames (int) — 生成影片中的目標幀數。

返回

int

更新後的序列長度

將條件序列裁剪到允許的幀數。

LTXLatentUpsamplePipeline

class diffusers.LTXLatentUpsamplePipeline

< >

( vae: AutoencoderKLLTXVideo latent_upsampler: LTXLatentUpsamplerModel )

__call__

< >

( video: typing.Optional[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 height: int = 512 width: int = 704 latents: typing.Optional[torch.Tensor] = None decode_timestep: typing.Union[float, typing.List[float]] = 0.0 decode_noise_scale: typing.Union[float, typing.List[float], NoneType] = None adain_factor: float = 0.0 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None output_type: typing.Optional[str] = 'pil' return_dict: bool = True )

adain_filter_latent

< >

( latents: Tensor reference_latents: Tensor factor: float = 1.0 ) torch.Tensor

引數

  • latent (torch.Tensor) — 要標準化的輸入潛在變數
  • reference_latents (torch.Tensor) — 提供樣式統計資訊的參考潛在變數。
  • factor (float) — 原始潛在變數與變換後潛在變數之間的混合因子。範圍:-10.0 到 10.0,預設值:1.0

返回

torch.Tensor

變換後的潛在張量

根據參考潛在張量的統計資訊,對潛在張量應用自適應例項歸一化 (AdaIN)。

disable_vae_slicing

< >

( )

停用切片 VAE 解碼。如果之前啟用了 enable_vae_slicing,此方法將返回一步計算解碼。

disable_vae_tiling

< >

( )

停用平鋪 VAE 解碼。如果之前啟用了 enable_vae_tiling,此方法將恢復一步計算解碼。

enable_vae_slicing

< >

( )

啟用切片 VAE 解碼。啟用此選項後,VAE 會將輸入張量分片,分步計算解碼。這有助於節省一些記憶體並允許更大的批次大小。

enable_vae_tiling

< >

( )

啟用平鋪 VAE 解碼。啟用此選項後,VAE 將把輸入張量分割成瓦片,分多步計算編碼和解碼。這對於節省大量記憶體和處理更大的影像非常有用。

LTXPipelineOutput

class diffusers.pipelines.ltx.pipeline_output.LTXPipelineOutput

< >

( 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張量。

LTX管道的輸出類。

< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.