Diffusers 文件

Text2Video-Zero

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

Text2Video-Zero

LoRA

Text2Video-Zero: Text-to-Image Diffusion Models are Zero-Shot Video Generators 由 Levon Khachatryan、Andranik Movsisyan、Vahram Tadevosyan、Roberto Henschel、張揚王、Shant Navasardyan、Humphrey Shi 撰寫。

Text2Video-Zero 支援零樣本影片生成,可使用:

  1. 文字提示詞
  2. 結合姿勢或邊緣引導的提示詞
  3. 影片 Instruct-Pix2Pix (指令引導的影片編輯)

結果在時間上保持一致,並嚴格遵循引導和文字提示詞。

teaser-img

論文摘要如下:

最近的文字到影片生成方法依賴於計算密集型訓練,並且需要大規模影片資料集。在本文中,我們引入了零樣本文字到影片生成的新任務,並提出了一種低成本方法(無需任何訓練或最佳化),透過利用現有文字到影像合成方法(例如 Stable Diffusion)的強大功能,使其適用於影片領域。我們的關鍵修改包括 (i) 透過運動動態豐富生成幀的潛在程式碼,以保持全域性場景和背景的時間一致性;以及 (ii) 使用每個幀對第一幀的新跨幀注意力重新程式設計幀級自注意力,以保留前景物件的上下文、外觀和身份。實驗表明,這導致了低開銷、高質量且顯著一致的影片生成。此外,我們的方法不僅限於文字到影片合成,還適用於其他任務,例如條件和內容專用影片生成,以及影片 Instruct-Pix2Pix,即指令引導的影片編輯。正如實驗所示,儘管未在額外影片資料上進行訓練,但我們的方法與最新方法相比表現相當甚至更好。

您可以在專案頁面論文原始程式碼庫上找到有關 Text2Video-Zero 的更多資訊。

使用示例

文字到影片

要從提示生成影片,請執行以下 Python 程式碼:

import torch
from diffusers import TextToVideoZeroPipeline
import imageio

model_id = "stable-diffusion-v1-5/stable-diffusion-v1-5"
pipe = TextToVideoZeroPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to("cuda")

prompt = "A panda is playing guitar on times square"
result = pipe(prompt=prompt).images
result = [(r * 255).astype("uint8") for r in result]
imageio.mimsave("video.mp4", result, fps=4)

您可以在管道呼叫中更改這些引數

  • 運動場強度(參閱論文,第 3.3.1 節)
    • motion_field_strength_xmotion_field_strength_y。預設值:motion_field_strength_x=12motion_field_strength_y=12
  • TT'(參閱論文,第 3.3.1 節)
    • t0t1 範圍為 {0, ..., num_inference_steps}。預設值:t0=45t1=48
  • 影片長度
    • video_length,要生成的影片幀數。預設值:video_length=8

我們也可以透過分塊處理來生成更長的影片

import torch
from diffusers import TextToVideoZeroPipeline
import numpy as np

model_id = "stable-diffusion-v1-5/stable-diffusion-v1-5"
pipe = TextToVideoZeroPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to("cuda")
seed = 0
video_length = 24  #24 ÷ 4fps = 6 seconds
chunk_size = 8
prompt = "A panda is playing guitar on times square"

# Generate the video chunk-by-chunk
result = []
chunk_ids = np.arange(0, video_length, chunk_size - 1)
generator = torch.Generator(device="cuda")
for i in range(len(chunk_ids)):
    print(f"Processing chunk {i + 1} / {len(chunk_ids)}")
    ch_start = chunk_ids[i]
    ch_end = video_length if i == len(chunk_ids) - 1 else chunk_ids[i + 1]
    # Attach the first frame for Cross Frame Attention
    frame_ids = [0] + list(range(ch_start, ch_end))
    # Fix the seed for the temporal consistency
    generator.manual_seed(seed)
    output = pipe(prompt=prompt, video_length=len(frame_ids), generator=generator, frame_ids=frame_ids)
    result.append(output.images[1:])

# Concatenate chunks and save
result = np.concatenate(result)
result = [(r * 255).astype("uint8") for r in result]
imageio.mimsave("video.mp4", result, fps=4)
  • SDXL 支援

    為了在從提示生成影片時使用 SDXL 模型,請使用 TextToVideoZeroSDXLPipeline 管道。
