File size: 4,386 Bytes
f572670
29b350b
 
 
 
 
 
 
 
 
 
 
 
 
 
f572670
 
29b350b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"""Minimal end-to-end use of the PP-OCRv6 Hanzi IDS model.

    python example_usage.py path/to/glyph.png

With no argument the script renders a few characters from a local font and
predicts them, so it can be pasted into a fresh environment as a smoke test.
"""
import json
import sys
from pathlib import Path

import numpy as np
import torch
from PIL import Image, ImageDraw, ImageFont

from hanzi_ids import CanonicalOutput, IDSModel, beam_batch
from hanzi_ids.render_utils import normalize_image

MODEL_DIR = Path(__file__).resolve().parent

# Default fonts used only by the self-test path. Any TTF/OTF works.
FONT_MAIN = "C:/Windows/Fonts/simsun.ttc"
FONT_EXT_B = "C:/Windows/Fonts/simsunb.ttf"


class IDSRunner:
    """Load the checkpoint ensemble and predict IDS candidates for glyph images."""

    def __init__(self, model_dir=MODEL_DIR, device="cpu", threads=4):
        model_dir = Path(model_dir)
        torch.set_num_threads(threads)
        self.device = torch.device(device)
        self.config = json.loads(
            (model_dir / "inference_config.json").read_text(encoding="utf-8")
        )
        self.models = []
        for name in self.config["checkpoints"]:
            ck = torch.load(model_dir / name, map_location="cpu", weights_only=True)
            model = IDSModel(
                ck["vocab"],
                ck["args"]["dim"],
                ck["args"]["layers"],
                tree_position=ck["args"].get("tree_position", False),
            )
            model.load_state_dict(ck["model"])
            model.eval()
            model.to(self.device)
            self.models.append(model)
        self.policy = CanonicalOutput.load(model_dir / self.config["output_policy"])

    def _batch(self, images):
        rows = [
            np.repeat(
                (np.asarray(normalize_image(im), dtype=np.float32) / 127.5 - 1)[None],
                3,
                axis=0,
            )
            for im in images
        ]
        return torch.stack([torch.from_numpy(r) for r in rows]).to(self.device)

    def predict(self, images, beam_size=5):
        """Return up to five distinct IDS candidates per image, best first."""
        with torch.inference_mode():
            batch = self._batch(images)
            results = []
            for tensor, raw in zip(batch, beam_batch(self.models, batch, beam_size)):
                candidates = self.policy.apply(raw, merge=True)
                if len(candidates) < 5:
                    wider = beam_batch(self.models, tensor[None], 12)[0]
                    candidates = self.policy.apply(wider, merge=True)
                results.append(
                    [
                        {
                            "ids": c["ids"],
                            "canonical_log_mass": c["canonical_log_mass"],
                            "mean_token_probability": c["mean_token_probability"],
                            "equivalent_raw_ids": c.get("equivalent_raw_ids", []),
                            "valid": c["valid"],
                        }
                        for c in candidates[:5]
                    ]
                )
            return results


def render_glyph(char, font_path, px=200):
    font = ImageFont.truetype(font_path, px)
    left, top, right, bottom = font.getbbox(char)
    image = Image.new("L", (right - left + 20, bottom - top + 20), 255)
    ImageDraw.Draw(image).text((10 - left, 10 - top), char, font=font, fill=0)
    return image


def pick_font(char):
    return FONT_EXT_B if ord(char) >= 0x20000 else FONT_MAIN


def self_test(runner):
    chars = ["\u4e00", "\u54c1", "\u5b57", "\u660e", "\u6728", "\u6797", "\u68ee"]
    for char in chars:
        print(f"=== {char}  U+{ord(char):04X}")
        for c in runner.predict([render_glyph(char, pick_font(char))])[0]:
            print(
                f"    {c['ids']:<28} mass={c['canonical_log_mass']:>8.3f}  "
                f"p={c['mean_token_probability']:.4f}"
            )
    return True


def main():
    runner = IDSRunner()
    if len(sys.argv) > 1:
        path = Path(sys.argv[1])
        candidates = runner.predict([Image.open(path)])[0]
        print(json.dumps({"file": str(path), "candidates": candidates},
                         ensure_ascii=False, indent=2))
        return 0
    return 0 if self_test(runner) else 1


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