DiffusionRenderer 是一种适用于神经逆向渲染与正向渲染的通用方法。该方法能够从输入的图像或视频中精确估算几何与材质缓冲数据,并在指定光照条件下生成逼真的图像,为图像编辑应用提供了基础工具。
摘要
理解与建模光照效果是计算机视觉与图形学的基础任务。经典的基于物理的渲染(PBR)能精确模拟光线传输,但依赖于难以在实际场景中获取的精确场景表达——包括显式三维几何、高质量材质属性与光照条件。为此,我们提出DiffusionRenderer,一种在统一框架内同步解决逆向渲染与正向渲染的神经方法。通过利用强大的视频扩散模型先验,逆向渲染模型可从真实视频中准确估计G缓冲数据,为图像编辑任务提供接口,并为渲染模型生成训练数据。反之,我们的渲染模型无需显式光线传输模拟,即可基于G缓冲生成逼真图像。实验表明,DiffusionRenderer在逆向与正向渲染任务中均显著优于现有最佳方法,实现了高效近似。该模型支持单视频输入的实用应用,包括重光照、材质编辑与真实感物体插入。
方法概述。给定输入视频,神经逆向渲染器逐像素估算几何形状与材质属性。它每次生成一个场景属性,并通过域嵌入标识待生成的目标属性。与之对应,神经正向渲染器根据光照信息、几何缓冲和材质缓冲生成逼真图像。光照条件通过交叉注意力层注入基础视频扩散模型。在使用合成数据与真实数据进行联合训练时,我们为真实数据源采用可优化的LoRA模块。
安装指南
最低系统需求
- Python 3.10 环境
- NVIDIA显卡(显存至少16GB,推荐≥24GB)
- NVIDIA驱动及CUDA 12.0或更高版本
- 至少70GB可用磁盘空间
已通过测试的环境:
- Ubuntu 20.04系统
- NVIDIA A100显卡(80GB显存),NVIDIA A5000显卡(24GB显存)
Conda 环境
以下命令将创建 cosmos-predict1
conda 环境并安装推理所需的依赖项:
# Create the cosmos-predict1 conda environment.
conda env create --file cosmos-predict1.yaml
# Activate the cosmos-predict1 conda environment.
conda activate cosmos-predict1
# Install the dependencies.
pip install -r requirements.txt
# Patch Transformer engine linking issues in conda environments.
ln -sf $CONDA_PREFIX/lib/python3.10/site-packages/nvidia/*/include/* $CONDA_PREFIX/include/
ln -sf $CONDA_PREFIX/lib/python3.10/site-packages/nvidia/*/include/* $CONDA_PREFIX/include/python3.10
# Install Transformer engine.
pip install transformer-engine[pytorch]==1.12.0
如果依赖项已妥善处理,请通过以下命令安装nvdiffrast
:
# Patch dependency for nvdiffrast
ln -sf $CONDA_PREFIX/lib/python3.10/site-packages/triton/backends/nvidia/include/crt $CONDA_PREFIX/include/
pip install git+https://github.com/NVlabs/nvdiffrast.git
对于非 Ubuntu 平台,请查阅 nvdiffrast 官方文档 及其 Dockerfile。
下载模型权重(约 56GB)
模型权重可在 Hugging Face 获取。
生成 Hugging Face 访问令牌(若尚未生成)。将访问令牌权限设置为「读取」(默认为「细粒度」权限)。
使用访问令牌登录 Hugging Face:
huggingface-cli login
从Hugging Face下载DiffusionRenderer模型权重:
CUDA_HOME=$CONDA_PREFIX PYTHONPATH=$(pwd) python scripts/download_diffusion_renderer_checkpoints.py --checkpoint_dir checkpoints
推理:图像示例
此示例展示了如何使用 DiffusionRenderer 对一组图像进行光照增强和重照明,这些图像位于 asset/examples/image_examples/
文件夹中。模型将处理文件夹中的每张图像;使用较少的图像会减少总处理时间。
建议 GPU 显存约为 16GB。如果遇到内存不足的错误,请在命令中添加 --offload_diffusion_transformer --offload_tokenizer
以减少 GPU 内存使用量。
图像的逆向渲染
此操作将使用预训练的逆向渲染器模型从每张输入图像中估算反照率、金属度、粗糙度、深度和法线(G-缓冲区)。推理脚本为 cosmos_predict1/diffusion/inference/inference_inverse_renderer.py
。
要对一组图像执行逆向渲染,请使用以下命令:
CUDA_HOME=$CONDA_PREFIX PYTHONPATH=$(pwd) python cosmos_predict1/diffusion/inference/inference_inverse_renderer.py \
--checkpoint_dir checkpoints --diffusion_transformer_dir Diffusion_Renderer_Inverse_Cosmos_7B \
--dataset_path=asset/examples/image_examples/ --num_video_frames 1 --group_mode webdataset \
--video_save_folder=asset/example_results/image_delighting/ --save_video=False
此处配置说明:
--checkpoint_dir
指定包含模型检查点的目录,默认为checkpoints/
。--diffusion_transformer_dir
选择要使用的具体模型变体。--dataset_path
指向包含输入图像的文件夹路径。--num_video_frames 1
将每张图像作为独立单帧处理。--video_save_folder
设置结果输出目录。--save_video=False
禁用视频文件保存(因当前处理静态图像)。
其他参数说明可在脚本内查阅。
此外,--inference_passes
参数控制逆向渲染器估算并保存的G缓冲贴图类型,默认执行五通道计算:basecolor
(基础色)、normal
(法线)、depth
(深度)、roughness
(粗糙度)和metallic
(金属度)。可通过指定子集仅生成部分输出。
图像重光照
基于上一步骤生成的G缓冲帧(路径:asset/example_results/image_delighting/gbuffer_frames
),我们使用前向渲染器结合用户提供的环境贴图对图像进行重光照处理。
CUDA_HOME=$CONDA_PREFIX PYTHONPATH=$(pwd) python cosmos_predict1/diffusion/inference/inference_forward_renderer.py \
--checkpoint_dir checkpoints --diffusion_transformer_dir Diffusion_Renderer_Forward_Cosmos_7B \
--dataset_path=asset/example_results/image_delighting/gbuffer_frames --num_video_frames 1 \
--envlight_ind 0 1 2 3 --use_custom_envmap=True \
--video_save_folder=asset/example_results/image_relighting/
此处 --envlight_ind 0 1 2 3
参数用于指定重光照时使用的环境贴图(HDRIs)。每个数字对应代码中预定义的不同光照环境(参见 inference_forward_renderer.py
中的 ENV_LIGHT_PATH_LIST
)。
通过传入多个索引(如 0 1 2 3
),前向渲染器会使用所有选定的环境贴图为每个输入执行重光照,从而为每个输入生成多重光照输出结果。您也可以选择子集(例如 --envlight_ind 0 2
)来仅使用特定光照条件。脚本运行结果将保存在 asset/example_results/image_relighting/
目录中。
图像光照随机化
当环境贴图不可用时,可通过以下命令修改随机种子来实现光照随机化效果。
CUDA_HOME=$CONDA_PREFIX PYTHONPATH=$(pwd) python cosmos_predict1/diffusion/inference/inference_forward_renderer.py \
--checkpoint_dir checkpoints --diffusion_transformer_dir Diffusion_Renderer_Forward_Cosmos_7B \
--dataset_path=asset/example_results/image_delighting/gbuffer_frames --num_video_frames 1 \
--envlight_ind 0 1 2 3 --use_custom_envmap=False \
--video_save_folder=asset/example_results/image_relighting_random/
通过将 --use_custom_envmap
设置为 False
。
脚本将在 asset/example_results/image_relighting_random/
目录中生成结果。
推理:视频示例
本示例使用放置在 asset/examples/video_examples/
文件夹中的视频。模型将处理该文件夹中的每个视频;使用较少的视频将缩短总处理时间。
峰值 GPU 内存使用量约为 27GB。如果遇到内存不足错误,请在命令中添加 --offload_diffusion_transformer --offload_tokenizer
以减少 GPU 内存使用量。
从视频中提取帧
在对视频运行逆向渲染器之前,需要从每个视频文件中提取单独的帧。此步骤将每个视频转换为图像序列,这些图像随后将用作渲染管道的输入。
以下命令将处理 asset/examples/video_examples/
目录中的所有视频,提取帧并将其保存到 asset/examples/video_frames_examples/
文件夹中:
python scripts/dataproc_extract_frames_from_video.py --input_folder asset/examples/video_examples/ --output_folder asset/examples/video_frames_examples/
--frame_rate 24 --resize 1280x704 --max_frames=57
视频逆向渲染
此步骤对视频帧序列进行逆向渲染,以估算每一帧的底层G缓冲图(如基础色、法线、深度、粗糙度和金属度)。
示例命令
CUDA_HOME=$CONDA_PREFIX PYTHONPATH=$(pwd) python cosmos_predict1/diffusion/inference/inference_inverse_renderer.py \
--checkpoint_dir checkpoints --diffusion_transformer_dir Diffusion_Renderer_Inverse_Cosmos_7B \
--dataset_path=asset/examples/video_frames_examples/ --num_video_frames 57 --group_mode folder \
--video_save_folder=asset/example_results/video_delighting/
视频重光照
此步骤利用逆向渲染器生成的G缓冲帧,并应用新的光照条件以生成重光照视频帧。以下指令使用四种不同的环境贴图(由--envlight_ind 0 1 2 3
指定)对视频进行重光照处理。
CUDA_HOME=$CONDA_PREFIX PYTHONPATH=$(pwd) python cosmos_predict1/diffusion/inference/inference_forward_renderer.py \
--checkpoint_dir checkpoints --diffusion_transformer_dir Diffusion_Renderer_Forward_Cosmos_7B \
--dataset_path=asset/example_results/video_delighting/gbuffer_frames --num_video_frames 57 \
--envlight_ind 0 1 2 3 --use_custom_envmap=True \
--video_save_folder=asset/example_results/video_relighting/
我们还可以使用静态帧并通过指定--rotate_light=True --use_fixed_frame_ind=True
来展示旋转环境光下的重光照效果:
CUDA_HOME=$CONDA_PREFIX PYTHONPATH=$(pwd) python cosmos_predict1/diffusion/inference/inference_forward_renderer.py \
--checkpoint_dir checkpoints --diffusion_transformer_dir Diffusion_Renderer_Forward_Cosmos_7B \
--dataset_path=asset/example_results/video_delighting/gbuffer_frames --num_video_frames 57 \
--envlight_ind 0 1 2 3 --use_custom_envmap=True \
--video_save_folder=asset/example_results/video_relighting_rotation/ --rotate_light=True --use_fixed_frame_ind=True
资料
https://research.nvidia.com/labs/toronto-ai/DiffusionRenderer/
https://github.com/nv-tlabs/cosmos1-diffusion-renderer#