비디오 생성

Hugging Face로 배우는 멀티모달 모델

James Chapman

Curriculum Manager, DataCamp

비디오 생성

비디오 생성 단계

1 https://link.springer.com/article/10.1007/s11263-024-02271-9
Hugging Face로 배우는 멀티모달 모델

비디오 생성

import torch
from diffusers import CogVideoXPipeline


pipe = CogVideoXPipeline.from_pretrained( "THUDM/CogVideoX-2b", torch_dtype=torch.float16 )
pipe.enable_model_cpu_offload() pipe.enable_sequential_cpu_offload()
pipe.vae.enable_slicing() pipe.vae.enable_tiling()

프롬프트에 따라 우주비행사가 외계인과 악수하는 CogVideoX 예시 비디오

1 https://huggingface.co/THUDM/CogVideoX-2b
Hugging Face로 배우는 멀티모달 모델

비디오 생성

prompt = "A majestic lion in a sunlit African savanna, sitting regally 
on a rock formation. Golden sunlight illuminates its magnificent mane, 
then a big smile appears on its face"

video = pipe(
    prompt=prompt,

num_inference_steps=20,
num_frames=20,
guidance_scale=6,
generator=torch.Generator(device="cuda").manual_seed(42), ).frames[0]
Hugging Face로 배우는 멀티모달 모델

비디오 생성

from diffusers.utils import export_to_video
from moviepy.editor import VideoFileClip


video_path = export_to_video(video, "output.mp4", fps=8) video = VideoFileClip(video_path)
video.write_gif("video.gif")

앞서 사용한 프롬프트에서 미소 짓는 사자의 GIF

Hugging Face로 배우는 멀티모달 모델

정량 분석

  • 비디오는 프롬프트 준수가 어려움
  • CLIP으로 접근 가능:

CLIP 비디오 점수 비교 방법 다이어그램

Hugging Face로 배우는 멀티모달 모델

정량 분석

from diffusers.utils import load_video
from torchmetrics.functional.multimodal import clip_score
from functools import partial

frames = load_video(video_path) clip_score_fn = partial(clip_score, model_name_or_path="openai/clip-vit-base-patch16")
scores = [] for frame in frames: frame_int = np.array(frame).astype("uint8") frame_tensor = torch.from_numpy(frame_int).unsqueeze(0).permute(0, 3, 1, 2)
score = clip_score_fn(frame_tensor, [prompt]).detach() scores.append(float(score)) avg_clip_score = round(np.mean(scores), 4) print(f"Average CLIP score: {avg_clip_score}")
Average CLIP score: 30.6274
Hugging Face로 배우는 멀티모달 모델

연습해 봅시다!

Hugging Face로 배우는 멀티모달 모델

Preparing Video For Download...