| |
| """Create public metadata and split indexes from the canonical CARLA tree.""" |
|
|
| from __future__ import annotations |
|
|
| import argparse |
| import json |
| import re |
| import shutil |
| from datetime import datetime, timezone |
| from pathlib import Path |
|
|
| WEATHERS = ("ClearDay", "ClearNight", "HeavyFoggyNight", "HeavyRainFoggyNight") |
| MODALITIES = ("image_2", "gt_image_2", "depth_u16", "depth_meters", "normal", "calib") |
| STEM_RE = re.compile(r"^(Town04|Town05|Town06)_(ClearDay|ClearNight|HeavyFoggyNight|HeavyRainFoggyNight)_(\d{6})$") |
|
|
|
|
| def sanitize_manifest(source: Path, target: Path) -> None: |
| data = json.loads(source.read_text(encoding="utf-8")) |
| |
| if isinstance(data, dict): |
| if "source" in data: |
| data["source"] = "CARLA_Unified_Dataset (local source; not redistributed)" |
| if "output" in data: |
| data["output"] = "." |
| target.write_text(json.dumps(data, indent=2) + "\n", encoding="utf-8") |
|
|
|
|
| def main() -> int: |
| ap = argparse.ArgumentParser() |
| ap.add_argument("--source-root", type=Path, required=True) |
| ap.add_argument("--release-root", type=Path, required=True) |
| args = ap.parse_args() |
| src = args.source_root.resolve() |
| out = args.release_root.resolve() |
| (out / "metadata").mkdir(parents=True, exist_ok=True) |
| (out / "splits").mkdir(parents=True, exist_ok=True) |
| sanitize_manifest(src / "MANIFEST_reconciled_town_split.json", out / "metadata/MANIFEST_reconciled_town_split.json") |
| shutil.copy2(src / "02_reconciled_protocol.json", out / "metadata/02_reconciled_protocol.json") |
| shutil.copy2(src / "SELECTED_STEMS.sha256", out / "metadata/SELECTED_STEMS.sha256") |
|
|
| split_records = {} |
| for split in ("training", "validation"): |
| stems = sorted(p.stem for p in (src / split / "image_2").glob("*.png")) |
| records = [] |
| for stem in stems: |
| match = STEM_RE.match(stem) |
| if not match: |
| raise ValueError(f"invalid stem: {stem}") |
| town, weather, frame = match.groups() |
| records.append((stem, town, weather, int(frame))) |
| split_records[split] = records |
| (out / "splits" / f"{split}.txt").write_text( |
| "".join(f"{r[0]}\n" for r in records), encoding="utf-8" |
| ) |
| (out / "splits" / f"{split}.tsv").write_text( |
| "stem\ttown\tweather\tframe\n" + "".join( |
| f"{stem}\t{town}\t{weather}\t{frame:06d}\n" for stem, town, weather, frame in records |
| ), encoding="utf-8" |
| ) |
|
|
| source_manifest = json.loads((src / "MANIFEST_reconciled_town_split.json").read_text(encoding="utf-8")) |
| protocol = json.loads((src / "02_reconciled_protocol.json").read_text(encoding="utf-8")) |
| archives = {} |
| archive_manifest = out / "archive_manifest.json" |
| if archive_manifest.exists(): |
| archives = json.loads(archive_manifest.read_text(encoding="utf-8")).get("archives", []) |
| manifest = { |
| "dataset": "CARLA-MWRS", |
| "version": "1.0.0", |
| "status": "PASS", |
| "release_date": datetime.now(timezone.utc).date().isoformat(), |
| "protocol": protocol.get("protocol", "CARLA-MWRS Town05+Town06 training / Town04 held-out validation"), |
| "selection_seed": source_manifest.get("seed", protocol.get("seed", 42)), |
| "weathers": list(WEATHERS), |
| "model_input_size_hw": protocol.get("input_size", [512, 1024]), |
| "materialized_file_size_hw": [384, 1248], |
| "splits": { |
| split: { |
| "samples": len(records), |
| "towns": sorted({r[1] for r in records}), |
| "weather_counts": {w: sum(r[2] == w for r in records) for w in WEATHERS}, |
| "stem_sha256": source_manifest.get("filename_sha256", {}).get(split), |
| "archives": [f"{split}/{m}.tar.zst" for m in MODALITIES], |
| } |
| for split, records in split_records.items() |
| }, |
| "modalities": { |
| "image_2": {"extension": ".png", "shape": [384, 1248, 3], "dtype": "uint8", "encoding": "RGB PNG"}, |
| "gt_image_2": {"extension": ".png", "shape": [384, 1248], "dtype": "uint8", "values": [0, 255], "encoding": "binary road mask"}, |
| "depth_u16": {"extension": ".png", "shape": [384, 1248], "dtype": "uint16", "unit": "millimetres", "saturation": 65535}, |
| "depth_meters": {"extension": ".npy", "shape": [384, 1248], "dtype": "float32", "unit": "metres"}, |
| "normal": {"extension": ".npy", "shape": [3, 384, 1248], "dtype": "float32", "layout": "C,H,W", "unit": "unit camera-frame vector"}, |
| "calib": {"extension": ".txt", "records": ["P2: 12 values (3x4)", "Vehicle_pos: 3 values"]}, |
| }, |
| "depth_u16_conversion": "clip(floor(float32(depth_meters) * float32(1000)), 0, 65535).astype(uint16)", |
| "archive_layout": "one deterministic tar.zst per split and modality; archive members are <modality>/<stem><extension>", |
| "archives": archives, |
| "source_manifests": [ |
| "metadata/MANIFEST_reconciled_town_split.json", |
| "metadata/02_reconciled_protocol.json", |
| "metadata/SELECTED_STEMS.sha256", |
| ], |
| "validation": { |
| "validator": "scripts/validate_release.py", |
| "source_validation_report": "source_validation_report.json", |
| "source_file_hashes": "SOURCE_FILES.sha256", |
| "archive_validator": "scripts/validate_archives.py", |
| "archive_validation_report": "archive_validation_report.json", |
| }, |
| } |
| (out / "dataset_manifest.json").write_text(json.dumps(manifest, indent=2, sort_keys=True) + "\n", encoding="utf-8") |
| print(f"wrote metadata for {sum(len(v) for v in split_records.values())} samples to {out}") |
| return 0 |
|
|
|
|
| if __name__ == "__main__": |
| raise SystemExit(main()) |
|
|