Diffusers 文件
使用 PIA(個性化影像動畫器)進行影像到影片生成
並獲得增強的文件體驗
開始使用
使用 PIA(個性化影像動畫器)進行影像到影片生成
概述
PIA:透過文字到影像模型中的即插即用模組實現個性化影像動畫 作者:Yiming Zhang、Zhening Xing、Yanhong Zeng、Youqing Fang、Kai Chen
個性化文字到影像(T2I)模型的最新進展徹底改變了內容創作,使非專業人士也能生成具有獨特風格的精美影像。儘管前景廣闊,但透過文字將逼真的動作新增到這些個性化影像中,在保持獨特風格、高保真細節以及實現文字動作可控性方面面臨重大挑戰。在本文中,我們提出了 PIA,一個個性化影像動畫器,它擅長與條件影像對齊,透過文字實現動作可控性,並與各種個性化 T2I 模型相容而無需專門調整。為了實現這些目標,PIA 以一個具有良好訓練的時間對齊層的基本 T2I 模型為基礎,從而使任何個性化 T2I 模型都能無縫轉換為影像動畫模型。PIA 的一個關鍵元件是引入了條件模組,該模組利用條件幀和幀間親和性作為輸入,在潛在空間中,在親和性提示的引導下傳輸外觀資訊以進行單個幀合成。這種設計緩解了外觀相關影像對齊的挑戰,並允許更專注於與動作相關引導的對齊。
可用管道
流水線 | 任務 | 演示 |
---|---|---|
PIAPipeline | 使用 PIA 進行影像到影片生成 |
可用檢查點
PIA 的 Motion Adapter 檢查點可在 OpenMMLab 組織下找到。這些檢查點旨在與任何基於 Stable Diffusion 1.5 的模型一起使用。
使用示例
PIA 與 MotionAdapter 檢查點和 Stable Diffusion 1.5 模型檢查點一起工作。MotionAdapter 是一組運動模組,負責在影像幀之間新增連貫的運動。這些模組在 Stable Diffusion UNet 中的 Resnet 和 Attention 塊之後應用。除了運動模組,PIA 還用 9 通道輸入卷積層替換了 SD 1.5 UNet 模型的輸入卷積層。
以下示例演示瞭如何使用 PIA 從單個影像生成影片。
import torch
from diffusers import (
EulerDiscreteScheduler,
MotionAdapter,
PIAPipeline,
)
from diffusers.utils import export_to_gif, load_image
adapter = MotionAdapter.from_pretrained("openmmlab/PIA-condition-adapter")
pipe = PIAPipeline.from_pretrained("SG161222/Realistic_Vision_V6.0_B1_noVAE", motion_adapter=adapter, torch_dtype=torch.float16)
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config)
pipe.enable_model_cpu_offload()
pipe.enable_vae_slicing()
image = load_image(
"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/pix2pix/cat_6.png?download=true"
)
image = image.resize((512, 512))
prompt = "cat in a field"
negative_prompt = "wrong white balance, dark, sketches,worst quality,low quality"
generator = torch.Generator("cpu").manual_seed(0)
output = pipe(image=image, prompt=prompt, generator=generator)
frames = output.frames[0]
export_to_gif(frames, "pia-animation.gif")
以下是一些樣本輸出
![]() |
如果您計劃使用可以裁剪樣本的排程器,請務必透過在排程器中設定 clip_sample=False
來停用它,因為這也會對生成的樣本產生不利影響。此外,PIA 檢查點可能對排程器的 beta 排程敏感。我們建議將其設定為 linear
。
使用 FreeInit
FreeInit:彌補影片擴散模型中的初始化差距 作者:Tianxing Wu、Chenyang Si、Yuming Jiang、Ziqi Huang、Ziwei Liu。
FreeInit 是一種有效的方法,無需任何額外訓練即可提高使用影片擴散模型生成的影片的時間一致性和整體質量。它可以在推理時無縫應用於 PIA、AnimateDiff、ModelScope、VideoCrafter 和其他各種影片生成模型,並透過迭代最佳化潛在初始化噪聲來工作。更多詳細資訊可在論文中找到。
以下示例演示了 FreeInit 的用法。
import torch
from diffusers import (
DDIMScheduler,
MotionAdapter,
PIAPipeline,
)
from diffusers.utils import export_to_gif, load_image
adapter = MotionAdapter.from_pretrained("openmmlab/PIA-condition-adapter")
pipe = PIAPipeline.from_pretrained("SG161222/Realistic_Vision_V6.0_B1_noVAE", motion_adapter=adapter)
# enable FreeInit
# Refer to the enable_free_init documentation for a full list of configurable parameters
pipe.enable_free_init(method="butterworth", use_fast_sampling=True)
# Memory saving options
pipe.enable_model_cpu_offload()
pipe.enable_vae_slicing()
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
image = load_image(
"https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/pix2pix/cat_6.png?download=true"
)
image = image.resize((512, 512))
prompt = "cat in a field"
negative_prompt = "wrong white balance, dark, sketches,worst quality,low quality"
generator = torch.Generator("cpu").manual_seed(0)
output = pipe(image=image, prompt=prompt, generator=generator)
frames = output.frames[0]
export_to_gif(frames, "pia-freeinit-animation.gif")
![]() |
FreeInit 並非真正“免費”——提高質量的代價是額外的計算。它需要根據啟用時設定的 num_iters
引數進行多次額外取樣。將 use_fast_sampling
引數設定為 True
可以提高整體效能(代價是與 use_fast_sampling=False
相比質量較低,但仍優於普通影片生成模型)。
PIAPipeline
class diffusers.PIAPipeline
< 來源 >( vae: AutoencoderKL text_encoder: CLIPTextModel tokenizer: CLIPTokenizer unet: typing.Union[diffusers.models.unets.unet_2d_condition.UNet2DConditionModel, diffusers.models.unets.unet_motion_model.UNetMotionModel] scheduler: typing.Union[diffusers.schedulers.scheduling_ddim.DDIMScheduler, diffusers.schedulers.scheduling_pndm.PNDMScheduler, diffusers.schedulers.scheduling_lms_discrete.LMSDiscreteScheduler, diffusers.schedulers.scheduling_euler_discrete.EulerDiscreteScheduler, diffusers.schedulers.scheduling_euler_ancestral_discrete.EulerAncestralDiscreteScheduler, diffusers.schedulers.scheduling_dpmsolver_multistep.DPMSolverMultistepScheduler] motion_adapter: typing.Optional[diffusers.models.unets.unet_motion_model.MotionAdapter] = None feature_extractor: CLIPImageProcessor = None image_encoder: CLIPVisionModelWithProjection = None )
__call__
< 來源 >( image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, typing.List[PIL.Image.Image], typing.List[numpy.ndarray], typing.List[torch.Tensor]] prompt: typing.Union[str, typing.List[str]] = None strength: float = 1.0 num_frames: typing.Optional[int] = 16 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 prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None ip_adapter_image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, typing.List[PIL.Image.Image], typing.List[numpy.ndarray], typing.List[torch.Tensor], NoneType] = None ip_adapter_image_embeds: typing.Optional[typing.List[torch.Tensor]] = None motion_scale: int = 0 output_type: typing.Optional[str] = 'pil' return_dict: bool = True cross_attention_kwargs: typing.Optional[typing.Dict[str, typing.Any]] = None clip_skip: typing.Optional[int] = 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'] ) → PIAPipelineOutput 或 tuple
引數
- image (
PipelineImageInput
) — 用於影片生成的輸入影像。 - prompt (
str
或List[str]
, 可選) — 用於引導影像生成的提示詞或提示詞列表。如果未定義,則需要傳遞prompt_embeds
。 - strength (
float
, 可選, 預設為 1.0) — 指示轉換參考image
的程度。必須在 0 到 1 之間。 - height (
int
, 可選, 預設為self.unet.config.sample_size * self.vae_scale_factor
) — 生成影片的高度(畫素)。 - width (
int
, 可選, 預設為self.unet.config.sample_size * self.vae_scale_factor
) — 生成影片的寬度(畫素)。 - num_frames (
int
, 可選, 預設為 16) — 生成的影片幀數。預設為 16 幀,即每秒 8 幀,總計 2 秒影片。 - num_inference_steps (
int
, 可選, 預設為 50) — 去噪步數。更多去噪步數通常會導致更高質量的影片,但推理速度會變慢。 - guidance_scale (
float
, 可選, 預設為 7.5) — 較高的引導比例值會鼓勵模型生成與文字prompt
緊密相關的影像,但會犧牲影像質量。當guidance_scale > 1
時啟用引導比例。 - negative_prompt (
str
或List[str]
, 可選) — 用於引導影像生成中不包含內容的提示詞或提示詞列表。如果未定義,則需要傳遞negative_prompt_embeds
。當不使用引導時(guidance_scale < 1
)會被忽略。 - eta (
float
, 可選, 預設為 0.0) — 對應於 DDIM 論文中的引數 eta (η)。僅適用於 DDIMScheduler,在其他排程器中會被忽略。 - generator (
torch.Generator
或List[torch.Generator]
, 可選) — 用於使生成具有確定性的torch.Generator
。 - latents (
torch.Tensor
, 可選) — 從高斯分佈中取樣的預生成噪聲潛在變數,用作影片生成的輸入。可用於使用不同提示調整相同的生成。如果未提供,則使用提供的隨機generator
取樣生成潛在張量。潛在變數應為形狀(batch_size, num_channel, num_frames, height, width)
。 - prompt_embeds (
torch.Tensor
, 可選) — 預生成的文字嵌入。可用於輕鬆調整文字輸入(提示權重)。如果未提供,則從prompt
輸入引數生成文字嵌入。 - negative_prompt_embeds (
torch.Tensor
, 可選) — 預生成的負面文字嵌入。可用於輕鬆調整文字輸入(提示權重)。如果未提供,則從negative_prompt
輸入引數生成negative_prompt_embeds
。 - ip_adapter_image — (
PipelineImageInput
, 可選): 用於 IP 介面卡的可選影像輸入。 - ip_adapter_image_embeds (
List[torch.Tensor]
, 可選) — IP-Adapter 的預生成影像嵌入。它應該是一個列表,長度與 IP 介面卡數量相同。每個元素應該是一個形狀為(batch_size, num_images, emb_dim)
的張量。如果do_classifier_free_guidance
設定為True
,它應該包含負影像嵌入。如果未提供,則從ip_adapter_image
輸入引數計算嵌入。 - motion_scale — (
int
, 可選, 預設為 0):控制新增到影像中的運動量和型別的引數。增加該值會增加運動量,而特定的值範圍則控制所新增的運動型別。必須在 0 到 8 之間。設定為 0-2 以僅增加運動量。設定為 3-5 以建立迴圈運動。設定為 6-8 以執行影像風格遷移的運動。 - output_type (
str
, 可選, 預設為"pil"
) — 生成影片的輸出格式。在torch.Tensor
,PIL.Image
或np.array
中選擇。 - return_dict (
bool
, 可選, 預設為True
) — 是否返回 TextToVideoSDPipelineOutput 而不是純元組。 - cross_attention_kwargs (
dict
, 可選) — 一個 kwargs 字典,如果指定,將作為self.processor
中定義的AttentionProcessor
的引數傳遞。 - clip_skip (
int
, 可選) — 從 CLIP 中跳過的層數,用於計算提示嵌入。值為 1 表示將使用倒數第二層的輸出計算提示嵌入。 - 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
屬性中列出的變數。
返回
PIAPipelineOutput 或 tuple
如果 return_dict
為 True
,則返回 PIAPipelineOutput;否則返回 tuple
,其中第一個元素是生成的幀列表。
用於生成的管道的呼叫函式。
示例
>>> import torch
>>> from diffusers import EulerDiscreteScheduler, MotionAdapter, PIAPipeline
>>> from diffusers.utils import export_to_gif, load_image
>>> adapter = MotionAdapter.from_pretrained("openmmlab/PIA-condition-adapter")
>>> pipe = PIAPipeline.from_pretrained(
... "SG161222/Realistic_Vision_V6.0_B1_noVAE", motion_adapter=adapter, torch_dtype=torch.float16
... )
>>> pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config)
>>> image = load_image(
... "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/pix2pix/cat_6.png?download=true"
... )
>>> image = image.resize((512, 512))
>>> prompt = "cat in a hat"
>>> negative_prompt = "wrong white balance, dark, sketches, worst quality, low quality, deformed, distorted"
>>> generator = torch.Generator("cpu").manual_seed(0)
>>> output = pipe(image=image, prompt=prompt, negative_prompt=negative_prompt, generator=generator)
>>> frames = output.frames[0]
>>> export_to_gif(frames, "pia-animation.gif")
encode_prompt
< source >( 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 (
str
或List[str]
, 可選) — 待編碼的提示詞 - device — (
torch.device
): torch 裝置 - num_images_per_prompt (
int
) — 每個提示詞應生成的影像數量 - do_classifier_free_guidance (
bool
) — 是否使用無分類器引導 - negative_prompt (
str
或List[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 表示使用倒數第二層的輸出計算提示詞嵌入。
將提示編碼為文字編碼器隱藏狀態。
- enable_freeu
- disable_freeu
- enable_free_init
- disable_free_init
- enable_vae_slicing
- disable_vae_slicing
- enable_vae_tiling
- disable_vae_tiling
PIAPipelineOutput
class diffusers.pipelines.pia.PIAPipelineOutput
< source >( frames: typing.Union[torch.Tensor, numpy.ndarray, typing.List[typing.List[PIL.Image.Image]]] )
PIAPipeline 的輸出類。