-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathrender.py
More file actions
188 lines (154 loc) · 8.38 KB
/
render.py
File metadata and controls
188 lines (154 loc) · 8.38 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
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
#
# 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 time
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.image_utils import psnr
import numpy as np
import cv2
def render_set(model_path, mask_tensor, name, iteration, views, gaussians, pipeline, background, train_test_exp, near_threshold=0.2, asso_mode=0):
max_allocated_memory_before = torch.cuda.max_memory_allocated()
print(f"Max Allocated Memory Before Rendering: {max_allocated_memory_before} bytes")
torch.cuda.empty_cache()
render_path = os.path.join(model_path, name, "ours_{}".format(iteration), "renders")
gts_path = os.path.join(model_path, name, "ours_{}".format(iteration), "gt")
# asso_path = os.path.join(model_path, name, "ours_{}".format(iteration), "asso")
makedirs(render_path, exist_ok=True)
makedirs(gts_path, exist_ok=True)
# makedirs(asso_path, exist_ok=True)
render_times_overall = []
render_times_prep = []
render_times_dup = []
render_times_sort = []
render_times_render = []
render_times = []
image_save_times = []
range_lens = []
for idx, view in enumerate(tqdm(views, desc="Rendering progress")):
render_start = time.time()
rendering_pkg = render(view, gaussians, pipeline, background, use_trained_exp=train_test_exp, near_threshold=near_threshold, asso_mode=asso_mode)
render_end = time.time()
rendering = rendering_pkg["render"]
runtime = rendering_pkg["time"]
range_len = rendering_pkg["range_len"] # ranges for each tile
range_lens.append(range_len)
render_times.append((render_end - render_start)*1000)
render_times_overall.append(runtime[0])
render_times_prep.append(runtime[1])
render_times_dup.append(runtime[2])
render_times_sort.append(runtime[3])
render_times_render.append(runtime[4])
image_save_start = time.time()
gt = view.original_image[0:3, :, :]
if mask_tensor is not None:
rendering[mask_tensor == 0] = 0.0
torchvision.utils.save_image(rendering, os.path.join(render_path, '{0:05d}'.format(idx) + ".png"))
torchvision.utils.save_image(gt, os.path.join(gts_path, '{0:05d}'.format(idx) + ".png"))
# grid_size = 16
# grid_w = int((rendering.shape[2] + grid_size - 1) / grid_size)
# grid_h = int((rendering.shape[1] + grid_size - 1) / grid_size)
# torchvision.utils.save_image((range_len.reshape(grid_h, grid_w)[:, :, None].float() / range_len.max()).permute(2,0,1), os.path.join(asso_path, '{0:05d}'.format(idx) + ".png"))
image_save_end = time.time()
image_save_times.append((image_save_end - image_save_start)*1000)
means = torch.tensor(render_times).mean()
maxs = torch.tensor(render_times).max()
FPS = 1.0 / (means / 1000.0)
print(f" AVG_Render_Time : {means} ms")
print(f" MAX_Render_Time : {maxs} ms")
print(f" FPS: {FPS}")
max_allocated_memory_after = torch.cuda.max_memory_allocated()
print(f"Max Allocated Memory After Rendering: {max_allocated_memory_after} bytes")
# Print memory usage statistics
print(f"Memory Usage: {max_allocated_memory_after - max_allocated_memory_before} bytes")
range_lens = torch.cat(range_lens, dim=0)
print(f"Associated Gaus num of each tile\n: mean {range_lens.float().mean().item()} Gaussians, std {range_lens.float().std().item()} Gaussians, min {range_lens.float().min().item()} Gaussians, max {range_lens.float().max().item()} Gaussians")
means = torch.tensor(render_times_overall).mean()
FPS = 1.0 / (means / 1000.0)
print(f" AVG_OVERALL_Time : {means} ms")
print(f" AVG_OVERALL_Time FPS: {FPS}")
means = torch.tensor(render_times_prep).mean()
print(f" AVG_PREP_Time : {means} ms")
means = torch.tensor(render_times_dup).mean()
print(f" AVG_DUP_Time : {means} ms")
means = torch.tensor(render_times_sort).mean()
print(f" AVG_SORT_Time : {means} ms")
means = torch.tensor(render_times_render).mean()
print(f" AVG_RenFunc_Time : {means} ms")
def render_sets(dataset : ModelParams, iteration : int, pipeline : PipelineParams, skip_train : bool, skip_test : bool, \
fov_mod: float, sample_step: float, render_model: str, \
focal_scaling: float, distortion_scaling: float, mirror_shift: float, raymap_path: str, mask_path: str, near_threshold: float = 0.2, asso_mode: int = 0):
with torch.no_grad():
gaussians = GaussianModel(dataset.sh_degree)
dataset.fov_mod = fov_mod
dataset.sample_step = sample_step
dataset.raymap = None
if raymap_path is not None and os.path.exists(raymap_path):
dataset.raymap = np.load(raymap_path)
dataset.render_model = render_model
dataset.focal_scaling = focal_scaling
dataset.distortion_scaling = distortion_scaling
dataset.mirror_shift = mirror_shift
scene = Scene(dataset, gaussians, load_iteration=iteration, shuffle=False, skip_train_cameras=skip_train, skip_test_cameras=skip_test)
valid_mask = None
if mask_path is not None:
valid_mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
valid_mask = np.repeat(valid_mask[None, ...], 3, axis=0)
valid_mask = torch.tensor(valid_mask)
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, valid_mask, "train", scene.loaded_iter, scene.getTrainCameras(), gaussians, pipeline, background, dataset.train_test_exp, near_threshold, asso_mode)
if not skip_test:
dataset.train_test_exp = False
render_set(dataset.model_path, valid_mask, "test", scene.loaded_iter, scene.getTestCameras(), gaussians, pipeline, background, dataset.train_test_exp, near_threshold, asso_mode)
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("--quiet", action="store_true")
parser.add_argument("--render_model", type=str, default = 'BEAP')
parser.add_argument("--focal_scaling", type=float, default = 1.0)
parser.add_argument("--distortion_scaling", type=float, default = 1.0)
parser.add_argument("--mirror_shift", type=float, default = 0.0)
parser.add_argument("--raymap_path", type=str, default = None)
parser.add_argument("--mask_path", type=str, default = None)
parser.add_argument("--sample_step", type=float, default = None)
parser.add_argument("--fov_mod", type=float, default = None)
parser.add_argument("--near_threshold", type=float, default = 0.2)
parser.add_argument("--asso_mode", type=int, default = 0,
help="Association mode: 0=PBF (default), 1=EWA, 2=UT")
args = get_combined_args(parser)
for k in ["fov_mod", "sample_step", "distortion_scaling", "focal_scaling", "mirror_shift", "raymap_path", "mask_path"]:
if not hasattr(args, k):
setattr(args, k, None)
if not hasattr(args, "near_threshold"):
setattr(args, "near_threshold", 0.2)
if not hasattr(args, "asso_mode"):
setattr(args, "asso_mode", 0)
print("Rendering " + args.model_path)
# Initialize system state (RNG)
safe_state(args.quiet)
render_sets(model.extract(args), args.iteration, pipeline.extract(args), args.skip_train, args.skip_test, \
args.fov_mod, args.sample_step, args.render_model, \
args.focal_scaling, args.distortion_scaling, args.mirror_shift, args.raymap_path, args.mask_path, args.near_threshold, args.asso_mode)