-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrender.py
More file actions
137 lines (118 loc) · 6.51 KB
/
render.py
File metadata and controls
137 lines (118 loc) · 6.51 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#
# Copyright (C) 2023, Inria
# GRAPHDECO research group, https://team.inria.fr/graphdeco
# All rights reserved.
#
# This software is free for non-commercial, research and evaluation use
# under the terms of the LICENSE.md file.
#
# For inquiries contact george.drettakis@inria.fr
#
import torch
from scene import Scene
import os
from tqdm import tqdm
from os import makedirs
from gaussian_renderer import render
import torchvision
from utils.general_utils import safe_state
from argparse import ArgumentParser
from arguments import ModelParams, PipelineParams, get_combined_args
from gaussian_renderer import GaussianModel
from utils.mesh_utils import GaussianExtractor, to_cam_open3d, post_process_mesh
from utils.render_utils import generate_path, create_videos
import open3d as o3d
from icecream import ic
if __name__ == "__main__":
# Set up command line argument parser
parser = ArgumentParser(description="Testing script parameters")
model = ModelParams(parser, sentinel=True)
pipeline = PipelineParams(parser)
parser.add_argument("--iteration", default=-1, type=int)
parser.add_argument("--skip_train", action="store_true")
parser.add_argument("--skip_test", action="store_true")
parser.add_argument("--skip_mesh", action="store_true")
parser.add_argument("--quiet", action="store_true")
parser.add_argument("--render_path", action="store_true")
parser.add_argument("--voxel_size", default=-1.0, type=float, help='Mesh: voxel size for TSDF')
parser.add_argument("--depth_trunc", default=-1.0, type=float, help='Mesh: Max depth range for TSDF')
parser.add_argument("--sdf_trunc", default=-1.0, type=float, help='Mesh: truncation value for TSDF')
parser.add_argument("--num_cluster", default=50, type=int, help='Mesh: number of connected clusters to export')
parser.add_argument("--unbounded", action="store_true", help='Mesh: using unbounded mode for meshing')
parser.add_argument("--mesh_res", default=1024, type=int, help='Mesh: resolution for unbounded mesh extraction')
args = get_combined_args(parser)
print("Rendering " + args.model_path)
dataset, iteration, pipe = model.extract(args), args.iteration, pipeline.extract(args)
case = os.path.basename(dataset.source_path)
shiny_cases = ['car', 'helmet', 'teapot']
default_roughness = 0
if case in shiny_cases:
default_roughness = -100
ic(case)
ic(default_roughness)
gaussians = GaussianModel(dataset.sh_degree, dataset.brdf_dim, dataset.brdf_mode, dataset.brdf_envmap_res, default_roughness=default_roughness)
scene = Scene(dataset, gaussians, load_iteration=iteration, shuffle=False)
bg_color = [1,1,1] if dataset.white_background else [0, 0, 0]
background = torch.tensor(bg_color, dtype=torch.float32, device="cuda")
gaussExtractor = GaussianExtractor(gaussians, render, pipe, bg_color=bg_color)
gaussExtractor.orb_scene_name = None
gaussExtractor.orb_image_name = None
train_dir = os.path.join(args.model_path, 'train', "ours_{}".format(scene.loaded_iter))
test_dir = os.path.join(args.model_path, 'test', "ours_{}".format(scene.loaded_iter))
if dataset.novel_brdf_envmap != "":
novel_brdf_envmap_basename = os.path.basename(dataset.novel_brdf_envmap).split('.')[0]
train_dir = os.path.join(args.model_path, 'train_{}'.format(novel_brdf_envmap_basename), "ours_{}".format(scene.loaded_iter))
test_dir = os.path.join(args.model_path, 'test_{}'.format(novel_brdf_envmap_basename), "ours_{}".format(scene.loaded_iter))
if dataset.novel_brdf_envmap != "" and 'stanford_ORB' in dataset.novel_brdf_envmap:
splits = os.path.basename(dataset.novel_brdf_envmap).split('.')[0].split('_')
orb_scene_name = splits[0] + '_' + splits[1]
orb_image_name = splits[2]
ic(orb_scene_name, orb_image_name)
gaussExtractor.orb_scene_name = orb_scene_name
gaussExtractor.orb_image_name = orb_image_name
train_dir = os.path.join(args.model_path, 'relight')
test_dir = os.path.join(args.model_path, 'relight')
if not args.skip_train:
print("export training images ...")
os.makedirs(train_dir, exist_ok=True)
gaussExtractor.reconstruction(scene.getTrainCameras())
gaussExtractor.export_image(train_dir)
if (not args.skip_test) and (len(scene.getTestCameras()) > 0):
print("export rendered testing images ...")
os.makedirs(test_dir, exist_ok=True)
gaussExtractor.reconstruction(scene.getTestCameras())
gaussExtractor.export_image(test_dir)
if args.render_path:
print("render videos ...")
traj_dir = os.path.join(args.model_path, 'traj', "ours_{}".format(scene.loaded_iter))
os.makedirs(traj_dir, exist_ok=True)
n_fames = 240
cam_traj = generate_path(scene.getTrainCameras(), n_frames=n_fames)
gaussExtractor.reconstruction(cam_traj)
gaussExtractor.export_image(traj_dir)
create_videos(base_dir=traj_dir,
input_dir=traj_dir,
out_name='render_traj',
num_frames=n_fames)
if not args.skip_mesh:
print("export mesh ...")
os.makedirs(train_dir, exist_ok=True)
# set the active_sh to 0 to export only diffuse texture
gaussExtractor.gaussians.active_sh_degree = 0
gaussExtractor.reconstruction(scene.getTrainCameras())
# extract the mesh and save
if args.unbounded:
name = 'fuse_unbounded.ply'
mesh = gaussExtractor.extract_mesh_unbounded(resolution=args.mesh_res)
else:
name = 'fuse.ply'
depth_trunc = (gaussExtractor.radius * 2.0) if args.depth_trunc < 0 else args.depth_trunc
voxel_size = (depth_trunc / args.mesh_res) if args.voxel_size < 0 else args.voxel_size
sdf_trunc = 5.0 * voxel_size if args.sdf_trunc < 0 else args.sdf_trunc
mesh = gaussExtractor.extract_mesh_bounded(voxel_size=voxel_size, sdf_trunc=sdf_trunc, depth_trunc=depth_trunc)
o3d.io.write_triangle_mesh(os.path.join(train_dir, name), mesh)
print("mesh saved at {}".format(os.path.join(train_dir, name)))
# post-process the mesh and save, saving the largest N clusters
mesh_post = post_process_mesh(mesh, cluster_to_keep=args.num_cluster)
o3d.io.write_triangle_mesh(os.path.join(train_dir, name.replace('.ply', '_post.ply')), mesh_post)
print("mesh post processed saved at {}".format(os.path.join(train_dir, name.replace('.ply', '_post.ply'))))