【视频生成模型】通义万相Wan2.1模型本地部署和LoRA微调

目录

  • 1 简介
  • 2 本地部署
    • 2.1 配置环境
    • 2.2 下载模型
  • 3 文生视频
    • 3.1 运行命令
    • 3.2 生成结果
  • 4 图生视频
    • 4.1 运行命令
    • 4.2 生成结果
  • 5 首尾帧生成视频
    • 5.1 运行命令
    • 5.2 生成结果
  • 6 提示词扩展
  • 7 LoRA微调

1 简介

通义万相 2.1 在 2025 年 1 月推出,2 月 25 日阿里巴巴宣布全面开源该模型。此次开源意义重大,让全球开发者都能参与到模型的应用与优化中。它提供了 14B 专业版和 1.3B 轻量版两种规格,满足不同场景需求。在权威评测 VBench 中,14B 版本以 86.22% 总分超越 Sora、Luma 等国内外模型,在运动质量、视觉质量等 14 个主要维度评测中斩获 5 项第一。1.3B 轻量版则主打高效率,在 RTX 4090 显卡上仅需 8.2GB 显存即可生成 480P 视频,4 分钟内完成 5 秒视频生成,兼容主流消费级 GPU。

本文来实测一下。

2 本地部署

2.1 配置环境

将Wan2.1工程克隆到本地:

git clone https://github.com/Wan-Video/Wan2.1.git
cd Wan2.1

安装依赖库:

# Ensure torch >= 2.4.0
pip install -r requirements.txt

如果安装flash_attn较慢,可以直接下载flash-attn安装包,离线安装,下载地址:https://github.com/Dao-AILab/flash-attention/releases

2.2 下载模型

到modelscope上下载模型:

pip install modelscope
modelscope download Wan-AI/Wan2.1-T2V-14B --local_dir ./Wan2.1-T2V-14B

3 文生视频

3.1 运行命令

Single-GPU:

python generate.py  --task t2v-14B --size 1280*720 --ckpt_dir ./Wan2.1-T2V-14B --prompt "Two anthropomorphic cats in comfy boxing gear and bright gloves fight intensely on a spotlighted stage."

如果显存较小,遇到OOM(内存不足)问题,可以使用–offload_model True和–t5_cpu选项来减少GPU内存使用。例如,在RTX 4090 GPU上:

python generate.py  --task t2v-1.3B --size 832*480 --ckpt_dir ./Wan2.1-T2V-1.3B --offload_model True --t5_cpu --sample_shift 8 --sample_guide_scale 6 --prompt "Two anthropomorphic cats in comfy boxing gear and bright gloves fight intensely on a spotlighted stage."

Multi-GPU:

pip install "xfuser>=0.4.1"
torchrun --nproc_per_node=8 generate.py --task t2v-14B --size 1280*720 --ckpt_dir ./Wan2.1-T2V-14B --dit_fsdp --t5_fsdp --ulysses_size 8 --prompt "Two anthropomorphic cats in comfy boxing gear and bright gloves fight intensely on a spotlighted stage."

3.2 生成结果

使用Wan2.1-T2V-14B模型,迭代50步:

单卡耗时约2小时,显存占用80多GB。
(和官方说的5秒视频需要10分钟不太相符,是什么原因?)

t2v-14b耗时

t2v-14b显存

8卡耗时约15分钟,每张卡占用显存30多GB。

8gpu耗时
8GPU显存

生成结果:
bilibili

视频截图

4 图生视频

4.1 运行命令

Single-GPU:

python generate.py --task i2v-14B --size 1280*720 --ckpt_dir ./Wan2.1-I2V-14B-720P --image examples/i2v_input.JPG --prompt "Summer beach vacation style, a white cat wearing sunglasses sits on a surfboard. The fluffy-furred feline gazes directly at the camera with a relaxed expression. Blurred beach scenery forms the background featuring crystal-clear waters, distant green hills, and a blue sky dotted with white clouds. The cat assumes a naturally relaxed posture, as if savoring the sea breeze and warm sunlight. A close-up shot highlights the feline's intricate details and the refreshing atmosphere of the seaside."

Multi-GPU:

