Datasets:
Download evaluate.py from KORMo-VL/Ko-Screenspot: direct link, hf CLI and curl.
- Browser
- Download file 5.3 kB
-
https://huggingface.co/datasets/KORMo-VL/Ko-Screenspot/resolve/main/evaluate.py
- Command line
-
hf download hf://datasets/KORMo-VL/Ko-Screenspot/evaluate.py
-
curl -L -o evaluate.py https://huggingface.co/datasets/KORMo-VL/Ko-Screenspot/resolve/main/evaluate.py
5.3 kB
| """Score predictions on the Korean ScreenSpot grounding set. | |
| Predictions are a JSONL file keyed by ``sample_id``:: | |
| {"sample_id": "screenspot::0714_Kaggle_1#2", "point": [0.771, 0.034]} | |
| {"sample_id": "screenspot::0714_Kaggle_1#3", "bbox": [0.40, 0.45, 0.59, 0.49]} | |
| Coordinates may be normalised to [0, 1] or given in screenshot pixels; the | |
| scorer detects which. A prediction hits when its point falls inside the gold | |
| box (``--match point``, the ScreenSpot convention) or when its box reaches the | |
| IoU threshold (``--match iou``). | |
| Usage: | |
| python evaluate.py --pred preds.jsonl [--match iou --iou 0.5] | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import os | |
| from collections import defaultdict | |
| def load_jsonl(path: str) -> list[dict]: | |
| with open(path, encoding="utf-8") as fh: | |
| return [json.loads(line) for line in fh if line.strip()] | |
| GRID = 999 | |
| def to_grid999(coords, size): | |
| """예측 좌표 -> 0-999 그리드. 채점은 전부 이 좌표계에서 한다. | |
| 골든이 `gold_bbox_999`(0-999 정수)이므로 예측도 같은 격자로 옮겨서 비교한다. | |
| 입력 판별: | |
| 전부 <= 1.0 -> 0-1 정규화 -> x 999 | |
| 그 외(> 1.0 포함) -> 스크린샷 픽셀 -> / image_size x 999 | |
| 0-999 값을 그대로 준 경우도 픽셀 분기를 타지만, 999 이하 값을 픽셀로 보고 | |
| 나누면 어긋나므로 **예측은 0-1 정규화 또는 픽셀로만 낼 것**(README 참고). | |
| """ | |
| if coords is None: | |
| return None | |
| coords = [float(c) for c in coords] | |
| if all(c <= 1.0 for c in coords): | |
| return [c * GRID for c in coords] | |
| w, h = size | |
| return [c / (w if i % 2 == 0 else h) * GRID for i, c in enumerate(coords)] | |
| def iou(a, b) -> float: | |
| ix1, iy1 = max(a[0], b[0]), max(a[1], b[1]) | |
| ix2, iy2 = min(a[2], b[2]), min(a[3], b[3]) | |
| if ix2 <= ix1 or iy2 <= iy1: | |
| return 0.0 | |
| inter = (ix2 - ix1) * (iy2 - iy1) | |
| area_a = (a[2] - a[0]) * (a[3] - a[1]) | |
| area_b = (b[2] - b[0]) * (b[3] - b[1]) | |
| return inter / (area_a + area_b - inter) | |
| def hit(pred: dict, gold: dict, match: str, iou_thr: float) -> bool: | |
| """★채점은 0-999 그리드에서만 한다★ — 골든은 `gold_bbox_999`. | |
| 예전에는 픽셀 공간(`gold_bbox`)에서 쟀다. 격자로 옮기면 박스 경계가 그리드에 | |
| 맞춰 양자화되므로 경계에 걸친 예측의 판정이 달라질 수 있다. | |
| """ | |
| size, box = gold["image_size"], gold["gold_bbox_999"] | |
| if match == "iou": | |
| pred_box = to_grid999(pred.get("bbox"), size) | |
| return bool(pred_box) and iou(pred_box, box) >= iou_thr | |
| point = to_grid999(pred.get("point"), size) | |
| if point is None: | |
| pred_box = to_grid999(pred.get("bbox"), size) | |
| if pred_box is None: | |
| return False | |
| point = [(pred_box[0] + pred_box[2]) / 2, (pred_box[1] + pred_box[3]) / 2] | |
| return box[0] <= point[0] <= box[2] and box[1] <= point[1] <= box[3] | |
| def summarise(counts): | |
| return { | |
| key: {"correct": c, "total": t, "score": round(c / t, 4) if t else None} | |
| for key, (c, t) in sorted(counts.items()) | |
| } | |
| def main() -> None: | |
| here = os.path.dirname(os.path.abspath(__file__)) | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--pred", required=True) | |
| # 기본 골든은 samples_functional.jsonl — 지시문이 요소 이름이 아니라 기능을 | |
| # 자연어로 표현한 판이다("로그인하기" vs '"Sign In" 버튼'). 3,319문항 | |
| # (3,605 → 4개 영문 사이트 제거 3,482 → 다중 정답 문항 제거 3,319). | |
| # 옛 판(요소 이름 그대로, 3,735문항)은 previous_golden/samples.jsonl 에 있다. | |
| parser.add_argument("--data", default=os.path.join(here, "samples_functional.jsonl")) | |
| parser.add_argument("--match", choices=["point", "iou"], default="point") | |
| parser.add_argument("--iou", type=float, default=0.5) | |
| parser.add_argument("--by-website", action="store_true") | |
| parser.add_argument("--out", default=None) | |
| opts = parser.parse_args() | |
| preds = {p["sample_id"]: p for p in load_jsonl(opts.pred)} | |
| gold_rows = load_jsonl(opts.data) | |
| total_hit = 0 | |
| by_type: dict[str, list[int]] = defaultdict(lambda: [0, 0]) | |
| by_site: dict[str, list[int]] = defaultdict(lambda: [0, 0]) | |
| for gold in gold_rows: | |
| pred = preds.get(gold["sample_id"]) | |
| ok = bool(pred) and hit(pred, gold, opts.match, opts.iou) | |
| total_hit += ok | |
| for bucket in (by_type[gold["element_type"]], by_site[gold["website"]]): | |
| bucket[0] += ok | |
| bucket[1] += 1 | |
| report = { | |
| "predictions": len(preds), | |
| "match": opts.match, | |
| "iou_threshold": opts.iou if opts.match == "iou" else None, | |
| "grounding_accuracy": { | |
| "correct": total_hit, | |
| "total": len(gold_rows), | |
| "score": round(total_hit / len(gold_rows), 4) if gold_rows else None, | |
| }, | |
| "by_element_type": summarise(by_type), | |
| } | |
| if opts.by_website: | |
| report["by_website"] = summarise(by_site) | |
| text = json.dumps(report, ensure_ascii=False, indent=2) | |
| print(text) | |
| if opts.out: | |
| with open(opts.out, "w", encoding="utf-8") as fh: | |
| fh.write(text) | |
| if __name__ == "__main__": | |
| main() | |