File size: 1,742 Bytes
d82db76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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())