![見出し画像](https://assets.st-note.com/production/uploads/images/134670987/rectangle_large_type_2_9d42ce3511748030a271377430b2bd97.png?width=800)
Google Colab で AnimateDiff-Lightning を試す
「Google Colab」で「AnimateDiff-Lightning」を試したので、まとめました。
【注意】Google Colab Pro/Pro+のA100で動作確認しています。
1. AnimateDiff-Lightning
「AnimateDiff-Lightning」は、超高速のText-to-Videoモデルです。オリジナルの「AnimateDiff」よりも10倍以上速く動画生成できます。AnimateDiff SD1.5 v2からから抽出されたもので、1 / 2 / 4 / 8ステップの蒸留モデルのチェックポイントが含まれています。
詳しくは、「AnimateDiff-Lightning: Cross-Model Diffusion Distillation」を参照してください。
2. Colabでの実行
Colabでの実行手順は、次のとおりです。
(1) Colabのノートブックを開き、メニュー「編集 → ノートブックの設定」で「GPU」の「A100」を選択。
(2) パッケージのインストール。
# パッケージのインストール
!pip install diffusers
(3) 「HuggingFace」からAPIキー (Access Token) を取得し、Colabのシークレットマネージャーに登録。
キーは「HF_KEY」とします。
![](https://assets.st-note.com/img/1711029140327-VmIOdPOvQt.png?width=800)
(4) アダプタとパイプラインの準備。
今回はベースモデルに「Meina/MeinaMix_V10」を指定しました。
import torch
import safetensors
from diffusers import AnimateDiffPipeline, MotionAdapter, EulerDiscreteScheduler
from diffusers.utils import export_to_gif
from huggingface_hub import hf_hub_download
from safetensors.torch import load_file
device = "cuda"
dtype = torch.float16
step = 8 # オプション: [1,2,4,8]
repo = "ByteDance/AnimateDiff-Lightning"
ckpt = f"animatediff_lightning_{step}step_diffusers.safetensors"
base = "Meina/MeinaMix_V10" # ベースモデルの選択
# アダプタとパイプラインの準備
adapter = MotionAdapter().to(device, dtype)
adapter.load_state_dict(load_file(hf_hub_download(repo ,ckpt), device=device))
pipe = AnimateDiffPipeline.from_pretrained(base, motion_adapter=adapter, torch_dtype=dtype).to(device)
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing", beta_schedule="linear")
(5) 動画生成の実行。
「animation.gif」が4秒ほどで生成されます。
# 動画生成の実行
output = pipe(
prompt="A girl smiling",
guidance_scale=1.0,
num_inference_steps=step
)
export_to_gif(output.frames[0], "animation.gif")
Google Colab で AnimateDiff-Lightning をお試し中https://t.co/DT2wLf7bM4 pic.twitter.com/DhhgTdM9R4
— 布留川英一 / Hidekazu Furukawa (@npaka123) March 21, 2024
プロンプトは、次のようにしました。
・A girl smiling (左上)
・A girl blinking (右上)
・A girl dancing (左下)
・A girl dancing (右下)
GPUメモリの消費量は、次のとおりです。
![](https://assets.st-note.com/img/1711029525655-WBA9qxz28x.png?width=800)
この記事が気に入ったらサポートをしてみませんか?