Download scripts/b1_page_check/b1_page_score.py from treoa/turkic-ocr-code: direct link, hf CLI and curl.
- Browser
- Download file 4.08 kB
-
https://huggingface.co/treoa/turkic-ocr-code/resolve/main/scripts/b1_page_check/b1_page_score.py
- Command line
-
hf download hf://treoa/turkic-ocr-code/scripts/b1_page_check/b1_page_score.py
-
curl -L -o b1_page_score.py https://huggingface.co/treoa/turkic-ocr-code/resolve/main/scripts/b1_page_check/b1_page_score.py
4.08 kB
| """Score the B1 whole-page check, and route A (YOLO11s stage 2 + A1 long) on the SAME pages for comparison. | |
| Page CER exactly as linedet.det_eval.page_cers (metric M28 reading order: lines joined by one space; M29 order free: | |
| lines matched one to one by minimum edit distance, unmatched lines count in full). Micro average: summed edits over | |
| summed reference characters. B1 lines keep the model's own output order. | |
| usage: b1_page_score.py --dir OUT_DIR --routeA_dir DET_EVAL_DIR --out JSON | |
| """ | |
| import argparse | |
| import glob | |
| import json | |
| import os | |
| import numpy as np | |
| from rapidfuzz import process | |
| from rapidfuzz.distance import Levenshtein | |
| from scipy.optimize import linear_sum_assignment | |
| def page_cers(hyp_lines, ref_lines): | |
| # copied from linedet.det_eval.page_cers so the figures are comparable with route A's 0.16 and 0.43 | |
| nref = sum(len(r) for r in ref_lines) | |
| ro = Levenshtein.distance(" ".join(hyp_lines), " ".join(ref_lines)) | |
| if not hyp_lines or not ref_lines: | |
| of = sum(len(h) for h in hyp_lines) + nref | |
| else: | |
| C = process.cdist(hyp_lines, ref_lines, scorer=Levenshtein.distance, dtype=np.int32) | |
| ri, ci = linear_sum_assignment(C) | |
| matched = int(C[ri, ci].sum()) | |
| un_h = sum(len(hyp_lines[i]) for i in range(len(hyp_lines)) if i not in set(ri.tolist())) | |
| un_r = sum(len(ref_lines[j]) for j in range(len(ref_lines)) if j not in set(ci.tolist())) | |
| of = matched + un_h + un_r | |
| return ro, of, nref | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--dir", required=True) | |
| ap.add_argument("--routeA_dir", required=True) | |
| ap.add_argument("--out", required=True) | |
| a = ap.parse_args() | |
| recs = {} | |
| for fn in sorted(glob.glob(os.path.join(a.dir, "*.jsonl"))): | |
| s, pr = os.path.basename(fn).split("_")[:2] | |
| for ln in open(fn, encoding="utf-8"): | |
| r = json.loads(ln) | |
| recs.setdefault((s, pr), {})[r["id"]] = r | |
| routeA = {} | |
| for s, name in (("gen", "e2e_yolo_s2_gen_val_A1_long"), ("school", "e2e_yolo_s2_school_val_A1_long")): | |
| p = os.path.join(a.routeA_dir, name + "_pages.jsonl") | |
| if os.path.exists(p): | |
| routeA[s] = {json.loads(ln)["id"]: json.loads(ln) for ln in open(p, encoding="utf-8")} | |
| res = {"note": "smoke sample of validation pages, not a benchmark; B1 = PaddleOCR-VL 1.6 R5 leader, bf16 on CPU, greedy, " | |
| "default processor size (at most 1,003,520 pixels)", "cells": []} | |
| for (s, pr), pages in sorted(recs.items()): | |
| ro = of = n = 0 | |
| aro = aof = an = 0 | |
| per = [] | |
| for pid, r in sorted(pages.items()): | |
| e_ro, e_of, nr = page_cers(r["hyp_lines"], r["ref_lines"]) | |
| ro, of, n = ro + e_ro, of + e_of, n + nr | |
| row = {"id": pid, "ref_chars": nr, "ro_cer": round(e_ro / max(nr, 1), 4), "of_cer": round(e_of / max(nr, 1), 4), | |
| "n_hyp_lines": len(r["hyp_lines"]), "n_ref_lines": len(r["ref_lines"]), "stopped": r["stopped"], | |
| "n_new": r["n_new"], "seconds": r["seconds"]} | |
| A = routeA.get(s, {}).get(pid) | |
| if A is not None and "hyp_lines" in A: | |
| x_ro, x_of, _ = page_cers(A["hyp_lines"], r["ref_lines"]) | |
| aro, aof, an = aro + x_ro, aof + x_of, an + nr | |
| row["routeA_ro_cer"] = round(x_ro / max(nr, 1), 4) | |
| row["routeA_of_cer"] = round(x_of / max(nr, 1), 4) | |
| per.append(row) | |
| res["cells"].append({"set": s, "prompt": pr, "pages": len(pages), "ref_chars": n, | |
| "B1_reading_order_cer": round(ro / max(n, 1), 4), "B1_order_free_cer": round(of / max(n, 1), 4), | |
| "routeA_same_pages_reading_order_cer": round(aro / an, 4) if an else None, | |
| "routeA_same_pages_order_free_cer": round(aof / an, 4) if an else None, | |
| "per_page": per}) | |
| json.dump(res, open(a.out, "w", encoding="utf-8"), ensure_ascii=False, indent=1) | |
| for c in res["cells"]: | |
| print(c["set"], c["prompt"], "pages", c["pages"], "B1 ro/of", c["B1_reading_order_cer"], c["B1_order_free_cer"], | |
| "routeA ro/of", c["routeA_same_pages_reading_order_cer"], c["routeA_same_pages_order_free_cer"]) | |