Datasets:
Download scripts/eval_dets.py from oneryalcin/ui-controls-web: direct link, hf CLI and curl.
- Browser
- Download file 2.54 kB
-
https://huggingface.co/datasets/oneryalcin/ui-controls-web/resolve/main/scripts/eval_dets.py
- Command line
-
hf download hf://datasets/oneryalcin/ui-controls-web/scripts/eval_dets.py
-
curl -L -o eval_dets.py https://huggingface.co/datasets/oneryalcin/ui-controls-web/resolve/main/scripts/eval_dets.py
2.54 kB
| """Score saved detections (from dets.py or modal_libre.py) on held-out test pages, per class and size. | |
| Usage: [DATA=coco5] [CONTROLS=1] python eval_dets.py <dets.json> [thresholds...] | |
| dets: {image_stem: [[conf, [x0,y0,x1,y1], class], ...]}. CONTROLS=1 scores only the three control classes: | |
| labels that are not controls and boxes of the other classes (plain text, image) are left out; BOXES=all keeps | |
| every box. | |
| Per class and size: labels found by any box / by a box of the right class / out of.""" | |
| import json, os, sys | |
| D = json.load(open(sys.argv[1])); thrs = [float(t) for t in sys.argv[2:]] or [0.2] | |
| ONLY = os.environ.get("CONTROLS") == "1"; BOXES = os.environ.get("BOXES") | |
| C = json.load(open(f"data/{os.environ.get('DATA', 'coco')}/test/_annotations.coco.json")); A = {} | |
| NAMES = [k["name"] for k in C["categories"]] | |
| for a in C["annotations"]: | |
| if not (ONLY and not a.get("control", a["category_id"] <= 2)): A.setdefault(a["image_id"], []).append(a) | |
| def iou(a, b): | |
| i = max(0, min(a[2], b[2]) - max(a[0], b[0])) * max(0, min(a[3], b[3]) - max(a[1], b[1])) | |
| return i / ((a[2] - a[0]) * (a[3] - a[1]) + (b[2] - b[0]) * (b[3] - b[1]) - i + 1e-9) | |
| ins = lambda p, r: r[0] <= p[0] <= r[2] and r[1] <= p[1] <= r[3]; c = lambda b: ((b[0] + b[2]) / 2, (b[1] + b[3]) / 2) | |
| match = lambda r, q: iou(r, q) >= 0.5 or (ins(c(q), r) and ins(c(r), q)) | |
| for thr in thrs: | |
| f = {}; fr = {}; n = {}; boxes = good = 0 | |
| for im in C["images"]: | |
| ds = [(b, k) for s, b, k in D[im["file_name"][:-4]] if s >= thr and not (ONLY and k > 2 and BOXES != "all")] | |
| gt = [([a["bbox"][0], a["bbox"][1], a["bbox"][0] + a["bbox"][2], a["bbox"][1] + a["bbox"][3]], a["category_id"]) for a in A.get(im["id"], [])] | |
| boxes += len(ds); good += sum(any(match(r, q) for r, _ in gt) for q, _ in ds) | |
| for (r, k), a in zip(gt, A.get(im["id"], [])): | |
| key = (NAMES[k], "under 24 px" if min(a["bbox"][2], a["bbox"][3]) < 24 else "24 px and up") | |
| m = [dk for q, dk in ds if match(r, q)] | |
| n[key] = n.get(key, 0) + 1; f[key] = f.get(key, 0) + bool(m); fr[key] = fr.get(key, 0) + (k in m) | |
| tot = sum(f.values()), sum(fr.values()), sum(n.values()) | |
| print(f"thr {thr}: all {tot[0]}/{tot[2]} ({100 * tot[0] / tot[2]:.1f}%), right class {tot[1]} ({100 * tot[1] / tot[2]:.1f}%) | boxes {boxes}, matching a label {100 * good / max(boxes, 1):.0f}%") | |
| for k in sorted(n): print(f" {k[0]:13s}{k[1]:12s} any {f[k]:4d} right class {fr[k]:4d} of {n[k]:4d} ({100 * f[k] / n[k]:.0f}% / {100 * fr[k] / n[k]:.0f}%)") | |