import torch
from diffusers import TextToVideoZeroSDXLPipeline

model_id = "stabilityai/stable-diffusion-xl-base-1.0"
pipe = TextToVideoZeroSDXLPipeline.from_pretrained(
    model_id, torch_dtype=torch.float16, variant="fp16", use_safetensors=True
).to("cuda")

帶姿態控制的文字到影片

透過姿態控制從提示生成影片

  1. 下載演示影片

    from huggingface_hub import hf_hub_download
    
    filename = "__assets__/poses_skeleton_gifs/dance1_corr.mp4"
    repo_id = "PAIR/Text2Video-Zero"
    video_path = hf_hub_download(repo_type="space", repo_id=repo_id, filename=filename)
  1. 讀取包含提取姿態影像的影片

    from PIL import Image
    import imageio
    
    reader = imageio.get_reader(video_path, "ffmpeg")
    frame_count = 8
    pose_images = [Image.fromarray(reader.get_data(i)) for i in range(frame_count)]

    要從實際影片中提取姿態,請閱讀 ControlNet 文件

  2. 使用我們的自定義注意力處理器執行 StableDiffusionControlNetPipeline

    import torch
    from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
    from diffusers.pipelines.text_to_video_synthesis.pipeline_text_to_video_zero import CrossFrameAttnProcessor
    
    model_id = "stable-diffusion-v1-5/stable-diffusion-v1-5"
    controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-openpose", torch_dtype=torch.float16)
    pipe = StableDiffusionControlNetPipeline.from_pretrained(
        model_id, controlnet=controlnet, torch_dtype=torch.float16
    ).to("cuda")
    
    # Set the attention processor
    pipe.unet.set_attn_processor(CrossFrameAttnProcessor(batch_size=2))
    pipe.controlnet.set_attn_processor(CrossFrameAttnProcessor(batch_size=2))
    
    # fix latents for all frames
    latents = torch.randn((1, 4, 64, 64), device="cuda", dtype=torch.float16).repeat(len(pose_images), 1, 1, 1)
    
    prompt = "Darth Vader dancing in a desert"
    result = pipe(prompt=[prompt] * len(pose_images), image=pose_images, latents=latents).images
    imageio.mimsave("video.mp4", result, fps=4)
  • SDXL 支援

    由於我們的注意力處理器也支援 SDXL,因此可以利用它來生成由 SDXL 驅動的 ControlNet 模型所支援的影片。

    import torch
    from diffusers import StableDiffusionXLControlNetPipeline, ControlNetModel
    from diffusers.pipelines.text_to_video_synthesis.pipeline_text_to_video_zero import CrossFrameAttnProcessor
    
    controlnet_model_id = 'thibaud/controlnet-openpose-sdxl-1.0'
    model_id = 'stabilityai/stable-diffusion-xl-base-1.0'
    
    controlnet = ControlNetModel.from_pretrained(controlnet_model_id, torch_dtype=torch.float16)
    pipe = StableDiffusionControlNetPipeline.from_pretrained(
    	model_id, controlnet=controlnet, torch_dtype=torch.float16
    ).to('cuda')
    
    # Set the attention processor
    pipe.unet.set_attn_processor(CrossFrameAttnProcessor(batch_size=2))
    pipe.controlnet.set_attn_processor(CrossFrameAttnProcessor(batch_size=2))
    
    # fix latents for all frames
    latents = torch.randn((1, 4, 128, 128), device="cuda", dtype=torch.float16).repeat(len(pose_images), 1, 1, 1)
    
    prompt = "Darth Vader dancing in a desert"
    result = pipe(prompt=[prompt] * len(pose_images), image=pose_images, latents=latents).images
    imageio.mimsave("video.mp4", result, fps=4)

帶邊緣控制的文字到影片

要透過附加的 Canny 邊緣控制從提示生成影片,請按照上述姿態引導生成中描述的相同步驟,使用Canny 邊緣 ControlNet 模型

影片 Instruct-Pix2Pix

