Skip to main content

Open-Sora 1.2 镜像

OpenSora 镜像和数据目前不支持 4090 集群,请在更高配的云主机上使用。

1.潞晨云配置

1.1 获取本地密钥

# 免密设置,在本地终端上生成密钥
ssh-keygen -t rsa -f ~/.ssh/id_rsa -P "" -q
# 打印本地密钥,复制终端中打印出的内容用于下一步免密配置
cat ~/.ssh/id_rsa.pub

1.2 进行免密配置

点击潞晨云右上角界面,“添加新 SSH 公钥”。

将本地终端中打印出的密钥复制粘贴到“公钥”位置。可根据个人喜好填写“公钥名”。

启动机器时需要选择“公共镜像”,点击第二页,选择“OpenSora (1.2)”,并勾选挂载公开数据。我们已经将模型的权重和一个测试数据加载到公开数据中,勾选“挂载公开数据”可以免下载直接使用。机器可以选择单卡的 A800, 我们的代码可以在单卡 A800 上跑训练和推理。当然如果想要更快地训练和推理较大的数据量,可以考虑使用多卡。

复制"SSH 链接"地址,在终端中粘贴并回车开启链接,例如ssh root@123.456.789.123 -p 12345

注意:您在本地新开的每一个终端,如果需要链接潞晨云机器终端,都需要执行一遍以上指令,否则即本地终端。

# 开启SSH链接
ssh root@123.456.789.123 -p 12345
# 注意:此处需要替换成您在云主机“SSH链接”复制的内容!

2.配置 GPT4o 文本润色服务【可选项】

如果用户需要非英文输入文本提示(如中文),或者需要在用户的输入的基础上进行润色,我们需要配置 GPT4o 来将处理初始输入。如果您可以直接英文输入且不需要润色,可以直接跳过这一板块。

GPT4o 是 OpenAI 提供的一项付费模型,如果您还没有 OpenAI 的账户,需要先注册:https://platform.openai.com/signup/ .

完成注册并登录后,点击右上角设置(settings), 如下图所示:

然后点击左侧界面中的个人账户(Your profile)。在右侧图中的上方选项中选择“User API Key”选项,然后点击“+ Create new secret key"来创建个人 OpenAI 密钥。

如下方左图所示,可以根据个人喜好对密钥命名,然后点击生成密钥 “Create secret key”。完成后立刻点击复制(Copy),并将您的密钥存储到安全的文件中,因为当您关闭这个页面之后,就无法重新看到已生成的密钥!如若忘记,则需要重新生成新的密钥。

之后在潞晨云终端环境中需要设置这个密钥,指令如下:

export OPENAI_API_KEY='您的OPENAI密钥'

3. 使用 Open-Sora

目前潞晨云的公开数据中已经加入了 Open-Sora 模型的权重以及一个测试数据,需要在开启云主机时选择加载公开数据,详情请见第 1.2 部分。

数据路径分别在:

  • 视频压缩模型(VAE)权重:/root/commonData/OpenSora-VAE-v1.2/
  • 视频生成模型(diffusion)权重:/root/commonData/OpenSora-STDiT-v3
  • 测试数据:/root/commonData/Inter4K/scene_cut

此外,镜像中保存了 VAE 训练所需的 LPIPS 模型权重: /root/.cache/torch/hub/checkpoints/vgg16-397923af.pth

这些路径都已更新到 Open-Sora 代码的feature/mirror_1.2分支,您无需任何操作,只需要进入我们对应的 Open-Sora 文件夹即可:

# 进入Open-Sora的文件夹
cd /home/root/Open-Sora
# 更新git分支
git fetch origin feature/mirror_v1.2
# 进入镜像对应分支
git checkout feature/mirror_v1.2

3.1 Gradio 网页生成视频指南

3.1.1 基本使用

Gardio 是可以帮助我们快速的建立可视化网页界面,无需写任何前段网页代码。Open-Sora 已经配置了 Gardio 应用,您不需要输入生成视频代码治理,就可以通过网页的互动来快速体验视频生成。如果您想直接用代码进行视频生成,也可以跳过此部分。

