Download scripts/build_metadata.py from XZhi/ViViD-5k: direct link, hf CLI and curl.
- Browser
- Download file 3.38 kB
-
https://huggingface.co/datasets/XZhi/ViViD-5k/resolve/main/scripts/build_metadata.py
- Command line
-
hf download hf://datasets/XZhi/ViViD-5k/scripts/build_metadata.py
-
curl -L -o build_metadata.py https://huggingface.co/datasets/XZhi/ViViD-5k/resolve/main/scripts/build_metadata.py
3.38 kB
| """Build data/metadata.csv and scripts/cache/clusters.csv from raw ViViD-5k data. | |
| Loads the 515 MB COCO json once (~1-2 min). Fails loudly on any mismatch | |
| between images, point files, split entries, and COCO entries. | |
| """ | |
| import json | |
| import sys | |
| from pathlib import Path | |
| import numpy as np | |
| import pandas as pd | |
| from tqdm import tqdm | |
| sys.path.insert(0, str(Path(__file__).resolve().parent)) | |
| from vivid_utils import load_splits, parse_bbox | |
| ROOT = Path(__file__).resolve().parents[1] | |
| DATA = ROOT / "data" | |
| CACHE = ROOT / "scripts" / "cache" | |
| GRAPE_CATEGORY_ID = 1 | |
| def main(): | |
| splits = load_splits(DATA) | |
| img_files = {p.name: p for p in (DATA / "imgs").iterdir()} | |
| point_files = {p.stem: p for p in (DATA / "anns" / "points").glob("*.npy")} | |
| errors = [] | |
| for name in splits: | |
| if name not in img_files: | |
| errors.append(f"in split but no image file: {name}") | |
| if Path(name).stem not in point_files: | |
| errors.append(f"no points npy for image: {name}") | |
| for name in img_files: | |
| if name not in splits: | |
| errors.append(f"image not in any split: {name}") | |
| if errors: | |
| print("\n".join(errors), file=sys.stderr) | |
| sys.exit(f"ABORT: {len(errors)} consistency errors") | |
| print("Loading COCO json (this takes a minute)...") | |
| coco = json.loads((DATA / "anns" / "instances_updated.json").read_text()) | |
| coco_by_id = {im["id"]: im for im in coco["images"]} | |
| coco_by_name = {im["file_name"]: im for im in coco["images"]} | |
| missing_coco = [n for n in splits if n not in coco_by_name] | |
| if missing_coco: | |
| sys.exit(f"ABORT: {len(missing_coco)} images missing from COCO json: {missing_coco[:5]}") | |
| cluster_count = {n: 0 for n in splits} | |
| cluster_rows = [] | |
| skipped_ids = [] | |
| for ann in tqdm(coco["annotations"], desc="annotations"): | |
| if ann["category_id"] != GRAPE_CATEGORY_ID: | |
| continue | |
| if not ann["bbox"]: | |
| skipped_ids.append(ann["id"]) | |
| continue | |
| im = coco_by_id[ann["image_id"]] | |
| name = im["file_name"] | |
| cluster_count[name] += 1 | |
| x, y, w, h = parse_bbox(ann["bbox"]) | |
| cluster_rows.append( | |
| {"file_name": name, "bbox_w": w, "bbox_h": h, | |
| "rel_area": (w * h) / (im["width"] * im["height"])} | |
| ) | |
| if skipped_ids: | |
| print(f"WARNING: skipped {len(skipped_ids)} degenerate annotations " | |
| f"(empty bbox): ids={sorted(skipped_ids)}") | |
| rows = [] | |
| for name, split in tqdm(sorted(splits.items()), desc="images"): | |
| pts = np.load(point_files[Path(name).stem]) | |
| if pts.ndim != 2 or pts.shape[1] != 2: | |
| sys.exit(f"ABORT: unexpected points shape {pts.shape} for {name}") | |
| im = coco_by_name[name] | |
| rows.append( | |
| {"file_name": name, "split": split, "berry_count": len(pts), | |
| "cluster_count": cluster_count[name], | |
| "width": im["width"], "height": im["height"]} | |
| ) | |
| meta = pd.DataFrame(rows) | |
| meta.to_csv(DATA / "metadata.csv", index=False) | |
| CACHE.mkdir(parents=True, exist_ok=True) | |
| pd.DataFrame(cluster_rows).to_csv(CACHE / "clusters.csv", index=False) | |
| print(f"TOTALS images={len(meta)} berries={meta.berry_count.sum()} " | |
| f"clusters={meta.cluster_count.sum()}") | |
| print(meta.groupby("split").size()) | |
| if __name__ == "__main__": | |
| main() | |