Download scripts/r5_testpass2/tp2_results.py from treoa/turkic-ocr-code: direct link, hf CLI and curl.
- Browser
- Download file 7.23 kB
-
https://huggingface.co/treoa/turkic-ocr-code/resolve/main/scripts/r5_testpass2/tp2_results.py
- Command line
-
hf download hf://treoa/turkic-ocr-code/scripts/r5_testpass2/tp2_results.py
-
curl -L -o tp2_results.py https://huggingface.co/treoa/turkic-ocr-code/resolve/main/scripts/r5_testpass2/tp2_results.py
7.23 kB
| """Test pass 2 results table (aggregates only; no label text is written). | |
| Per crop set and model: n, CER (plain profile = decision figure, amendment 3.2 C10) with the scorer's 95% cluster-bootstrap interval, | |
| WER, exact-match rate, invented-text figures (HR3 overlong or garbage outputs, HR4 repetition loops, fabricated-word rate, | |
| empty-output rate), hit-cap rows, cluster count and unit. Models: M1 (testpass2 score files), M2 greedy and M2 + gated LM (score files | |
| of the 24 Nov sealed pass, reused), Qwen3-VL-2B where run. Secondary, descriptive: paired cluster bootstrap of M1 minus M2 greedy | |
| (r5eval.stats.paired_ratio_difference on the same sorted cluster list); sensitivity without the test rows that have a duplicate | |
| training partner in the 21 Nov report-only duplicate check (micro CER recomputed from the prediction TSVs with the scorer's N0 text | |
| and its edit distance). Pages: the tp2_page_score.py files. | |
| usage: tp2_results.py --root DATA/outputs/r5/testpass2 --out results.json | |
| """ | |
| import argparse | |
| import csv | |
| import json | |
| import os | |
| import sys | |
| csv.field_size_limit(1 << 30) | |
| DATA = "/path/to/TURKIC_OCR_DATA" | |
| sys.path.insert(0, "/path/to/TURKIC_OCR_CODE/src") | |
| from r5eval import stats, textnorm # noqa: E402 | |
| from r5eval.align import lev_fast # noqa: E402 | |
| SETS = ["az_localdoc_test", "rukopys_test", "hkr_test1", "hkr_test2", "chd_test", "school_test", "synth_hand_test", "synth_typed_test"] | |
| KEYS = ("cer", "wer", "exact_match", "hr3", "hr4", "fabricated_word_rate", "empty_output_rate", "cer_casefold") | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--root", required=True) | |
| ap.add_argument("--out", required=True) | |
| a = ap.parse_args() | |
| def load(p): | |
| return json.load(open(p, encoding="utf-8")) if os.path.exists(p) else None | |
| def summ(d, pred_path): | |
| if d is None: | |
| return {"status": "not run"} | |
| if d.get("status") != "scored": | |
| return {"status": d.get("status")} | |
| pl = d["profiles"]["plain"] | |
| m = pl["groups"]["all"]["metrics"] | |
| r = {"status": "scored", "n": d["set_info"]["n_scored"], "n_clusters": d["set_info"]["n_clusters"], | |
| "cluster_unit": d["set_info"]["cluster_unit"], "coverage_complete": d["coverage"]["complete"], | |
| "scored_utc": d.get("scored_utc"), "scorer_code_sha256": d["scorer"]["code_sha256"], "pred_path": pred_path, | |
| "pred_sha256": d["inputs"]["pred_sha256"]} | |
| for k in KEYS: | |
| if k in m: | |
| r[k] = {x: m[k].get(x) for x in ("value", "lo", "hi", "num", "den")} | |
| cap = None | |
| if pred_path and os.path.exists(pred_path): | |
| with open(pred_path, encoding="utf-8", newline="") as f: | |
| cap = sum(1 for row in csv.DictReader(f, delimiter="\t", quoting=csv.QUOTE_NONE) if row.get("hit_cap") in ("1", "True")) | |
| r["hit_cap_rows"] = cap | |
| return r | |
| def micro_cer_excluding(pred_path, manifest, drop): | |
| refs = {} | |
| with open(manifest, encoding="utf-8", newline="") as f: | |
| for row in csv.DictReader(f, delimiter="\t", quoting=csv.QUOTE_NONE): | |
| refs[row["id"]] = row["text"] | |
| num = den = n = 0 | |
| with open(pred_path, encoding="utf-8", newline="") as f: | |
| for row in csv.DictReader(f, delimiter="\t", quoting=csv.QUOTE_NONE): | |
| if row["id"] in drop or row["id"] not in refs: | |
| continue | |
| ref = textnorm.norm(refs[row["id"]]) | |
| if not ref: | |
| continue | |
| num += lev_fast(ref, textnorm.norm(row["hyp"])) | |
| den += len(ref) | |
| n += 1 | |
| return {"n": n, "cer": num / den if den else None, "num": num, "den": den} | |
| reg = {e["name"]: e for e in json.load(open(DATA + "/data/processed/r5/evalsets.json", encoding="utf-8"))["sets"]} | |
| ov = load(os.path.join(a.root, "checks", "overlap.json")) or {} | |
| partners = ov.get("dedupe_all_test_rows_with_a_training_partner", {}) | |
| out = {"crops": {}, "pages": {}, "notes": []} | |
| for s in SETS: | |
| cell = {} | |
| p1 = "%s/preds/m1_%s/paddleocr_vl_1_6/leader/%s.tsv" % (a.root, s, s) | |
| d1 = load("%s/scores/m1_%s/paddleocr_vl_1_6/leader/%s.json" % (a.root, s, s)) | |
| cell["M1"] = summ(d1, p1) | |
| dq = load("%s/scores/q_%s/qwen3_vl_2b/leader/%s.json" % (a.root, s, s)) | |
| cell["Qwen3-VL-2B"] = summ(dq, "%s/preds/q_%s/qwen3_vl_2b/leader/%s.tsv" % (a.root, s, s)) | |
| d2 = load("%s/outputs/r5/scores/A1/long2x/%s.json" % (DATA, s)) | |
| cell["M2_greedy"] = summ(d2, "%s/outputs/r5/preds/A1/long2x/%s.tsv" % (DATA, s)) | |
| d3 = load("%s/outputs/r5/scores/A1/long2x_lm_gated/%s.json" % (DATA, s)) | |
| cell["M2_gated_lm"] = summ(d3, "%s/outputs/r5/preds/A1/long2x_lm_gated/%s.tsv" % (DATA, s)) | |
| for nm in ("M2_greedy", "M2_gated_lm"): | |
| cell[nm]["batching"] = "label-length batching (leaks reference length); 24 Nov sealed pass; NOT the result (amendment 3)" | |
| p4 = "%s/preds/m2lf_%s/A1/long2x_labelfree/%s.tsv" % (a.root, s, s) | |
| d4 = load("%s/scores/m2lf_%s/A1/long2x_labelfree/%s.json" % (a.root, s, s)) | |
| cell["M2_labelfree_greedy"] = summ(d4, p4) | |
| p5 = "%s/preds/m2lf_%s/A1/long2x_labelfree_lm_gated/%s.tsv" % (a.root, s, s) | |
| d5 = load("%s/scores/m2lf_%s/A1/long2x_labelfree_lm_gated/%s.json" % (a.root, s, s)) | |
| cell["M2_labelfree_gated_lm"] = summ(d5, p5) | |
| for nm in ("M2_labelfree_greedy", "M2_labelfree_gated_lm"): | |
| cell[nm]["batching"] = "label-free: width-sorted, batch 32, 256 extra zero columns (release infer.py); THE M2 RESULT (amendment 3)" | |
| for key, dy in (("paired_M1_minus_M2_labelfree_greedy", d4), ("paired_M1_minus_M2_greedy_labellength", d2)): | |
| if d1 is not None and dy is not None and d1.get("status") == "scored" and dy.get("status") == "scored": | |
| a1, a2 = d1["profiles"]["plain"], dy["profiles"]["plain"] | |
| if a1["cluster_ids"] == a2["cluster_ids"]: | |
| pr = stats.paired_ratio_difference(a1["cluster_sums"]["all"]["dist"], a1["cluster_sums"]["all"]["ref_len"], | |
| a2["cluster_sums"]["all"]["dist"], a2["cluster_sums"]["all"]["ref_len"]) | |
| pr.pop("_rel_reps", None) | |
| cell[key] = pr | |
| else: | |
| cell[key] = {"status": "cluster lists differ; not computed"} | |
| drop = set(partners.get(s, [])) | |
| if drop: | |
| sens = {"excluded_test_rows": len(drop)} | |
| for nm, pth in (("M1", p1), ("M2_labelfree_greedy", p4), ("M2_labelfree_gated_lm", p5), | |
| ("M2_greedy", "%s/outputs/r5/preds/A1/long2x/%s.tsv" % (DATA, s)), | |
| ("M2_gated_lm", "%s/outputs/r5/preds/A1/long2x_lm_gated/%s.tsv" % (DATA, s)), | |
| ("Qwen3-VL-2B", "%s/preds/q_%s/qwen3_vl_2b/leader/%s.tsv" % (a.root, s, s))): | |
| if os.path.exists(pth): | |
| sens[nm] = micro_cer_excluding(pth, reg[s]["manifest"], drop) | |
| sens[nm + "_all_rows_same_method"] = micro_cer_excluding(pth, reg[s]["manifest"], set()) | |
| cell["sensitivity_without_duplicate_partner_rows"] = sens | |
| out["crops"][s] = cell | |
| for nm in ("tob_hand", "tob_print"): | |
| out["pages"][nm] = load(os.path.join(a.root, "pages", "page_score_%s.json" % nm)) | |
| if out["pages"][nm]: | |
| out["pages"][nm].pop("per_page_counts", None) | |
| json.dump(out, open(a.out, "w", encoding="utf-8"), indent=1, ensure_ascii=False) | |
| print("written", a.out) | |