torchrun --nproc_per_node=8 generate.py --task i2v-14B --size 1280*720 --ckpt_dir ./Wan2.1-I2V-14B-720P --image examples/i2v_input.JPG --dit_fsdp --t5_fsdp --ulysses_size 8 --prompt "Summer beach vacation style, a white cat wearing sunglasses sits on a surfboard. The fluffy-furred feline gazes directly at the camera with a relaxed expression. Blurred beach scenery forms the background featuring crystal-clear waters, distant green hills, and a blue sky dotted with white clouds. The cat assumes a naturally relaxed posture, as if savoring the sea breeze and warm sunlight. A close-up shot highlights the feline's intricate details and the refreshing atmosphere of the seaside."

4.2 生成结果

8卡耗时12分钟左右,每张卡显存占用约48GB。

I2V

输入图片:

在这里插入图片描述

生成结果:bilibili

5 首尾帧生成视频

5.1 运行命令

Single-GPU:

python generate.py --task flf2v-14B --size 1280*720 --ckpt_dir ./Wan2.1-FLF2V-14B-720P --first_frame examples/flf2v_input_first_frame.png --last_frame examples/flf2v_input_last_frame.png --prompt "CG animation style, a small blue bird takes off from the ground, flapping its wings. The bird’s feathers are delicate, with a unique pattern on its chest. The background shows a blue sky with white clouds under bright sunshine. The camera follows the bird upward, capturing its flight and the vastness of the sky from a close-up, low-angle perspective."

Multi-GPU:

torchrun --nproc_per_node=8 generate.py --task flf2v-14B --size 1280*720 --ckpt_dir ./Wan2.1-FLF2V-14B-720P --first_frame examples/flf2v_input_first_frame.png --last_frame examples/flf2v_input_last_frame.png --dit_fsdp --t5_fsdp --ulysses_size 8 --prompt "CG animation style, a small blue bird takes off from the ground, flapping its wings. The bird’s feathers are delicate, with a unique pattern on its chest. The background shows a blue sky with white clouds under bright sunshine. The camera follows the bird upward, capturing its flight and the vastness of the sky from a close-up, low-angle perspective."

5.2 生成结果

8卡耗时30分钟左右,每张卡显存占用约48GB。

输入首帧:

first

输入尾帧:

last

生成结果:bilibili

6 提示词扩展

此外,我们还可以使用本地模型进行提示词扩展(Prompt Extension)。

对于文本到视频的任务,您可以使用Qwen/Qwen2.5-14B-Instruct、Qwen/Qwen2.5-7B-Instruct 和Qwen/Qwen2.5-3B-Instruct等模型。

对于图像到视频或最后一帧到视频的任务,您可以使用Qwen/Qwen2.5-VL-7B-Instruct和Qwen/Qwen2.5-VL-3B-Instruct等模型。

较大的模型通常提供更好的扩展结果,但需要更多的GPU内存。

可以使用参数–prompt_extension_model修改用于扩展的模型,例如:

  1. 文生视频:
python generate.py  --task t2v-14B --size 1280*720 --ckpt_dir ./Wan2.1-T2V-14B --prompt "Two anthropomorphic cats in comfy boxing gear and bright gloves fight intensely on a spotlighted stage" --use_prompt_extend --prompt_extend_method 'local_qwen' --prompt_extend_target_lang 'zh'
  1. 图生视频:
python generate.py --task i2v-14B --size 1280*720 --ckpt_dir ./Wan2.1-I2V-14B-720P --image examples/i2v_input.JPG --use_prompt_extend --prompt_extend_model Qwen/Qwen2.5-VL-7B-Instruct --prompt "Summer beach vacation style, a white cat wearing sunglasses sits on a surfboard. The fluffy-furred feline gazes directly at the camera with a relaxed expression. Blurred beach scenery forms the background featuring crystal-clear waters, distant green hills, and a blue sky dotted with white clouds. The cat assumes a naturally relaxed posture, as if savoring the sea breeze and warm sunlight. A close-up shot highlights the feline's intricate details and the refreshing atmosphere of the seaside."
  1. 首尾帧生成视频:
python generate.py --task flf2v-14B --size 1280*720 --ckpt_dir ./Wan2.1-FLF2V-14B-720P --first_frame examples/flf2v_input_first_frame.png --last_frame examples/flf2v_input_last_frame.png --use_prompt_extend --prompt_extend_model Qwen/Qwen2.5-VL-7B-Instruct --prompt "CG animation style, a small blue bird takes off from the ground, flapping its wings. The bird’s feathers are delicate, with a unique pattern on its chest. The background shows a blue sky with white clouds under bright sunshine. The camera follows the bird upward, capturing its flight and the vastness of the sky from a close-up, low-angle perspective."

