prosperious's picture
Add 1000 files (ocr-tech/tests/test_audit_limbu_book_missed_line_review_coverage.py ...)
d82db76 verified
Raw History Blame Contribute Delete
1.74 kB
"""Check that the torch port reproduces the official Paddle Devanagari recognizer.
Usage: parity.py <model_dir> <crop_root> <crop_labels> <paddle_preds.tsv>
``paddle_preds.tsv`` holds ``relpath<TAB>text`` from the Paddle model on the same crops.
Both BGR (PaddleX convention) and RGB channel orders are scored.
"""
from __future__ import annotations
import sys
from pathlib import Path
import torch
from common import batch_tensor, ctc_greedy_decode, load_character_list, load_model, read_bgr, resized_width
def main() -> int:
model_dir, crop_root, labels, paddle_preds = (Path(x) for x in sys.argv[1:5])
chars = load_character_list(model_dir)
model = load_model(model_dir, torch.device("cpu")).eval()
paddle = dict(line.split("\t", 1) for line in paddle_preds.read_text(encoding="utf-8").splitlines() if "\t" in line)
rows = [line.split("\t", 1) for line in labels.read_text(encoding="utf-8").splitlines() if "\t" in line]
for order in ("bgr", "rgb"):
same = 0
mismatches = []
for rel, _ in rows:
image = read_bgr(crop_root / rel)
if order == "rgb":
image = image[:, :, ::-1].copy()
with torch.no_grad():
probs = model(batch_tensor([image], [resized_width(image)])).last_hidden_state
text = ctc_greedy_decode(probs, chars)[0][0]
if text == paddle.get(rel, "").strip():
same += 1
elif len(mismatches) < 4:
mismatches.append((rel, paddle.get(rel), text))
print(f"{order}: torch == paddle on {same}/{len(rows)} crops")
for item in mismatches:
print(" ", item)
return 0
if __name__ == "__main__":
sys.exit(main())