"""Closed-loop perception check: the tracker rows fed to the model against the privileged oracle bank. With OBJECT_SOURCE=detector and OBJECT_COMPARE_DIR= the agent also steps an OracleObjectSource in shadow (never fed to the model) and scores the tracker rows against it in the ego frame the model sees, in the terms of the stage-2 acceptance criteria: recall of currently visible bank objects per range bin, false tracks per second, position error and velocity error. One npz per route; aggregate with scripts/analysis/object_compare_summary.py. """ from pathlib import Path import numpy as np from dataloader.object_bank import RAW_CLASS, RAW_SPEED, RAW_VISIBLE, RAW_X, RAW_Y, RAW_YAW from models.detector_metrics import match CAMERA_X = -1.5 # gt columns: range, class, matched, speed, tracked class (-1 unmatched) # error columns: range, class, position error, longitudinal, lateral, speed error, speed, yaw error GT_DIM, ERR_DIM = 5, 8 class ShadowComparison: def __init__(self, max_range=50.0, frame_dt=0.25): self.max_range = float(max_range) self.frame_dt = float(frame_dt) self.frames = 0 self.false_tracks = 0 self.tracked_rows = 0 self.oracle_rows = 0 self.gt = [] self.errors = [] def add(self, oracle_raw, oracle_count, tracked_raw, tracked_count): o = oracle_raw[:oracle_count].astype(np.float64) d = tracked_raw[:tracked_count].astype(np.float64) self.frames += 1 o_xy, d_xy = o[:, [RAW_X, RAW_Y]], d[:, [RAW_X, RAW_Y]] o_range = np.hypot(o_xy[:, 0] - CAMERA_X, o_xy[:, 1]) d_range = np.hypot(d_xy[:, 0] - CAMERA_X, d_xy[:, 1]) self.tracked_rows += int((d_range <= self.max_range).sum()) visible = (o[:, RAW_VISIBLE] > 0.5) & (o_range <= self.max_range) self.oracle_rows += int(visible.sum()) # tracked rows are matched against every oracle row, so a coasted track of an occluded object # is not a false track; nearest rows claim their object first assigned = match(d_xy, -d_range, o_xy, o_range) self.false_tracks += int(((assigned < 0) & (d_range <= self.max_range)).sum()) owner = np.full(len(o), -1, dtype=np.int64) owner[assigned[assigned >= 0]] = np.flatnonzero(assigned >= 0) for g in np.flatnonzero(visible): p = owner[g] self.gt.append((o_range[g], o[g, RAW_CLASS], p >= 0, o[g, RAW_SPEED], d[p, RAW_CLASS] if p >= 0 else -1)) if p < 0: continue delta = d_xy[p] - o_xy[g] ray = (o_xy[g] - (CAMERA_X, 0.0)) / max(o_range[g], 1e-3) lon = float(delta @ ray) lat = float(delta @ np.array([-ray[1], ray[0]])) yaw_err = abs((d[p, RAW_YAW] - o[g, RAW_YAW] + np.pi) % (2 * np.pi) - np.pi) self.errors.append((o_range[g], o[g, RAW_CLASS], np.hypot(*delta), lon, lat, abs(d[p, RAW_SPEED] - o[g, RAW_SPEED]), o[g, RAW_SPEED], yaw_err)) def save(self, path): Path(path).parent.mkdir(parents=True, exist_ok=True) np.savez_compressed( path, gt=np.asarray(self.gt, np.float32).reshape(-1, GT_DIM), errors=np.asarray(self.errors, np.float32).reshape(-1, ERR_DIM), frames=self.frames, frame_dt=self.frame_dt, false_tracks=self.false_tracks, tracked_rows=self.tracked_rows, oracle_rows=self.oracle_rows) def brief(self): gt = np.asarray(self.gt, np.float32).reshape(-1, GT_DIM) recall = float(gt[:, 2].mean()) if len(gt) else float("nan") seconds = max(self.frames * self.frame_dt, 1e-6) return (f"recall(visible<{self.max_range:.0f}m)={recall:.3f} false_tracks/s={self.false_tracks / seconds:.2f} " f"rows/frame tracked={self.tracked_rows / max(self.frames, 1):.1f} " f"oracle_visible={self.oracle_rows / max(self.frames, 1):.1f}")