-
Notifications
You must be signed in to change notification settings - Fork 22
/
render.py
239 lines (210 loc) · 7.45 KB
/
render.py
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#
# 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 [email protected]
#
import torch
import torch.distributed as dist
from scene import Scene, SceneDataset
import os
from tqdm import tqdm
from os import makedirs
from gaussian_renderer import (
# preprocess3dgs_and_all2all,
# render
distributed_preprocess3dgs_and_all2all_final,
render_final,
)
import torchvision
from utils.general_utils import (
safe_state,
set_args,
init_distributed,
set_log_file,
set_cur_iter,
)
from argparse import ArgumentParser
from arguments import ModelParams, PipelineParams, get_combined_args
from gaussian_renderer import GaussianModel
from gaussian_renderer.loss_distribution import load_camera_from_cpu_to_all_gpu_for_eval
from gaussian_renderer.workload_division import (
start_strategy_final,
DivisionStrategyHistoryFinal,
)
from arguments import (
AuxiliaryParams,
ModelParams,
PipelineParams,
OptimizationParams,
DistributionParams,
BenchmarkParams,
DebugParams,
print_all_args,
init_args,
)
import utils.general_utils as utils
def render_set(model_path, name, iteration, views, gaussians, pipeline, background):
render_path = os.path.join(model_path, name, "ours_{}".format(iteration), "renders")
gts_path = os.path.join(model_path, name, "ours_{}".format(iteration), "gt")
makedirs(render_path, exist_ok=True)
makedirs(gts_path, exist_ok=True)
dataset = SceneDataset(views)
set_cur_iter(iteration)
generated_cnt = 0
num_cameras = len(views)
strategy_history = DivisionStrategyHistoryFinal(
dataset, utils.DEFAULT_GROUP.size(), utils.DEFAULT_GROUP.rank()
)
progress_bar = tqdm(
range(1, num_cameras + 1),
desc="Rendering progress",
disable=(utils.LOCAL_RANK != 0),
)
for idx in range(1, num_cameras + 1, args.bsz):
progress_bar.update(args.bsz)
num_camera_to_load = min(args.bsz, num_cameras - idx + 1)
batched_cameras = dataset.get_batched_cameras(num_camera_to_load)
batched_strategies, gpuid2tasks = start_strategy_final(
batched_cameras, strategy_history
)
load_camera_from_cpu_to_all_gpu_for_eval(
batched_cameras, batched_strategies, gpuid2tasks
)
batched_screenspace_pkg = distributed_preprocess3dgs_and_all2all_final(
batched_cameras,
gaussians,
pipeline,
background,
batched_strategies=batched_strategies,
mode="test",
)
batched_image, _ = render_final(batched_screenspace_pkg, batched_strategies)
for camera_id, (image, gt_camera) in enumerate(
zip(batched_image, batched_cameras)
):
actual_idx = idx + camera_id
if args.sample_freq != -1 and actual_idx % args.sample_freq != 0:
continue
if generated_cnt == args.generate_num:
break
if os.path.exists(
os.path.join(render_path, "{0:05d}".format(actual_idx) + ".png")
):
continue
if args.l != -1 and args.r != -1:
if actual_idx < args.l or actual_idx >= args.r:
continue
generated_cnt += 1
if (
image is None or len(image.shape) == 0
): # The image is not rendered locally.
image = torch.zeros(
gt_camera.original_image.shape, device="cuda", dtype=torch.float32
)
if utils.DEFAULT_GROUP.size() > 1:
torch.distributed.all_reduce(
image, op=dist.ReduceOp.SUM, group=utils.DEFAULT_GROUP
)
image = torch.clamp(image, 0.0, 1.0)
gt_image = torch.clamp(gt_camera.original_image / 255.0, 0.0, 1.0)
if utils.GLOBAL_RANK == 0:
torchvision.utils.save_image(
image,
os.path.join(render_path, "{0:05d}".format(actual_idx) + ".png"),
)
torchvision.utils.save_image(
gt_image,
os.path.join(gts_path, "{0:05d}".format(actual_idx) + ".png"),
)
gt_camera.original_image = None
if generated_cnt == args.generate_num:
break
def render_sets(
dataset: ModelParams,
iteration: int,
pipeline: PipelineParams,
skip_train: bool,
skip_test: bool,
):
with torch.no_grad():
args = utils.get_args()
gaussians = GaussianModel(dataset.sh_degree)
scene = Scene(args, 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")
if not skip_train:
render_set(
dataset.model_path,
"train",
scene.loaded_iter,
scene.getTrainCameras(),
gaussians,
pipeline,
background,
)
if not skip_test:
render_set(
dataset.model_path,
"test",
scene.loaded_iter,
scene.getTestCameras(),
gaussians,
pipeline,
background,
)
if __name__ == "__main__":
# Set up command line argument parser
parser = ArgumentParser(description="Training script parameters")
ap = AuxiliaryParams(parser)
lp = ModelParams(parser, sentinel=True)
op = OptimizationParams(parser)
pp = PipelineParams(parser)
dist_p = DistributionParams(parser)
bench_p = BenchmarkParams(parser)
debug_p = DebugParams(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("--generate_num", default=-1, type=int)
parser.add_argument("--sample_freq", default=-1, type=int)
parser.add_argument("--distributed_load", action="store_true") # TODO: delete this.
parser.add_argument("--l", default=-1, type=int)
parser.add_argument("--r", default=-1, type=int)
args = get_combined_args(parser)
print("Rendering " + args.model_path)
init_distributed(args)
# This script only supports single-gpu rendering.
# I need to put the flags here because the render() function need it.
# However, disable them during render.py because they are only needed during training.
log_file = open(
args.model_path
+ f"/render_ws={utils.DEFAULT_GROUP.size()}_rk_{utils.DEFAULT_GROUP.rank()}.log",
"w",
)
set_log_file(log_file)
## Prepare arguments.
# Check arguments
init_args(args)
if args.skip_train:
args.num_train_cameras = 0
if args.skip_test:
args.num_test_cameras = 0
# Set up global args
set_args(args)
print_all_args(args, log_file)
if utils.WORLD_SIZE > 1:
torch.distributed.barrier(group=utils.DEFAULT_GROUP)
# Initialize system state (RNG)
safe_state(args.quiet)
render_sets(
lp.extract(args),
args.iteration,
pp.extract(args),
args.skip_train,
args.skip_test,
)