-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbenchmark.py
More file actions
executable file
·384 lines (306 loc) · 11.9 KB
/
Copy pathbenchmark.py
File metadata and controls
executable file
·384 lines (306 loc) · 11.9 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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import gc
import os
import csv
import torch
import shutil
import argparse
from tqdm import tqdm
from pathlib import Path
from nav.pointnav_agent import PointnavAgent
from utils.frontier_utils import read_config_yaml
from utils.config_utils import ovon_config, hm3d_config, mp3d_config, DATA_PATH
import habitat
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
os.environ["MAGNUM_LOG"] = "quiet"
os.environ["HABITAT_SIM_LOG"] = "quiet"
def write_metrics(metrics, path="objnav_hm3d.csv"):
with open(path, mode="w", newline="") as csv_file:
fieldnames = metrics[0].keys()
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(metrics)
def get_args():
p = argparse.ArgumentParser()
p.add_argument("--eval_episodes", type=int, default=-1)
p.add_argument(
"--benchmark",
type=str,
required=True,
help="hm3d, mp3d, or OVON",
)
p.add_argument(
"--nickname",
type=str,
required=True,
help="Nickname for the benchmark run",
)
p.add_argument(
"--config",
type=str,
default="config/hm3d_navigation.yaml",
help="OpenFrontier configuration file",
)
p.add_argument(
"--max_steps",
type=int,
default=500,
help="Maximum number of navigation steps",
)
p.add_argument(
"--max_time", type=int, default=3600, help="Maximum navigation time in seconds"
)
p.add_argument(
"--vis_graph",
action="store_true",
default=False,
help="Visualize the topological graph",
)
p.add_argument(
"--save-images",
"--save_images",
dest="save_images",
action="store_true",
default=False,
help="Save intermediate navigation images during benchmark episodes",
)
p.add_argument(
"--unet_weight",
type=Path,
default=Path("model_weights/rgbd_11cls.pth"),
help="Path to UNet model weights",
)
p.add_argument(
"--log_level",
"-ll",
type=int,
default=20,
help="logging level (0=notset, 10=debug, 20=info...)",
)
p.add_argument(
"--split",
type=int,
default=(1, 1),
nargs=2,
help="Split number for evaluation in the format #subset_index #total_subsets",
)
p.add_argument(
"--output-path",
type=str,
required=True,
)
return p.parse_known_args()[0]
if __name__ == "__main__":
args = get_args()
# list all the scenes in objnav path
benchmark = args.benchmark.lower()
output_path = args.output_path.lower()
if benchmark == "ovon":
directory = Path(DATA_PATH + "ovon/val_unseen/content/")
elif benchmark == "hm3d":
directory = Path(DATA_PATH + f"objectnav_hm3d_v2/val/content/")
elif benchmark == "mp3d":
directory = Path(DATA_PATH + f"objectnav_mp3d_v1/val/content/")
else:
raise ValueError(f"Benchmark {benchmark} not recognized")
scenes = sorted([f.stem.split(".")[0] for f in directory.glob("*.json.gz")])
if len(scenes) == 0:
raise ValueError(f"No scenes found in {directory}")
# split the scenes based on args.split and select the corresponding subset
total_splits = args.split[1]
split_index = args.split[0] - 1
scenes = [
scene for i, scene in enumerate(scenes) if i % total_splits == split_index
]
scenes_data = {}
killed = False
exception = None
fn_config = read_config_yaml(args.config)
probabilities_source = (
fn_config.get("probabilities_source", "gemini-2.5").lower().split("-")[0]
)
detection_source = (
fn_config.get("detection_source", "gemini-2.5").lower().split("-")[0]
)
segmentation_source = (
fn_config.get("segmentation_source", "sam3").lower().split("-")[0]
)
benchmark_nickname = args.nickname
output_dir = Path(
f"{output_path}/{benchmark_nickname}_{segmentation_source}_{probabilities_source}_{detection_source}"
)
output_dir.mkdir(parents=True, exist_ok=True)
for scene in scenes:
if killed:
raise exception if exception is not None else KeyboardInterrupt()
killed = False
exception = None
print("Evaluating Scene: %s" % scene)
if args.benchmark.lower() == "hm3d":
habitat_config = hm3d_config(stage=scene, episodes=args.eval_episodes)
elif args.benchmark.lower() == "mp3d":
habitat_config = mp3d_config(stage=scene, episodes=args.eval_episodes)
elif args.benchmark.lower() == "ovon":
habitat_config = ovon_config(stage=scene, episodes=args.eval_episodes)
else:
raise ValueError("Benchmark not recognized: %s" % args.benchmark)
try:
habitat_env = habitat.Env(habitat_config)
except Exception as e:
print(e)
continue
if args.eval_episodes == -1:
num_episodes = habitat_env.number_of_episodes
else:
num_episodes = args.eval_episodes
# Inside output dir
scene_dir = Path.joinpath(output_dir, scene)
scene_dir.mkdir(parents=True, exist_ok=True)
metrics_dir = Path.joinpath(output_dir, "metrics/%s.csv" % scene)
metrics_dir.parent.mkdir(parents=True, exist_ok=True)
evaluation_metrics = []
# Load existing metrics
if metrics_dir.exists():
with open(metrics_dir, mode="r") as csv_file:
reader = csv.DictReader(csv_file)
for row in reader:
evaluation_metrics.append(
{
"episode": int(row["episode"]),
"success": float(row["success"]),
"spl": float(row["spl"]),
"distance_to_goal": float(row["distance_to_goal"]),
"object_goal": row["object_goal"],
"termination_reason": row["termination_reason"],
}
)
# check how many episodes have been evaluated for this scene
evaluated_episodes = len(evaluation_metrics)
if evaluated_episodes >= num_episodes:
print(
"All %d episodes for scene %s have already been evaluated. Skipping..."
% (num_episodes, scene)
)
habitat_env.close()
continue
for i in tqdm(range(num_episodes)):
if killed:
break
habitat_env.reset()
target = habitat_env.current_episode.object_category
target_name = target.replace(" ", "_")
folder_name = f"episode-{i}-{target_name}"
path = scene_dir / folder_name
make_dir = Path(path)
if any(m["episode"] == i for m in evaluation_metrics):
print(
"Episode %d for scene %s has already been evaluated. Skipping..."
% (i, scene)
)
continue
episode = habitat_env.current_episode
habitat_agent = PointnavAgent(
habitat_env,
args,
save_dir=path,
openfrontier_config=fn_config,
habitat_config=habitat_config,
scene=scene,
)
habitat_agent.setup_system()
habitat_agent.initialize()
reason = "unknown"
try:
killed = False
exception = None
while (
not habitat_env.episode_over
and habitat_agent.navigation_steps <= 500
):
navigate, reason = habitat_agent.navigation(
save_images=args.save_images
)
habitat_agent.update_video()
if not navigate:
habitat_env.step(0)
except Exception as e:
exception = e
# If keyboard interrupt, stop evaluation
if isinstance(e, KeyboardInterrupt):
killed = True
print("Keyboard Interrupt. Stopping evaluation...")
reason = "keyboard_interrupt"
elif "exhausted" in str(e).lower():
killed = True
print("All API keys exhausted. Stopping evaluation...")
reason = "api_keys_exhausted"
elif "banned" in str(e).lower():
killed = True
print("Account banned. Stopping evaluation...")
reason = "account_banned"
else:
print("Exception during evaluation of episode %d: %s" % (i, str(e)))
reason = "exception_occurred"
with open(path / "exception.txt", "w") as f:
# Write the full exception traceback to the file
import traceback
traceback.print_exc(file=f)
finally:
try:
metrics = habitat_env.get_metrics()
if int(metrics["success"]) == 0 and reason == "object_found":
reason = "false_positive"
episode_metrics = {
"episode": i,
"success": metrics["success"],
"spl": metrics["spl"],
"distance_to_goal": metrics["distance_to_goal"],
"object_goal": episode.object_category,
"termination_reason": reason,
}
episode_metrics_dir = Path.joinpath(path, "metrics.csv")
write_metrics(
[episode_metrics],
episode_metrics_dir,
)
habitat_agent.save_trajectory(path)
# subsitute if already existing
evaluation_metrics = [
m for m in evaluation_metrics if m["episode"] != i
]
evaluation_metrics.append(episode_metrics)
write_metrics(evaluation_metrics, path=metrics_dir)
if metrics["success"]:
# move to success folder
success_path = scene_dir / f"success"
success_path.mkdir(parents=True, exist_ok=True)
new_folder = success_path / folder_name
# if folder exists, remove
if new_folder.exists():
shutil.rmtree(new_folder)
os.rename(path, new_folder)
else:
# move to failure folder
failure_path = scene_dir / f"failure"
failure_path.mkdir(parents=True, exist_ok=True)
folder_name = folder_name + f"-{reason}/"
new_folder = failure_path / folder_name
if new_folder.exists():
shutil.rmtree(new_folder)
os.rename(path, new_folder)
del habitat_agent
del episode
gc.collect()
try:
torch.cuda.empty_cache()
except:
pass
except Exception as e:
print("Exception during metrics logging: %s" % str(e))
habitat_env.close()
del habitat_env
gc.collect()
try:
torch.cuda.empty_cache()
except:
pass
print("Closed environment for scene: %s" % scene)