# 可选:如果您需要用OpenAI的模型(GPT4o)对用户的文字输入进行润色,需要在环境中设置您的OpenAI密钥信息
# 将以下'您的OPENAI密钥'部分替换成您的密钥,并在终端执行;
# 关于如何获取OPENAI密钥,参考本文档第2部分
export OPENAI_API_KEY='您的OPENAI密钥'

# 启动Gardio
python gradio/app.py --port 6006
# 成功启动后会输出:Running on local URL: http://0.0.0.0:6006

开启 Gardio 之后,您需要将本地终端和潞晨云终端建立连接。首先,您可以通过潞晨云的云主机界面来查看复制 SSH 云主机地址,如内容为ssh root@123.456.789.123 -p 12345。注意,您的机器只有在开启状态(默认)才有可复制的 SSH 地址,如果已经关机,需要先点击开机才可以使用。

那么,通过以下指令将本地终端和潞晨云终端(需要:10 新开启一个本地终端页面)建立连接(主要要将ssh root@123.456.789.123 -p 12345替换成您在云主机界面复制的内容):

ssh root@123.456.789.123 -p 12345 -CNg -L 6006:127.0.0.1:6006

那么您可以在本地的任意网页输入 "http://127.0.0.1:6006"。您会看到以下界面:

生成视频最简单的方式是输入文本提示(在"Describe your video here"的位置,如需中文输入,需要配置 GPT4o, 详情参考上一部分),然后点击“生成视频”按钮(屏幕右侧下方的“Generate Video”,如果找不到,请向下滚动)。生成的视频将显示在右侧面板中。勾选“使用 GPT4o 增强提示”("Enhance prompt with GPT4o")将使用 GPT-4o 来细化提示,而“随机提示”("Random Prompt")按钮将由 GPT-4o 为您生成随机提示。由于 OpenAI 的 API 限制,提示细化结果具有一定的随机性。需要注意的是,GPT4o 是 OpenAI 的一项付费服务。如果您没有 OpenAI 的密钥,勾选图中“使用 GPT4o 增强提示”(“Enhance prompt with GPT4o”)的选项无效。

然后,图片下半部分从下往上的选项,可以选择生成视频的分辨率(Resolution)、长宽比(Aspect Ration)、时长 (Video Length)。不同的分辨率和视频长度会影响视频生成速度,建议可以首先用低分辨率短视频进行生成体验。在 80G H100 GPU 上,生成速度和峰值内存使用量为:

生成的视频可以通过点击网页 gradio 的右上角的向下符号进行下载到本地。 时间过长,高像素的视频从生成到上传到您本地的 gradio 可能需要更多时间(取决于网络速度),如果太慢,可以考虑之间参考 3.2 部分在服务器上指令生成再下载到本地。

如果遇到以下情况,是因为网速过慢,视频已经生成但是还未完全传输。需要耐心等待,视频传输完毕后会有具体时长数字,而且可播放。

3.1.2 高级设置 【进阶】

此外,Gardio 中还有更多选项:

如图所示,您可以设置不同的种子(Seed)来增加随机性, 这样即使是相同的文本提示也会生成不同的视频。您可以提高或降低采样数量(Sampling steps), 通常来讲,采样数量越高视频质量越好,但是所需生成视频的时长也会显著增加。您可以通过增高控制力度(CFG Scale)来改变文本提示对输出视频的控制力度,控制力度越高,视频的内容会越符合文本提示的要求,但是过高的控制力度也可能导致视频质量的下降(我们建议使用 7)。

您也可以调整运动幅度(Motion Strength)来控制视频的动态,低的数值生成的视频相对运动幅度较小,但是过高的数值会导致视频变糊。美学分数(aesthetic score)则控制视频的画面质量,我们建议用 6 分以上的数值。此外,镜头运动方向(Camera Motion)有时可以控制生成视频的镜头动态,我们正在努力改进这一系列功能中:向右移动(pan right), 向左移动(pan left), 向上倾斜(tilt up), 向下倾斜(tilt down), 拉进镜头(zoom in), 调远镜头(zoom out), 静止(static),默认选项(none)即什么都不选。

