-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathhabitat_evaluator.py
More file actions
435 lines (381 loc) · 19.6 KB
/
habitat_evaluator.py
File metadata and controls
435 lines (381 loc) · 19.6 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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# eval utils
from eval import get_closest_dist, FMMPlanner
from eval.actor import Actor
from eval.dataset_utils.gibson_dataset import load_gibson_episodes
from mapping import rerun_logger
from config import EvalConf
from onemap_utils import monochannel_to_inferno_rgb
from eval.dataset_utils import *
# os / filsystem
import bz2
import os
from os import listdir
import gzip
import json
import pathlib
# cv2
import cv2
# numpy
import numpy as np
# skimage
import skimage
# dataclasses
from dataclasses import dataclass
# quaternion
import quaternion
# typing
from typing import Tuple, List, Dict
import enum
# habitat
import habitat_sim
from habitat_sim import ActionSpec, ActuationSpec
from habitat_sim.utils import common as utils
# tabulate
from tabulate import tabulate
# rerun
import rerun as rr
# pandas
import pandas as pd
# pickle
import pickle
# scipy
from scipy.spatial.transform import Rotation as R
class Result(enum.Enum):
SUCCESS = 1
FAILURE_MISDETECT = 2
FAILURE_STUCK = 3
FAILURE_OOT = 4
FAILURE_NOT_REACHED = 5
FAILURE_ALL_EXPLORED = 6
class HabitatEvaluator:
def __init__(self,
config: EvalConf,
actor: Actor,
) -> None:
self.config = config
self.multi_object = config.multi_object
self.max_steps = config.max_steps
self.max_dist = config.max_dist
self.controller = config.controller
self.mapping = config.mapping
self.planner = config.planner
self.log_rerun = config.log_rerun
self.object_nav_path = config.object_nav_path
self.scene_path = config.scene_path
self.scene_data = {}
self.episodes = []
self.exclude_ids = []
self.is_gibson = config.is_gibson
self.sim = None
self.actor = actor
self.vel_control = habitat_sim.physics.VelocityControl()
self.vel_control.controlling_lin_vel = True
self.vel_control.lin_vel_is_local = True
self.vel_control.controlling_ang_vel = True
self.vel_control.ang_vel_is_local = True
self.control_frequency = config.controller.control_freq
self.max_vel = config.controller.max_vel
self.max_ang_vel = config.controller.max_ang_vel
self.time_step = 1.0 / self.control_frequency
if self.is_gibson:
dataset_info_file = str(pathlib.Path(self.object_nav_path).parent.absolute()) + \
"/val_info.pbz2".format(split="val")
with bz2.BZ2File(dataset_info_file, 'rb') as f:
self.dataset_info = pickle.load(f)
else:
self.dataset_info = None
if self.is_gibson:
self.episodes, self.scene_data = GibsonDataset.load_gibson_episodes(self.episodes,
self.scene_data,
self.dataset_info,
self.object_nav_path)
else:
if self.multi_object:
self.episodes, self.scene_data = HM3DMultiDataset.load_hm3d_multi_episodes(self.episodes,
self.scene_data,
self.object_nav_path)
else:
self.episodes, self.scene_data = HM3DDataset.load_hm3d_episodes(self.episodes,
self.scene_data,
self.object_nav_path)
if self.actor is not None:
self.logger = rerun_logger.RerunLogger(self.actor.mapper, False, "") if self.log_rerun else None
self.results_path = "/home/finn/active/MON/results_gibson" if self.is_gibson else "results/"
def load_scene(self, scene_id: str):
if self.sim is not None:
self.sim.close()
backend_cfg = habitat_sim.SimulatorConfiguration()
backend_cfg.scene_id = self.scene_path + scene_id
if self.is_gibson:
pass # TODO
else:
backend_cfg.scene_dataset_config_file = self.scene_path + "hm3d/hm3d_annotated_basis.scene_dataset_config.json"
hfov = 90
rgb = habitat_sim.CameraSensorSpec()
rgb.uuid = "rgb"
rgb.hfov = hfov
rgb.position = np.array([0, 0.88, 0])
rgb.sensor_type = habitat_sim.SensorType.COLOR
res = 640
rgb.resolution = [res, res]
depth = habitat_sim.CameraSensorSpec()
depth.uuid = "depth"
depth.hfov = hfov
depth.sensor_type = habitat_sim.SensorType.DEPTH
depth.position = np.array([0, 0.88, 0])
depth.resolution = [res, res]
agent_cfg = habitat_sim.agent.AgentConfiguration(action_space=dict(
move_forward=ActionSpec("move_forward", ActuationSpec(amount=0.25)),
turn_left=ActionSpec("turn_left", ActuationSpec(amount=5.0)),
turn_right=ActionSpec("turn_right", ActuationSpec(amount=5.0)),
))
agent_cfg.sensor_specifications = [rgb, depth]
sim_cfg = habitat_sim.Configuration(backend_cfg, [agent_cfg])
self.sim = habitat_sim.Simulator(sim_cfg)
if self.scene_data[scene_id].objects_loaded:
return
if not self.is_gibson:
self.scene_data = HM3DDataset.load_hm3d_objects(self.scene_data, self.sim.semantic_scene.objects, scene_id)
else:
self.scene_data = GibsonDataset.load_gibson_objects(self.scene_data, self.dataset_info, scene_id)
def execute_action(self, action: Dict
):
if 'discrete' in action.keys():
# We have a discrete actor
self.sim.step(action['discrete'])
elif 'continuous' in action.keys():
# We have a continuous actor
self.vel_control.angular_velocity = action['continuous']['angular']
self.vel_control.linear_velocity = action['continuous']['linear']
agent_state = self.sim.get_agent(0).state
previous_rigid_state = habitat_sim.RigidState(
utils.quat_to_magnum(agent_state.rotation), agent_state.position
)
# manually integrate the rigid state
target_rigid_state = self.vel_control.integrate_transform(
self.time_step, previous_rigid_state
)
# snap rigid state to navmesh and set state to object/sim
# calls pathfinder.try_step or self.pathfinder.try_step_no_sliding
end_pos = self.sim.step_filter(
previous_rigid_state.translation, target_rigid_state.translation
)
# set the computed state
agent_state.position = end_pos
agent_state.rotation = utils.quat_from_magnum(
target_rigid_state.rotation
)
self.sim.get_agent(0).set_state(agent_state)
self.sim.step_physics(self.time_step)
def read_results(self, path, sort_by):
state_dir = os.path.join(path, 'state')
state_results = {}
object_query = {}
scene_name = {}
spl = {}
# Check if the state directory exists
if not os.path.isdir(state_dir):
print(f"Error: {state_dir} is not a valid directory")
return state_results
pose_dir = os.path.join(os.path.abspath(os.path.join(state_dir, os.pardir)), "trajectories")
# Iterate through all files in the state directory
for filename in os.listdir(state_dir):
if filename.startswith('state_') and filename.endswith('.txt'):
try:
# Extract the experiment number from the filename
experiment_num = int(filename[6:-4]) # removes 'state_' and '.txt'
# if experiment_num > 1045:
# continue
# Read the content of the file
with open(os.path.join(state_dir, filename), 'r') as file:
content = file.read().strip()
# Convert the content to a number (assuming it's a float)
state_value = int(content)
# Store the result in the dictionary
state_results[experiment_num] = state_value
object_query[experiment_num] = self.episodes[experiment_num].obj_sequence[0]
scene_name[experiment_num] = self.episodes[experiment_num].scene_id
poses = np.genfromtxt(os.path.join(pose_dir, "poses_" + str(experiment_num) + ".csv"), delimiter=",")
deltas = poses[1:, :2] - poses[:-1, :2]
distance_traveled = np.linalg.norm(deltas, axis=1).sum()
if state_value == 1:
spl[experiment_num] = self.episodes[experiment_num].best_dist / max(self.episodes[experiment_num].best_dist, distance_traveled)
else:
spl[experiment_num] = 0
if self.episodes[experiment_num].episode_id != experiment_num:
print(f"Warning, exerpiment_num {experiment_num} does not correctly resolve to episode_id {self.episodes[experiment_num].episode_id}")
except ValueError:
print(f"Warning: Skipping {filename} due to invalid format")
except Exception as e:
print(f"Error reading {filename}: {str(e)}")
dict_res = {"state": state_results, "obj" : object_query, "scene" : scene_name, "spl" : spl}
data = pd.DataFrame.from_dict(dict_res)
states = data["state"].unique()
def calculate_percentages(group):
total = len(group)
result = pd.Series({Result(state).name: (group['state'] == state).sum() / total for state in states})
# Calculate average SPL and multiply by 100
avg_spl = group['spl'].mean()
result['Average SPL'] = avg_spl
return result
# Per-object results
object_results = data.groupby('obj').apply(calculate_percentages).reset_index()
object_results = object_results.rename(columns={'obj': 'Object'})
# Per-scene results
scene_results = data.groupby('scene').apply(calculate_percentages).reset_index()
scene_results = scene_results.rename(columns={'scene': 'Scene'})
# Overall results
overall_percentages = calculate_percentages(data)
overall_row = pd.DataFrame([{'Object': 'Overall'} | overall_percentages.to_dict()])
object_results = pd.concat([overall_row, object_results], ignore_index=True)
overall_row = pd.DataFrame([{'Scene': 'Overall'} | overall_percentages.to_dict()])
scene_results = pd.concat([overall_row, scene_results], ignore_index=True)
# Sorting
object_results = object_results.sort_values(by=sort_by, ascending=False)
scene_results = scene_results.sort_values(by=sort_by, ascending=False)
# Function to format percentages
def format_percentages(val):
return f"{val:.2%}" if isinstance(val, float) else val
# Apply formatting to all columns except the first one (Object/Scene)
object_table = object_results.iloc[:, 0].to_frame().join(
object_results.iloc[:, 1:].applymap(format_percentages))
scene_table = scene_results.iloc[:, 0].to_frame().join(
scene_results.iloc[:, 1:].applymap(format_percentages))
print(f"Results by Object (sorted by {sort_by} rate, descending):")
print(tabulate(object_table, headers='keys', tablefmt='pretty', floatfmt='.2%'))
print(f"\nResults by Scene (sorted by {sort_by} rate, descending):")
print(tabulate(scene_table, headers='keys', tablefmt='pretty', floatfmt='.2%'))
return data
def evaluate(self):
success = 0
n_eps = 0
# randomly shuffle episodes
# random.shuffle(self.episodes)
success_per_obj = {}
obj_count = {}
results = []
# restart at 930
for n_ep, episode in enumerate(self.episodes):
# for n_ep, episode in enumerate(self.episodes[492:]):
poses = []
results.append(Result.FAILURE_OOT)
steps = 0
if n_ep in self.exclude_ids:
continue
n_eps += 1
if self.sim is None or not self.sim.curr_scene_name in episode.scene_id:
self.load_scene(episode.scene_id)
# if self.is_gibson:
# episode = self.compute_gt_path_gibson(episode)
self.sim.initialize_agent(0, habitat_sim.AgentState(episode.start_position, episode.start_rotation))
self.actor.reset()
current_obj_id = 0
current_obj = episode.obj_sequence[current_obj_id]
if current_obj not in success_per_obj:
success_per_obj[current_obj] = 0
obj_count[current_obj] = 1
else:
obj_count[current_obj] += 1
self.actor.set_query(current_obj)
if self.log_rerun:
pts = []
for obj in self.scene_data[episode.scene_id].object_locations[current_obj]:
if not self.is_gibson:
pt = obj.bbox.center[[0, 2]]
pt = (-pt[1], -pt[0])
pts.append(self.actor.mapper.one_map.metric_to_px(*pt))
else:
for pt_ in obj:
pt = (pt_[0], pt_[1])
pts.append(self.actor.mapper.one_map.metric_to_px(*pt))
pts = np.array(pts)
rr.log("map/ground_truth", rr.Points2D(pts, colors=[[255, 255, 0]], radii=[1]))
while steps < self.max_steps and current_obj_id < len(episode.obj_sequence):
observations = self.sim.get_sensor_observations()
# observations['depth'] = fill_depth_holes(observations['depth'])
observations['state'] = self.sim.get_agent(0).get_state()
pose = np.zeros((4, ))
pose[0] = -observations['state'].position[2]
pose[1] = -observations['state'].position[0]
pose[2] = observations['state'].position[1]
# yaw
orientation = observations['state'].rotation
q0 = orientation.x
q1 = orientation.y
q2 = orientation.z
q3 = orientation.w
r = R.from_quat([q0, q1, q2, q3])
# r to euler
yaw, _, _1 = r.as_euler("yxz")
pose[3] = yaw
poses.append(pose)
if self.log_rerun:
cam_x = -self.sim.get_agent(0).get_state().position[2]
cam_y = -self.sim.get_agent(0).get_state().position[0]
rr.log("camera/rgb", rr.Image(observations["rgb"]).compress(jpeg_quality=50))
# rr.log("camera/depth", rr.Image((observations["depth"] - observations["depth"].min()) / (
# observations["depth"].max() - observations["depth"].min())))
self.logger.log_pos(cam_x, cam_y)
action, called_found = self.actor.act(observations)
self.execute_action(action)
if self.log_rerun:
self.logger.log_map()
if called_found:
# We will now compute the closest distance to the bounding box of the object
dist = get_closest_dist(self.sim.get_agent(0).get_state().position[[0, 2]],
self.scene_data[episode.scene_id].object_locations[current_obj], self.is_gibson)
if dist < self.max_dist:
results[n_ep] = Result.SUCCESS
success += 1
print("Object found!")
success_per_obj[current_obj] += 1
else:
pos = self.actor.mapper.chosen_detection
pos_metric = self.actor.mapper.one_map.px_to_metric(pos[0], pos[1])
dist_detect = get_closest_dist([-pos_metric[1], -pos_metric[0]],
self.scene_data[episode.scene_id].object_locations[current_obj], self.is_gibson)
if dist_detect < self.max_dist:
results[n_ep] = Result.FAILURE_NOT_REACHED
else:
results[n_ep] = Result.FAILURE_MISDETECT
print(f"Object not found! Dist {dist}, detect dist: {dist_detect}.")
current_obj_id += 1
# if current_obj_id < len(episode.obj_sequence):
# current_obj = episode.obj_sequence[current_obj_id]
# if current_obj not in success_per_obj:
# success_per_obj[current_obj] = 0
# obj_count[current_obj] = 1
# obj_count[current_obj] += 1
# self.actor.set_query(current_obj)
if steps % 100 == 0:
dist = get_closest_dist(self.sim.get_agent(0).get_state().position[[0, 2]],
self.scene_data[episode.scene_id].object_locations[current_obj], self.is_gibson)
print(f"Step {steps}, current object: {current_obj}, episode_id: {episode.episode_id}, distance to closest object: {dist}")
steps += 1
poses = np.array(poses)
# If the last 10 poses didn't change much and we have OOT, assume stuck
if results[n_ep] == Result.FAILURE_OOT and np.linalg.norm(poses[-1] - poses[-10]) < 0.05:
results[n_ep] = Result.FAILURE_STUCK
num_frontiers = len(self.actor.mapper.nav_goals)
# Create directories to avoid missing path errors
os.makedirs(f"{self.results_path}/trajectories", exist_ok=True)
os.makedirs(f"{self.results_path}/similarities", exist_ok=True)
os.makedirs(f"{self.results_path}/state", exist_ok=True)
np.savetxt(f"{self.results_path}/trajectories/poses_{episode.episode_id}.csv", poses, delimiter=",")
# save final sim to image file
final_sim = (self.actor.mapper.get_map() + 1.0) / 2.0
final_sim = final_sim[0]
final_sim = final_sim.transpose((1, 0))
final_sim = np.flip(final_sim, axis=0)
final_sim = monochannel_to_inferno_rgb(final_sim)
cv2.imwrite(f"{self.results_path}/similarities/final_sim_{episode.episode_id}.png", final_sim)
if (results[n_ep] == Result.FAILURE_STUCK or results[n_ep] == Result.FAILURE_OOT) and num_frontiers == 0:
results[n_ep] = Result.FAILURE_ALL_EXPLORED
print(f"Overall success: {success / (n_eps)}, per object: ")
for obj in success_per_obj.keys():
print(f"{obj}: {success_per_obj[obj] / obj_count[obj]}")
print(
f"Result distribution: successes: {results.count(Result.SUCCESS)}, misdetects: {results.count(Result.FAILURE_MISDETECT)}, OOT: {results.count(Result.FAILURE_OOT)}, stuck: {results.count(Result.FAILURE_STUCK)}, not reached: {results.count(Result.FAILURE_NOT_REACHED)}, all explored: {results.count(Result.FAILURE_ALL_EXPLORED)}")
# Write result to file
with open(f"{self.results_path}/state/state_{episode.episode_id}.txt", 'w') as f:
f.write(str(results[n_ep].value))