Commit ·
61563db
1
Parent(s): 1c67cee
updated config generation files.
Browse files
tools/__pycache__/make_configs.cpython-313.pyc
ADDED
|
Binary file (6.59 kB). View file
|
|
|
tools/__pycache__/make_master_configs.cpython-313.pyc
ADDED
|
Binary file (12.3 kB). View file
|
|
|
tools/make_configs.py
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import json
|
| 3 |
+
import random
|
| 4 |
+
import zlib
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
from typing import Dict, List
|
| 7 |
+
|
| 8 |
+
from make_master_configs import (
|
| 9 |
+
build_light_index,
|
| 10 |
+
build_master_configs,
|
| 11 |
+
rows_from_files,
|
| 12 |
+
write_rows_to_parquet,
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def _stable_seed(base_seed: int, cfg_name: str, tag: str) -> int:
|
| 17 |
+
h = zlib.adler32(f"{cfg_name}::{tag}".encode("utf-8")) & 0xFFFFFFFF
|
| 18 |
+
return (base_seed * 1_000_003 + h * 97) & 0xFFFFFFFF
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def _sample_k(items: List[str], k: int, seed: int) -> List[str]:
|
| 22 |
+
if k <= 0:
|
| 23 |
+
return []
|
| 24 |
+
if k >= len(items):
|
| 25 |
+
return sorted(items)
|
| 26 |
+
rng = random.Random(seed)
|
| 27 |
+
return sorted(rng.sample(sorted(items), k))
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def build_fewshot_split_map(
|
| 31 |
+
base_split_map: Dict[str, Dict[str, List[str]]],
|
| 32 |
+
seed: int,
|
| 33 |
+
) -> Dict[str, Dict[str, List[str]]]:
|
| 34 |
+
out: Dict[str, Dict[str, List[str]]] = {}
|
| 35 |
+
|
| 36 |
+
for base_cfg, splits in base_split_map.items():
|
| 37 |
+
base_train = sorted(splits["train"])
|
| 38 |
+
base_id_test = sorted(splits["id_test"])
|
| 39 |
+
base_ood = sorted(splits["ood_test"])
|
| 40 |
+
|
| 41 |
+
k_targets = {
|
| 42 |
+
"1": 1,
|
| 43 |
+
"10": 10,
|
| 44 |
+
"100": 100,
|
| 45 |
+
"all": len(base_ood),
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
for label, k_target in k_targets.items():
|
| 49 |
+
k_actual = min(k_target, len(base_ood))
|
| 50 |
+
chosen = _sample_k(base_ood, k_actual, _stable_seed(seed, base_cfg, f"fs{label}"))
|
| 51 |
+
chosen_set = set(chosen)
|
| 52 |
+
|
| 53 |
+
fs_train = sorted(set(base_train) | chosen_set)
|
| 54 |
+
fs_id_test = list(base_id_test)
|
| 55 |
+
fs_ood = sorted(set(base_ood) - chosen_set)
|
| 56 |
+
|
| 57 |
+
fs_cfg_name = f"{base_cfg}__fs{label}"
|
| 58 |
+
out[fs_cfg_name] = {
|
| 59 |
+
"train": fs_train,
|
| 60 |
+
"id_test": fs_id_test,
|
| 61 |
+
"ood_test": fs_ood,
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
return out
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def write_configs_from_split_map(
|
| 68 |
+
split_map: Dict[str, Dict[str, List[str]]],
|
| 69 |
+
out_configs_dir: Path,
|
| 70 |
+
rows_per_shard: int,
|
| 71 |
+
light_index: Dict[str, Dict],
|
| 72 |
+
images_root: Path,
|
| 73 |
+
) -> None:
|
| 74 |
+
out_configs_dir.mkdir(parents=True, exist_ok=True)
|
| 75 |
+
for cfg_name, splits in split_map.items():
|
| 76 |
+
cfg_dir = out_configs_dir / cfg_name
|
| 77 |
+
cfg_dir.mkdir(parents=True, exist_ok=True)
|
| 78 |
+
for split in ("train", "id_test", "ood_test"):
|
| 79 |
+
write_rows_to_parquet(
|
| 80 |
+
rows_from_files(splits[split], light_index, images_root),
|
| 81 |
+
cfg_dir,
|
| 82 |
+
split,
|
| 83 |
+
rows_per_shard,
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def main() -> None:
|
| 88 |
+
ap = argparse.ArgumentParser()
|
| 89 |
+
ap.add_argument("--src_root", required=True, help="root containing metadata.csv and world_images/")
|
| 90 |
+
ap.add_argument("--master_dir", required=True, help="hf_repo/data/master (parquet shards)")
|
| 91 |
+
ap.add_argument("--out_configs_dir", required=True, help="hf_repo/data/configs")
|
| 92 |
+
ap.add_argument("--train_ratio", type=float, default=0.9)
|
| 93 |
+
ap.add_argument("--seed", type=int, default=42)
|
| 94 |
+
ap.add_argument("--rows_per_shard", type=int, default=16)
|
| 95 |
+
ap.add_argument("--scan_batch_size", type=int, default=32)
|
| 96 |
+
ap.add_argument("--min_pool", type=int, default=200)
|
| 97 |
+
ap.add_argument("--manifest_path", default=None, help="optional path to write JSON manifest")
|
| 98 |
+
args = ap.parse_args()
|
| 99 |
+
|
| 100 |
+
src_root = Path(args.src_root)
|
| 101 |
+
out_configs_dir = Path(args.out_configs_dir)
|
| 102 |
+
images_root = src_root / "world_images"
|
| 103 |
+
|
| 104 |
+
base_split_map = build_master_configs(
|
| 105 |
+
src_root=src_root,
|
| 106 |
+
master_dir=Path(args.master_dir),
|
| 107 |
+
out_configs_dir=out_configs_dir,
|
| 108 |
+
train_ratio=args.train_ratio,
|
| 109 |
+
seed=args.seed,
|
| 110 |
+
rows_per_shard=args.rows_per_shard,
|
| 111 |
+
scan_batch_size=args.scan_batch_size,
|
| 112 |
+
min_pool=args.min_pool,
|
| 113 |
+
write_parquet=False,
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
fewshot_split_map = build_fewshot_split_map(base_split_map, seed=args.seed)
|
| 117 |
+
|
| 118 |
+
all_split_map = {}
|
| 119 |
+
all_split_map.update(base_split_map)
|
| 120 |
+
all_split_map.update(fewshot_split_map)
|
| 121 |
+
|
| 122 |
+
light_index = build_light_index(str(args.master_dir), args.scan_batch_size)
|
| 123 |
+
write_configs_from_split_map(
|
| 124 |
+
split_map=all_split_map,
|
| 125 |
+
out_configs_dir=out_configs_dir,
|
| 126 |
+
rows_per_shard=args.rows_per_shard,
|
| 127 |
+
light_index=light_index,
|
| 128 |
+
images_root=images_root,
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
base_cfgs = sorted(base_split_map.keys())
|
| 132 |
+
fs_cfgs = sorted(fewshot_split_map.keys())
|
| 133 |
+
manifest = {
|
| 134 |
+
"base_config_count": len(base_cfgs),
|
| 135 |
+
"fewshot_per_base": ["fs1", "fs10", "fs100", "fsall"],
|
| 136 |
+
"total_config_count": len(all_split_map),
|
| 137 |
+
"base_configs": base_cfgs,
|
| 138 |
+
"fewshot_configs": fs_cfgs,
|
| 139 |
+
"all_configs": sorted(all_split_map.keys()),
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
if args.manifest_path:
|
| 143 |
+
manifest_path = Path(args.manifest_path)
|
| 144 |
+
manifest_path.parent.mkdir(parents=True, exist_ok=True)
|
| 145 |
+
manifest_path.write_text(json.dumps(manifest, indent=2))
|
| 146 |
+
|
| 147 |
+
print(json.dumps(manifest, indent=2))
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
if __name__ == "__main__":
|
| 151 |
+
main()
|
tools/make_master_configs.py
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import json
|
| 3 |
+
import random
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
from typing import Dict, List, Tuple
|
| 6 |
+
|
| 7 |
+
import pandas as pd
|
| 8 |
+
import pyarrow as pa
|
| 9 |
+
import pyarrow.dataset as ds
|
| 10 |
+
import pyarrow.parquet as pq
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def split_pool(items: List[str], train_ratio: float, seed: int) -> Tuple[List[str], List[str]]:
|
| 14 |
+
rng = random.Random(seed)
|
| 15 |
+
items = list(items)
|
| 16 |
+
rng.shuffle(items)
|
| 17 |
+
n_train = int(round(train_ratio * len(items)))
|
| 18 |
+
return items[:n_train], items[n_train:]
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def ensure_min(name: str, files: List[str], min_count: int) -> None:
|
| 22 |
+
if len(files) < min_count:
|
| 23 |
+
raise RuntimeError(f"{name}: only {len(files)} files (<{min_count}).")
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def build_light_index(master_dir: str, scan_batch_size: int) -> Dict[str, Dict]:
|
| 27 |
+
master = ds.dataset(master_dir, format="parquet")
|
| 28 |
+
cols = [
|
| 29 |
+
"image_id",
|
| 30 |
+
"filename",
|
| 31 |
+
"country",
|
| 32 |
+
"state",
|
| 33 |
+
"zone",
|
| 34 |
+
"region",
|
| 35 |
+
"width",
|
| 36 |
+
"height",
|
| 37 |
+
"coco_annotations",
|
| 38 |
+
"coco_categories",
|
| 39 |
+
]
|
| 40 |
+
scanner = master.scanner(columns=cols, batch_size=scan_batch_size)
|
| 41 |
+
|
| 42 |
+
idx: Dict[str, Dict] = {}
|
| 43 |
+
for batch in scanner.to_batches():
|
| 44 |
+
b = batch.to_pydict()
|
| 45 |
+
n = len(b["filename"])
|
| 46 |
+
for i in range(n):
|
| 47 |
+
fn = b["filename"][i]
|
| 48 |
+
idx[fn] = {k: b[k][i] for k in cols}
|
| 49 |
+
return idx
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def write_rows_to_parquet(rows_iter, out_dir: Path, split: str, rows_per_shard: int) -> None:
|
| 53 |
+
out_dir.mkdir(parents=True, exist_ok=True)
|
| 54 |
+
buf = []
|
| 55 |
+
shard = 0
|
| 56 |
+
for row in rows_iter:
|
| 57 |
+
buf.append(row)
|
| 58 |
+
if len(buf) >= rows_per_shard:
|
| 59 |
+
table = pa.Table.from_pylist(buf)
|
| 60 |
+
pq.write_table(table, out_dir / f"{split}-{shard:05d}.parquet", compression="zstd")
|
| 61 |
+
buf = []
|
| 62 |
+
shard += 1
|
| 63 |
+
if buf:
|
| 64 |
+
table = pa.Table.from_pylist(buf)
|
| 65 |
+
pq.write_table(table, out_dir / f"{split}-{shard:05d}.parquet", compression="zstd")
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def rows_from_files(filenames: List[str], light_index: Dict[str, Dict], images_root: Path):
|
| 69 |
+
for fn in filenames:
|
| 70 |
+
base = light_index.get(fn)
|
| 71 |
+
if base is None:
|
| 72 |
+
continue
|
| 73 |
+
img_path = images_root / fn
|
| 74 |
+
yield {**base, "image_bytes": img_path.read_bytes()}
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def _normalize_label(s: str) -> str:
|
| 78 |
+
return str(s).strip().lower()
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def _karnataka_elevation_files(meta: pd.DataFrame, label: str) -> List[str]:
|
| 82 |
+
kar = meta[(meta["country"] == "India") & (meta["state"] == "Karnataka")].copy()
|
| 83 |
+
kar["elevation_class_zonewise"] = kar["elevation_class_zonewise"].astype(str).map(_normalize_label)
|
| 84 |
+
return kar[kar["elevation_class_zonewise"] == label]["filename"].tolist()
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def collect_base_splits(
|
| 88 |
+
meta: pd.DataFrame,
|
| 89 |
+
train_ratio: float,
|
| 90 |
+
seed: int,
|
| 91 |
+
min_pool: int,
|
| 92 |
+
) -> Dict[str, Dict[str, List[str]]]:
|
| 93 |
+
meta = meta.copy()
|
| 94 |
+
meta["biome"] = meta["biome"].astype(str).str.upper().str.strip()
|
| 95 |
+
meta["region"] = meta["region"].astype(str).str.strip()
|
| 96 |
+
|
| 97 |
+
required = {"filename", "country", "state", "zone", "biome", "region", "elevation_class_zonewise"}
|
| 98 |
+
missing = required - set(meta.columns)
|
| 99 |
+
if missing:
|
| 100 |
+
raise RuntimeError(f"metadata.csv missing required columns: {sorted(missing)}")
|
| 101 |
+
|
| 102 |
+
split_map: Dict[str, Dict[str, List[str]]] = {}
|
| 103 |
+
|
| 104 |
+
def add_single_config(cfg_name: str, id_pool: List[str], ood_pool: List[str]) -> None:
|
| 105 |
+
train_files, id_test_files = split_pool(id_pool, train_ratio, seed)
|
| 106 |
+
ood_test_files = list(ood_pool)
|
| 107 |
+
|
| 108 |
+
ensure_min(cfg_name + ":train", train_files, min_pool)
|
| 109 |
+
ensure_min(cfg_name + ":id_test", id_test_files, 10)
|
| 110 |
+
ensure_min(cfg_name + ":ood_test", ood_test_files, 10)
|
| 111 |
+
|
| 112 |
+
split_map[cfg_name] = {
|
| 113 |
+
"train": sorted(train_files),
|
| 114 |
+
"id_test": sorted(id_test_files),
|
| 115 |
+
"ood_test": sorted(ood_test_files),
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
# 1) Country shift (India <-> US)
|
| 119 |
+
files_in = meta[meta["country"] == "India"]["filename"].tolist()
|
| 120 |
+
files_us = meta[meta["country"] == "US"]["filename"].tolist()
|
| 121 |
+
add_single_config("intl_train_IN__ood_US", files_in, files_us)
|
| 122 |
+
add_single_config("intl_train_US__ood_IN", files_us, files_in)
|
| 123 |
+
|
| 124 |
+
# 2) Rajasthan biome shift (WET <-> DRY)
|
| 125 |
+
raj = meta[(meta["country"] == "India") & (meta["state"] == "Rajasthan")]
|
| 126 |
+
raj_wet = raj[raj["biome"] == "WET"]["filename"].tolist()
|
| 127 |
+
raj_dry = raj[raj["biome"] == "DRY"]["filename"].tolist()
|
| 128 |
+
add_single_config("biome_Rajasthan_train_WET__ood_DRY", raj_wet, raj_dry)
|
| 129 |
+
add_single_config("biome_Rajasthan_train_DRY__ood_WET", raj_dry, raj_wet)
|
| 130 |
+
|
| 131 |
+
# 3) Karnataka elevation shift (HIGH <-> LOW)
|
| 132 |
+
kar_high = _karnataka_elevation_files(meta, "high")
|
| 133 |
+
kar_low = _karnataka_elevation_files(meta, "low")
|
| 134 |
+
add_single_config("elev_Karnataka_train_HIGH__ood_LOW", kar_high, kar_low)
|
| 135 |
+
add_single_config("elev_Karnataka_train_LOW__ood_HIGH", kar_low, kar_high)
|
| 136 |
+
|
| 137 |
+
# 4) India region shift (North <-> South)
|
| 138 |
+
north = meta[(meta["country"] == "India") & (meta["region"] == "North")]["filename"].tolist()
|
| 139 |
+
south = meta[(meta["country"] == "India") & (meta["region"] == "South")]["filename"].tolist()
|
| 140 |
+
add_single_config("region_train_North__ood_South", north, south)
|
| 141 |
+
add_single_config("region_train_South__ood_North", south, north)
|
| 142 |
+
|
| 143 |
+
return split_map
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
def write_config_from_split_map(
|
| 147 |
+
split_map: Dict[str, Dict[str, List[str]]],
|
| 148 |
+
out_configs_dir: Path,
|
| 149 |
+
rows_per_shard: int,
|
| 150 |
+
light_index: Dict[str, Dict],
|
| 151 |
+
images_root: Path,
|
| 152 |
+
) -> None:
|
| 153 |
+
out_configs_dir.mkdir(parents=True, exist_ok=True)
|
| 154 |
+
for cfg_name, splits in split_map.items():
|
| 155 |
+
cfg_dir = out_configs_dir / cfg_name
|
| 156 |
+
cfg_dir.mkdir(parents=True, exist_ok=True)
|
| 157 |
+
for split in ("train", "id_test", "ood_test"):
|
| 158 |
+
write_rows_to_parquet(
|
| 159 |
+
rows_from_files(splits[split], light_index, images_root),
|
| 160 |
+
cfg_dir,
|
| 161 |
+
split,
|
| 162 |
+
rows_per_shard,
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
def build_master_configs(
|
| 167 |
+
src_root: Path,
|
| 168 |
+
master_dir: Path,
|
| 169 |
+
out_configs_dir: Path,
|
| 170 |
+
train_ratio: float,
|
| 171 |
+
seed: int,
|
| 172 |
+
rows_per_shard: int,
|
| 173 |
+
scan_batch_size: int,
|
| 174 |
+
min_pool: int,
|
| 175 |
+
write_parquet: bool = True,
|
| 176 |
+
) -> Dict[str, Dict[str, List[str]]]:
|
| 177 |
+
meta_path = src_root / "metadata.csv"
|
| 178 |
+
images_root = src_root / "world_images"
|
| 179 |
+
if not meta_path.exists():
|
| 180 |
+
raise FileNotFoundError(f"metadata.csv not found at: {meta_path}")
|
| 181 |
+
if not images_root.exists():
|
| 182 |
+
raise FileNotFoundError(f"world_images/ not found at: {images_root}")
|
| 183 |
+
|
| 184 |
+
meta = pd.read_csv(meta_path)
|
| 185 |
+
split_map = collect_base_splits(meta, train_ratio=train_ratio, seed=seed, min_pool=min_pool)
|
| 186 |
+
|
| 187 |
+
if write_parquet:
|
| 188 |
+
light_index = build_light_index(str(master_dir), scan_batch_size)
|
| 189 |
+
write_config_from_split_map(
|
| 190 |
+
split_map=split_map,
|
| 191 |
+
out_configs_dir=out_configs_dir,
|
| 192 |
+
rows_per_shard=rows_per_shard,
|
| 193 |
+
light_index=light_index,
|
| 194 |
+
images_root=images_root,
|
| 195 |
+
)
|
| 196 |
+
|
| 197 |
+
return split_map
|
| 198 |
+
|
| 199 |
+
|
| 200 |
+
def main() -> None:
|
| 201 |
+
ap = argparse.ArgumentParser()
|
| 202 |
+
ap.add_argument("--src_root", required=True, help="root containing metadata.csv and world_images/")
|
| 203 |
+
ap.add_argument("--master_dir", required=True, help="hf_repo/data/master (parquet shards)")
|
| 204 |
+
ap.add_argument("--out_configs_dir", required=True, help="hf_repo/data/configs")
|
| 205 |
+
ap.add_argument("--train_ratio", type=float, default=0.9)
|
| 206 |
+
ap.add_argument("--seed", type=int, default=42)
|
| 207 |
+
ap.add_argument("--rows_per_shard", type=int, default=16)
|
| 208 |
+
ap.add_argument("--scan_batch_size", type=int, default=32)
|
| 209 |
+
ap.add_argument("--min_pool", type=int, default=200)
|
| 210 |
+
ap.add_argument("--manifest_path", default=None, help="optional path to write JSON manifest")
|
| 211 |
+
args = ap.parse_args()
|
| 212 |
+
|
| 213 |
+
split_map = build_master_configs(
|
| 214 |
+
src_root=Path(args.src_root),
|
| 215 |
+
master_dir=Path(args.master_dir),
|
| 216 |
+
out_configs_dir=Path(args.out_configs_dir),
|
| 217 |
+
train_ratio=args.train_ratio,
|
| 218 |
+
seed=args.seed,
|
| 219 |
+
rows_per_shard=args.rows_per_shard,
|
| 220 |
+
scan_batch_size=args.scan_batch_size,
|
| 221 |
+
min_pool=args.min_pool,
|
| 222 |
+
write_parquet=True,
|
| 223 |
+
)
|
| 224 |
+
|
| 225 |
+
manifest = {
|
| 226 |
+
"base_config_count": len(split_map),
|
| 227 |
+
"base_configs": sorted(split_map.keys()),
|
| 228 |
+
"split_sizes": {
|
| 229 |
+
cfg: {split: len(files) for split, files in splits.items()}
|
| 230 |
+
for cfg, splits in split_map.items()
|
| 231 |
+
},
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
if args.manifest_path:
|
| 235 |
+
manifest_path = Path(args.manifest_path)
|
| 236 |
+
manifest_path.parent.mkdir(parents=True, exist_ok=True)
|
| 237 |
+
manifest_path.write_text(json.dumps(manifest, indent=2))
|
| 238 |
+
|
| 239 |
+
print(json.dumps(manifest, indent=2))
|
| 240 |
+
|
| 241 |
+
|
| 242 |
+
if __name__ == "__main__":
|
| 243 |
+
main()
|