3.1.3 关闭 Gradio

如果您只有单显卡,并需要用显卡来做其他任务(如尝试 3.2,3.3,或 3.4 章节中内容)需要先关闭 gradio 服务,否则该任务会占用显卡内存导致后续任务内存溢出报错(OOM)。

关闭 Gradio 可以直接在连接潞晨云的跑 gradio 的终端输入 ctrl + c;如果该进程卡住,您需要新开启一个潞晨云的终端连接,用指令关闭 gradio。

# 首先查看进程
ps -aux

找到 gradio 对应的 PID, 如此处为 2947

# 关闭gradio
kill 2947

如果您有多卡,也可以不关 gradio, 但是在后续生成的指令中指定使用其他的显卡(CUDA_VISIBLE_DEVICES=<GPU卡号>),例如:

CUDA_VISIBLE_DEVICES=1 \
python scripts/inference.py configs/opensora-v1-2/inference/sample.py ...

3.2 指令生成视频指南

3.2.1 输入润色【可选项】

如果您需要用 OpenAI 的模型(GPT4o)对用户的文字输入进行润色,需要在环境中设置您的 OpenAI 密钥信息(具体设置可参考本文档第 2 部分)。可以将以下'您的 OPENAI 密钥'部分替换成您的密钥,并在终端执行。

export OPENAI_API_KEY='您的OPENAI密钥'

在此后您可以在指令上加入 --llm-refine True 来开启模型中的输入润色功能。

注 GPT4o 是 OpenAI 的一项付费服务,如果您未提供OPENAI_API_KEY我们将无法执行 --llm-refine True 的命令。

3.2.2 基础使用:文字生成视频

此处会生成 4 秒(--num-frames)的视频, 视频像素(--resolution)为 720p, 高宽比(--aspect-ratio)是 9:16, 同时输入文本(--prompt)提示为“a beautiful waterfall”(美丽的瀑布)。

关于生成时长,像素,高宽比我们支持的选项可以参考上一部分(第三部分)中 a 部分(基本使用)Gradio 图片中的可选目录。

# 以下视频会保存到 /home/root/Open-Sora/samples/waterfall/waterfall_720p_4s_base_0000.mp4
# 以下视频生成时间约130秒,详情请见本文档3.1.1的H100机器测速表格
# 若您想快速测速生成,可以先生成低像素短视频, 修改第二行对应内容为`--num-frames 2s --resolution 144p`
python scripts/inference.py configs/opensora-v1-2/inference/sample.py \
--num-frames 4s --resolution 480p --aspect-ratio 9:16 \
--prompt "a beautiful waterfall" \
--save-dir ./samples/waterfall \
--sample-name waterfall_720p_4s_base

此外也可以使用更多的控制,如采样数量(--num-sampling-steps),镜头运动幅度(--flow),美学分数(--aes)等。

# 以下视频会保存到 /home/root/Open-Sora/samples/waterfall/waterfall_720p_4s_steps30_flow5_aes6.5_0000.mp4
# 以下视频生成时间约130秒,详情请见本文档3.1.1的H100机器测速表格
python scripts/inference.py configs/opensora-v1-2/inference/sample.py \
--num-frames 4s --resolution 480p --aspect-ratio 9:16 \
--num-sampling-steps 30 --flow 5 --aes 6.5 \
--prompt "a beautiful waterfall" \
--save-dir ./samples/waterfall \
--sample-name waterfall_720p_4s_steps30_flow5_aes6.5

用 txt 来生成多个视频:

  # 生成时间较长,会对`assets/texts/t2v_sora.txt`中每行的英文生成一个视频,
# 保存到/home/root/Open-Sora/samples/t2v_sora文件夹中
python scripts/inference.py configs/opensora-v1-2/inference/sample.py \
--num-frames 4s --resolution 480p --aspect-ratio 9:16 \
--num-sampling-steps 30 --flow 5 --aes 6.5 \
--prompt-path assets/texts/t2v_sora.txt \
--batch-size 1 \
--save-dir ./samples/t2v_sora

