Diffusers 文档
Stable Video Diffusion
加入 Hugging Face 社区
并获得增强的文档体验
开始使用
Stable Video Diffusion
Stable Video Diffusion (SVD) 是一个强大的图像到视频生成模型,可以根据输入图像生成 2-4 秒的高分辨率(576x1024)视频。
本指南将向您展示如何使用 SVD 从图像生成短视频。
开始之前,请确保已安装以下库:
# Colab에서 필요한 라이브러리를 설치하기 위해 주석을 제외하세요
!pip install -q -U diffusers transformers accelerate
此模型有两个变体:SVD 和 SVD-XT。SVD 检查点用于生成 14 帧,而 SVD-XT 检查点经过进一步微调以生成 25 帧。
本指南将使用 SVD-XT 检查点。
import torch
from diffusers import StableVideoDiffusionPipeline
from diffusers.utils import load_image, export_to_video
pipe = StableVideoDiffusionPipeline.from_pretrained(
"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")
image = image.resize((1024, 576))
generator = torch.manual_seed(42)
frames = pipe(image, decode_chunk_size=8, generator=generator).frames[0]
export_to_video(frames, "generated.mp4", fps=7)


torch.compile
您可以通过编译 UNet 来提高 20-25% 的速度,但会略微增加内存占用。
- pipe.enable_model_cpu_offload()
+ pipe.to("cuda")
+ pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
减少内存使用
视频生成是内存密集型任务,因为您本质上是同时生成 num_frames
,类似于高批量大小的文本到图像生成。为了减少内存需求,有多种选项可以在推理速度和内存需求之间进行权衡:
- 启用模型卸载:管道的每个组件在不再需要时会卸载到 CPU。
- 启用前馈分块:前馈层以循环方式运行,而不是以巨大的批量大小运行单个前馈。
- 减少
decode_chunk_size
:VAE 分块解码帧,而不是一次性解码所有帧。将decode_chunk_size=1
设置为每次解码一帧并使用最少量的内存(我们建议根据您的 GPU 内存调整此值),但视频可能会有一些闪烁。
- pipe.enable_model_cpu_offload()
- frames = pipe(image, decode_chunk_size=8, generator=generator).frames[0]
+ pipe.enable_model_cpu_offload()
+ pipe.unet.enable_forward_chunking()
+ frames = pipe(image, decode_chunk_size=2, generator=generator, num_frames=25).frames[0]
结合使用所有这些技巧应将内存需求降低到少于 8GB VRAM。
微条件
Stable Diffusion Video 除了条件图像外,还支持微条件,从而可以更好地控制生成的视频
fps
:生成视频的每秒帧数。motion_bucket_id
:用于生成视频的运动桶 ID。这可用于控制生成视频的运动。增加运动桶 ID 会增加生成视频的运动。noise_aug_strength
:添加到条件图像的噪声量。值越高,视频与条件图像的相似度越低。增加此值还会增加生成视频的运动。
例如,要生成更多运动的视频,请使用 motion_bucket_id
和 noise_aug_strength
微条件参数。
import torch
from diffusers import StableVideoDiffusionPipeline
from diffusers.utils import load_image, export_to_video
pipe = StableVideoDiffusionPipeline.from_pretrained(
"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")
image = image.resize((1024, 576))
generator = torch.manual_seed(42)
frames = pipe(image, decode_chunk_size=8, generator=generator, motion_bucket_id=180, noise_aug_strength=0.1).frames[0]
export_to_video(frames, "generated.mp4", fps=7)