loader: split= support with guard assertion
Browse files
examples/react_video_dataset.py
CHANGED
|
@@ -80,7 +80,8 @@ def _decode_frames(mp4_path: Path, frame_indices, depth=False):
|
|
| 80 |
class ReactVideoDataset:
|
| 81 |
def __init__(self, task_root, window_length=16, stride=1, window_step=None,
|
| 82 |
mode="segment", streams=ALL_STREAMS, skip_bad=True,
|
| 83 |
-
which_sensors="any", load_depth=False, tactile_latency=0
|
|
|
|
| 84 |
self.root = Path(task_root)
|
| 85 |
self.W = window_length
|
| 86 |
self.stride = stride
|
|
@@ -102,6 +103,33 @@ class ReactVideoDataset:
|
|
| 102 |
self._TACT_COLS = ("tactile_left_intensity", "tactile_right_intensity",
|
| 103 |
"tactile_left_mixed", "tactile_right_mixed")
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
self.segments = json.loads((self.root / "segments.json").read_text())["segments"]
|
| 106 |
self.bad = json.loads((self.root / "bad_frames.json").read_text())["episodes"]
|
| 107 |
self.index = self._build_index()
|
|
@@ -123,16 +151,37 @@ class ReactVideoDataset:
|
|
| 123 |
m[max(0, a):min(T, b + 1)] = True
|
| 124 |
return m
|
| 125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
def _build_index(self):
|
| 127 |
items = []
|
| 128 |
span = (self.W - 1) * self.stride + 1
|
| 129 |
lat = self.tactile_latency # tactile read at idx+lat must stay in bounds
|
|
|
|
| 130 |
if self.mode == "segment":
|
| 131 |
for s in self.segments:
|
| 132 |
ek, a, b = s["source_episode"], s["frame_range"][0], s["frame_range"][1]
|
| 133 |
start = a
|
| 134 |
while start + span - 1 + lat <= b:
|
| 135 |
-
|
|
|
|
| 136 |
start += self.step
|
| 137 |
else: # window over whole episode
|
| 138 |
eps = sorted({s["source_episode"] for s in self.segments})
|
|
@@ -142,7 +191,7 @@ class ReactVideoDataset:
|
|
| 142 |
start = 0
|
| 143 |
while start + span - 1 + lat < T:
|
| 144 |
idx = range(start, start + span, self.stride)
|
| 145 |
-
if not (self.skip_bad and bad[list(idx)].any()):
|
| 146 |
items.append((ek, start))
|
| 147 |
start += self.step
|
| 148 |
return items
|
|
|
|
| 80 |
class ReactVideoDataset:
|
| 81 |
def __init__(self, task_root, window_length=16, stride=1, window_step=None,
|
| 82 |
mode="segment", streams=ALL_STREAMS, skip_bad=True,
|
| 83 |
+
which_sensors="any", load_depth=False, tactile_latency=0,
|
| 84 |
+
split="all", splits_file="splits.json"):
|
| 85 |
self.root = Path(task_root)
|
| 86 |
self.W = window_length
|
| 87 |
self.stride = stride
|
|
|
|
| 103 |
self._TACT_COLS = ("tactile_left_intensity", "tactile_right_intensity",
|
| 104 |
"tactile_left_mixed", "tactile_right_mixed")
|
| 105 |
|
| 106 |
+
# SPLIT. "train" | "test" | "all". The release holds out INTERVALS from
|
| 107 |
+
# inside episodes rather than whole episodes: there are only 32 of them,
|
| 108 |
+
# and a short-horizon world model must generalise over dynamics within a
|
| 109 |
+
# scene, not over scenes.
|
| 110 |
+
#
|
| 111 |
+
# A training window starting shortly BEFORE a held-out interval still
|
| 112 |
+
# contains its frames, so `splits.json` carries a guard of
|
| 113 |
+
# max_train_window - 1 and this loader REFUSES a longer window instead
|
| 114 |
+
# of leaking — a leak here leaves no trace in any metric.
|
| 115 |
+
self.split = split
|
| 116 |
+
self.splits = None
|
| 117 |
+
if split != "all":
|
| 118 |
+
sp = self.root / splits_file
|
| 119 |
+
if not sp.is_file():
|
| 120 |
+
raise FileNotFoundError(
|
| 121 |
+
f"split={split!r} needs {sp}; pass split='all' to use every "
|
| 122 |
+
f"frame, or rebuild it with scripts/build_splits.py")
|
| 123 |
+
with open(sp) as f:
|
| 124 |
+
self.splits = json.load(f)
|
| 125 |
+
span = (window_length - 1) * stride + 1 + int(tactile_latency)
|
| 126 |
+
if span - 1 > self.splits["guard_frames"]:
|
| 127 |
+
raise ValueError(
|
| 128 |
+
f"window spans {span} frames but the split has "
|
| 129 |
+
f"guard_frames={self.splits['guard_frames']}; a window this "
|
| 130 |
+
f"long would overlap held-out intervals. Rebuild with "
|
| 131 |
+
f"max_train_window >= {span}.")
|
| 132 |
+
|
| 133 |
self.segments = json.loads((self.root / "segments.json").read_text())["segments"]
|
| 134 |
self.bad = json.loads((self.root / "bad_frames.json").read_text())["episodes"]
|
| 135 |
self.index = self._build_index()
|
|
|
|
| 151 |
m[max(0, a):min(T, b + 1)] = True
|
| 152 |
return m
|
| 153 |
|
| 154 |
+
def _split_filter(self, span):
|
| 155 |
+
"""(keep_fn) deciding whether a window starting at `s` is in this split."""
|
| 156 |
+
if self.splits is None:
|
| 157 |
+
return lambda ek, s: True
|
| 158 |
+
E = self.splits["episodes"]
|
| 159 |
+
|
| 160 |
+
def keep(ek, s):
|
| 161 |
+
e = E.get(ek)
|
| 162 |
+
if e is None:
|
| 163 |
+
return self.split == "train" # unlisted episode -> train
|
| 164 |
+
if e["whole"] == "test":
|
| 165 |
+
return self.split == "test"
|
| 166 |
+
if self.split == "test":
|
| 167 |
+
# the WHOLE window must lie inside one held-out interval
|
| 168 |
+
return any(a <= s and s + span - 1 <= b for a, b in e["test"])
|
| 169 |
+
# train: reject starts in [a - (span-1), b], not just [a, b]
|
| 170 |
+
return not any(a - (span - 1) <= s <= b for a, b in e["test"])
|
| 171 |
+
return keep
|
| 172 |
+
|
| 173 |
def _build_index(self):
|
| 174 |
items = []
|
| 175 |
span = (self.W - 1) * self.stride + 1
|
| 176 |
lat = self.tactile_latency # tactile read at idx+lat must stay in bounds
|
| 177 |
+
keep = self._split_filter(span + lat)
|
| 178 |
if self.mode == "segment":
|
| 179 |
for s in self.segments:
|
| 180 |
ek, a, b = s["source_episode"], s["frame_range"][0], s["frame_range"][1]
|
| 181 |
start = a
|
| 182 |
while start + span - 1 + lat <= b:
|
| 183 |
+
if keep(ek, start):
|
| 184 |
+
items.append((ek, start))
|
| 185 |
start += self.step
|
| 186 |
else: # window over whole episode
|
| 187 |
eps = sorted({s["source_episode"] for s in self.segments})
|
|
|
|
| 191 |
start = 0
|
| 192 |
while start + span - 1 + lat < T:
|
| 193 |
idx = range(start, start + span, self.stride)
|
| 194 |
+
if not (self.skip_bad and bad[list(idx)].any()) and keep(ek, start):
|
| 195 |
items.append((ek, start))
|
| 196 |
start += self.step
|
| 197 |
return items
|