執行文字引導影片編輯(使用 InstructPix2Pix

  1. 下載演示影片

    from huggingface_hub import hf_hub_download
    
    filename = "__assets__/pix2pix video/camel.mp4"
    repo_id = "PAIR/Text2Video-Zero"
    video_path = hf_hub_download(repo_type="space", repo_id=repo_id, filename=filename)
  2. 從路徑讀取影片

    from PIL import Image
    import imageio
    
    reader = imageio.get_reader(video_path, "ffmpeg")
    frame_count = 8
    video = [Image.fromarray(reader.get_data(i)) for i in range(frame_count)]
  3. 使用我們的自定義注意力處理器執行 StableDiffusionInstructPix2PixPipeline

    import torch
    from diffusers import StableDiffusionInstructPix2PixPipeline
    from diffusers.pipelines.text_to_video_synthesis.pipeline_text_to_video_zero import CrossFrameAttnProcessor
    
    model_id = "timbrooks/instruct-pix2pix"
    pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to("cuda")
    pipe.unet.set_attn_processor(CrossFrameAttnProcessor(batch_size=3))
    
    prompt = "make it Van Gogh Starry Night style"
    result = pipe(prompt=[prompt] * len(video), image=video).images
    imageio.mimsave("edited_video.mp4", result, fps=4)

DreamBooth 特化

方法 文字到影片帶姿態控制的文字到影片帶邊緣控制的文字到影片 可以與自定義 DreamBooth 模型一起執行,如下所示,適用於 Canny 邊緣 ControlNet 模型Avatar 風格 DreamBooth 模型。

  1. 下載演示影片

    from huggingface_hub import hf_hub_download
    
    filename = "__assets__/canny_videos_mp4/girl_turning.mp4"
    repo_id = "PAIR/Text2Video-Zero"
    video_path = hf_hub_download(repo_type="space", repo_id=repo_id, filename=filename)
  2. 從路徑讀取影片

    from PIL import Image
    import imageio
    
    reader = imageio.get_reader(video_path, "ffmpeg")
    frame_count = 8
    canny_edges = [Image.fromarray(reader.get_data(i)) for i in range(frame_count)]
  3. 執行帶自定義訓練 DreamBooth 模型的 StableDiffusionControlNetPipeline

    import torch
    from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
    from diffusers.pipelines.text_to_video_synthesis.pipeline_text_to_video_zero import CrossFrameAttnProcessor
    
    # set model id to custom model
    model_id = "PAIR/text2video-zero-controlnet-canny-avatar"
    controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny", torch_dtype=torch.float16)
    pipe = StableDiffusionControlNetPipeline.from_pretrained(
        model_id, controlnet=controlnet, torch_dtype=torch.float16
    ).to("cuda")
    
    # Set the attention processor
    pipe.unet.set_attn_processor(CrossFrameAttnProcessor(batch_size=2))
    pipe.controlnet.set_attn_processor(CrossFrameAttnProcessor(batch_size=2))
    
    # fix latents for all frames
    latents = torch.randn((1, 4, 64, 64), device="cuda", dtype=torch.float16).repeat(len(canny_edges), 1, 1, 1)
    
    prompt = "oil painting of a beautiful girl avatar style"
    result = pipe(prompt=[prompt] * len(canny_edges), image=canny_edges, latents=latents).images
    imageio.mimsave("video.mp4", result, fps=4)

您可以透過此連結篩選出一些可用的 DreamBooth 訓練模型。

請務必檢視排程器指南,瞭解如何探索排程器速度和質量之間的權衡,並參閱跨管道重用元件部分,瞭解如何有效地將相同元件載入到多個管道中。

TextToVideoZeroPipeline

class diffusers.TextToVideoZeroPipeline

< >

( vae: AutoencoderKL text_encoder: CLIPTextModel tokenizer: CLIPTokenizer unet: UNet2DConditionModel scheduler: KarrasDiffusionSchedulers safety_checker: StableDiffusionSafetyChecker feature_extractor: CLIPImageProcessor requires_safety_checker: bool = True )

__call__

< >

( prompt: typing.Union[str, typing.List[str]] video_length: typing.Optional[int] = 8 height: typing.Optional[int] = None width: typing.Optional[int] = None num_inference_steps: int = 50 guidance_scale: float = 7.5 negative_prompt: typing.Union[str, typing.List[str], NoneType] = None num_videos_per_prompt: typing.Optional[int] = 1 eta: float = 0.0 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.Tensor] = None motion_field_strength_x: float = 12 motion_field_strength_y: float = 12 output_type: typing.Optional[str] = 'tensor' return_dict: bool = True callback: typing.Optional[typing.Callable[[int, int, torch.Tensor], NoneType]] = None callback_steps: typing.Optional[int] = 1 t0: int = 44 t1: int = 47 frame_ids: typing.Optional[typing.List[int]] = None ) TextToVideoPipelineOutput

