-
Notifications
You must be signed in to change notification settings - Fork 237
/
Copy patheval.py
510 lines (446 loc) · 16.5 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
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
import os
import argparse
import math
import csv
import itertools
import logging
from typing import Dict, List
from collections import defaultdict
from dataclasses import dataclass
import torch
import pandas as pd
import numpy as np
from scipy.optimize import linear_sum_assignment
from shapely import Polygon, LineString, polygonize, polygonize_full, make_valid
from bbox import BBox3D
from bbox.metrics import iou_3d
from terminaltables import AsciiTable
from spatiallm import Layout
from spatiallm.layout.entity import Wall, Door, Window, Bbox
log = logging.getLogger(__name__)
ZERO_TOLERANCE = 1e-6
LARGE_COST_VALUE = 1e6
OBJECTS = [
"curtain",
"nightstand",
"chandelier",
"wardrobe",
"bed",
"sofa",
"chair",
"cabinet",
"dining table",
"plants",
"tv cabinet",
"coffee table",
"side table",
"air conditioner",
"dresser",
]
THIN_OBJECTS = [
"painting",
"carpet",
"tv",
"door",
"window",
]
@dataclass
class EvalTuple:
tp: int
num_pred: int
num_gt: int
@property
def precision(self):
return self.tp / self.num_pred if self.num_pred > 0 else 0
@property
def recall(self):
return self.tp / self.num_gt if self.num_gt > 0 else 0
@property
def f1(self):
return (
(2 * self.precision * self.recall) / (self.precision + self.recall)
if (self.precision + self.recall) > 0
else 0
)
@property
def masked(self):
return self.num_pred == 0 and self.num_gt == 0
def calc_poly_iou(poly1, poly2):
if poly1.intersects(poly2):
inter_area = poly1.intersection(poly2).area
union_area = poly1.union(poly2).area
poly_iou = inter_area / union_area if union_area > 0 else 0
else:
poly_iou = 0
return poly_iou
def construct_polygon(lines: List[LineString]):
try:
poly = polygonize(lines)
if poly.is_empty:
candidates = []
for p in polygonize_full(lines):
if p.is_empty:
continue
candidate = p.geoms[0]
if isinstance(candidate, Polygon):
candidates.append(candidate)
elif isinstance(candidate, LineString):
candidates.append(Polygon(candidate))
else:
log.warning(
f"Unsupported geom_type {candidate.geom_type} to construct polygon."
)
candidates.sort(key=lambda x: x.area, reverse=True)
poly = candidates[0]
if not poly.is_valid:
poly = make_valid(poly)
return poly
except Exception as e:
log.error(f"Fail to construct polygon by lines {lines}", e)
return Polygon()
def read_label_mapping(
label_path: str, label_from="spatiallm59", label_to="spatiallm18"
):
assert os.path.isfile(label_path), f"Label mapping file {label_path} does not exist"
mapping = dict()
with open(label_path) as csvfile:
reader = csv.DictReader(csvfile, delimiter="\t")
for row in reader:
label_from_value = row[label_from]
label_to_value = row[label_to]
if label_from_value == "" or label_to_value == "":
continue
mapping[label_from_value] = label_to_value
return mapping
def assign_class_map(entities: List[Bbox], class_map=Dict[str, str]):
res_entities = list()
for entity in entities:
mapping_to_class = class_map.get(entity.class_name.replace("_", " "))
if mapping_to_class:
entity.class_name = mapping_to_class
res_entities.append(entity)
return res_entities
def get_entity_class(entity):
try:
return entity.class_name
except:
return entity.entity_label
def get_BBox3D(entity: Bbox):
return BBox3D(
entity.position_x,
entity.position_y,
entity.position_z,
entity.scale_x,
entity.scale_y,
entity.scale_z,
euler_angles=[0, 0, entity.angle_z],
is_center=True,
)
def calc_bbox_tp(
pred_entities: List[Bbox], gt_entities: List[Bbox], iou_threshold: float = 0.25
):
num_pred = len(pred_entities)
num_gt = len(gt_entities)
if num_pred == 0 or num_gt == 0:
return EvalTuple(0, num_pred, num_gt)
iou_matrix = torch.as_tensor(
[
iou_3d(bbox_1, bbox_2)
for bbox_1, bbox_2 in itertools.product(
[get_BBox3D(entity) for entity in pred_entities],
[get_BBox3D(entity) for entity in gt_entities],
)
]
).resize(num_pred, num_gt)
cost_matrix = torch.full((num_pred, num_gt), LARGE_COST_VALUE)
cost_matrix[iou_matrix > iou_threshold] = -1
indices = linear_sum_assignment(cost_matrix.numpy())
tp_percent = iou_matrix[
torch.as_tensor(indices[0], dtype=torch.int64),
torch.as_tensor(indices[1], dtype=torch.int64),
]
tp = torch.sum(tp_percent >= iou_threshold).item()
return EvalTuple(tp, num_pred, num_gt)
def is_valid_dw(entity: Door | Window, wall_id_lookup: Dict[int, Wall]):
attach_wall = wall_id_lookup.get(entity.id, None)
if attach_wall is None:
return False
wall_extent_x = max(
max(attach_wall.ax, attach_wall.bx) - min(attach_wall.ax, attach_wall.bx), 0
)
wall_extent_y = max(
max(attach_wall.ay, attach_wall.by) - min(attach_wall.ay, attach_wall.by), 0
)
return wall_extent_x > ZERO_TOLERANCE or wall_extent_y > ZERO_TOLERANCE
def get_corners(entity: Door | Window | Bbox, wall_id_lookup: Dict[int, Wall]):
if isinstance(entity, (Door, Window)):
attach_wall = wall_id_lookup.get(entity.id, None)
if attach_wall is None:
log.error(f"{entity} attach wall is None")
return
wall_start = np.array([attach_wall.ax, attach_wall.ay])
wall_end = np.array([attach_wall.bx, attach_wall.by])
wall_length = np.linalg.norm(wall_end - wall_start)
wall_xy_unit_vec = (wall_end - wall_start) / wall_length
wall_xy_unit_vec = np.nan_to_num(wall_xy_unit_vec, nan=0)
door_center = np.array(
[entity.position_x, entity.position_y, entity.position_z]
)
offset = 0.5 * np.concatenate(
[wall_xy_unit_vec * entity.width, np.array([entity.height])]
)
door_start_xyz = door_center - offset
door_end_xyz = door_center + offset
return np.array(
[
[door_start_xyz[0], door_start_xyz[1], door_start_xyz[2]],
[door_end_xyz[0], door_end_xyz[1], door_start_xyz[2]],
[door_end_xyz[0], door_end_xyz[1], door_end_xyz[2]],
[door_start_xyz[0], door_start_xyz[1], door_end_xyz[2]],
]
)
elif isinstance(entity, Bbox):
bbox_points = get_BBox3D(entity).p
scale_key = ["scale_x", "scale_y", "scale_z"]
match min(scale_key, key=lambda k: abs(getattr(entity, k))):
case "scale_x":
return np.array(
[
(bbox_points[0] + bbox_points[1]) / 2,
(bbox_points[2] + bbox_points[3]) / 2,
(bbox_points[6] + bbox_points[7]) / 2,
(bbox_points[4] + bbox_points[5]) / 2,
]
)
case "scale_y":
return np.array(
[
(bbox_points[0] + bbox_points[3]) / 2,
(bbox_points[4] + bbox_points[7]) / 2,
(bbox_points[5] + bbox_points[6]) / 2,
(bbox_points[1] + bbox_points[2]) / 2,
]
)
case "scale_z":
return np.array(
[
(bbox_points[0] + bbox_points[4]) / 2,
(bbox_points[1] + bbox_points[5]) / 2,
(bbox_points[2] + bbox_points[6]) / 2,
(bbox_points[3] + bbox_points[7]) / 2,
]
)
case _:
log.error(f"Unrecognized named attribute for {entity} in get_corners")
return
def are_planes_parallel_and_close(
corners_1: np.ndarray,
corners_2: np.ndarray,
parallel_tolerance: float,
dist_tolerance: float,
):
p1, p2, p3, _ = corners_1
q1, q2, q3, _ = corners_2
n1 = np.cross(np.subtract(p2, p1), np.subtract(p3, p1))
n2 = np.cross(np.subtract(q2, q1), np.subtract(q3, q1))
n1_length = np.linalg.norm(n1)
n2_length = np.linalg.norm(n2)
assert (
n1_length * n2_length > ZERO_TOLERANCE
), f"Invalid plane corners, corners_1: {corners_1}, corners_2: {corners_2}"
return (
np.linalg.norm(np.cross(n1, n2)) / (n1_length * n2_length) < parallel_tolerance
and np.dot(np.subtract(q1, p1), n1) / n1_length < dist_tolerance
)
def calc_thin_bbox_iou_2d(
corners_1: np.ndarray,
corners_2: np.ndarray,
parallel_tolerance: float,
dist_tolerance: float,
):
if are_planes_parallel_and_close(
corners_1, corners_2, parallel_tolerance, dist_tolerance
):
p1, p2, _, p4 = corners_1
v1 = np.subtract(p2, p1)
v2 = np.subtract(p4, p1)
basis1 = v1 / np.linalg.norm(v1)
basis1_orth = v2 - np.dot(v2, basis1) * basis1
basis2 = basis1_orth / np.linalg.norm(basis1_orth)
projected_corners_1 = [
[
np.dot(np.subtract(point, p1), basis1),
np.dot(np.subtract(point, p1), basis2),
]
for point in corners_1
]
projected_corners_2 = [
[
np.dot(np.subtract(point, p1), basis1),
np.dot(np.subtract(point, p1), basis2),
]
for point in corners_2
]
box1 = Polygon(projected_corners_1)
box2 = Polygon(projected_corners_2)
return calc_poly_iou(box1, box2)
else:
return 0
def calc_thin_bbox_tp(
pred_entities: List[Door | Window | Bbox],
gt_entities: List[Door | Window | Bbox],
pred_wall_id_lookup: Dict[int, Wall],
gt_wall_id_lookup: Dict[int, Wall],
iou_threshold: float = 0.25,
parallel_tolerance: float = math.sin(math.radians(5)),
dist_tolerance: float = 0.2,
):
num_pred = len(pred_entities)
num_gt = len(gt_entities)
if num_pred == 0 or num_gt == 0:
return EvalTuple(0, num_pred, num_gt)
iou_matrix = torch.as_tensor(
[
calc_thin_bbox_iou_2d(
corners_1, corners_2, parallel_tolerance, dist_tolerance
)
for corners_1, corners_2 in itertools.product(
[get_corners(entity, pred_wall_id_lookup) for entity in pred_entities],
[get_corners(entity, gt_wall_id_lookup) for entity in gt_entities],
)
]
).resize(num_pred, num_gt)
cost_matrix = torch.full((num_pred, num_gt), LARGE_COST_VALUE)
cost_matrix[iou_matrix > iou_threshold] = -1
indices = linear_sum_assignment(cost_matrix.numpy())
tp_percent = iou_matrix[
torch.as_tensor(indices[0], dtype=torch.int64),
torch.as_tensor(indices[1], dtype=torch.int64),
]
tp = torch.sum(tp_percent >= iou_threshold).item()
return EvalTuple(tp, num_pred, num_gt)
if __name__ == "__main__":
parser = argparse.ArgumentParser("SpatialLM evaluation script")
parser.add_argument(
"--metadata",
type=str,
required=True,
help="metadata CSV file with columns id, pcd, layout",
)
parser.add_argument(
"--gt_dir",
type=str,
required=True,
help="Path to the gt layout txt directory",
)
parser.add_argument(
"--pred_dir",
type=str,
required=True,
help="Path to the pred layout txt directory",
)
parser.add_argument(
"--label_mapping",
type=str,
required=True,
help="Path to the label mapping file",
)
args = parser.parse_args()
df = pd.read_csv(args.metadata)
scene_id_list = df["id"].tolist()
class_map = read_label_mapping(args.label_mapping)
floorplan_ious = list()
classwise_eval_tuples: Dict[str, List[EvalTuple]] = defaultdict(list)
for scene_id in scene_id_list:
log.info(f"Evaluating scene {scene_id}")
with open(os.path.join(args.pred_dir, f"{scene_id}.txt"), "r") as f:
pred_layout = Layout(f.read())
with open(os.path.join(args.gt_dir, f"{scene_id}.txt"), "r") as f:
gt_layout = Layout(f.read())
pred_layout.bboxes = assign_class_map(pred_layout.bboxes, class_map)
gt_layout.bboxes = assign_class_map(gt_layout.bboxes, class_map)
# Floorplan, IoU
pred_poly = construct_polygon(
[LineString([(w.ax, w.ay), (w.bx, w.by)]) for w in pred_layout.walls]
)
gt_poly = construct_polygon(
[LineString([(w.ax, w.ay), (w.bx, w.by)]) for w in gt_layout.walls]
)
floorplan_ious.append(calc_poly_iou(pred_poly, gt_poly))
# Normal Objects, F1
pred_normal_objects = [
b for b in pred_layout.bboxes if b.class_name in OBJECTS
]
gt_normal_objects = [
b for b in gt_layout.bboxes if b.class_name in OBJECTS
]
for class_name in OBJECTS:
classwise_eval_tuples[class_name].append(
calc_bbox_tp(
pred_entities=[
b
for b in pred_normal_objects
if get_entity_class(b) == class_name
],
gt_entities=[
b
for b in gt_normal_objects
if get_entity_class(b) == class_name
],
)
)
# Thin Objects, F1
pred_thin_objects = [
b for b in pred_layout.bboxes if b.class_name in THIN_OBJECTS
]
gt_thin_objects = [b for b in gt_layout.bboxes if b.class_name in THIN_OBJECTS]
pred_wall_id_lookup = {w.id: w for w in pred_layout.walls}
gt_wall_id_lookup = {w.id: w for w in gt_layout.walls}
pred_thin_objects += [
e
for e in pred_layout.doors + pred_layout.windows
if is_valid_dw(e, pred_wall_id_lookup)
]
gt_thin_objects += [
e
for e in gt_layout.doors + gt_layout.windows
if is_valid_dw(e, gt_wall_id_lookup)
]
for class_name in THIN_OBJECTS:
classwise_eval_tuples[class_name].append(
calc_thin_bbox_tp(
pred_entities=[
b
for b in pred_thin_objects
if get_entity_class(b) == class_name
],
gt_entities=[
b for b in gt_thin_objects if get_entity_class(b) == class_name
],
pred_wall_id_lookup=pred_wall_id_lookup,
gt_wall_id_lookup=gt_wall_id_lookup,
)
)
# table print
headers = ["Floorplan", "mean IoU"]
table_data = [headers]
table_data += [["wall", np.mean(floorplan_ious)]]
print("\n" + AsciiTable(table_data).table)
headers = ["Objects", "F1 @.25 IoU"]
table_data = [headers]
for class_name in OBJECTS:
tuples = classwise_eval_tuples[class_name]
f1 = np.ma.masked_where(
[t.masked for t in tuples], [t.f1 for t in tuples]
).mean()
table_data.append([class_name, f1])
print("\n" + AsciiTable(table_data).table)
headers = ["Thin Objects", "F1 @.25 IoU"]
table_data = [headers]
for class_name in THIN_OBJECTS:
tuples = classwise_eval_tuples[class_name]
f1 = np.ma.masked_where(
[t.masked for t in tuples], [t.f1 for t in tuples]
).mean()
table_data.append([class_name, f1])
print("\n" + AsciiTable(table_data).table)