7 LoRA微调

请参考:LoRA微调Wan2.1教程

git clone https://github.com/modelscope/DiffSynth-Studio.git
cd DiffSynth-Studio
pip install -e .

Step 1: Install additional packages

pip install peft lightning pandas

Step 2: Prepare your dataset

You need to manage the training videos as follows:

data/example_dataset/
├── metadata.csv
└── train├── video_00001.mp4└── image_00002.jpg

metadata.csv:

file_name,text
video_00001.mp4,"video description"
image_00002.jpg,"video description"

We support both images and videos. An image is treated as a single frame of video.

Step 3: Data process

CUDA_VISIBLE_DEVICES="0" python examples/wanvideo/train_wan_t2v.py \--task data_process \--dataset_path data/example_dataset \--output_path ./models \--text_encoder_path "models/Wan-AI/Wan2.1-T2V-1.3B/models_t5_umt5-xxl-enc-bf16.pth" \--vae_path "models/Wan-AI/Wan2.1-T2V-1.3B/Wan2.1_VAE.pth" \--tiled \--num_frames 81 \--height 480 \--width 832

After that, some cached files will be stored in the dataset folder.

data/example_dataset/
├── metadata.csv
└── train├── video_00001.mp4├── video_00001.mp4.tensors.pth├── video_00002.mp4└── video_00002.mp4.tensors.pth

Step 4: Train

LoRA training:

CUDA_VISIBLE_DEVICES="0" python examples/wanvideo/train_wan_t2v.py \--task train \--train_architecture lora \--dataset_path data/example_dataset \--output_path ./models \--dit_path "models/Wan-AI/Wan2.1-T2V-1.3B/diffusion_pytorch_model.safetensors" \--steps_per_epoch 500 \--max_epochs 10 \--learning_rate 1e-4 \--lora_rank 16 \--lora_alpha 16 \--lora_target_modules "q,k,v,o,ffn.0,ffn.2" \--accumulate_grad_batches 1 \--use_gradient_checkpointing

Full training:

CUDA_VISIBLE_DEVICES="0" python examples/wanvideo/train_wan_t2v.py \--task train \--train_architecture full \--dataset_path data/example_dataset \--output_path ./models \--dit_path "models/Wan-AI/Wan2.1-T2V-1.3B/diffusion_pytorch_model.safetensors" \--steps_per_epoch 500 \--max_epochs 10 \--learning_rate 1e-4 \--accumulate_grad_batches 1 \--use_gradient_checkpointing

If you wish to train the 14B model, please separate the safetensor files with a comma. For example: models/Wan-AI/Wan2.1-T2V-14B/diffusion_pytorch_model-00001-of-00006.safetensors,models/Wan-AI/Wan2.1-T2V-14B/diffusion_pytorch_model-00002-of-00006.safetensors,models/Wan-AI/Wan2.1-T2V-14B/diffusion_pytorch_model-00003-of-00006.safetensors,models/Wan-AI/Wan2.1-T2V-14B/diffusion_pytorch_model-00004-of-00006.safetensors,models/Wan-AI/Wan2.1-T2V-14B/diffusion_pytorch_model-00005-of-00006.safetensors,models/Wan-AI/Wan2.1-T2V-14B/diffusion_pytorch_model-00006-of-00006.safetensors.

If you wish to train the image-to-video model, please add an extra parameter --image_encoder_path "models/Wan-AI/Wan2.1-I2V-14B-480P/models_clip_open-clip-xlm-roberta-large-vit-huge-14.pth".

For LoRA training, the Wan-1.3B-T2V model requires 16G of VRAM for processing 81 frames at 480P, while the Wan-14B-T2V model requires 60G of VRAM for the same configuration. To further reduce VRAM requirements by 20%-30%, you can include the parameter --use_gradient_checkpointing_offload.

Step 5: Test

Test LoRA:

