cover
cv #nerf #instant-ngp #cv

Instant-ngp复现和自拍数据集的运行

Asea
2022-06-08 20

# instant-ngp

readme写的很清晰很清晰了,建议先认真看一下README.md

# 环境

  1. win10(内存至少16G+支持cuda的英伟达显卡,8G就别试了,会溢出的;至于linux我没配置过不甚清楚)我的配置:i7-9750H、RTX 2060

  2. 安装vs2019(2022不行...)(勾选桌面C++开发)

  3. 安装cmake(直接官网下,最新就行,记得勾选环境变量)

  4. 安装cuda(同上)

  5. 贴一下官方的配置

    # Requirements

    1. An NVIDIA GPU; tensor cores increase performance when available. All shown results come from an RTX 3090.
    2. A C++14 capable compiler. The following choices are recommended and have been tested:
    3. Windows: Visual Studio 2019
    4. Linux: GCC/G++ 7.5 or higher
    5. CUDA v10.2 or higher and CMake v3.21 or higher.
    6. (optional) Python 3.7 or higher for interactive bindings. Also, run pip install -r requirements.txt.(如果用cmake生成testbed.exe则不需要pip install)
    7. (optional) OptiX 7.3 or higher for faster mesh SDF training. Set the environment variable OptiX_INSTALL_DIR to the installation directory if it is not discovered automatically.
    8. (optional) Vulkan SDK for DLSS support.
  6. # 注意这里的--recursive不能丢,否则不会下载依赖
    git clone --recursive https://github.com/nvlabs/instant-ngp    
    #或
    git clone --recursive git@github.com:NVlabs/instant-ngp.git
    

    拉取中间如果没有成功拉取依赖,不必删掉,继续执行下面操作

    # 修改git配置
    git config --global http.sslBackend schannel
    # 拉取依赖
    git submodule update --init --recursive
    

    至此,项目已经拉取完毕,下面开始复现

# 复现

在instant-ngp根目录里执行

cmake . -B build

如果环境搭建好了应该不会报错,如果报了cuda相关的错可以考虑重装cuda。这里我用win11遇到了无法解决的错,最后妥协用了win10才行。然后执行

cmake --build build --config RelWithDebInfo -j 16

这两步可能需要一段时间,耐心等待编译结束后会在build目录生成testbed.exe,下面就可以复现了,依然是在根目录

build/testbed --scene data/nerf/fox

此时就会弹出GUI,恭喜你成功复现(如果是8G的内存会无法启动....),关于GUI的使用以及如何保存,下面说。你可以选择不带GUI的模式,例如在colab运行时,默认是100000,参考这里

# GUI的使用

具体可调参数较多,我也没完全了解,这里只说一些我用的

# 摄像机

展开cameraPath,鼠标移动场景,滚轮可以放大缩小场景,调整好角度以后,点击add from cam即可将在当前视角添加摄像机。你可以有规律的选择一系列视角添加摄像机,随后点击save,即可保存在base_cam.json文件当中。后面生成视频需要用到。

# 快照

你可以将当前的训练进度保存,下一次load快照即可继续迭代。点击snapshot,点击save即可保存在base.msg_pack中。下一次启动GUI以后可以点击load进行加载。或者直接--snapshot [路径]来启动快照。其他参数这里不讲了,参照视频

# 生成视频

原理就是通过保存的base_cam.json记录好的摄像机角度,渲染出来视频。这里我参照了【环境搭建】instant-ngp的环境搭建和demo这篇文章,写的很好推荐一看。

作者写的脚本

#!/usr/bin/env python3

# Copyright (c) 2020-2022, NVIDIA CORPORATION.  All rights reserved.
#
# NVIDIA CORPORATION and its licensors retain all intellectual property
# and proprietary rights in and to this software, related documentation
# and any modifications thereto.  Any use, reproduction, disclosure or
# distribution of this software and related documentation without an express
# license agreement from NVIDIA CORPORATION is strictly prohibited.

