File size: 4,693 Bytes
24aaa76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""Test pass 2: per-language results from EXISTING score files only (no prediction, no tuning). Language = the manifests' lang column
(the scorer's 'lang:<x>' groups). Figures: plain profile (N0) CER with the scorer's 95% cluster-bootstrap interval, rows, reference
characters (the CER denominator); for synth_clutter_test the HR1 count (empty-target images with any output). KOHTD also in the
repaired_refs reading (the verdict's). Pages: the page score files of test pass 2 (TurkishOCR-Bench, Kazakh benchmark).

usage: tp2_per_language.py --root DATA/outputs/r5/testpass2 --out JSON
"""
import argparse
import json
import os

D = "/path/to/TURKIC_OCR_DATA/outputs/r5"
LANGS = ["kk", "ky", "uz_latn", "uz_cyrl", "tr", "az", "tt", "ba", "tk", "sah"]
ap = argparse.ArgumentParser()
ap.add_argument("--root", required=True)
ap.add_argument("--out", required=True)
a = ap.parse_args()
T = a.root


def load(p):
    return json.load(open(p, encoding="utf-8")) if os.path.exists(p) else None


def files(s):
    """model label -> (score file, batching note)"""
    f = {"M1 PaddleOCR-VL-1.6 (final)": ["%s/scores/m1_%s/paddleocr_vl_1_6/leader/%s.json" % (T, s, s), D + "/scores/paddleocr_vl_1_6/leader/%s.json" % s],
         "Qwen3-VL-2B": ["%s/scores/q_%s/qwen3_vl_2b/leader/%s.json" % (T, s, s), D + "/scores/qwen3_vl_2b/leader/%s.json" % s]}
    out = {}
    for k, ps in f.items():
        for p in ps:
            if os.path.exists(p):
                out[k] = (p, "test pass 2 (CPU)" if "/testpass2/" in p else "24 Nov sealed pass (GPU)")
                break
    lf = "%s/scores/m2lf_%s/A1/long2x_labelfree/%s.json" % (T, s, s)
    lfl = "%s/scores/m2lf_%s/A1/long2x_labelfree_lm_gated/%s.json" % (T, s, s)
    if os.path.exists(lf):
        out["PP-OCRv6 medium greedy"] = (lf, "label-free batching (test pass 2)")
        out["PP-OCRv6 medium + gated LM"] = (lfl, "label-free batching (test pass 2)")
    else:
        out["PP-OCRv6 medium greedy"] = (D + "/scores/A1/long2x/%s.json" % s, "label-length batching (leaks), 24 Nov")
        out["PP-OCRv6 medium + gated LM"] = (D + "/scores/A1/long2x_lm_gated/%s.json" % s, "label-length batching (leaks), 24 Nov")
    return out


def lang_rows(d, prof="plain", metric="cer"):
    res = {}
    for g, v in d["profiles"][prof]["groups"].items():
        if g.startswith("lang:") or g == "all":
            m = v["metrics"].get(metric)
            if m is None:
                continue
            res[g.replace("lang:", "")] = {"rows": v["n_items"], "value": m.get("value"), "lo": m.get("lo"), "hi": m.get("hi"),
                                           "num": m.get("num"), "den": m.get("den")}
    return res


out = {"note": "existing predictions and score files only; plain profile (N0) CER; 95% cluster bootstrap of the scorer", "target_languages": LANGS,
       "synthetic_and_print": {}, "real_sets": {}}
for s in ("synth_hand_test", "synth_typed_test", "print_real_test"):
    cell = {}
    for mdl, (p, note) in files(s).items():
        d = load(p)
        if d is None or d.get("status") != "scored":
            continue
        cell[mdl] = {"source": note, "score_file": p, "per_language_cer": lang_rows(d)}
    out["synthetic_and_print"][s] = cell
cell = {}
for mdl, (p, note) in files("synth_clutter_test").items():
    d = load(p)
    if d is None or d.get("status") != "scored":
        continue
    cell[mdl] = {"source": note, "score_file": p, "per_language_invented_text_HR1": lang_rows(d, metric="hr1"),
                 "per_language_mean_output_chars": lang_rows(d, metric="hr1_mean_output_chars")}
out["synthetic_and_print"]["synth_clutter_test"] = cell
for s in ("kohtd_test", "az_localdoc_test", "rukopys_test", "hkr_test1", "hkr_test2", "chd_test", "school_test"):
    cell = {}
    for mdl, (p, note) in files(s).items():
        d = load(p)
        if d is None or d.get("status") != "scored":
            continue
        cell[mdl] = {"source": note, "score_file": p, "per_language_cer": lang_rows(d)}
        if s == "kohtd_test":
            cell[mdl]["repaired_refs_cer"] = lang_rows(d, prof="repaired_refs")
    out["real_sets"][s] = cell
for nm, p in (("turkishocr_bench_handwritten_pages (tr)", T + "/pages/page_score_tob_hand.json"),
              ("turkishocr_bench_printed_text_pages (tr)", T + "/pages/page_score_tob_print.json"),
              ("kz_benchmark_handwritten_pages (kk, descriptive only)", T + "/kzb/page_score_kzb.json")):
    d = load(p)
    if d:
        d.pop("per_page_counts", None)
        out["real_sets"][nm] = {"M1 PaddleOCR-VL-1.6 (final), YOLO11s stage-2 lines": d}
json.dump(out, open(a.out, "w", encoding="utf-8"), indent=1, ensure_ascii=False)
print("written", a.out)