Diffusers 文件
LTX-影片
並獲得增強的文件體驗
開始使用
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.float32
或torch.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。
- Transformer、VAE 和文字編碼器推薦的資料型別是
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
應設定在4
到10
之間以獲得良好的生成質量。您還應使用以下自定義時間步以獲得最佳結果。- 為上取樣做準備的基礎模型推理:
[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 )
引數
- Transformer (LTXVideoTransformer3DModel) — 用於對編碼影片潛在表示進行去噪的條件 Transformer 架構。
- 排程器 (FlowMatchEulerDiscreteScheduler) — 與
transformer
結合使用的排程器,用於對編碼影像潛在表示進行去噪。 - vae (AutoencoderKLLTXVideo) — 變分自編碼器 (VAE) 模型,用於將影像編碼和解碼為潛在表示。
- text_encoder (
T5EncoderModel
) — T5,特別是 google/t5-v1_1-xxl 變體。 - 分詞器 (
CLIPTokenizer
) — CLIPTokenizer 類的分詞器。 - 分詞器 (
T5TokenizerFast
) — 第二個 T5TokenizerFast 類的分詞器。
用於文字到影片生成的流水線。
參考資料: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 (
str
或List[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.Generator
或List[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"
) — 生成影像的輸出格式。選擇 PIL:PIL.Image.Image
或np.array
。 - 返回字典 (
bool
, 可選, 預設為True
) — 是否返回~pipelines.ltx.LTXPipelineOutput
而不是普通元組。 - 注意力 kwargs (
dict
, 可選) — 一個 kwargs 字典,如果指定,則傳遞給 diffusers.models.attention_processor 中self.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.LTXPipelineOutput
或 tuple
如果 return_dict
為 True
,則返回 ~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 )
引數
- 提示 (
str
或List[str]
, 可選) — 待編碼的提示。 - 負提示 (
str
或List[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 )
引數
- 變換器 (LTXVideoTransformer3DModel) — 用於對編碼影片潛在變數進行去噪的條件變換器架構。
- 排程器 (FlowMatchEulerDiscreteScheduler) — 與
transformer
結合使用以對編碼影像潛在變數進行去噪的排程器。 - 變分自動編碼器 (AutoencoderKLLTXVideo) — 用於將影像編碼和解碼為潛在表示的變分自動編碼器 (VAE) 模型。
- 文字編碼器 (
T5EncoderModel
) — T5,特別是 google/t5-v1_1-xxl 變體。 - 分詞器 (
CLIPTokenizer
) — CLIPTokenizer 類的分詞器。 - 分詞器 (
T5TokenizerFast
) — 第二個 T5TokenizerFast 類的分詞器。
影像到影片生成管道。
參考資料: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.LTXPipelineOutput
或 tuple
引數
- 影像 (
PipelineImageInput
) — 用於條件生成的輸入影像。必須是影像、影像列表或torch.Tensor
。 - 提示 (
str
或List[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.Generator
或List[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"
) — 生成影像的輸出格式。選擇 PIL:PIL.Image.Image
或np.array
。 - 返回字典 (
bool
, 可選, 預設為True
) — 是否返回~pipelines.ltx.LTXPipelineOutput
而不是普通元組。 - 注意力 kwargs (
dict
, 可選) — 一個 kwargs 字典,如果指定,則傳遞給 diffusers.models.attention_processor 中self.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.LTXPipelineOutput
或 tuple
如果 return_dict
為 True
,則返回 ~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)
編碼提示
< source >( 提示: 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 (
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
將提示編碼為文字編碼器隱藏狀態。
LTXConditionPipeline
class diffusers.LTXConditionPipeline
< source >( 排程器: FlowMatchEulerDiscreteScheduler vae: AutoencoderKLLTXVideo 文字編碼器: T5EncoderModel 分詞器: T5TokenizerFast Transformer: LTXVideoTransformer3DModel )
引數
- transformer (LTXVideoTransformer3DModel) — 用於對編碼影片潛在表示進行去噪的條件Transformer架構。
- scheduler (FlowMatchEulerDiscreteScheduler) — 與
transformer
結合使用的排程器,用於對編碼影像潛在表示進行去噪。 - vae (AutoencoderKLLTXVideo) — 用於編碼和解碼影像到潛在表示的變分自編碼器 (VAE) 模型。
- text_encoder (
T5EncoderModel
) — T5,特別是 google/t5-v1_1-xxl 變體。 - tokenizer (
CLIPTokenizer
) — CLIPTokenizer 類的分詞器。 - tokenizer (
T5TokenizerFast
) — T5TokenizerFast 類的第二個分詞器。
用於文字/影像/影片到影片生成的管道。
參考資料:https://github.com/Lightricks/LTX-Video
__call__
< source >( 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.LTXPipelineOutput
或 tuple
引數
- conditions (
List[LTXVideoCondition], *可選*
) — 影片生成的幀條件項列表。如果未提供,將使用image
、video
、frame_index
和strength
建立條件。 - image (
PipelineImageInput
或List[PipelineImageInput]
, 可選) — 用於條件影片生成的影像。如果未提供,則必須傳入video
或conditions
。 - video (
List[PipelineImageInput]
, 可選) — 用於條件影片生成的影片。如果未提供,則必須傳入image
或conditions
。 - frame_index (
int
或List[int]
, 可選) — 影像或影片將條件影響影片生成的幀索引或幀索引列表。如果未提供,則必須傳入conditions
。 - strength (
float
或List[float]
, 可選) — 條件作用的強度。如果未提供,則必須傳入conditions
。 - denoise_strength (
float
, 預設為1.0
) — 新增到潛在表示中的噪聲強度,用於編輯。強度越高,新增到潛在表示中的噪聲越多,導致原始影片和生成影片之間的差異越大。這對於影片到影片編輯很有用。 - prompt (
str
或List[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.Generator
或List[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.Image
或np.array
。 - return_dict (
bool
, 可選, 預設為True
) — 是否返回~pipelines.ltx.LTXPipelineOutput
而不是普通元組。 - attention_kwargs (
dict
, 可選) — 一個 kwargs 字典,如果指定,則傳遞給 diffusers.models.attention_processor 中self.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.LTXPipelineOutput
或 tuple
如果 return_dict
為 True
,則返回 ~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
< source >( t: float init_latents: Tensor latents: Tensor noise_scale: float conditioning_mask: Tensor generator eps = 1e-06 )
為硬條件潛在表示新增時間步相關的噪聲。這有助於運動的連續性,特別是在單個幀上進行條件化時。
編碼提示
< 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 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 (
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資料型別
將提示編碼為文字編碼器隱藏狀態。
trim_conditioning_sequence
< 原始碼 >( 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
根據參考潛在張量的統計資訊,對潛在張量應用自適應例項歸一化 (AdaIN)。
停用切片 VAE 解碼。如果之前啟用了 enable_vae_slicing
,此方法將返回一步計算解碼。
停用平鋪 VAE 解碼。如果之前啟用了 enable_vae_tiling
,此方法將恢復一步計算解碼。
啟用切片 VAE 解碼。啟用此選項後,VAE 會將輸入張量分片,分步計算解碼。這有助於節省一些記憶體並允許更大的批次大小。
啟用平鋪 VAE 解碼。啟用此選項後,VAE 將把輸入張量分割成瓦片,分多步計算編碼和解碼。這對於節省大量記憶體和處理更大的影像非常有用。
LTXPipelineOutput
class diffusers.pipelines.ltx.pipeline_output.LTXPipelineOutput
< 原始碼 >( frames: Tensor )
LTX管道的輸出類。