Uh oh!
There was an error while loading. Please reload this page.
forked from RuijieZhu94/ObjectGS
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender_object.py
More file actions
Latest commit
executable file
·190 lines (154 loc) · 7.91 KB
/
Copy pathrender_object.py
File metadata and controls
executable file
·190 lines (154 loc) · 7.91 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
189
190
#
# 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
#
importos
importsys
importimageio
importyaml
fromosimportmakedirs
importtorch
importnumpyasnp
importsubprocess
cmd='nvidia-smi -q -d Memory |grep -A4 GPU|grep Used'
result=subprocess.run(cmd, shell=True, stdout=subprocess.PIPE).stdout.decode().split('\n')
os.environ['CUDA_VISIBLE_DEVICES']=str(np.argmin([int(x.split()[2]) forxinresult[:-1]]))
os.system('echo $CUDA_VISIBLE_DEVICES')
fromsceneimportScene
importjson
importtime
importtorchvision
fromtqdmimporttqdm
fromutils.general_utilsimportsafe_state, parse_cfg, visualize_depth, visualize_normal
fromutils.image_utilsimportsave_rgba
fromargparseimportArgumentParser
defrender_set(model_path, name, iteration, views, gaussians, pipe, background, query_label_id):
vis_normal=False
vis_depth=False
ifgaussians.gs_attr=="2D":
vis_normal=True
vis_depth=True
render_path=os.path.join(model_path, name, "id_{}".format(query_label_id), "renders")
error_path=os.path.join(model_path, name, "id_{}".format(query_label_id), "errors")
gts_path=os.path.join(model_path, name, "id_{}".format(query_label_id), "gt")
makedirs(render_path, exist_ok=True)
makedirs(error_path, exist_ok=True)
makedirs(gts_path, exist_ok=True)
ifvis_normal:
normal_path=os.path.join(model_path, name, "id_{}".format(query_label_id), "normal")
makedirs(normal_path, exist_ok=True)
ifvis_depth:
depth_path=os.path.join(model_path, name, "id_{}".format(query_label_id), "depth")
makedirs(depth_path, exist_ok=True)
# specify label id
object_mask= (gaussians.label_ids.squeeze() ==query_label_id)
modules=__import__('gaussian_renderer')
t_list= []
visible_count_list= []
per_view_dict= {}
foridx, viewinenumerate(tqdm(views, desc="rendering progress")):
# if idx > 30:
# continue
# visible mask
ifgaussians.explicit_gs:
gaussians.set_gs_mask(view.camera_center, view.resolution_scale)
visible_mask=gaussians._gs_mask
else:
gaussians.set_anchor_mask(view.camera_center, view.resolution_scale)
fromgaussian_renderer.renderimportprefilter_voxel
visible_mask=prefilter_voxel(view, gaussians).squeeze() ifpipe.add_prefilterelsegaussians._anchor_mask
# render function
torch.cuda.synchronize();t_start=time.time()
render_pkg=getattr(modules, 'render')(view, gaussians, pipe, background, visible_mask=visible_mask, object_mask=object_mask)
torch.cuda.synchronize();t_end=time.time()
t_list.append(t_end-t_start)
# renders
rendering=torch.clamp(render_pkg["render"], 0.0, 1.0)
visible_count=render_pkg["visibility_filter"].sum()
# gts
gt=view.original_image.cuda()
alpha_mask=view.alpha_mask.cuda()
# alpha_mask = rendering.any(dim=0, keepdim=True)
rendering=torch.cat([rendering, alpha_mask], dim=0)
gt=torch.cat([gt, alpha_mask], dim=0)
# error maps
ifgt.device!=rendering.device:
rendering=rendering.to(gt.device)
# errormap = (rendering - gt).abs()
# if vis_normal == True:
# normal_map = render_pkg['render_normals'][0]
# vis_normal_map = visualize_normal(normal_map, view)
# vis_alpha_mask = ((alpha_mask * 255).byte()).permute(1, 2, 0).cpu().numpy()
# vis_normal_map = np.concatenate((vis_normal_map,vis_alpha_mask),axis=2)
# imageio.imwrite(os.path.join(normal_path, '{0:05d}'.format(idx) + ".png"), vis_normal_map)
# if vis_depth == True:
# depth_map = render_pkg["render_depth"]
# vis_depth_map = visualize_depth(depth_map)
# vis_depth_map = torch.concat([vis_depth_map,alpha_mask],dim=0)
# torchvision.utils.save_image(vis_depth_map, os.path.join(depth_path, '{0:05d}'.format(idx) + ".png"))
save_rgba(rendering, os.path.join(render_path, '{0:05d}'.format(idx) +".png"))
# save_rgba(errormap, os.path.join(error_path, '{0:05d}'.format(idx) + ".png"))
# save_rgba(gt, os.path.join(gts_path, '{0:05d}'.format(idx) + ".png"))
visible_count_list.append(visible_count)
per_view_dict['{0:05d}'.format(idx) +".png"] =visible_count.item()
# print((len(aerial_t_list)-5+len(street_t_list)-5)/( sum(street_t_list[5:]) + sum(aerial_t_list[5:])))
defrender_sets(dataset, opt, pipe, iteration, skip_train, skip_test, ape_code, explicit, query_label_id):
withtorch.no_grad():
ifpipe.no_prefilter_step>0:
pipe.add_prefilter=False
else:
pipe.add_prefilter=True
modules=__import__('scene')
model_config=dataset.model_config
model_config['kwargs']['ape_code'] =ape_code
gaussians=getattr(modules, model_config['name'])(**model_config['kwargs'])
scene=Scene(dataset, gaussians, load_iteration=iteration, shuffle=False, explicit=explicit)
gaussians.eval()
ifnotos.path.exists(dataset.model_path):
os.makedirs(dataset.model_path)
scene.background=torch.ones_like(scene.background).cuda()
ifquery_label_id==-1:
forlabelingaussians.label_ids.unique():
ifnotskip_train:
render_set(dataset.model_path, "train", scene.loaded_iter, scene.getTrainCameras(), gaussians, pipe, scene.background, label)
ifnotskip_test:
render_set(dataset.model_path, "test", scene.loaded_iter, scene.getTestCameras(), gaussians, pipe, scene.background, label)
else:
ifnotskip_train:
render_set(dataset.model_path, "train", scene.loaded_iter, scene.getTrainCameras(), gaussians, pipe, scene.background, query_label_id)
ifnotskip_test:
render_set(dataset.model_path, "test", scene.loaded_iter, scene.getTestCameras(), gaussians, pipe, scene.background, query_label_id)
if__name__=="__main__":
# Set up command line argument parser
parser=ArgumentParser(description="Testing script parameters")
parser.add_argument('-m', '--model_path', type=str, required=True)
parser.add_argument("--iteration", default=-1, type=int)
parser.add_argument("--ape", default=-1, type=int)
parser.add_argument("--query_label_id", default=-1, type=int)
parser.add_argument("--scene_name", default=None, 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("--explicit", action="store_true")
args=parser.parse_args(sys.argv[1:])
args.scene_name=args.model_path.split('/')[-2]
withopen(os.path.join(args.model_path, "config.yaml")) asf:
cfg=yaml.load(f, Loader=yaml.FullLoader)
try:
cfg["model_params"]["exp_name"] =os.path.join(cfg["model_params"]["exp_name"], args.scene_name)
cfg["model_params"]["source_path"] =os.path.join(cfg["model_params"]["source_path"], args.scene_name)
except:
print("OverrideError: Cannot override 'exp_name' and 'source_path' in 'model_params'. Exiting.")
sys.exit(1)
lp, op, pp=parse_cfg(cfg)
lp.model_path=args.model_path
print("Rendering "+args.model_path)
# Initialize system state (RNG)
safe_state(args.quiet)
render_sets(lp, op, pp, args.iteration, args.skip_train, args.skip_test, args.ape, args.explicit, args.query_label_id)