-
Notifications
You must be signed in to change notification settings - Fork 1
/
render.py
189 lines (160 loc) · 7.71 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
#
# 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 csv
import os
from argparse import ArgumentParser
from os import makedirs
from time import gmtime, strftime
import torch
import torchvision
from tqdm import tqdm
from arguments import ModelParams, PipelineParams, get_combined_args_render
from gaussian_renderer import GaussianModel, render
from lpipsPyTorch import lpips
from scene import Scene
from utils.general_utils import safe_state
from utils.image_utils import psnr
from utils.loss_utils import ssim
from utils.system_utils import MAIN_DIR
from utils.render_utils import generate_path, create_color_videos
def render_video(model_path, name, scene, pipeline, gaussians, background, render_args):
n_fames = 480
cam_traj = generate_path(scene.getTrainCameras(), n_frames=n_fames)
makedirs(os.path.join(model_path, name, render_args.save_dir_name), exist_ok=True)
render_path = os.path.join(model_path, name, render_args.save_dir_name, "renders")
makedirs(render_path, exist_ok=True)
for idx, cam in enumerate(tqdm(cam_traj, desc="Rendering progress")):
render_results = render(cam, gaussians, pipeline, background, debug=render_args.debug, clamp_color=True)
rendering = render_results["render"]
torchvision.utils.save_image(rendering, os.path.join(render_path, '{0:05d}'.format(idx) + ".png"))
create_color_videos(base_dir=render_args.video_dir,
input_dir=render_path,
out_name='test1',
num_frames=n_fames)
def render_set(model_path, name, iteration, views, gaussians, pipeline, background, render_args):
makedirs(os.path.join(model_path, name, render_args.save_dir_name), exist_ok=True)
render_path = os.path.join(model_path, name, render_args.save_dir_name, "renders")
gts_path = os.path.join(model_path, name, render_args.save_dir_name, "gt")
makedirs(render_path, exist_ok=True)
makedirs(gts_path, exist_ok=True)
ssims = []
psnrs = []
lpipss = []
for idx, view in enumerate(tqdm(views, desc="Rendering progress")):
render_results = render(view, gaussians, pipeline, background, debug=render_args.debug, clamp_color=True)
rendering = render_results["render"]
gt = view.original_image[0:3, :, :]
# print('rendering.shape', rendering.shape)
# print('gt.shape', gt.shape)
# images = torch.cat((images, rendering.unsqueeze(0)), dim=0)
# gts = torch.cat((gts, gt.unsqueeze(0)), dim=0)
if not render_args.quick:
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"))
psnrs.append(psnr(rendering, gt))
if not render_args.quick:
ssims.append(ssim(rendering, gt))
# print(psnrs)
lpipss.append(lpips(rendering, gt, net_type='vgg'))
ssim_val = torch.tensor(ssims).mean()
psnr_val = torch.stack(psnrs).mean()
lpips_val = torch.tensor(lpipss).mean()
print('psnr, ssim, lpips')
print("{:>12.7f}".format(psnr_val, ".5")+ '\t' + "{:>12.7f}".format(ssim_val, ".5")+ '\t' + "{:>12.7f}".format(lpips_val, ".5"))
print("")
return psnr_val.item(), ssim_val.item(), lpips_val.item()
def render_sets(dataset : ModelParams, iteration : int, pipeline : PipelineParams, render_args):
with torch.no_grad():
gaussians = GaussianModel(dataset.sh_degree)
scene = Scene(dataset, gaussians, load_iteration=iteration,
shuffle=False,
given_ply_path=render_args.given_ply_path,
dec_cov=render_args.dec_cov,
dec_euler=render_args.dec_euler,
dec_npz=render_args.dec_npz)
bg_color = [1,1,1] if dataset.white_background else [0, 0, 0]
# print('bg_color', bg_color)
background = torch.tensor(bg_color, dtype=torch.float32, device="cuda")
metric_vals = []
if render_args.render_video:
render_video(dataset.model_path, 'train', scene, pipeline, gaussians, background, render_args)
if not render_args.skip_train:
psnrv, ssimv, lpipsv = render_set(dataset.model_path, "train", scene.loaded_iter, scene.getTrainCameras(), gaussians, pipeline, background, render_args)
metric_vals.extend([psnrv, ssimv, lpipsv])
else:
metric_vals.extend([0, 0, 0])
if not render_args.skip_test:
psnrv, ssimv, lpipsv = render_set(dataset.model_path, "test", scene.loaded_iter, scene.getTestCameras(), gaussians, pipeline, background, render_args)
metric_vals.extend([psnrv, ssimv, lpipsv])
else:
metric_vals.extend([0, 0, 0])
return metric_vals
if __name__ == "__main__":
# Set up command line argument parser
parser = ArgumentParser(description="Testing script parameters")
model = ModelParams(parser)
pipeline = PipelineParams(parser)
parser.add_argument("--iteration", default=-1, type=int)
parser.add_argument("--all_bits", action="store_true")
# parser.add_argument("--num_bits", default=32, type=int)
parser.add_argument("--given_ply_path", default='', type=str)
parser.add_argument("--save_dir_name", default='', type=str)
parser.add_argument("--scene_name", default='', type=str)
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("--save_imp", action="store_true")
parser.add_argument("--dec_cov", action="store_true")
parser.add_argument("--dec_euler", action="store_true")
parser.add_argument("--dec_npz", action="store_true")
parser.add_argument("--render_video", action="store_true")
parser.add_argument("--log_name", default='', type=str)
parser.add_argument("--video_dir", default='', type=str)
parser.add_argument("--quick", action="store_true")
# args = get_combined_args(parser)
args = get_combined_args_render(parser)
# args = parser.parse_args(sys.argv[1:])
print(args)
render_args = args
# render_args = parser.parse_args()
print("Rendering " + args.model_path)
# Initialize system state (RNG)
safe_state(args.quiet)
csv_path = os.path.join(f'{MAIN_DIR}/exp_data/csv', args.log_name + '.csv')
if not os.path.exists(f'{MAIN_DIR}/exp_data/csv'):
makedirs(f'{MAIN_DIR}/exp_data/csv')
if not os.path.exists(csv_path):
f = open(csv_path, 'a+')
wtr = csv.writer(f)
wtr.writerow(['name', 'train_p', 'train_s', 'train_l', 'test_p', 'test_s', 'test_l'])
f.close()
if not os.path.exists(f'{MAIN_DIR}/exp_data/txt'):
makedirs(f'{MAIN_DIR}/exp_data/txt')
print(model.extract(args))
metric_vals = render_sets(
model.extract(args),
args.iteration,
pipeline.extract(args),
render_args,)
# if args.render_video:
# render_video(
# model.extract(args),
# )
source_path = model.extract(args).source_path
source_name = source_path.split('/')[-1]
cur_time = strftime("%Y_%m_%d_%H_%M_%S", gmtime())
row = []
row.append(args.scene_name + '_' + args.save_dir_name)
f = open(csv_path, 'a+')
wtr = csv.writer(f)
row.extend(metric_vals)
wtr.writerow(row)
f.close()