File size: 2,283 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
47
48
49
50
51
52
"""Predict eval sets with a torch recognizer checkpoint (HF format) on MPS/CPU.

Usage: torch_predict.py <model_dir> <out_dir> name:root:labels [name:root:labels ...]
Writes ``<out_dir>/<name>.tsv`` rows ``relpath<TAB>prediction<TAB>score``. Only crops with
the identical PaddleX pad width share a batch, so every crop sees exactly the padding it
gets from single-image PaddleX inference (SVTR attention is padding-sensitive).
"""

from __future__ import annotations

import sys
from collections import defaultdict
from pathlib import Path

import torch

from common import batch_tensor, ctc_greedy_decode, load_character_list, load_model, padded_width, pick_device, read_bgr, resized_width


def main() -> int:
    model_dir, out_dir = Path(sys.argv[1]), Path(sys.argv[2])
    out_dir.mkdir(parents=True, exist_ok=True)
    device = pick_device("auto")
    chars = load_character_list(model_dir)
    model = load_model(model_dir, device).eval()
    for spec in sys.argv[3:]:
        name, root, labels = spec.split(":")
        rows = [line.split("\t", 1)[0] for line in Path(labels).read_text(encoding="utf-8").splitlines() if "\t" in line]
        images = [read_bgr(Path(root) / rel) for rel in rows]
        widths = [resized_width(img) for img in images]
        by_pad: dict[int, list[int]] = defaultdict(list)
        for i, width in enumerate(widths):
            by_pad[padded_width(width)].append(i)
        preds: dict[int, tuple[str, float]] = {}
        with torch.no_grad():
            for group in by_pad.values():
                for start in range(0, len(group), 32):
                    idx = group[start:start + 32]
                    probs = model(batch_tensor([images[i] for i in idx], [widths[i] for i in idx]).to(device)).last_hidden_state
                    for i, result in zip(idx, ctc_greedy_decode(probs.float().cpu(), chars), strict=True):
                        preds[i] = result
        with (out_dir / f"{name}.tsv").open("w", encoding="utf-8") as handle:
            for i, rel in enumerate(rows):
                text, score = preds[i]
                handle.write(f"{rel}\t{text.replace(chr(9), ' ')}\t{score:.4f}\n")
        print(f"{name}: {len(rows)} predictions", flush=True)
    return 0


if __name__ == "__main__":
    sys.exit(main())