引數

  • prompt (strList[str], 可選) — 用於引導影像生成的提示詞。如果未定義,您需要傳入 prompt_embeds
  • video_length (int, 可選, 預設為 8) — 生成的影片幀數。
  • height (int, 可選, 預設為 self.unet.config.sample_size * self.vae_scale_factor) — 生成影像的畫素高度。
  • width (int, 可選, 預設為 self.unet.config.sample_size * self.vae_scale_factor) — 生成影像的畫素寬度。
  • num_inference_steps (int, 可選, 預設為 50) — 去噪步數。更多的去噪步通常會生成更高質量的影像,但推理速度會變慢。
  • guidance_scale (float, 可選, 預設為 7.5) — 較高的引導比例值鼓勵模型生成與文字 prompt 密切相關的影像,但會犧牲較低的影像質量。當 guidance_scale > 1 時,啟用引導比例。
  • negative_prompt (strList[str], 可選) — 引導影片生成中不包含內容的提示詞。如果未定義,您需要傳入 negative_prompt_embeds。當不使用引導時 (guidance_scale < 1),該引數將被忽略。
  • num_videos_per_prompt (int, 可選, 預設為 1) — 每個提示詞生成的影片數量。
  • eta (float, 可選, 預設為 0.0) — 對應於 DDIM 論文中的引數 eta (η)。僅適用於 DDIMScheduler,在其他排程器中將被忽略。
  • generator (torch.GeneratorList[torch.Generator], 可選) — 用於使生成具有確定性的 torch.Generator
  • latents (torch.Tensor, 可選) — 從高斯分佈中取樣的預生成噪聲潛在變數,用作影片生成的輸入。可用於使用不同的提示詞調整相同的生成。如果未提供,則使用提供的隨機 generator 取樣生成潛在張量。
  • output_type (str, 可選, 預設為 "np") — 生成影片的輸出格式。在 "latent""np" 之間選擇。
  • return_dict (bool, 可選, 預設為 True) — 是否返回 TextToVideoPipelineOutput 物件而非普通元組。
  • callback (Callable, 可選) — 在推理過程中每隔 callback_steps 步呼叫的函式。該函式以以下引數呼叫:callback(step: int, timestep: int, latents: torch.Tensor)
  • callback_steps (int, 可選, 預設為 1) — 呼叫 callback 函式的頻率。如果未指定,將在每個步驟呼叫 callback。
  • motion_field_strength_x (float, 可選, 預設為 12) — 生成影片沿 x 軸的運動強度。請參閱論文第 3.3.1 節。
  • motion_field_strength_y (float, 可選, 預設為 12) — 生成影片沿 y 軸的運動強度。請參閱論文第 3.3.1 節。
  • t0 (int, 可選, 預設為 44) — 時間步 t0。應在 [0, num_inference_steps - 1] 範圍內。請參閱論文第 3.3.1 節。
  • t1 (int, 可選, 預設為 47) — 時間步 t0。應在 [t0 + 1, num_inference_steps - 1] 範圍內。請參閱論文第 3.3.1 節。
  • frame_ids (List[int], 可選) — 正在生成的幀索引。在分塊生成較長影片時使用。

返回

TextToVideoPipelineOutput

輸出包含生成影片的 ndarray,當 output_type != "latent" 時;否則為生成影片的潛在編碼和指示相應生成影片是否包含“不適合工作”(nsfw) 內容的 bool 列表。

用於生成的管道的呼叫函式。

backward_loop

< >

( latents timesteps prompt_embeds guidance_scale callback callback_steps num_warmup_steps extra_step_kwargs cross_attention_kwargs = None ) latents

