🚀 Diffusers - 文本到视频生成库
Diffusers库中的AnimateDiff方法能够借助现有的Stable Diffusion文本到图像模型来创建视频。它通过在已冻结的文本到图像模型中插入运动模块层,并在视频片段上进行训练以提取运动先验信息,从而实现视频生成。
✨ 主要特性
- 运动模块插入:将运动模块层插入到Stable Diffusion UNet的ResNet和Attention块之后,使图像帧之间产生连贯的运动效果。
- 便捷使用:引入了MotionAdapter和UNetMotionModel的概念,方便在现有的Stable Diffusion模型中使用这些运动模块。
📦 安装指南
文档未提及安装步骤,暂不展示。
💻 使用示例
基础用法
import torch
from diffusers import MotionAdapter, AnimateDiffPipeline, DDIMScheduler
from diffusers.utils import export_to_gif
adapter = MotionAdapter.from_pretrained("guoyww/animatediff-motion-adapter-v1-5-2")
model_id = "SG161222/Realistic_Vision_V5.1_noVAE"
pipe = AnimateDiffPipeline.from_pretrained(model_id, motion_adapter=adapter)
scheduler = DDIMScheduler.from_pretrained(
model_id, subfolder="scheduler", clip_sample=False, timestep_spacing="linspace", steps_offset=1
)
pipe.scheduler = scheduler
pipe.enable_vae_slicing()
pipe.enable_model_cpu_offload()
output = pipe(
prompt=(
"masterpiece, bestquality, highlydetailed, ultradetailed, sunset, "
"orange sky, warm lighting, fishing boats, ocean waves seagulls, "
"rippling water, wharf, silhouette, serene atmosphere, dusk, evening glow, "
"golden hour, coastal landscape, seaside scenery"
),
negative_prompt="bad quality, worse quality",
num_frames=16,
guidance_scale=7.5,
num_inference_steps=25,
generator=torch.Generator("cpu").manual_seed(42),
)
frames = output.frames[0]
export_to_gif(frames, "animation.gif")
效果展示
masterpiece, bestquality, sunset.
|
🔧 技术细节
AnimateDiff通过在冻结的文本到图像模型中插入运动模块层,并在视频片段上进行训练以提取运动先验。这些运动模块被应用于Stable Diffusion UNet的ResNet和Attention块之后,目的是在图像帧之间引入连贯的运动。为了支持这些模块,引入了MotionAdapter和UNetMotionModel的概念,方便与现有的Stable Diffusion模型一起使用。
常用提示信息
⚠️ 重要提示
AnimateDiff在微调后的Stable Diffusion模型上效果更好。如果你打算使用可以裁剪样本的调度器,请确保在调度器中设置 clip_sample=False
来禁用它,因为这可能会对生成的样本产生不利影响。