import torch
from diffsynth import ModelManager, WanVideoPipeline, save_video, VideoDatamodel_manager = ModelManager(torch_dtype=torch.bfloat16, device="cpu")
model_manager.load_models(["models/Wan-AI/Wan2.1-T2V-1.3B/diffusion_pytorch_model.safetensors","models/Wan-AI/Wan2.1-T2V-1.3B/models_t5_umt5-xxl-enc-bf16.pth","models/Wan-AI/Wan2.1-T2V-1.3B/Wan2.1_VAE.pth",
])
model_manager.load_lora("models/lightning_logs/version_1/checkpoints/epoch=0-step=500.ckpt", lora_alpha=1.0)
pipe = WanVideoPipeline.from_model_manager(model_manager, device="cuda")
pipe.enable_vram_management(num_persistent_param_in_dit=None)video = pipe(prompt="...",negative_prompt="...",num_inference_steps=50,seed=0, tiled=True
)
save_video(video, "video.mp4", fps=30, quality=5)

Test fine-tuned base model:

import torch
from diffsynth import ModelManager, WanVideoPipeline, save_video, VideoDatamodel_manager = ModelManager(torch_dtype=torch.bfloat16, device="cpu")
model_manager.load_models(["models/lightning_logs/version_1/checkpoints/epoch=0-step=500.ckpt","models/Wan-AI/Wan2.1-T2V-1.3B/models_t5_umt5-xxl-enc-bf16.pth","models/Wan-AI/Wan2.1-T2V-1.3B/Wan2.1_VAE.pth",
])
pipe = WanVideoPipeline.from_model_manager(model_manager, device="cuda")
pipe.enable_vram_management(num_persistent_param_in_dit=None)video = pipe(prompt="...",negative_prompt="...",num_inference_steps=50,seed=0, tiled=True
)
save_video(video, "video.mp4", fps=30, quality=5)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/79112.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

模式识别的基本概念与理论体系

前面在讨论专家系统时曾经说过,为了使计算机具有自动获取知识的能力,除了应使它具有学习能力外,还应使它具有能识别诸如文字、图形、图象、声音等的能力,计算机的这种识别能力是模式识别研究的主要内容。当然,模式识别…

树的序列化 - 学习笔记

树的序列化可以有很多种类:可以变成 dfs 序,可以变成欧拉序,还有什么括号序的科技。 但是除了第一个以外其他的都没什么用(要么也可以被已有的算法给替代掉)。所以表面上是讲树的序列化,实际上还是讲的 df…

KBEngine 源代码分析(三):组网逻辑

machine 服务 machine 服务是 KBEngine 用来做服务治理的 每个节点上都需要部署 machine 服务 machine 服务使用 UDP 进行通信 服务发现的方法是其他服务使用 UDP 广播的方式,通知所有 machine 服务 machine 服务启动初始化 mahcine 服务初始化过程,主要做了监听 UDP 端…

git 怎样把本地仓库推送到新建的远程仓库

将本地 Git 仓库推送到一个新的远程仓库是一个常见的操作。以下是详细的步骤: 步骤 1: 创建一个新的远程仓库 首先,你需要在 GitHub、GitLab 或其他代码托管平台上创建一个新的远程仓库。 例如,在 GitHub 上创建一个新仓库: 登…

SPSS PCA+判别分析

1, 主成分分析PCA 我们只要对数化的变量数据: (1)对数据进行标准化处理: 选择【分析】—【描述统计】—【描述】 添加要标准化的变量,勾选【将标准化值另存为变量(Z)】,再点确定 SPSS软件本身不…

XWPFDocument生成word文档介绍(格式 .docx)

以下是针对 XWPFDocument 的详细解析,涵盖其核心功能、常见用法及实际开发中的关键点: XWPFDocument 1. XWPFDocument 简介2. 核心结构与类3. 核心操作详解**3.1 段落与文本****3.2 表格操作****3.3 列表与编号****3.4 图片插入** 4. 高级功能**4.1 页眉…

crashpad 编译

一环境配置 1.1设置系统UTF8编码 1.2vs2017语言环境设置英文包 二.获取depot_tools(此步骤可以跳过 最新工具包已上传下载使用即可) windows下载压缩包,然后放到系统PATH中 下载完以后,基本就是靠depot_tools这个工具集合了&am…

基于标注数据的情感分析模型研究

标题:基于标注数据的情感分析模型研究 内容:1.摘要 随着互联网的快速发展,大量文本数据蕴含着丰富的情感信息,对其进行情感分析具有重要的商业和社会价值。本研究的目的是构建基于标注数据的情感分析模型,以准确识别文本中的情感倾向。方法上…

【数据链路层深度解析】从帧结构到协议实现

目录 一、数据链路层核心定位1.1 OSI模型中的位置1.2 三大核心职责 二、帧结构详解2.1 以太网帧标准格式(IEEE 802.3)2.2 帧封装代码示例 三、核心协议机制3.1 MAC地址体系3.2 介质访问控制CSMA/CD(以太网冲突检测)现代交换机的演…

