Add full online Phase1 Phase2 final graph extraction pipeline
Browse files
tests/test_gwam_realtime_env_graph.py
CHANGED
|
@@ -7,19 +7,47 @@ import numpy as np
|
|
| 7 |
|
| 8 |
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "realtime"))
|
| 9 |
|
| 10 |
-
from gwam_realtime_env_graph import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
vals = []
|
| 18 |
cur = 0
|
| 19 |
for count in rle:
|
| 20 |
-
vals.extend([cur] * count)
|
| 21 |
cur = 1 - cur
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
|
| 25 |
def test_visibility_change_edges_are_self_edges_by_view():
|
|
@@ -51,3 +79,78 @@ def test_to_gnn_graph_active_masks_family_onehot_and_edges():
|
|
| 51 |
assert graph["edge_attr"].shape == (2, 8)
|
| 52 |
assert "family_idx" not in graph["feature_schema"]
|
| 53 |
assert "family_onehot_9" in graph["feature_schema"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "realtime"))
|
| 9 |
|
| 10 |
+
from gwam_realtime_env_graph import (
|
| 11 |
+
build_online_visual_features,
|
| 12 |
+
encode_rle,
|
| 13 |
+
to_gnn_graph,
|
| 14 |
+
visibility_change_edges,
|
| 15 |
+
)
|
| 16 |
|
| 17 |
|
| 18 |
+
class FakeVisualBackend:
|
| 19 |
+
def image_embedding(self, rgb_frame):
|
| 20 |
+
return np.asarray(rgb_frame, dtype=np.float32)
|
| 21 |
+
|
| 22 |
+
def pool_mask(self, image_embedding, mask):
|
| 23 |
+
if not mask.any():
|
| 24 |
+
return None
|
| 25 |
+
seed = float(image_embedding[mask].mean()) if image_embedding.ndim == 3 else float(mask.mean())
|
| 26 |
+
vec = np.linspace(0.0, 1.0, 256, dtype=np.float32) + seed / 255.0
|
| 27 |
+
vec /= np.linalg.norm(vec).clip(min=1e-6)
|
| 28 |
+
return vec.astype(np.float16)
|
| 29 |
+
|
| 30 |
+
def type_clip32(self, nodes):
|
| 31 |
+
arr = np.zeros((256, 32), dtype=np.float32)
|
| 32 |
+
for i in range(min(len(nodes), 256)):
|
| 33 |
+
arr[i, i % 32] = 1.0
|
| 34 |
+
return arr, {"model": "fake", "projection_seed": 20260702}
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def _decode_for_test(rle, shape):
|
| 38 |
vals = []
|
| 39 |
cur = 0
|
| 40 |
for count in rle:
|
| 41 |
+
vals.extend([cur] * int(count))
|
| 42 |
cur = 1 - cur
|
| 43 |
+
return np.array(vals, dtype=bool).reshape(shape)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def test_encode_rle_starts_with_zero_run_and_roundtrips_simple_mask():
|
| 47 |
+
mask = np.array([[False, True, True], [False, False, True]], dtype=bool)
|
| 48 |
+
rle = encode_rle(mask)
|
| 49 |
+
assert rle == [1, 2, 2, 1]
|
| 50 |
+
assert _decode_for_test(rle, mask.shape).tolist() == mask.tolist()
|
| 51 |
|
| 52 |
|
| 53 |
def test_visibility_change_edges_are_self_edges_by_view():
|
|
|
|
| 79 |
assert graph["edge_attr"].shape == (2, 8)
|
| 80 |
assert "family_idx" not in graph["feature_schema"]
|
| 81 |
assert "family_onehot_9" in graph["feature_schema"]
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def test_online_visual_features_sparse_block_matches_phase2_policy_with_fake_backend():
|
| 85 |
+
mask = np.zeros((256, 256), dtype=bool)
|
| 86 |
+
mask[20:40, 30:50] = True
|
| 87 |
+
rgb_frames = {0: np.full((256, 256, 3), 42, dtype=np.uint8)}
|
| 88 |
+
rle_rows = [{"n": 1, "v": 0, "rle": encode_rle(mask)}]
|
| 89 |
+
nodes = [{"category_text": "robot"}, {"category_text": "drawer handle"}]
|
| 90 |
+
visual = build_online_visual_features(
|
| 91 |
+
rgb_frames=rgb_frames,
|
| 92 |
+
rle_rows=rle_rows,
|
| 93 |
+
nodes=nodes,
|
| 94 |
+
visual_backend=FakeVisualBackend(),
|
| 95 |
+
t=0,
|
| 96 |
+
)
|
| 97 |
+
assert visual["t"].tolist() == [0]
|
| 98 |
+
assert visual["n"].tolist() == [1]
|
| 99 |
+
assert visual["v"].tolist() == [0]
|
| 100 |
+
assert visual["feat"].shape == (1, 256)
|
| 101 |
+
assert visual["feat"].dtype == np.float16
|
| 102 |
+
assert visual["type_clip32"].shape == (256, 32)
|
| 103 |
+
assert visual["visual_computed"].tolist() == [True]
|
| 104 |
+
assert visual["summary"]["features_written"] == 1
|
| 105 |
+
assert visual["summary"]["invalid_visible_pairs"] == 0
|
| 106 |
+
assert visual["summary"]["invisible_features_injected"] == 0
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
def test_final_fused_graph_has_packaged_loader_342d_feature_layout():
|
| 110 |
+
node_state = np.zeros((5, 32), dtype=np.float32)
|
| 111 |
+
node_state[[0, 1], 0] = 1
|
| 112 |
+
node_state[[0, 1], 1] = [9, 6]
|
| 113 |
+
node_state[[0, 1], 2:22] = np.arange(40, dtype=np.float32).reshape(2, 20)
|
| 114 |
+
|
| 115 |
+
view_visible = np.zeros((5, 3), dtype=bool)
|
| 116 |
+
view_visible[1, 0] = True
|
| 117 |
+
view_centroid = np.zeros((5, 3, 2), dtype=np.float16)
|
| 118 |
+
view_area = np.zeros((5, 3, 1), dtype=np.float16)
|
| 119 |
+
view_bbox = np.zeros((5, 3, 4), dtype=np.float16)
|
| 120 |
+
view_centroid[1, 0] = [0.25, 0.5]
|
| 121 |
+
view_area[1, 0, 0] = 0.1
|
| 122 |
+
view_bbox[1, 0] = [0.1, 0.2, 0.3, 0.4]
|
| 123 |
+
|
| 124 |
+
mask = np.zeros((256, 256), dtype=bool)
|
| 125 |
+
mask[10:20, 10:20] = True
|
| 126 |
+
visual = build_online_visual_features(
|
| 127 |
+
rgb_frames={0: np.full((256, 256, 3), 7, dtype=np.uint8)},
|
| 128 |
+
rle_rows=[{"n": 1, "v": 0, "rle": encode_rle(mask)}],
|
| 129 |
+
nodes=[{"category_text": "robot"}, {"category_text": "handle"}],
|
| 130 |
+
visual_backend=FakeVisualBackend(),
|
| 131 |
+
t=0,
|
| 132 |
+
)
|
| 133 |
+
graph = to_gnn_graph(
|
| 134 |
+
node_state=node_state,
|
| 135 |
+
static_edges=[{"src": 1, "dst": 0, "rel": "part_of"}],
|
| 136 |
+
dynamic_edges=[{"src": 0, "dst": 1, "rel": "contact"}],
|
| 137 |
+
view_visible=view_visible,
|
| 138 |
+
view_centroid=view_centroid,
|
| 139 |
+
view_area=view_area,
|
| 140 |
+
view_bbox=view_bbox,
|
| 141 |
+
visual_features=visual,
|
| 142 |
+
include_visual=True,
|
| 143 |
+
include_type_clip32=True,
|
| 144 |
+
include_full_view_evidence=True,
|
| 145 |
+
)
|
| 146 |
+
assert graph["slot_ids"].tolist() == [0, 1]
|
| 147 |
+
assert graph["x"].shape == (2, 342)
|
| 148 |
+
assert len(graph["feature_schema"]) == 342
|
| 149 |
+
assert graph["visual_feature_valid"].tolist() == [False, True]
|
| 150 |
+
assert graph["metadata"]["include_visual"] is True
|
| 151 |
+
assert graph["metadata"]["include_type_clip32"] is True
|
| 152 |
+
assert graph["metadata"]["include_full_view_evidence"] is True
|
| 153 |
+
assert graph["edge_attr"].shape == (2, 8)
|
| 154 |
+
assert graph["feature_schema"][30] == "visual_sam2_mean_000"
|
| 155 |
+
assert graph["feature_schema"][286] == "type_clip32_000"
|
| 156 |
+
assert graph["feature_schema"][318] == "view_right_visible"
|