import os, sys, shutil
import argparse
from tqdm import tqdm
import common
import pyngp as ngp # noqa
import numpy as np

"""Render the video based on specific camera poses.

    Render a series of images from specific views and then generate a smoothing video.

    Args:
    scene:
        The scene to load. Can be the scene's name or a full path to the training data.
    width:
        Resolution width of video
    height:
        Resolution width of video
    n_seconds:
        Number of seconds
    fps:
        FPS of the video
    render_name:
        Name of video.

    Returns:
    None. A video will be saved at scene root path
"""
def render_video(resolution, numframes, scene, name, spp, fps, exposure=0):
	testbed = ngp.Testbed(ngp.TestbedMode.Nerf)
	testbed.load_snapshot(os.path.join(scene, "base.msgpack"))
	testbed.load_camera_path(os.path.join(scene, "base_cam.json"))

	if 'temp' in os.listdir():
		shutil.rmtree('temp')
	os.makedirs('temp')	

	for i in tqdm(list(range(min(numframes,numframes+1))), unit="frames", desc=f"Rendering"):
		testbed.camera_smoothing = i > 0
		frame = testbed.render(resolution[0], resolution[1], spp, True, float(i)/numframes, float(i + 1)/numframes, fps, shutter_fraction=0.5)
		common.write_image(f"temp/{i:04d}.jpg", np.clip(frame * 2**exposure, 0.0, 1.0), quality=100)

	os.system(f"ffmpeg -i temp/%04d.jpg -vf \"fps={fps}\" -c:v libx264 -pix_fmt yuv420p {name}_test.mp4")
	shutil.rmtree('temp')


def parse_args():
	parser = argparse.ArgumentParser(description="render neural graphics primitives testbed")
	parser.add_argument("--scene", "--training_data", default="", help="The scene to load. Can be the scene's name or a full path to the training data.")

	parser.add_argument("--width", "--screenshot_w", type=int, default=1920, help="Resolution width of the render video")
	parser.add_argument("--height", "--screenshot_h", type=int, default=1080, help="Resolution height of the render video")
	parser.add_argument("--n_seconds", type=int, default=1, help="Number of seconds")
	parser.add_argument("--fps", type=int, default=60, help="number of fps")
	parser.add_argument("--render_name", type=str, default="", help="name of the result video")

	args = parser.parse_args()
	return args

if __name__ == "__main__":
	args = parse_args()	
	render_video([args.width, args.height], args.n_seconds*args.fps, args.scene, args.render_name, spp=8, fps=args.fps)

执行命令如下

python scripts/rendervideo.py --scene <scene_path> --n_seconds <seconds> --fps <fps> --render_name <name> --width <resolution_width> --height <resolution_height>

#比如:
python scripts/rendervideo.py --scene data/nerf/fox --n_seconds 10 --fps 60 --render_name foxvideo --width 1920 --height 1080

如果之前没有install requirements,那执行的时候缺哪个库装就完了。脚本具体的参数作者写的也非常详细,渲染速度看你设备,我这里渲染720x480的一帧大概五秒.....所以如果太慢的话,fps调成25、30,完全够了。

# 自己的数据集

这里instant写的也很详细,我感觉我不用多说...

贴一下地址:看这里

需要装一下ffmpeg和colmap,然后将colmap的bin和lib目录以及ffmpeg的bin目录加到path环境变量里。

我是直接用视频了,对应的是

python [path-to-instant-ngp]/scripts/colmap2nerf.py --video_in <filename of video> --video_fps 2 --run_colmap --aabb_scale 16

colmap2nerf.py 实现的效果是自动将视频分割成图片,然后调用colmap稀疏重建生成transform.json文件,最后执行即可,需要等待一段时间。

build/testbed --mode nerf --scene [path to training data folder containing transforms.json]

CC BY-NC-SA 4.0 Licensed

COMMENTS (0)

2025 AseaBlog

豫ICP备2022022909号