Download scripts/build_detection_labels.py from ZenoNing/ConsDrive: direct link, hf CLI and curl.
- Browser
- Download file 3.71 kB
-
https://huggingface.co/ZenoNing/ConsDrive/resolve/main/scripts/build_detection_labels.py
- Command line
-
hf download hf://ZenoNing/ConsDrive/scripts/build_detection_labels.py
-
curl -L -o build_detection_labels.py https://huggingface.co/ZenoNing/ConsDrive/resolve/main/scripts/build_detection_labels.py
3.71 kB
| """Build the stage-2 detection / traffic-control labels for every SimLingo route. | |
| Writes <data_root>/detection_labels/<...>/<route>/labels.npz (layout in dataloader.detection_labels). | |
| Usage: python scripts/build_detection_labels.py [--data-root simlingo_data] [--workers 192] [--limit N] | |
| """ | |
| import argparse | |
| import glob | |
| import gzip | |
| import os | |
| import sys | |
| import time | |
| from multiprocessing import Pool | |
| from pathlib import Path | |
| import numpy as np | |
| import ujson | |
| sys.path.insert(0, str(Path(__file__).resolve().parents[1])) | |
| from dataloader.detection_labels import EGO_DIM, MAX_OBJECTS, OBJ_DIM, frame_labels # noqa: E402 | |
| def output_path(route_dir, data_root): | |
| rel = Path(route_dir).relative_to(Path(data_root) / "data") | |
| return Path(data_root) / "detection_labels" / rel / "labels.npz" | |
| def build_route(args): | |
| route_dir, data_root = args | |
| out = output_path(route_dir, data_root) | |
| if out.exists(): | |
| return route_dir, "skipped", None | |
| box_files = sorted(glob.glob(f"{route_dir}/boxes/*.json.gz")) | |
| if not box_files: | |
| return route_dir, "no_boxes", None | |
| objects = np.zeros((len(box_files), MAX_OBJECTS, OBJ_DIM), dtype=np.float32) | |
| count = np.zeros(len(box_files), dtype=np.int16) | |
| ego = np.zeros((len(box_files), EGO_DIM), dtype=np.float32) | |
| try: | |
| for f, box_file in enumerate(box_files): | |
| frame = int(Path(box_file).name.split(".")[0]) | |
| if frame != f: | |
| return route_dir, f"error: non-contiguous frames at {frame}", None | |
| with gzip.open(box_file, "rt") as fh: | |
| boxes = ujson.load(fh) | |
| with gzip.open(f"{route_dir}/measurements/{frame:04d}.json.gz", "rt") as fh: | |
| measurement = ujson.load(fh) | |
| rows, ego[f] = frame_labels(boxes, measurement) | |
| objects[f, :len(rows)] = rows | |
| count[f] = len(rows) | |
| except Exception as exc: # keep the pool alive; report the route | |
| return route_dir, f"error: {type(exc).__name__}: {exc}", None | |
| out.parent.mkdir(parents=True, exist_ok=True) | |
| tmp = out.with_suffix(".tmp.npz") | |
| np.savez(tmp, objects=objects, count=count, ego=ego) | |
| os.replace(tmp, out) | |
| return route_dir, "built", (len(box_files), int(count.sum())) | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--data-root", default="simlingo_data") | |
| ap.add_argument("--workers", type=int, default=max(1, min(192, (os.cpu_count() or 2) - 8))) | |
| ap.add_argument("--limit", type=int, default=0, help="only the first N routes (smoke test)") | |
| args = ap.parse_args() | |
| data_root = str(Path(args.data_root).resolve()) | |
| routes = sorted(glob.glob(f"{data_root}/data/simlingo/*/*/*/Town*")) | |
| if args.limit: | |
| routes = routes[: args.limit] | |
| print(f"{len(routes)} routes, {args.workers} workers", flush=True) | |
| t0 = time.time() | |
| frames = objects = 0 | |
| status, errors = {}, [] | |
| with Pool(args.workers) as pool: | |
| for i, (route, state, stats) in enumerate(pool.imap_unordered( | |
| build_route, [(r, data_root) for r in routes], chunksize=2), 1): | |
| key = state.split(":")[0] | |
| status[key] = status.get(key, 0) + 1 | |
| if stats is not None: | |
| frames += stats[0] | |
| objects += stats[1] | |
| if key == "error": | |
| errors.append((route, state)) | |
| if i % 1000 == 0 or i == len(routes): | |
| print(f"[{i}/{len(routes)}] {time.time() - t0:.0f}s {status}", flush=True) | |
| print(f"frames {frames}, labelled objects/frame {objects / max(frames, 1):.2f}", flush=True) | |
| for route, state in errors[:20]: | |
| print("ERROR", route, state) | |
| if __name__ == "__main__": | |
| main() | |