-
Notifications
You must be signed in to change notification settings - Fork 2
/
demo.py
85 lines (73 loc) · 2.44 KB
/
demo.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
import argparse
import os
import os.path as osp
import cv2
import torch
from fires.configs.default import get_cfg_defaults
from fires.data.omnidataloader import SpDRDFMapper, load_taskonomy_pkl
from fires.model.spnet import load_model
from fires.utils.geometry_utils import save_scene_as_glb, get_point_image_colors
def parse_args():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument("--cfg-path", type=str, help="main config path")
parser.add_argument(
"--ckpt-path", type=str, default="", help="path to the checkpoint"
)
parser.add_argument("--output-dir", type=str, default="", help="output folder")
return parser.parse_args()
if __name__ == "__main__":
args = parse_args()
save_folder = args.output_dir
os.makedirs(save_folder, exist_ok=True)
cfg = get_cfg_defaults()
cfg.merge_from_file(args.cfg_path)
model_path = args.ckpt_path
device = "cuda"
model = load_model(model_path, cfg, device)
model.eval()
dataset_dicts = load_taskonomy_pkl(cfg.DATASETS.TEST[0])
dataloader = SpDRDFMapper(cfg, is_train=False)
dataset_dict = dataloader(dataset_dicts[0])
with torch.inference_mode():
predictions = model([dataset_dict])[0]
pcd, color = get_point_image_colors(
predictions["pcd_world"],
predictions["rayid"],
predictions["visibility"],
dataset_dict["camera_torch"],
dataset_dict["rgbs"],
dataset_dict["num_ray_per_img"],
device,
)
pred = {
"pcd": pcd,
"color": color,
}
for i in range(len(dataset_dict["rgbs"])):
cv2.imwrite(
osp.join(save_folder, f"rgb_{i}.jpg"),
dataset_dict["rgbs"][i]
.detach()
.cpu()
.numpy()
.transpose(1, 2, 0)[..., ::-1],
)
# save individual glb
for i in range(len(dataset_dict["rgbs"])):
save_scene_as_glb(
pred["pcd"][i == predictions["imgid"]],
pred["color"][i == predictions["imgid"]],
dataset_dict["camera_torch"][i],
osp.join(save_folder, f"pred_{i}.glb"),
highres=cfg.DATALOADER.HIGH_RES_OUTPUT,
camid=i,
)
save_scene_as_glb(
pred["pcd"],
pred["color"],
dataset_dict["camera_torch"],
osp.join(save_folder, "pred.glb"),
highres=cfg.DATALOADER.HIGH_RES_OUTPUT,
)