引數

  • latents — 時間步 timesteps[0] 處的潛在變數。
  • timesteps — 執行反向過程的時間步。
  • prompt_embeds — 預先生成的文字嵌入。
  • guidance_scale — 較高的引導比例值鼓勵模型生成與文字 prompt 緊密相關的影像,但會以較低的影像質量為代價。當 guidance_scale > 1 時啟用引導比例。
  • callback (Callable, 可選) — 在推理過程中每隔 callback_steps 步呼叫的函式。該函式以以下引數呼叫:callback(step: int, timestep: int, latents: torch.Tensor)
  • callback_steps (int, 可選, 預設為 1) — 呼叫 callback 函式的頻率。如果未指定,將在每個步驟呼叫 callback。
  • extra_step_kwargs — extra_step_kwargs。
  • cross_attention_kwargs — 一個 kwargs 字典,如果指定,將作為引數傳遞給 self.processor 中定義的 AttentionProcessor
  • num_warmup_steps — 熱身步數。

返回

latents

時間步 timesteps[-1] 處反向過程的潛在變數。

給定時間步列表執行反向過程。

encode_prompt

< >

( prompt device num_images_per_prompt do_classifier_free_guidance negative_prompt = None prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None lora_scale: typing.Optional[float] = None clip_skip: typing.Optional[int] = None )

引數

  • prompt (strList[str], 可選) — 要編碼的提示
  • device — (torch.device): torch 裝置
  • num_images_per_prompt (int) — 每個提示應生成的影像數量
  • do_classifier_free_guidance (bool) — 是否使用無分類器引導
  • negative_prompt (strList[str], 可選) — 不用於引導影像生成的提示。如果未定義,則必須傳遞 negative_prompt_embeds。當不使用引導時(即,如果 guidance_scale 小於 1 時被忽略)。
  • prompt_embeds (torch.Tensor, 可選) — 預先生成的文字嵌入。可用於輕鬆調整文字輸入,例如提示權重。如果未提供,將從 prompt 輸入引數生成文字嵌入。
  • negative_prompt_embeds (torch.Tensor, 可選) — 預先生成的負面文字嵌入。可用於輕鬆調整文字輸入,例如提示權重。如果未提供,negative_prompt_embeds 將從 negative_prompt 輸入引數生成。
  • lora_scale (float, 可選) — 如果載入了 LoRA 層,則將應用於文字編碼器所有 LoRA 層的 LoRA 比例。
  • clip_skip (int, 可選) — 從 CLIP 跳過的層數,用於計算提示嵌入。值為 1 表示使用倒數第二層的輸出計算提示嵌入。

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

forward_loop

< >

( x_t0 t0 t1 generator ) x_t1

引數

  • x_t0 — 時間 t0 處的潛在編碼。
  • t0 — t0 時間步。
  • t1 — t1 時間步。
  • generator (torch.GeneratorList[torch.Generator], 可選) — 一個 torch.Generator 用於使生成過程具有確定性。

返回

x_t1

從時間 t0 到 t1 應用於 x_t0 的前向過程。

從時間 t0 到 t1 執行 DDPM 前向過程。這與新增相應方差的噪聲相同。

TextToVideoZeroSDXLPipeline

diffusers.TextToVideoZeroSDXLPipeline

< >

( vae: AutoencoderKL text_encoder: CLIPTextModel text_encoder_2: CLIPTextModelWithProjection tokenizer: CLIPTokenizer tokenizer_2: CLIPTokenizer unet: UNet2DConditionModel scheduler: KarrasDiffusionSchedulers image_encoder: CLIPVisionModelWithProjection = None feature_extractor: CLIPImageProcessor = None force_zeros_for_empty_prompt: bool = True add_watermarker: typing.Optional[bool] = None )

__call__

< >

