Add reproducible SPARC release filtering script
Browse files- export_sparc_training_subset.py +132 -0
export_sparc_training_subset.py
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Materialize the exact SPARC VQA subset used by the released models."""
|
| 3 |
+
|
| 4 |
+
from __future__ import annotations
|
| 5 |
+
|
| 6 |
+
import argparse
|
| 7 |
+
import collections
|
| 8 |
+
import json
|
| 9 |
+
from pathlib import Path
|
| 10 |
+
|
| 11 |
+
import pyarrow as pa
|
| 12 |
+
import pyarrow.parquet as pq
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
VACANT_TASK_TYPES = {"vacant_goal", "vacant_start"}
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def parse_args() -> argparse.Namespace:
|
| 19 |
+
parser = argparse.ArgumentParser(description=__doc__)
|
| 20 |
+
parser.add_argument("input", type=Path, help="Raw SPARC VQA Parquet file")
|
| 21 |
+
parser.add_argument("output", type=Path, help="Filtered output Parquet file")
|
| 22 |
+
parser.add_argument("--quality-threshold", type=float, default=0.97)
|
| 23 |
+
parser.add_argument("--max-per-object", type=int, default=700)
|
| 24 |
+
parser.add_argument("--blocked-vacant-location", action="append")
|
| 25 |
+
return parser.parse_args()
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def trajectory_key(source: str | None, row_metadata: dict) -> str:
|
| 29 |
+
return "::".join(
|
| 30 |
+
(
|
| 31 |
+
source or "",
|
| 32 |
+
str(row_metadata.get("trajectory_name") or ""),
|
| 33 |
+
str(row_metadata.get("subtask_index") or ""),
|
| 34 |
+
)
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def object_name(row_metadata: dict) -> str:
|
| 39 |
+
task_object = row_metadata.get("task_obj_info") or {}
|
| 40 |
+
if isinstance(task_object, dict):
|
| 41 |
+
value = task_object.get("object") or row_metadata.get("object_phrase") or ""
|
| 42 |
+
else:
|
| 43 |
+
value = row_metadata.get("object_phrase") or ""
|
| 44 |
+
return str(value).strip().lower() or "<unknown>"
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def vacant_location(row_metadata: dict, task_type: str) -> str:
|
| 48 |
+
location_key = "target_location" if task_type == "vacant_goal" else "start_location"
|
| 49 |
+
task_object = row_metadata.get("task_obj_info") or {}
|
| 50 |
+
if isinstance(task_object, dict):
|
| 51 |
+
return str(task_object.get(location_key) or "")
|
| 52 |
+
return ""
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def is_blocked_vacant(row_metadata: dict, task_type: str, blocked_keywords: list[str]) -> bool:
|
| 56 |
+
if task_type not in VACANT_TASK_TYPES:
|
| 57 |
+
return False
|
| 58 |
+
location = vacant_location(row_metadata, task_type).lower()
|
| 59 |
+
return any(keyword in location for keyword in blocked_keywords)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def main() -> None:
|
| 63 |
+
args = parse_args()
|
| 64 |
+
blocked_keywords = [keyword.lower() for keyword in (args.blocked_vacant_location or ["gripper"])]
|
| 65 |
+
parquet = pq.ParquetFile(args.input)
|
| 66 |
+
candidates: list[tuple[float, int, int, str, dict, str]] = []
|
| 67 |
+
|
| 68 |
+
for row_group_index in range(parquet.metadata.num_row_groups):
|
| 69 |
+
table = parquet.read_row_group(row_group_index, columns=["metadata", "source", "task_type"])
|
| 70 |
+
metadata_column = table["metadata"].to_pylist()
|
| 71 |
+
source_column = table["source"].to_pylist()
|
| 72 |
+
task_type_column = table["task_type"].to_pylist()
|
| 73 |
+
for row_index, (metadata_string, source, task_type) in enumerate(
|
| 74 |
+
zip(metadata_column, source_column, task_type_column)
|
| 75 |
+
):
|
| 76 |
+
row_metadata = json.loads(metadata_string or "{}")
|
| 77 |
+
selected_score = float(row_metadata.get("selected_start_score") or 0.0)
|
| 78 |
+
if selected_score >= args.quality_threshold:
|
| 79 |
+
candidates.append(
|
| 80 |
+
(selected_score, row_group_index, row_index, source, row_metadata, task_type)
|
| 81 |
+
)
|
| 82 |
+
print(
|
| 83 |
+
f"Scanned row group {row_group_index + 1}/{parquet.metadata.num_row_groups}; "
|
| 84 |
+
f"{len(candidates):,} examples pass the score threshold",
|
| 85 |
+
flush=True,
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
candidates.sort(key=lambda candidate: candidate[0], reverse=True)
|
| 89 |
+
|
| 90 |
+
kept_trajectories: set[str] = set()
|
| 91 |
+
object_counts: collections.Counter[str] = collections.Counter()
|
| 92 |
+
for _, _, _, source, row_metadata, _ in candidates:
|
| 93 |
+
key = trajectory_key(source, row_metadata)
|
| 94 |
+
if key in kept_trajectories:
|
| 95 |
+
continue
|
| 96 |
+
name = object_name(row_metadata)
|
| 97 |
+
if object_counts[name] < args.max_per_object:
|
| 98 |
+
kept_trajectories.add(key)
|
| 99 |
+
object_counts[name] += 1
|
| 100 |
+
|
| 101 |
+
selected_by_row_group: dict[int, list[int]] = collections.defaultdict(list)
|
| 102 |
+
for _, row_group_index, row_index, source, row_metadata, task_type in candidates:
|
| 103 |
+
if trajectory_key(source, row_metadata) in kept_trajectories and not is_blocked_vacant(
|
| 104 |
+
row_metadata, task_type, blocked_keywords
|
| 105 |
+
):
|
| 106 |
+
selected_by_row_group[row_group_index].append(row_index)
|
| 107 |
+
|
| 108 |
+
selected_count = sum(len(indices) for indices in selected_by_row_group.values())
|
| 109 |
+
output = args.output
|
| 110 |
+
output.parent.mkdir(parents=True, exist_ok=True)
|
| 111 |
+
with pq.ParquetWriter(str(output), parquet.schema_arrow, compression="snappy") as writer:
|
| 112 |
+
for row_group_index in range(parquet.metadata.num_row_groups):
|
| 113 |
+
row_indices = selected_by_row_group.get(row_group_index)
|
| 114 |
+
if not row_indices:
|
| 115 |
+
continue
|
| 116 |
+
table = parquet.read_row_group(row_group_index)
|
| 117 |
+
writer.write_table(table.take(pa.array(row_indices, type=pa.int64())))
|
| 118 |
+
print(
|
| 119 |
+
f"Wrote row group {row_group_index + 1}/{parquet.metadata.num_row_groups}; "
|
| 120 |
+
f"{len(row_indices):,} selected examples",
|
| 121 |
+
flush=True,
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
print(
|
| 125 |
+
f"Wrote {selected_count:,} examples to {output} using score >= {args.quality_threshold}, "
|
| 126 |
+
f"max {args.max_per_object} trajectories per object, and blocked vacant locations {blocked_keywords}",
|
| 127 |
+
flush=True,
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
if __name__ == "__main__":
|
| 132 |
+
main()
|