生成图片: 只需要设置 --num-frames 1 即可,其他控制和生成视频一致,如:

# 以下图片会保存到 /home/root/Open-Sora/samples/waterfall_image_0000.png
python scripts/inference.py configs/opensora-v1-2/inference/sample.py \
--num-frames 1 --resolution 480p --aspect-ratio 9:16 \
--prompt "a beautiful waterfall" \
--save-dir ./samples/waterfall \
--sample-name waterfall_image

3.2.3 查看生成内容

# 进入对应的生成文件夹
cd /home/root/Open-Sora/samples

# 在对应的生成文件夹中启动python http服务, 需要设置对接端口号为6006
python -m http.server 6006

# 在本地连接服务器查看,
# 例如您在潞晨云主机复制的地址为ssh root@123.456.789.123 -p 12345
# 获取潞晨云主机地址,请参考3.1.1部分
# 将以下指令中"ssh root@xxx -p xxx"部分替换成您的地址,连接您的本地网址到潞晨云主机网址
ssh root@123.456.789.123 -p 12345 -CNg -L 6006:127.0.0.1:6006
# 然后在本地电脑浏览器上输入 http://127.0.0.1:6666, 即可看到生成的文件夹内容,点击可查看视频
# 如果有新的视频生成,需要刷新您的电脑浏览器网址 http://127.0.0.1:6006

3.2.4 下载生成内容

您可以直接在http://127.0.0.1:6006 的网址选择下载,点击右下角三个点处展开选项,点击下载(“Download”),即可。

或者也可以通过指令批量下载:

# 在本地的终端中输入 【注意:需要替换"-P 12345", "root@123.456.789.123"到您对应的接口数字和潞晨云主机地址】
scp -r -P 12345 root@123.456.789.123:/home/root/OpenSora/samples <您的本地路径>

3.2.5 高级控制【进阶】

我们的模型也可做到图片+文字生成视频,循环视频,视频延续生成, 指令如下:

# 图片+文字生成视频,
# 生成的视频会保存到 /home/root/Open-Sora/samples/advanced/image_cond_0000.mp4
python scripts/inference.py configs/opensora-v1-2/inference/sample.py \
--num-frames 4s --resolution 720p --aspect-ratio 9:16 \
--num-sampling-steps 30 --flow 5 --aes 6.5 \
--prompt "Drone view of waves crashing against the rugged cliffs along Big Sur’s garay point beach. The crashing blue waters create white-tipped waves, while the golden light of the setting sun illuminates the rocky shore. A small island with a lighthouse sits in the distance, and green shrubbery covers the cliff’s edge. The steep drop from the road down to the beach is a dramatic feat, with the cliff's edges jutting out over the sea. This is a view that captures the raw beauty of the coast and the rugged landscape of the Pacific Coast Highway." \
--reference-path assets/images/condition/cliff.png \
--loop 1 \
--mask-strategy "0" \
--save-dir ./samples/advanced \
--sample-name image_cond

# 循环视频,通过控制--mask-strategy实现
# 生成的视频会保存到 /home/root/Open-Sora/samples/advanced/same_start_end_0000.mp4
python scripts/inference.py configs/opensora-v1-2/inference/sample.py \
--num-frames 4s --resolution 720p --aspect-ratio 9:16 \
--num-sampling-steps 30 --flow 5 --aes 6.5 \
--prompt "Drone view of waves crashing against the rugged cliffs along Big Sur’s garay point beach. The crashing blue waters create white-tipped waves, while the golden light of the setting sun illuminates the rocky shore. A small island with a lighthouse sits in the distance, and green shrubbery covers the cliff’s edge. The steep drop from the road down to the beach is a dramatic feat, with the cliff's edges jutting out over the sea. This is a view that captures the raw beauty of the coast and the rugged landscape of the Pacific Coast Highway." \
--reference-path assets/images/condition/cliff.png \
--loop 1 \
--mask-strategy "0;0,0,0,-1,1" \
--save-dir ./samples/advanced \
--sample-name same_start_end