( prompt: typing.Union[str, typing.List[str]] prompt_2: typing.Union[str, typing.List[str], NoneType] = None video_length: typing.Optional[int] = 8 height: typing.Optional[int] = None width: typing.Optional[int] = None num_inference_steps: int = 50 denoising_end: typing.Optional[float] = None guidance_scale: float = 7.5 negative_prompt: typing.Union[str, typing.List[str], NoneType] = None negative_prompt_2: typing.Union[str, typing.List[str], NoneType] = None num_videos_per_prompt: typing.Optional[int] = 1 eta: float = 0.0 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None frame_ids: typing.Optional[typing.List[int]] = None prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None pooled_prompt_embeds: typing.Optional[torch.Tensor] = None negative_pooled_prompt_embeds: typing.Optional[torch.Tensor] = None latents: typing.Optional[torch.Tensor] = None motion_field_strength_x: float = 12 motion_field_strength_y: float = 12 output_type: typing.Optional[str] = 'tensor' return_dict: bool = True callback: typing.Optional[typing.Callable[[int, int, torch.Tensor], NoneType]] = None callback_steps: int = 1 cross_attention_kwargs: typing.Optional[typing.Dict[str, typing.Any]] = None guidance_rescale: float = 0.0 original_size: typing.Optional[typing.Tuple[int, int]] = None crops_coords_top_left: typing.Tuple[int, int] = (0, 0) target_size: typing.Optional[typing.Tuple[int, int]] = None t0: int = 44 t1: int = 47 )

引數

  • prompt (strList[str], 可選) — 引導影像生成的提示。如果未定義,則必須傳遞 prompt_embeds
  • prompt_2 (strList[str], 可選) — 要傳送到 tokenizer_2text_encoder_2 的提示。如果未定義,prompt 將用於兩個文字編碼器。
  • video_length (int, 可選, 預設為 8) — 生成影片幀的數量。
  • height (int, 可選, 預設為 self.unet.config.sample_size * self.vae_scale_factor) — 生成影像的高度(畫素)。
  • width (int, 可選, 預設為 self.unet.config.sample_size * self.vae_scale_factor) — 生成影像的寬度(畫素)。
  • num_inference_steps (int, 可選, 預設為 50) — 去噪步數。更多的去噪步數通常會帶來更高的影像質量,但推理速度會變慢。
  • denoising_end (float, 可選) — 指定後,確定在有意提前終止之前要完成的總去噪過程的百分比(介於 0.0 和 1.0 之間)。因此,返回的樣本仍將保留由排程器選擇的離散時間步所確定的顯著噪聲量。denoising_end 引數最好在此管道構成“去噪器混合”多管道設定的一部分時使用,詳見最佳化影像輸出
  • guidance_scale (float, 可選, 預設為 7.5) — 無分類器擴散引導中定義的引導比例。guidance_scale 被定義為 Imagen 論文中公式 2 的 w。透過設定 guidance_scale > 1 啟用引導比例。更高的引導比例鼓勵生成與文字 prompt 緊密相關的影像,通常以較低的影像質量為代價。
  • negative_prompt (strList[str], 可選) — 不用於引導影像生成的提示。如果未定義,則必須傳遞 negative_prompt_embeds。當不使用引導時(即,如果 guidance_scale 小於 1 時被忽略)。
  • negative_prompt_2 (strList[str], 可選) — 不用於引導影像生成的提示,將傳送到 tokenizer_2text_encoder_2。如果未定義,negative_prompt 將用於兩個文字編碼器。
  • num_videos_per_prompt (int, 可選, 預設為 1) — 每個提示要生成的影片數量。
  • eta (float, 可選, 預設為 0.0) — 對應 DDIM 論文中的引數 eta (η): https://huggingface.co/papers/2010.02502。僅適用於 schedulers.DDIMScheduler,對其他排程器將被忽略。
  • generator (torch.GeneratorList[torch.Generator], 可選) — 一個或多個 torch 生成器,使生成過程具有確定性。
  • frame_ids (List[int], 可選) — 正在生成的幀索引。在分塊生成較長影片時使用。
  • prompt_embeds (torch.Tensor, 可選) — 預先生成的文字嵌入。可用於輕鬆調整文字輸入,例如提示權重。如果未提供,將從 prompt 輸入引數生成文字嵌入。
  • negative_prompt_embeds (torch.Tensor, 可選) — 預先生成的負面文字嵌入。可用於輕鬆調整文字輸入,例如提示權重。如果未提供,negative_prompt_embeds 將從 negative_prompt 輸入引數生成。
  • pooled_prompt_embeds (torch.Tensor, optional) — 預生成的池化文字嵌入。可用於輕鬆調整文字輸入,例如提示詞權重。如果未提供,則將從`prompt`輸入引數生成池化文字嵌入。
  • negative_pooled_prompt_embeds (torch.Tensor, optional) — 預生成的負向池化文字嵌入。可用於輕鬆調整文字輸入,例如提示詞權重。如果未提供,負向池化文字嵌入將從`negative_prompt`輸入引數生成。
  • latents (torch.Tensor, optional) — 預生成的噪聲潛在量,從高斯分佈取樣,用作影像生成的輸入。可用於使用不同的提示詞調整相同的生成。如果未提供,則將使用提供的隨機`generator`進行取樣生成一個潛在量張量。
  • motion_field_strength_x (float, optional, defaults to 12) — 生成影片中沿 x 軸的運動強度。請參閱論文,第 3.3.1 節。
  • motion_field_strength_y (float, optional, defaults to 12) — 生成影片中沿 y 軸的運動強度。請參閱論文,第 3.3.1 節。
  • output_type (str, optional, defaults to "pil") — 生成影像的輸出格式。在 PIL: PIL.Image.Imagenp.array 之間選擇。
  • return_dict (bool, optional, defaults to True) — 是否返回 ~pipelines.stable_diffusion_xl.StableDiffusionXLPipelineOutput 而不是普通元組。
  • callback (Callable, optional) — 在推理過程中,每 callback_steps 步呼叫的函式。該函式將使用以下引數呼叫:callback(step: int, timestep: int, latents: torch.Tensor)
  • callback_steps (int, optional, defaults to 1) — 呼叫 callback 函式的頻率。如果未指定,則在每一步都呼叫回撥。
  • cross_attention_kwargs (dict, optional) — 如果指定,此 kwargs 字典將傳遞給 diffusers.cross_attention 中定義的 self.processorAttentionProcessor
  • guidance_rescale (float, optional, defaults to 0.7) — Common Diffusion Noise Schedules and Sample Steps are Flawed 提出的指導重縮放因子。guidance_scaleCommon Diffusion Noise Schedules and Sample Steps are Flawed 的公式 16 中定義為 φ。指導重縮放因子應該修復使用零終端信噪比時的過曝問題。
  • original_size (Tuple[int], optional, defaults to (1024, 1024)) — 如果 original_sizetarget_size 不同,影像將顯示為縮小或放大。如果未指定,original_size 預設為 (width, height)。SDXL 微條件的一部分,如 https://huggingface.co/papers/2307.01952 的第 2.2 節所述。
  • crops_coords_top_left (Tuple[int], optional, defaults to (0, 0)) — crops_coords_top_left 可用於生成一個看起來像是從 crops_coords_top_left 位置向下“裁剪”的影像。透過將 crops_coords_top_left 設定為 (0, 0),通常可以獲得有利的、居中的影像。SDXL 微條件的一部分,如 https://huggingface.co/papers/2307.01952 的第 2.2 節所述。
  • target_size (Tuple[int], optional, defaults to (1024, 1024)) — 在大多數情況下,target_size 應設定為生成影像所需的寬度和高度。如果未指定,它將預設為 (width, height)。SDXL 微條件的一部分,如 https://huggingface.co/papers/2307.01952 的第 2.2 節所述。
  • t0 (int, optional, defaults to 44) — 時間步 t0。應在 [0, num_inference_steps - 1] 範圍內。請參閱論文,第 3.3.1 節。
  • t1 (int, optional, defaults to 47) — 時間步 t0。應在 [t0 + 1, num_inference_steps - 1] 範圍內。請參閱論文,第 3.3.1 節。

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

