File size: 3,320 Bytes
f9bc0c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
from pathlib import Path

import cv2
import numpy as np
import onnxruntime as ort

from indra.indra_ocr.vocab import Vocab
from indra.indra_ocr.rec_preproc import pad_batch
from tools.exp.common import collapseIds, lev, norm

home = Path.home()
data = home / "ocr/ocr-deva-ft/data/chunks-v4/test"
manifest = Path("results/stratified_1500/sample_manifest.jsonl")
model = Path("models/indra-ocr-v3.1.1/rec.onnx")
outdir = Path("results/padding_variants")
outdir.mkdir(parents=True, exist_ok=True)

if not manifest.is_file():
    raise FileNotFoundError(f"Missing baseline manifest: {manifest}")

rows = [json.loads(line) for line in manifest.read_text(encoding="utf-8").splitlines() if line.strip()]
if len(rows) != 1500:
    raise RuntimeError(f"Expected 1500 manifest rows, got {len(rows)}")

session = ort.InferenceSession(str(model), providers=["CPUExecutionProvider"])
vocab = Vocab.multi()
variants = {
    "baseline": (0.00, 0.00),
    "small_pad": (0.05, 0.10),
    "moderate_pad": (0.20, 0.30),
}
results = []

for i, row in enumerate(rows, 1):
    path = data / row["image"]
    gray = cv2.imread(str(path), cv2.IMREAD_GRAYSCALE)
    if gray is None:
        raise RuntimeError(f"Cannot read {path}")

    h, w = gray.shape
    for name, (pad_y, pad_x) in variants.items():
        py, px = round(h * pad_y), round(h * pad_x)
        padded = cv2.copyMakeBorder(
            gray, py, py, px, px, cv2.BORDER_CONSTANT,
            value=255
        )
        ph, pw = padded.shape
        target_w = max(8, min(4096, round(pw * 32 / ph)))
        interp = cv2.INTER_AREA if ph > 32 else cv2.INTER_LINEAR
        resized = cv2.resize(padded, (target_w, 32), interpolation=interp)
        ink = 1.0 - resized.astype(np.float32) / 255.0
        x, widths = pad_batch([ink])
        ids, conf, lengths = session.run(
            ["ids", "conf", "lengths"],
            {"x": x, "widths": widths.astype(np.int64)}
        )
        seq = ids[0, :int(lengths[0])].tolist()
        pred = vocab.decode(collapseIds(seq))
        ref, hyp = norm(row["text"]), norm(pred)
        results.append({
            "image": row["image"],
            "edition": row["edition"],
            "variant": name,
            "ground_truth": row["text"],
            "prediction": pred,
            "edits": lev(ref, hyp),
            "reference_chars": len(ref),
            "exact_match": int(ref == hyp),
        })

    if i % 100 == 0:
        print(f"Processed {i}/{len(rows)} images")

output = outdir / "predictions.jsonl"
with output.open("w", encoding="utf-8") as f:
    for r in results:
        f.write(json.dumps(r, ensure_ascii=False) + "\n")

print("\n=== PADDING VARIANT RESULTS ===")
for edition in ["2082-03-24", "2082-08-12", "2082-12-25", "ALL"]:
    print(f"\nEdition: {edition}")
    for name in variants:
        subset = [r for r in results if r["variant"] == name and (edition == "ALL" or r["edition"] == edition)]
        edits = sum(r["edits"] for r in subset)
        chars = sum(r["reference_chars"] for r in subset)
        exact = sum(r["exact_match"] for r in subset)
        print(f"  {name}: exact={exact}/{len(subset)} ({100*exact/max(len(subset),1):.2f}%), CER={100*edits/max(chars,1):.4f}% ({edits}/{chars})")

print("\nPredictions:", output)
print("Providers:", session.get_providers())