在若依前后端分离项目中集成 ONLYOFFICE 以实现在线预览、编辑和协作功能

在若依前后端分离项目中集成 ONLYOFFICE 以实现在线预览、编辑和协作功能 概述 ONLYOFFICE 是一款开源的在线文档编辑套件,可实现文档预览、编辑、协作与转换等功能,可通过 Docker 部署 DocumentServer 服务,并通过 HTTP API 或 WOPI 接口与…

SpringMVC 通过ajax 前后端数据交互

在前端的开发过程中,经常在html页面通过ajax进行前后端数据的交互,SpringMVC的controller进行数据的接收,但是有的时候后端会出现数据无法接收到的情况,这个是因为我们的参数和前端ajax的contentType参数 类型不对应的情景&#x…

最新DeepSeek-Prover-V2-671B模型 简介、下载、体验、微调、数据集:专为数学定理自动证明设计的超大垂直领域语言模型(在线体验地址)

DeepSeek-Prover-V2-671B模型 简介、下载、体验、微调、数据集:专为数学定理自动证明设计的超大垂直领域语言模型(在线体验地址) 体验地址:[Hugging Face 在线体验]https://huggingface.co/playground?modelIddeepseek-ai/DeepS…

Kafka的Topic分区数如何合理设置?

一、分区数设置原则 1. 并发能力基准 分区数决定最大消费者并行度,建议设置为消费者组内消费者数量的整数倍 例如:消费者组有4个实例 → 分区数设为4/8/12等 这里定义的目的是为了让消费者能均匀的分配到分区,避免打破负载均衡,…

章越科技赋能消防训练体征监测与安全保障,从传统模式到智能跃迁的实践探索

引言:智能化转型浪潮下,消防训练的“破局”之需 2021年《“十四五”国家消防工作规划》的出台,标志着我国消防救援体系正式迈入“全灾种、大应急”的全新阶段。面对地震、洪涝、危化品泄漏等复杂救援场景,消防员不仅需要更强的体…

【数据库原理及安全实验】实验五 数据库备份与恢复

指导书原文 数据库的备份与恢复SSMS 【实验目的】 1) 熟悉并掌握利用界面操作进行数据库备份和恢复的原理和操作。 【实验原理】 1) 数据库的恢复包括大容量日志恢复模式和简单恢复模式。其中大容量日志恢复模式,简单地说就是要对大容量操作进行最小日志记录&a…

Linux 基础IO(上)--文件与文件描述符fd

前言: 在生活里,我们常和各种文件打交道,像用 Word 写文档、用播放器看视频,这些操作背后都离不开文件的输入输出(I/O)。在 Linux 系统中,文件 I/O 操作更是复杂且关键。 接下来我们将深入探讨…

快速了解Go+rpc

更多个人笔记:(仅供参考,非盈利) gitee: https://gitee.com/harryhack/it_note github: https://github.com/ZHLOVEYY/IT_note 文章目录 rpc基础概念GO的rpc应用简单编写json编写rpc rpc基础概念 电商系统…

基于大模型的膀胱肿瘤全周期诊疗方案研究报告

目录 一、引言 1.1 研究背景与意义 1.2 研究目的与方法 1.3 国内外研究现状 二、大模型预测膀胱肿瘤的原理与技术基础 2.1 大模型介绍 2.2 预测原理 2.3 技术支撑 三、术前风险预测与准备方案 3.1 肿瘤分期与恶性程度预测 3.2 患者身体状况评估 3.3 术前准备工作 …

2025年4月个人工作生活总结

本文为 2025年4月工作生活总结。 研发编码 一个项目的临时记录 自2月份领导让我牵头负责一个项目起,在本月算是有较多时间投入——但也是与之前的相比。 月初,清明节前一晚上,因某事务被叫上参加临时紧急远程会议,几方领导都在…

Python爬虫实战:获取软科网最新特定专业大学排名数据并做分析,为高考填报志愿做参考

一、引言 在高考升学的重要阶段,志愿填报成为考生和家长关注的核心问题。准确、全面且具有权威性的大学专业排名数据,是考生做出科学志愿决策的关键依据。软科网作为专业的大学排名信息发布平台,其发布的计算机科学与技术专业排名数据,因具有较高的公信力和参考价值,备受…