yxma commited on
Commit
a37e90e
·
verified ·
1 Parent(s): fbde5a6

toolbox: publish probe_eval.py

Browse files
Files changed (1) hide show
  1. toolbox/probe_eval.py +130 -0
toolbox/probe_eval.py ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Evaluate a world-model rollout against a synthetic probe's ground truth.
2
+
3
+ WHAT GROUND TRUTH MEANS HERE, AND WHAT IT DOES NOT
4
+
5
+ A probe is a commanded action sequence, not a recorded one. There is no
6
+ ground-truth future IMAGE — nobody performed this motion. What IS ground truth
7
+ is where the sensor WOULD BE if the action were executed exactly, which is a
8
+ pose sequence, and its projection into each camera. So a rollout is judged by
9
+ comparing the sensor the model draws against the pose the action commands, not
10
+ by pixel-matching a future frame that does not exist.
11
+
12
+ That is why the overlay matters: the comparison is spatial, and a scalar loss
13
+ against a nonexistent target would be meaningless.
14
+
15
+ THE OVERLAY'S OWN ERROR BAR
16
+
17
+ Projected ground truth is not exact. Measured on this rig:
18
+
19
+ camera reprojection rmse 4.7 / 5.3 / 7.5 mm -> 3.6-5.7 px at 800 mm
20
+ gel centre in the rigid frame <= ~5 mm -> ~3.8 px
21
+
22
+ so agreement inside roughly 6 px is at the noise floor and means "correct".
23
+ Sessions whose world frame is not pinned are excluded from the test set rather
24
+ than silently carrying a larger, unstated error — see the README.
25
+ """
26
+ from __future__ import annotations
27
+
28
+ import numpy as np
29
+
30
+
31
+ def project_gt(poses7, gel_center_mm, cam_calib):
32
+ """Ground-truth gel-centre pixels for a pose sequence. (T, 2), NaN if behind.
33
+
34
+ ONE PROJECTION. This calls `calibration.project_gel_to_pixel`, the same
35
+ function the dataset's own previews and the release fingerprint use, so an
36
+ overlay drawn here cannot disagree with one drawn there.
37
+ """
38
+ from .calibration import project_gel_to_pixel
39
+
40
+ out = np.full((len(np.atleast_2d(poses7)), 2), np.nan)
41
+ for i, p in enumerate(np.atleast_2d(np.asarray(poses7, float))):
42
+ uv = project_gel_to_pixel(p, gel_center_mm, cam_calib)
43
+ if uv is not None:
44
+ out[i] = uv
45
+ return out
46
+
47
+
48
+ def overlay_gt(frame_rgb, poses7, gel_center_mm, cam_calib, *,
49
+ held_pose7=None, held_gel_mm=None, every: int = 6,
50
+ color=(255, 210, 63), label=None):
51
+ """Draw the commanded trajectory on a frame. Returns a copy.
52
+
53
+ Start is a filled dot, end a ring, the path a polyline sampled every
54
+ `every` steps, and the sensor frame is drawn as a triad at both ends so
55
+ ORIENTATION is visible — a dot cannot show a rotation probe, where the gel
56
+ centre does not move at all.
57
+
58
+ `held_pose7` draws the stationary hand dimmed, so a viewer can see the
59
+ other sensor the rollout must also keep still.
60
+ """
61
+ import cv2
62
+
63
+ from .viz import draw_sensor_frame
64
+
65
+ out = np.ascontiguousarray(frame_rgb).copy()
66
+ h, w = out.shape[:2]
67
+ if held_pose7 is not None and held_gel_mm is not None:
68
+ out = draw_sensor_frame(out, held_pose7, held_gel_mm, cam_calib,
69
+ stem=True, dim=True, label="held")
70
+ # ORDER MATTERS. The triads go down first and the path markers on top:
71
+ # drawn the other way round, the start triad's grey centre dot lands
72
+ # exactly on the start marker and hides it — which is where the reader
73
+ # looks to see where the motion begins.
74
+ P = np.asarray(poses7, float)
75
+ out = draw_sensor_frame(out, P[0], gel_center_mm, cam_calib,
76
+ stem=True, dim=True)
77
+ out = draw_sensor_frame(out, P[-1], gel_center_mm, cam_calib,
78
+ stem=True, label=label)
79
+ px = project_gt(P, gel_center_mm, cam_calib)
80
+ pts = [(int(round(u)), int(round(v))) for u, v in px
81
+ if np.isfinite(u) and 0 <= u < w and 0 <= v < h]
82
+ for a, b in zip(pts[::every], pts[every::every]):
83
+ cv2.line(out, a, b, color, 1, cv2.LINE_AA)
84
+ if pts:
85
+ cv2.circle(out, pts[0], 4, (255, 255, 255), -1, cv2.LINE_AA)
86
+ cv2.circle(out, pts[0], 4, (40, 40, 40), 1, cv2.LINE_AA)
87
+ cv2.circle(out, pts[-1], 6, color, 2, cv2.LINE_AA)
88
+ return out
89
+
90
+
91
+ def rollout_error(pred_poses7, gt_poses7, gel_center_mm=None, cam_calib=None):
92
+ """Per-step error of a rollout against the commanded ground truth.
93
+
94
+ Returns a dict with, per step and summarised:
95
+ pos_mm Euclidean gel-centre error in world millimetres
96
+ rot_deg geodesic orientation error
97
+ px reprojection error, if a camera is given
98
+
99
+ The pixel figure is what a reader of the overlay sees, and the millimetre
100
+ figure is what the model actually got wrong; they differ by depth, so both
101
+ are reported rather than one standing in for the other.
102
+ """
103
+ from scipy.spatial.transform import Rotation
104
+
105
+ a = np.asarray(pred_poses7, float)
106
+ b = np.asarray(gt_poses7, float)
107
+ n = min(len(a), len(b))
108
+ a, b = a[:n], b[:n]
109
+ if gel_center_mm is not None:
110
+ Ra = Rotation.from_quat(a[:, 3:7]).as_matrix()
111
+ Rb = Rotation.from_quat(b[:, 3:7]).as_matrix()
112
+ c = np.asarray(gel_center_mm, float)
113
+ pa = a[:, :3]*1000.0 + np.einsum("nij,j->ni", Ra, c)
114
+ pb = b[:, :3]*1000.0 + np.einsum("nij,j->ni", Rb, c)
115
+ else:
116
+ pa, pb = a[:, :3]*1000.0, b[:, :3]*1000.0
117
+ pos = np.linalg.norm(pa - pb, axis=1)
118
+ qa, qb = Rotation.from_quat(a[:, 3:7]), Rotation.from_quat(b[:, 3:7])
119
+ rot = np.degrees((qa.inv() * qb).magnitude())
120
+ out = {"n_steps": int(n), "pos_mm": pos, "rot_deg": rot,
121
+ "pos_mm_final": float(pos[-1]), "rot_deg_final": float(rot[-1]),
122
+ "pos_mm_mean": float(pos.mean()), "rot_deg_mean": float(rot.mean())}
123
+ if cam_calib is not None and gel_center_mm is not None:
124
+ ua = project_gt(a, gel_center_mm, cam_calib)
125
+ ub = project_gt(b, gel_center_mm, cam_calib)
126
+ d = np.linalg.norm(ua - ub, axis=1)
127
+ out["px"] = d
128
+ out["px_final"] = float(d[-1])
129
+ out["px_mean"] = float(np.nanmean(d))
130
+ return out