🚀 Diffusers
AnimateDiffは、既存のStable Diffusionのテキストから画像へのモデルを使用して動画を作成することができる手法です。
これは、凍結されたテキストから画像へのモデルにモーションモジュール層を挿入し、動画クリップで学習させることでモーションプライオを抽出することで実現されます。
これらのモーションモジュールは、Stable DiffusionのUNet内のResNetとAttentionブロックの後に適用されます。その目的は、画像フレーム間に一貫した動きを導入することです。これらのモジュールをサポートするために、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のテキストから画像へのモデルでモーションモジュールとSparse ControlNetをどのように利用できるかを示しています。
closeup face photo of man in black clothes, night city street, bokeh, fireworks in background
|
|
💻 使用例
基本的な使用法
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-rgb"
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")
image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/animatediff-firework.png")
video = pipe(
prompt="closeup face photo of man in black clothes, night city street, bokeh, fireworks in background",
negative_prompt="low quality, worst quality",
num_inference_steps=25,
conditioning_frames=image,
controlnet_frame_indices=[0],
controlnet_conditioning_scale=1.0,
generator=torch.Generator().manual_seed(42),
).frames[0]
export_to_gif(video, "output.gif")