🚀 Diffusers - 文本到視頻生成庫
Diffusers庫中的AnimateDiff方法能夠藉助現有的Stable Diffusion文本到圖像模型來創建視頻。它通過在凍結的文本到圖像模型中插入運動模塊層,並在視頻片段上進行訓練以提取運動先驗,從而實現視頻生成。
🚀 快速開始
AnimateDiff允許你利用已有的Stable Diffusion文本到圖像模型來創建視頻。其實現方式是在凍結的文本到圖像模型中插入運動模塊層,並在視頻片段上進行訓練,以提取運動先驗。這些運動模塊被應用於Stable Diffusion UNet的ResNet和注意力塊之後,旨在引入跨圖像幀的連貫運動。為了支持這些模塊,我們引入了MotionAdapter和UNetMotionModel的概念,它們為在現有Stable Diffusion模型中使用這些運動模塊提供了便利。
SparseControlNetModel是針對AnimateDiff實現的ControlNet。ControlNet由Lvmin Zhang、Anyi Rao和Maneesh Agrawala在Adding Conditional Control to Text-to-Image Diffusion Models中提出。而ControlNet的SparseCtrl版本由Yuwei Guo、Ceyuan Yang、Anyi Rao、Maneesh Agrawala、Dahua Lin和Bo Dai在SparseCtrl: Adding Sparse Controls to Text-to-Video Diffusion Models中提出,用於在文本到視頻擴散模型中實現可控生成。
✨ 主要特性
- 利用現有模型:藉助已有的Stable Diffusion文本到圖像模型生成視頻。
- 運動模塊:通過插入運動模塊層引入跨圖像幀的連貫運動。
- SparseControlNetModel:實現對視頻生成的可控性。
💻 使用示例
基礎用法
import torch
from diffusers import AnimateDiffSparseControlNetPipeline
from diffusers.models import AutoencoderKL, MotionAdapter, SparseControlNetModel
from diffusers.schedulers import DPMSolverMultistepScheduler
from diffusers.utils import export_to_gif, load_image
model_id = "SG161222/Realistic_Vision_V5.1_noVAE"
motion_adapter_id = "guoyww/animatediff-motion-adapter-v1-5-3"
controlnet_id = "guoyww/animatediff-sparsectrl-scribble"
lora_adapter_id = "guoyww/animatediff-motion-lora-v1-5-3"
vae_id = "stabilityai/sd-vae-ft-mse"
device = "cuda"
motion_adapter = MotionAdapter.from_pretrained(motion_adapter_id, torch_dtype=torch.float16).to(device)
controlnet = SparseControlNetModel.from_pretrained(controlnet_id, torch_dtype=torch.float16).to(device)
vae = AutoencoderKL.from_pretrained(vae_id, torch_dtype=torch.float16).to(device)
scheduler = DPMSolverMultistepScheduler.from_pretrained(
model_id,
subfolder="scheduler",
beta_schedule="linear",
algorithm_type="dpmsolver++",
use_karras_sigmas=True,
)
pipe = AnimateDiffSparseControlNetPipeline.from_pretrained(
model_id,
motion_adapter=motion_adapter,
controlnet=controlnet,
vae=vae,
scheduler=scheduler,
torch_dtype=torch.float16,
).to(device)
pipe.load_lora_weights(lora_adapter_id, adapter_name="motion_lora")
pipe.fuse_lora(lora_scale=1.0)
prompt = "an aerial view of a cyberpunk city, night time, neon lights, masterpiece, high quality"
negative_prompt = "low quality, worst quality, letterboxed"
image_files = [
"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/animatediff-scribble-1.png",
"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/animatediff-scribble-2.png",
"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/animatediff-scribble-3.png"
]
condition_frame_indices = [0, 8, 15]
conditioning_frames = [load_image(img_file) for img_file in image_files]
video = pipe(
prompt=prompt,
negative_prompt=negative_prompt,
num_inference_steps=25,
conditioning_frames=conditioning_frames,
controlnet_conditioning_scale=1.0,
controlnet_frame_indices=condition_frame_indices,
generator=torch.Generator().manual_seed(1337),
).frames[0]
export_to_gif(video, "output.gif")
以下是使用上述代碼生成的示例: