-
Notifications
You must be signed in to change notification settings - Fork 62
/
eval.py
264 lines (239 loc) · 9.18 KB
/
eval.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
from typing import List, Optional
from omegaconf import OmegaConf
import os
import time
import json
import wandb
import logging
import argparse
import torch
from datasets.driving_dataset import DrivingDataset
from utils.misc import import_str
from models.trainers import BasicTrainer
from models.video_utils import (
render_images,
save_videos,
render_novel_views
)
logger = logging.getLogger()
current_time = time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime())
@torch.no_grad()
def do_evaluation(
step: int = 0,
cfg: OmegaConf = None,
trainer: BasicTrainer = None,
dataset: DrivingDataset = None,
args: argparse.Namespace = None,
render_keys: Optional[List[str]] = None,
post_fix: str = "",
log_metrics: bool = True
):
trainer.set_eval()
logger.info("Evaluating Pixels...")
if dataset.test_image_set is not None and cfg.render.render_test:
logger.info("Evaluating Test Set Pixels...")
render_results = render_images(
trainer=trainer,
dataset=dataset.test_image_set,
compute_metrics=True,
compute_error_map=cfg.render.vis_error,
)
if log_metrics:
eval_dict = {}
for k, v in render_results.items():
if k in [
"psnr",
"ssim",
"lpips",
"occupied_psnr",
"occupied_ssim",
"masked_psnr",
"masked_ssim",
"human_psnr",
"human_ssim",
"vehicle_psnr",
"vehicle_ssim",
]:
eval_dict[f"image_metrics/test/{k}"] = v
if args.enable_wandb:
wandb.log(eval_dict)
test_metrics_file = f"{cfg.log_dir}/metrics{post_fix}/images_test_{current_time}.json"
with open(test_metrics_file, "w") as f:
json.dump(eval_dict, f)
logger.info(f"Image evaluation metrics saved to {test_metrics_file}")
if args.render_video_postfix is None:
video_output_pth = f"{cfg.log_dir}/videos{post_fix}/test_set_{step}.mp4"
else:
video_output_pth = (
f"{cfg.log_dir}/videos{post_fix}/test_set_{step}_{args.render_video_postfix}.mp4"
)
vis_frame_dict = save_videos(
render_results,
video_output_pth,
layout=dataset.layout,
num_timestamps=dataset.num_test_timesteps,
keys=render_keys,
num_cams=dataset.pixel_source.num_cams,
save_seperate_video=cfg.logging.save_seperate_video,
fps=2,
verbose=True,
save_images=False,
)
if args.enable_wandb:
for k, v in vis_frame_dict.items():
wandb.log({"image_rendering/test/" + k: wandb.Image(v)})
del render_results, vis_frame_dict
torch.cuda.empty_cache()
if cfg.render.render_full:
logger.info("Evaluating Full Set...")
render_results = render_images(
trainer=trainer,
dataset=dataset.full_image_set,
compute_metrics=True,
compute_error_map=cfg.render.vis_error,
)
if log_metrics:
eval_dict = {}
for k, v in render_results.items():
if k in [
"psnr",
"ssim",
"lpips",
"occupied_psnr",
"occupied_ssim",
"masked_psnr",
"masked_ssim",
"human_psnr",
"human_ssim",
"vehicle_psnr",
"vehicle_ssim",
]:
eval_dict[f"image_metrics/full/{k}"] = v
if args.enable_wandb:
wandb.log(eval_dict)
full_metrics_file = f"{cfg.log_dir}/metrics{post_fix}/images_full_{current_time}.json"
with open(full_metrics_file, "w") as f:
json.dump(eval_dict, f)
logger.info(f"Image evaluation metrics saved to {full_metrics_file}")
if args.render_video_postfix is None:
video_output_pth = f"{cfg.log_dir}/videos{post_fix}/full_set_{step}.mp4"
else:
video_output_pth = (
f"{cfg.log_dir}/videos{post_fix}/full_set_{step}_{args.render_video_postfix}.mp4"
)
vis_frame_dict = save_videos(
render_results,
video_output_pth,
layout=dataset.layout,
num_timestamps=dataset.num_img_timesteps,
keys=render_keys,
num_cams=dataset.pixel_source.num_cams,
save_seperate_video=cfg.logging.save_seperate_video,
fps=cfg.render.fps,
verbose=True,
)
if args.enable_wandb:
for k, v in vis_frame_dict.items():
wandb.log({"image_rendering/full/" + k: wandb.Image(v)})
del render_results, vis_frame_dict
torch.cuda.empty_cache()
render_novel_cfg = cfg.render.get("render_novel", None)
if render_novel_cfg is not None:
logger.info("Rendering novel views...")
render_traj = dataset.get_novel_render_traj(
traj_types=render_novel_cfg.traj_types,
target_frames=render_novel_cfg.get("frames", dataset.frame_num),
)
video_output_dir = f"{cfg.log_dir}/videos{post_fix}/novel_{step}"
if not os.path.exists(video_output_dir):
os.makedirs(video_output_dir)
for traj_type, traj in render_traj.items():
# Prepare rendering data
render_data = dataset.prepare_novel_view_render_data(traj)
# Render and save video
save_path = os.path.join(video_output_dir, f"{traj_type}.mp4")
render_novel_views(
trainer, render_data, save_path,
fps=render_novel_cfg.get("fps", cfg.render.fps)
)
logger.info(f"Saved novel view video for trajectory type: {traj_type} to {save_path}")
def main(args):
log_dir = os.path.dirname(args.resume_from)
cfg = OmegaConf.load(os.path.join(log_dir, "config.yaml"))
cfg = OmegaConf.merge(cfg, OmegaConf.from_cli(args.opts))
args.enable_wandb = False
for folder in ["videos_eval", "metrics_eval"]:
os.makedirs(os.path.join(log_dir, folder), exist_ok=True)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# build dataset
dataset = DrivingDataset(data_cfg=cfg.data)
# setup trainer
trainer = import_str(cfg.trainer.type)(
**cfg.trainer,
num_timesteps=dataset.num_img_timesteps,
model_config=cfg.model,
num_train_images=len(dataset.train_image_set),
num_full_images=len(dataset.full_image_set),
test_set_indices=dataset.test_timesteps,
scene_aabb=dataset.get_aabb().reshape(2, 3),
device=device
)
# Resume from checkpoint
trainer.resume_from_checkpoint(
ckpt_path=args.resume_from,
load_only_model=True
)
logger.info(
f"Resuming training from {args.resume_from}, starting at step {trainer.step}"
)
if args.enable_viewer:
# a simple viewer for background visualization
trainer.init_viewer(port=args.viewer_port)
# define render keys
render_keys = [
"gt_rgbs",
"rgbs",
"Background_rgbs",
"RigidNodes_rgbs",
"DeformableNodes_rgbs",
"SMPLNodes_rgbs",
# "depths",
# "Background_depths",
# "RigidNodes_depths",
# "DeformableNodes_depths",
# "SMPLNodes_depths",
# "mask"
]
if cfg.render.vis_lidar:
render_keys.insert(0, "lidar_on_images")
if cfg.render.vis_sky:
render_keys += ["rgb_sky_blend", "rgb_sky"]
if cfg.render.vis_error:
render_keys.insert(render_keys.index("rgbs") + 1, "rgb_error_maps")
if args.save_catted_videos:
cfg.logging.save_seperate_video = False
do_evaluation(
step=trainer.step,
cfg=cfg,
trainer=trainer,
dataset=dataset,
render_keys=render_keys,
args=args,
post_fix="_eval"
)
if args.enable_viewer:
print("Viewer running... Ctrl+C to exit.")
time.sleep(1000000)
if __name__ == "__main__":
parser = argparse.ArgumentParser("Train Gaussian Splatting for a single scene")
# eval
parser.add_argument("--resume_from", default=None, help="path to checkpoint to resume from", type=str, required=True)
parser.add_argument("--render_video_postfix", type=str, default=None, help="an optional postfix for video")
parser.add_argument("--save_catted_videos", type=bool, default=False, help="visualize lidar on image")
# viewer
parser.add_argument("--enable_viewer", action="store_true", help="enable viewer")
parser.add_argument("--viewer_port", type=int, default=8080, help="viewer port")
# misc
parser.add_argument("opts", help="Modify config options using the command-line", default=None, nargs=argparse.REMAINDER)
args = parser.parse_args()
main(args)