backward_loop

< >

( latents timesteps prompt_embeds guidance_scale callback callback_steps num_warmup_steps extra_step_kwargs add_text_embeds add_time_ids cross_attention_kwargs = None guidance_rescale: float = 0.0 ) latents

引數

  • latents — 時間步timesteps[0]的潛在量。
  • timesteps — 執行反向過程的時間步。
  • prompt_embeds — 預生成的文字嵌入。
  • guidance_scale — 較高的指導比例值會鼓勵模型生成與文字 prompt 緊密相關的影像,但會犧牲影像質量。當 guidance_scale > 1 時啟用指導比例。
  • callback (Callable, optional) — 在推理過程中,每 callback_steps 步呼叫的函式。該函式將使用以下引數呼叫:callback(step: int, timestep: int, latents: torch.Tensor)
  • callback_steps (int, optional, defaults to 1) — 呼叫 callback 函式的頻率。如果未指定,則在每一步都呼叫回撥。
  • extra_step_kwargs — 額外步長kwargs。
  • cross_attention_kwargs — 如果指定,此 kwargs 字典將傳遞給 self.processor 中定義的 AttentionProcessor
  • num_warmup_steps — 熱身步數。

返回

latents

在時間步timesteps[-1]反向過程的潛在量

給定時間步列表執行反向過程

