【視頻生成模型】通義萬相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.pswp.cn/bicheng/79112.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/79112.shtml
英文地址,請注明出處:http://en.pswp.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爬蟲實戰:獲取軟科網最新特定專業大學排名數據并做分析,為高考填報志愿做參考

一、引言 在高考升學的重要階段,志愿填報成為考生和家長關注的核心問題。準確、全面且具有權威性的大學專業排名數據,是考生做出科學志愿決策的關鍵依據。軟科網作為專業的大學排名信息發布平臺,其發布的計算機科學與技術專業排名數據,因具有較高的公信力和參考價值,備受…