| from __future__ import annotations |
|
|
| import argparse |
| import json |
| from pathlib import Path |
|
|
| import numpy as np |
| from tqdm import tqdm |
|
|
|
|
| DEFAULT_TEST_REPLAYS = [ |
| "241019_TESvsT1_3Set_cropped", |
| "241020_GENvsFLY_3Set_cropped", |
| "241026_WBGvsBLG_3Set_cropped", |
| "241027_T1vsGEN_3Set_cropped", |
| "241102_T1vsBLG_3Set_cropped", |
| ] |
|
|
|
|
| def load_legacy_pair(path: Path) -> tuple[np.ndarray, np.ndarray]: |
| arr = np.load(path, allow_pickle=True) |
| frame = np.asarray(arr[0], dtype=np.uint8) |
| mask = np.asarray(arr[1], dtype=np.uint8) |
| if frame.shape != (2, 256, 256): |
| raise ValueError(f"Expected frame shape (2, 256, 256), got {frame.shape} in {path}") |
| if mask.shape != (1, 256, 256): |
| raise ValueError(f"Expected mask shape (1, 256, 256), got {mask.shape} in {path}") |
| return frame, mask |
|
|
|
|
| def export_replay(replay_dir: Path, out_dir: Path, chunk_size: int) -> list[dict]: |
| replay_id = replay_dir.name |
| files = sorted(replay_dir.glob("*.npy"), key=lambda item: int(item.stem)) |
| shards: list[dict] = [] |
|
|
| for chunk_start in tqdm(range(0, len(files), chunk_size), desc=replay_id): |
| chunk_files = files[chunk_start : chunk_start + chunk_size] |
| frames, masks, indices = [], [], [] |
| for path in chunk_files: |
| frame, mask = load_legacy_pair(path) |
| frames.append(frame) |
| masks.append(mask) |
| indices.append(int(path.stem)) |
|
|
| start_frame = indices[0] |
| shard_name = f"{replay_id}_{start_frame:06d}_{indices[-1]:06d}.npz" |
| shard_path = out_dir / shard_name |
| np.savez_compressed( |
| shard_path, |
| frames=np.stack(frames).astype(np.uint8), |
| masks=np.stack(masks).astype(np.uint8), |
| frame_indices=np.asarray(indices, dtype=np.int32), |
| ) |
| shards.append( |
| { |
| "replay_id": replay_id, |
| "path": f"shards/{shard_name}", |
| "start_frame": int(start_frame), |
| "num_frames": int(len(indices)), |
| } |
| ) |
| return shards |
|
|
|
|
| def main() -> None: |
| parser = argparse.ArgumentParser(description="Convert legacy per-frame .npy files into compressed release shards.") |
| parser.add_argument("--legacy-root", required=True, help="Path to data_viewport_youtube_1118") |
| parser.add_argument("--output-root", default="data/processed", help="Release dataset output root") |
| parser.add_argument("--chunk-size", type=int, default=1024) |
| parser.add_argument("--test-replays", nargs="+", default=DEFAULT_TEST_REPLAYS) |
| args = parser.parse_args() |
|
|
| legacy_root = Path(args.legacy_root) |
| output_root = Path(args.output_root) |
| shard_dir = output_root / "shards" |
| shard_dir.mkdir(parents=True, exist_ok=True) |
|
|
| replay_dirs = sorted([path for path in legacy_root.iterdir() if path.is_dir()]) |
| test_replays = set(args.test_replays) |
| train_replays = [path.name for path in replay_dirs if path.name not in test_replays] |
|
|
| shards: list[dict] = [] |
| for replay_dir in replay_dirs: |
| shards.extend(export_replay(replay_dir, shard_dir, args.chunk_size)) |
|
|
| manifest = { |
| "format": "lol_viewport_sharded_npz_v1", |
| "frame_shape": [2, 256, 256], |
| "mask_shape": [1, 256, 256], |
| "role_values": {"0": "background", "1": "TOP", "2": "JUNGLE", "3": "MID", "4": "BOT", "5": "SUPPORT"}, |
| "splits": { |
| "train": train_replays, |
| "test": list(args.test_replays), |
| "validation": "sampled from train during optimization", |
| }, |
| "shards": shards, |
| } |
| with (output_root / "manifest.json").open("w", encoding="utf-8") as f: |
| json.dump(manifest, f, indent=2) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|