Download scripts/verify.py from niru-5/manama_course_dataset_grain_detection: direct link, hf CLI and curl.
- Browser
- Download file 4.86 kB
-
https://huggingface.co/datasets/niru-5/manama_course_dataset_grain_detection/resolve/main/scripts/verify.py
- Command line
-
hf download hf://datasets/niru-5/manama_course_dataset_grain_detection/scripts/verify.py
-
curl -L -o verify.py https://huggingface.co/datasets/niru-5/manama_course_dataset_grain_detection/resolve/main/scripts/verify.py
4.86 kB
| """Integrity checks for the release. Run from anywhere: python scripts/verify.py (needs Pillow) | |
| Checks: ids, boxes inside images, no duplicate names, no photo in two splits, tile/photo consistency, | |
| counts vs stats.json and README.md, and checksums.sha256.""" | |
| import hashlib, json, re, sys | |
| from pathlib import Path | |
| from PIL import Image | |
| root = Path(__file__).resolve().parent.parent | |
| errs = [] | |
| def err(m): errs.append(m) | |
| J = lambda p: json.loads((root / p).read_text()) | |
| classes = J("classes.json"); names = [c["name"] for c in classes] | |
| assert [c["id"] for c in classes] == list(range(6)) and names == ["corn", "peanuts", "popcorn_corn", "pumpkin", "sunflower", "wheat"] | |
| splits = J("splits.json"); stats = J("stats.json") | |
| def check_coco(path, imgdir, tiles): | |
| d = J(path) | |
| if [c["name"] for c in d["categories"]] != names or [c["id"] for c in d["categories"]] != list(range(6)): err(f"{path}: categories") | |
| ids = [i["id"] for i in d["images"]]; fns = [i["file_name"] for i in d["images"]] | |
| if len(set(ids)) != len(ids): err(f"{path}: duplicate image ids") | |
| if len(set(fns)) != len(fns): err(f"{path}: duplicate file names") | |
| aids = [a["id"] for a in d["annotations"]] | |
| if len(set(aids)) != len(aids): err(f"{path}: duplicate annotation ids") | |
| im = {i["id"]: i for i in d["images"]} | |
| for i in d["images"]: | |
| p = root / imgdir / i["file_name"] | |
| if not p.exists(): err(f"{path}: missing {p}"); continue | |
| with Image.open(p) as x: | |
| if x.size != (i["width"], i["height"]): err(f"{path}: size mismatch {i['file_name']}") | |
| for a in d["annotations"]: | |
| i = im.get(a["image_id"]) | |
| if i is None: err(f"{path}: ann {a['id']} bad image_id"); continue | |
| x, y, w, h = a["bbox"] | |
| if not (0 <= a["category_id"] < 6): err(f"{path}: ann {a['id']} bad category") | |
| if w <= 0 or h <= 0 or x < -0.5 or y < -0.5 or x + w > i["width"] + 0.5 or y + h > i["height"] + 0.5: err(f"{path}: ann {a['id']} box outside {i['file_name']}") | |
| on_disk = {p.name for p in (root / imgdir).glob("*.jpg")} | |
| if on_disk != set(fns): err(f"{path}: files on disk differ from labels ({len(on_disk)} vs {len(fns)})") | |
| return d | |
| D = {s: check_coco(f"coco/{s}/labels.json", f"coco/{s}/images", True) for s in ("train", "val", "test")} | |
| P = {s: check_coco(f"annotations/{s}_photos.json", f"photos/{s}", False) for s in ("val", "test")} | |
| # no photo in two splits; tiles belong to the photo's split; photo files match the split map | |
| photo_of = lambda fn: re.sub(r"_x\d+_y\d+\.jpg$", ".jpg", fn) | |
| seen = {} | |
| for s, d in D.items(): | |
| for i in d["images"]: | |
| ph = photo_of(i["file_name"]) | |
| if splits.get(ph) != s: err(f"tile {i['file_name']} in {s} but photo is in {splits.get(ph)}") | |
| seen.setdefault(ph, set()).add(s) | |
| for ph, ss in seen.items(): | |
| if len(ss) > 1: err(f"photo {ph} in several splits: {ss}") | |
| for s in ("val", "test"): | |
| for i in P[s]["images"]: | |
| if splits.get(i["file_name"]) != s: err(f"photo {i['file_name']} not in split {s}") | |
| if len(set(splits)) != len(splits): err("duplicate photo names in splits.json") | |
| for ph in splits: | |
| if not re.fullmatch(r"(corn|peanuts|popcorn_corn|pumpkin|sunflower|wheat)_\d{4}\.jpg", ph): err(f"bad photo name {ph}") | |
| # counts vs stats.json and README | |
| for s in ("train", "val", "test"): | |
| t = stats["totals"][s] | |
| n_ph = sum(1 for v in splits.values() if v == s) | |
| if (t["tiles"], t["boxes"], t["photos"]) != (len(D[s]["images"]), len(D[s]["annotations"]), n_ph): err(f"stats.json mismatch for {s}") | |
| readme = (root / "README.md").read_text() | |
| for s in ("train", "val", "test"): | |
| t = stats["totals"][s] | |
| row = f"| **all** | **{s}** | {t['photos']} | {t['tiles']} |" | |
| if row not in readme: err(f"README table row for {s} does not match the data") | |
| if f"{len(splits)} photos" not in readme: err("README photo total") | |
| # checksums | |
| cs = root / "checksums.sha256" | |
| if cs.exists(): | |
| n = 0 | |
| for line in cs.read_text().splitlines(): | |
| h, _, rel = line.partition(" ") | |
| if hashlib.sha256((root / rel).read_bytes()).hexdigest() != h: err(f"checksum mismatch {rel}") | |
| n += 1 | |
| listed = {l.partition(" ")[2] for l in cs.read_text().splitlines()} | |
| for p in root.rglob("*"): | |
| if ".cache" in p.relative_to(root).parts or p.name == ".gitattributes": # added by the Hub / `hf download` | |
| continue | |
| if p.is_file() and p.name != "checksums.sha256" and str(p.relative_to(root)) not in listed: err(f"not in checksums: {p.relative_to(root)}") | |
| else: | |
| err("checksums.sha256 missing") | |
| print("tiles:", {s: len(D[s]["images"]) for s in D}, "boxes:", {s: len(D[s]["annotations"]) for s in D}, "photos:", {s: sum(1 for v in splits.values() if v == s) for s in D}) | |
| if errs: | |
| print(len(errs), "PROBLEMS"); [print(" -", e) for e in errs[:30]]; sys.exit(1) | |
| print("OK: all checks passed") | |