Datasets:
Download scripts/prepare_labels.py from fatmur/dynamicworld-expert: direct link, hf CLI and curl.
- Browser
- Download file 4.58 kB
-
https://huggingface.co/datasets/fatmur/dynamicworld-expert/resolve/refs%2Fpr%2F1/scripts/prepare_labels.py
- Command line
-
hf download hf://datasets/fatmur/dynamicworld-expert@refs/pr/1/scripts/prepare_labels.py
-
curl -L -o prepare_labels.py https://huggingface.co/datasets/fatmur/dynamicworld-expert/resolve/refs%2Fpr%2F1/scripts/prepare_labels.py
4.58 kB
| """Prepare the expert label archive and the nested 3,609/409/409 benchmark. | |
| python scripts/prepare_labels.py PARENT SUBSET DESTINATION | |
| PARENT contains the full Dynamic World `processed/label.parquet` and `raw/` | |
| source files. SUBSET contains dynamicworld-300's `processed/label.parquet` | |
| with its 200/50/50 assignment (revision 1037fcb). Image ingestion is separate. | |
| """ | |
| import argparse | |
| import shutil | |
| from pathlib import Path | |
| from tempfile import TemporaryDirectory | |
| import geopandas as gpd | |
| import pandas as pd | |
| def prepare_labels(parent: Path, subset: Path, destination: Path) -> None: | |
| """Copy original expert annotations once and assign splits by footprint.""" | |
| parent_labels = gpd.read_parquet(parent / "processed/label.parquet") | |
| subset_labels = pd.read_parquet( | |
| subset / "processed/label.parquet", | |
| columns=["annotation_id", "split", "location_id"], | |
| ) | |
| labels = parent_labels.loc[parent_labels["annotation_source"].eq("expert")].copy() | |
| labels["parent_split"] = labels["split"] | |
| # All alternate expert votes and voting composites for a test location stay together. | |
| consensus = labels["source_group"].eq("expert_composites") & labels[ | |
| "voting_rule" | |
| ].eq("consensus").fillna(False) | |
| test_locations = set(labels.loc[consensus, "location_id"]) | |
| parent_val = ( | |
| labels.loc[ | |
| labels["source_group"].eq("experts") & labels["parent_split"].eq("val"), | |
| "location_id", | |
| ] | |
| .drop_duplicates() | |
| .sort_values() | |
| ) | |
| fixed_val = set(subset_labels.loc[subset_labels["split"].eq("val"), "location_id"]) | |
| if not fixed_val.issubset(parent_val): | |
| raise ValueError( | |
| "The teaching subset's validation locations must belong to parent validation" | |
| ) | |
| candidates = parent_val.loc[~parent_val.isin(fixed_val)] | |
| draw = candidates.sample(n=len(test_locations) - len(fixed_val), random_state=0) | |
| val_locations = fixed_val.union(draw) | |
| # Use exactly the parent EDA's one footprint per location and intersects predicate. | |
| footprints = parent_labels[["location_id", "geometry"]].drop_duplicates( | |
| "location_id" | |
| ) | |
| held_out = footprints.loc[ | |
| footprints["location_id"].isin(test_locations | val_locations) | |
| ] | |
| intersections = gpd.sjoin( | |
| footprints, held_out[["geometry"]], predicate="intersects" | |
| ) | |
| labels["split"] = "train" | |
| labels.loc[labels["location_id"].isin(intersections["location_id"]), "split"] = ( | |
| "excluded" | |
| ) | |
| labels.loc[labels["location_id"].isin(val_locations), "split"] = "val" | |
| labels.loc[labels["location_id"].isin(test_locations), "split"] = "test" | |
| labels["benchmark"] = ( | |
| labels["source_group"].eq("experts") & labels["split"].isin(["train", "val"]) | |
| ) | (consensus & labels["split"].eq("test")) | |
| labels["label_path"] = "../" + labels["source_path"] | |
| benchmark = labels.loc[labels["benchmark"]] | |
| counts = benchmark.groupby("split").size().to_dict() | |
| if ( | |
| counts != {"train": 3609, "val": 409, "test": 409} | |
| or not benchmark["scene_id"].is_unique | |
| ): | |
| raise ValueError(f"Unexpected benchmark assignment: {counts}") | |
| inherited = benchmark.set_index("annotation_id")["split"].reindex( | |
| subset_labels["annotation_id"] | |
| ) | |
| if ( | |
| inherited.isna().any() | |
| or not (inherited.to_numpy() == subset_labels["split"].to_numpy()).all() | |
| ): | |
| raise ValueError("The teaching subset must retain its published assignments") | |
| processed = destination / "processed" | |
| processed.mkdir(parents=True, exist_ok=True) | |
| with TemporaryDirectory(dir=destination) as temp: | |
| pending = Path(temp) / "label" | |
| for relative_path in [*labels["source_path"], "raw/README.txt", "labels.yaml"]: | |
| target = destination / relative_path | |
| target.parent.mkdir(parents=True, exist_ok=True) | |
| shutil.copyfile(parent / relative_path, pending) | |
| pending.replace(target) | |
| labels.to_parquet(pending, index=False) | |
| pending.replace(processed / "label.parquet") | |
| print(f"{len(labels):,} annotations across {labels['scene_id'].nunique():,} scenes") | |
| print(benchmark.groupby("split")["scene_id"].nunique().to_string()) | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("parent", type=Path) | |
| parser.add_argument("subset", type=Path) | |
| parser.add_argument("destination", type=Path) | |
| args = parser.parse_args() | |
| prepare_labels(args.parent, args.subset, args.destination) | |