encode_prompt

< >

( prompt: str prompt_2: typing.Optional[str] = None device: typing.Optional[torch.device] = None num_images_per_prompt: int = 1 do_classifier_free_guidance: bool = True negative_prompt: typing.Optional[str] = None negative_prompt_2: typing.Optional[str] = None prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None pooled_prompt_embeds: typing.Optional[torch.Tensor] = None negative_pooled_prompt_embeds: typing.Optional[torch.Tensor] = None lora_scale: typing.Optional[float] = None clip_skip: typing.Optional[int] = None )

引數

  • prompt (str or List[str], optional) — 要編碼的提示詞
  • prompt_2 (str or List[str], optional) — 要傳送到 tokenizer_2text_encoder_2 的提示詞。如果未定義,prompt 將用於兩個文字編碼器。
  • device — (torch.device): torch 裝置
  • num_images_per_prompt (int) — 每個提示詞應生成的影像數量
  • do_classifier_free_guidance (bool) — 是否使用分類器自由指導
  • negative_prompt (str or List[str], optional) — 不用於指導影像生成的提示詞。如果未定義,則必須傳遞 negative_prompt_embeds。當不使用指導時(即,如果 guidance_scale 小於 1),則忽略。
  • negative_prompt_2 (str or List[str], optional) — 不用於指導影像生成併發送到 tokenizer_2text_encoder_2 的提示詞。如果未定義,則 negative_prompt 將用於兩個文字編碼器。
  • prompt_embeds (torch.Tensor, optional) — 預生成的文字嵌入。可用於輕鬆調整文字輸入,例如提示詞權重。如果未提供,則文字嵌入將從 prompt 輸入引數生成。
  • negative_prompt_embeds (torch.Tensor, optional) — 預生成的負向文字嵌入。可用於輕鬆調整文字輸入,例如提示詞權重。如果未提供,負向提示詞嵌入將從 negative_prompt 輸入引數生成。
  • pooled_prompt_embeds (torch.Tensor, optional) — 預生成的池化文字嵌入。可用於輕鬆調整文字輸入,例如提示詞權重。如果未提供,則池化文字嵌入將從 prompt 輸入引數生成。
  • negative_pooled_prompt_embeds (torch.Tensor, optional) — 預生成的負向池化文字嵌入。可用於輕鬆調整文字輸入,例如提示詞權重。如果未提供,負向池化文字嵌入將從 negative_prompt 輸入引數生成。
  • lora_scale (float, optional) — 如果載入了 LoRA 層,將應用於文字編碼器所有 LoRA 層的 LoRA 比例。
  • clip_skip (int, optional) — 在計算提示詞嵌入時要跳過的 CLIP 層數。值為 1 表示將使用倒數第二層的輸出計算提示詞嵌入。

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

forward_loop

< >

( x_t0 t0 t1 generator ) x_t1

引數

  • x_t0 — 時間 t0 的潛在程式碼。
  • t0 — 時間步 t0。
  • t1 — 時間步 t1。
  • generator (torch.Generator or List[torch.Generator], optional) — 一個 torch.Generator,用於使生成確定性。

返回

x_t1

從時間 t0 到 t1 應用於 x_t0 的前向過程。

從時間 t0 到 t1 執行 DDPM 前向過程。這與新增相應方差的噪聲相同。

TextToVideoPipelineOutput

class diffusers.pipelines.text_to_video_synthesis.pipeline_text_to_video_zero.TextToVideoPipelineOutput

< >

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

引數

  • images ([List[PIL.Image.Image], np.ndarray]) — 去噪後的 PIL 影像列表,長度為 batch_size,或形狀為 (batch_size, height, width, num_channels) 的 NumPy 陣列。
  • nsfw_content_detected ([List[bool]]) — 表示相應生成的影像是否包含“不安全工作”(nsfw)內容的列表,如果無法執行安全檢查,則為 None

零樣本文字到影片管道的輸出類。

< > 在 GitHub 上更新

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