# 视频延续生成,通过--loop开控制延续生成次数,此处5即连续生成五次
# 生成的视频会保存到 /home/root/Open-Sora/samples/advanced/loop_5_0000.mp4
# 以下视频生成时间较长,因为采取了5次的连续生成,单次生成时间详情请见本文档3.1.1的H100机器测速表格
python scripts/inference.py configs/opensora-v1-2/inference/sample.py \
--num-frames 4s --resolution 720p --aspect-ratio 9:16 \
--num-sampling-steps 30 --flow 5 --aes 6.5 \
--prompt "Drone view of waves crashing against the rugged cliffs along Big Sur’s garay point beach. The crashing blue waters create white-tipped waves, while the golden light of the setting sun illuminates the rocky shore. A small island with a lighthouse sits in the distance, and green shrubbery covers the cliff’s edge. The steep drop from the road down to the beach is a dramatic feat, with the cliff's edges jutting out over the sea. This is a view that captures the raw beauty of the coast and the rugged landscape of the Pacific Coast Highway." \
--reference-path assets/images/condition/cliff.png \
--loop 5 --condition-frame-length 5 \
--mask-strategy "0" \
--save-dir ./samples/advanced \
--sample-name loop_5

更多例子可以参考:https://github.com/hpcaitech/Open-Sora-Dev/blob/dev/v1.2/docs/commands.md

3.3 微调训练 OpenSora 【进阶】

我们为您准备了一个小数据集,方便您尝试微调。 数据信息路径:/root/commonData/Inter4K/meta/meta_inter4k_ready.csv

# 单卡训练
torchrun --standalone --nproc_per_node 1 scripts/train.py configs/opensora-v1-2/train/stage3.py \
--data-path /root/commonData/Inter4K/meta/meta_inter4k_ready.csv

# 8卡训练
torchrun --standalone --nproc_per_node 8 scripts/train.py configs/opensora-v1-2/train/stage3.py \
--data-path /root/commonData/Inter4K/meta/meta_inter4k_ready.csv

你可以用您的 csv 数据文档替换我们的数据集( --data-path 您的 csv 文件路径 ),并上传您的视频文件。关于如何使用我们的代码准备数据 csv 文件,请参考: https://github.com/hpcaitech/Open-Sora/tree/feature/mirror_v1.2/tools/datasets 上传您的文件到潞晨云:

# 在本地的终端中上传数据
# 注意:需要替换"-P 12345", "root@123.456.789.123"到您对应的潞晨云接口数字和主机地址
# 注意:需要替换<您的本地视频路径>和<您视频文件名>到对应路径和名字
scp -r -P 12345 <您的本地视频路径> root@123.456.789.123:/home/root/data/<您视频文件名>

3.4 训练 3D 视频压缩模型(VAE) 【进阶】

此外,您也可以用自己的数据继续训练我们的 3D 视频压缩模型(VAE):

# 单卡训练,如果8卡可以调整 --nproc_per_node=8
torchrun --nnodes=1 --nproc_per_node=1 scripts/train_vae.py \
configs/vae/train/continue_train_vae_1.2.py \
--data-path /root/commonData/Inter4K/meta/meta_inter4k_ready.csv

3D 视频压缩模型压缩再解码生成:

# 注:查看效果最好用与训练数据不同的数据,此处我们仅有单一数据,所以以此此示例
# 生成的内容会被保存到`/root/samples/vae_ori`, ``vae_rec, 和`vae_rec2d`三个文件夹中,
# 分别是初始视频,我们的VAE模型编码解码后的内容,我们的VAE只在空间层面编码解码过的内容
torchrun --standalone --nnodes=1 --nproc_per_node=1 scripts/inference_vae.py configs/vae/inference/video.py \
--data-path /root/commonData/Inter4K/meta/meta_inter4k_ready.csv \
--save-dir samples/vae

下载这些视频查看效果,详情参考 3.2.4 部分指南。