0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Stable Video Diffusionを試す on Databricks

Posted at

これは試さざるを得ない。

導入

Stability AI社より、画像から動画を生成するStable Video Diffusionがリリースされました。

さらにDiffusers 0.2.4でStable Video Diffusionがサポートされました。

あまり画像生成系はやってこなかったのですが、これは凄いと思ったのでDatabricks上で試してみました。

基本的にはこちらの内容のウォークスルーです。

Step 1. モデルのダウンロード

SVD-XTモデルを公式リポジトリからダウンロードします。

def download_model(model_id:str):
    import os
    from huggingface_hub import snapshot_download

    UC_VOLUME = "/Volumes/training/llm/model_snapshots"

    local_dir = f"/tmp/{model_id}"
    uc_dir = f"/models--{model_id.replace('/', '--')}"

    snapshot_location = snapshot_download(
        repo_id=model_id,
        local_dir=local_dir,
        local_dir_use_symlinks=False,
    )

    dbutils.fs.cp(f"file:{local_dir}", f"{UC_VOLUME}{uc_dir}", recurse=True)

model_id = "stabilityai/stable-video-diffusion-img2vid-xt"
download_model(model_id)

Step 2. パッケージのインストール

指定のパッケージ+opencv-pythonをインストール。

%pip install -q -U diffusers transformers accelerate opencv-python

dbutils.library.restartPython()

Step 2. モデル読込 & 画像読込

ダウンロードしたモデルをロードし、また動画の基となる画像も読み込みます。
画像はhuggingfaceのサンプル画像をそのまま使っています。

import torch

from diffusers import StableVideoDiffusionPipeline
from diffusers.utils import load_image, export_to_video

pipe = StableVideoDiffusionPipeline.from_pretrained(
    "/Volumes/training/llm/model_snapshots/models--stabilityai--stable-video-diffusion-img2vid-xt",
    torch_dtype=torch.float16,
    variant="fp16",
)
pipe.enable_model_cpu_offload()

# Load the conditioning image
image = load_image(
    "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/svd/rocket.png?download=true"
)
image = image.resize((1024, 576))

image

image.png

Step 3. 生成

動画生成。
処理時間は、g4dn.xlarge(T4のクラスタ)を使って7分強かかりました。

generator = torch.manual_seed(42)
frames = pipe(image, decode_chunk_size=8, generator=generator).frames[0]

export_to_video(frames, "generated.mp4", fps=7)

生成結果はこちら。(GIFに変換しています)

generated.gif

おー公式サンプル通り。手軽にこういうのが作れるのがすごい。

まとめ

他にも何枚か動画生成してみましたが、確かに動きますね。
画像やパラメータ調整次第で結構おもしろいことができるのではないかと思います。

Stability AI社はSDXL Turboも最近リリースしたりと、非常に面白いことになっています。
私自身は本業への適用が難しいのでそこまでやっていないのですが、この進化の先を楽しみにしています。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?