1. 背景

cogvideoX是清华和清影同源的开源视频生成大模型,详情可以查看:CogvideoX
其中模型差异如下:

![[09 大模型专区/ob_photo/Pasted image 20241102155335.png]]

2. 搭建

2.1 搭建comfy-ui

下载和使用comfy-ui,配置可以查看:FLUX安装与使用

2.2 下载ComfyUI-CogVideoXWrapper

进入comfyUI,下载CogVideoXWrapper,这是一个cogvideo的中转器

1
2
3
cd ComfyUI/custom_nodes
git clone https://github.com/kijai/ComfyUI-CogVideoXWrapper.git
git clone https://github.com/MinusZoneAI/ComfyUI-CogVideoX-MZ

2.3 启动comfyui

启动即可,–listen是监听IP,一般是你自己服务器的IP,如果没有设置listen,则是默认的locatehost

1
python3 main.py --listen 10.229.41.60

然后访问对应的8188端口即可。

1
http://10.229.41.60:8188

⚠️ 注意:

  • ==操作前请备份重要文件。注意直接启动可能会报错,因为他运行会安装Python的依赖,其实就是需要安装ComfyUI-CogVideoXWrapper的依赖,这个使用需要仔细看原因,或者手动安装依赖.==
  • ==一定要安装python 3.12以上的版本,不然会跑不起来==
    1
    2
    3
    4
    5
    6
    7
    # 安装ComfyUI-CogVideoXWrapper的依赖
    cd custom_nodes/ComfyUI-CogVideoXWrapper
    pip install -r requirements.txt

    # 安装comfyui的依赖
    cd ComfyUI
    pip install -r requirements.txt

2.4 下载模型

直接启动有可能不成功,默认配置是从huggingface中下载模型,如果无法连接,就手动从魔塔社区手动下载。

1
2
3
4
5
6
7
8
# 进入models
cd ComfyUI/models

# 新建CogVideo文件夹
mkdir CogVideo

# 下载对应的模型

2.4.1 下载clip模型

国外huggingface: https://huggingface.co/comfyanonymous/flux_text_encoders/tree/main
国内用魔塔社区: https://modelscope.cn/models/AI-ModelScope/flux_text_encoders/files
需下载 t5xxl_fp16.safetensors 或 t5xxl_fp8_e4m3fn.safetensors (建议选择fp8 版本,如果你显存超过 32G 可选择 fp16 版本),用于文本对齐
需下载 clip_l.safetensors,用于文本对齐

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cd  ComfyUI/models/clip

# 下载clip_l.safetensors
wget https://huggingface.co/comfyanonymous/flux_text_encoders/blob/main/clip_l.safetensors
wget https://modelscope.cn/models/AI-ModelScope/flux_text_encoders/resolve/master/clip_l.safetensors

# 下载t5xxl_fp16.safetensors
wget https://huggingface.co/comfyanonymous/flux_text_encoders/blob/main/t5xxl_fp16.safetensors
wget https://modelscope.cn/models/AI-ModelScope/flux_text_encoders/resolve/master/t5xxl_fp16.safetensors

# 下载t5xxl_fp8_e4m3fn.safetensors
wget https://huggingface.co/comfyanonymous/flux_text_encoders/blob/main/t5xxl_fp8_e4m3fn.safetensors
wget https://modelscope.cn/models/AI-ModelScope/flux_text_encoders/resolve/master/t5xxl_fp8_e4m3fn.safetensors

从魔塔社区直接下载的是多个safetensors,需要合并到一起

2.4.2 魔塔社区下载

1
2
3
4
pip install modelscope

# 指定下载到dir位置
modelscope download --model ZhipuAI/CogVideoX-5b-I2V --local_dir ./dir

3. 多个safetensors合并

有些时候魔塔社区没有对应的safetensors的模型,需要手动将huggingface里的模型进行合并,可以通过如下Python脚本进行合并。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from safetensors import safe_open, safe_save
import torch

# 定义分块文件的路径
files = [
"diffusion_pytorch_model-00001-of-00002.safetensors",
"diffusion_pytorch_model-00002-of-00002.safetensors"
]

# 合并后的文件保存路径
merged_file_path = "merged_diffusion_model.safetensors"

# 初始化一个空的字典来存储所有参数
state_dict = {}

# 遍历每个分块文件并加载其内容
for file in files:
with safe_open(file, framework="pt") as f:
for key in f.keys():
state_dict[key] = f.get_tensor(key)

# 将合并的状态字典保存到一个新的safetensors文件
safe_save(state_dict, merged_file_path)
print("合并完成,保存路径:", merged_file_path)

下载其他模型:

1
2
3
4
5
6
7
8
# 下载到unet文件夹
wget https://modelscope.cn/models/wailovet/CogVideoX-5b/resolve/master/CogVideoX_5b_fp8_4em.safetensors

# 下载到vae文件夹(记得重命名文件名方便辨认)
wget https://modelscope.cn/models/zhipuai/cogvideox-5b/resolve/master/vae/diffusion_pytorch_model.safetensors

# 下载到clip文件夹(和sd3/flux通用,不需要重复下载
https://modelscope.cn/models/ai-modelscope/flux_text_encoders/resolve/master/t5xxl_fp8_e4m3fn.safetensors

4. 使用效果

通过img2video,使用效果如下:


使用cogvidex_fun_i2v_example_01.json

图生视频效果不好,clip很难理解图片中的各种关系,从而展示我希望的图片,提示词如下:

1
2
睁开眼睛慢慢的看着镜头
Open your eyes and slowly look at the camera.



1
2
3
幽暗的古堡,广阔的星空,月光下人形在缓慢的靠近,让人恐惧和未知

Dark and ancient castle, vast starry sky, a figure slowly approaching under the moonlight, instilling feelings of fear and the unknown.



1
The spacecraft begins to fly from the left side of the scene.


cogvideox_5b_example_01.json

文生视频效果还不错

1
2
在深邃的太空中,背景有小星球,一架宇宙飞船在画面正中心
In the depths of space, with small planets in the background, a spacecraft is at the center of the scene.The spacecraft begins to fly from the left side of the scene