Download scripts/r5_ref/ref_paper_metrics.py from treoa/turkic-ocr-code: direct link, hf CLI and curl.
- Browser
- Download file 2.49 kB
-
https://huggingface.co/treoa/turkic-ocr-code/resolve/main/scripts/r5_ref/ref_paper_metrics.py
- Command line
-
hf download hf://treoa/turkic-ocr-code/scripts/r5_ref/ref_paper_metrics.py
-
curl -L -o ref_paper_metrics.py https://huggingface.co/treoa/turkic-ocr-code/resolve/main/scripts/r5_ref/ref_paper_metrics.py
2.49 kB
| """R5 REF: the metrics the two published KOHTD results may have used, computed on a prediction file (columns id, ref, hyp). | |
| micro_cer sum of edit distances / sum of reference lengths, case kept, spaces counted (the formula printed in both papers: (S+I+D)/N) | |
| crop_error_rate share of crops not read exactly, case kept (Bimurat 2024 p.10: 'WER' = share of wrongly recognized images) | |
| flor_* the pinned HTR-Flor evaluation.ocr_metrics: lower-cased, distance / longer string, mean over crops (CER, WER by whitespace tokens, SER) | |
| paddle_acc, paddle_norm_edit_dis PaddleOCR RecMetric, ignore_space False: exact match; 1 - mean(distance / longer string), case kept | |
| Each is given on all crops and on crops whose reference has more than one character ('check_text' view of the pinned reader, which drops one-character labels). | |
| Usage: python -I ref_paper_metrics.py <pred.tsv> <out.json> | |
| """ | |
| import csv | |
| import json | |
| import os | |
| import sys | |
| HERE = os.path.dirname(os.path.abspath(__file__)) | |
| sys.path.insert(0, HERE) | |
| import ref_common as rc # noqa: E402 | |
| sys.path.insert(0, rc.HTRFLOR_SRC) | |
| from data import evaluation as flor_eval # pinned # noqa: E402 | |
| import editdistance # noqa: E402 | |
| def metrics(refs, hyps): | |
| n = len(refs) | |
| if n == 0: | |
| return {"n": 0} | |
| dist = [editdistance.eval(list(h), list(r)) for r, h in zip(refs, hyps)] | |
| tot_ref = sum(len(r) for r in refs) | |
| flor = flor_eval.ocr_metrics(predicts=hyps, ground_truth=refs) | |
| pad_nd = sum(d / max(len(r), len(h), 1) for d, r, h in zip(dist, refs, hyps)) / n | |
| return {"n": n, "micro_cer": sum(dist) / max(tot_ref, 1), "crop_error_rate": sum(1 for r, h in zip(refs, hyps) if r != h) / n, | |
| "flor_cer": float(flor[0]), "flor_wer": float(flor[1]), "flor_ser": float(flor[2]), | |
| "paddle_acc": sum(1 for r, h in zip(refs, hyps) if r == h) / n, "paddle_norm_edit_dis": 1 - pad_nd} | |
| def main(): | |
| rows = list(csv.DictReader(open(sys.argv[1], encoding="utf-8", newline=""), delimiter="\t", quoting=csv.QUOTE_NONE)) | |
| refs, hyps = [r["ref"] for r in rows], [r["hyp"] for r in rows] | |
| out = {"file": sys.argv[1], "all_crops": metrics(refs, hyps)} | |
| keep = [i for i, r in enumerate(refs) if len(r) > 1] | |
| out["ref_longer_than_1_char"] = metrics([refs[i] for i in keep], [hyps[i] for i in keep]) | |
| json.dump(out, open(sys.argv[2], "w"), indent=1) | |
| print(json.dumps(out, indent=1)) | |
| if __name__ == "__main__": | |
| main() | |