File size: 6,024 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"""Shared PyTorch helpers for fine-tuning the official devanagari_PP-OCRv5_mobile_rec.

The model is the Hugging Face ``transformers`` port (``PPOCRV5MobileRecForTextRecognition``)
initialised from ``PaddlePaddle/devanagari_PP-OCRv5_mobile_rec_safetensors``. Preprocessing
mirrors PaddleX ``OCRReisizeNormImg`` (the path the k2 OCR worker serves): BGR input,
height 48, aspect-preserving width ``ceil(48 * w / h)``, ``(x / 255 - 0.5) / 0.5``,
zero padding on the right. Class 0 is the CTC blank, 1..568 the dictionary, 569 space.
"""

from __future__ import annotations

import json
import math
from pathlib import Path

import cv2
import numpy as np
import torch
import torch.nn.functional as F
from transformers import PPOCRV5MobileRecForTextRecognition

IMG_H = 48
MIN_W = 320
MAX_W = 3200


def load_character_list(model_dir: Path) -> list[str]:
    cfg = json.loads((model_dir / "preprocessor_config.json").read_text(encoding="utf-8"))
    chars = cfg["character_list"]
    if chars[0] != "blank" or chars[-1] != " ":
        raise ValueError(f"unexpected character_list layout in {model_dir}: {chars[:2]} ... {chars[-2:]}")
    return chars


DEVICE_CHOICES = ("auto", "cuda", "mps", "cpu")


def pick_device(name: str = "auto") -> torch.device:
    """``auto`` prefers CUDA (T4), then MPS (k2), then CPU; an explicit device must be available."""
    if name not in DEVICE_CHOICES:
        raise ValueError(f"device must be one of {DEVICE_CHOICES}, got {name!r}")
    if name == "auto":
        if torch.cuda.is_available():
            return torch.device("cuda")
        return torch.device("mps" if torch.backends.mps.is_available() else "cpu")
    if name == "cuda" and not torch.cuda.is_available():
        raise RuntimeError("device cuda requested but torch.cuda.is_available() is False")
    if name == "mps" and not torch.backends.mps.is_available():
        raise RuntimeError("device mps requested but torch.backends.mps.is_available() is False")
    return torch.device(name)


def load_model(model_dir: Path, device: torch.device) -> PPOCRV5MobileRecForTextRecognition:
    model = PPOCRV5MobileRecForTextRecognition.from_pretrained(str(model_dir))
    return model.to(device)


def read_bgr(path: Path) -> np.ndarray:
    image = cv2.imread(str(path), cv2.IMREAD_COLOR)
    if image is None:
        raise FileNotFoundError(f"cannot decode image {path}")
    return image


def resized_width(image: np.ndarray) -> int:
    """Resized width exactly as PaddleX ``OCRReisizeNormImg.resize`` computes it.

    PaddleX pads to ``int(48 * max(320/48, w/h))`` (floor) and caps the aspect-preserving
    ``ceil(48 * w / h)`` at that value, so wide crops end up floor-width; above 3200 px the
    crop is squeezed to 3200. A 1-px mismatch here changes SVTR attention and flips
    low-confidence characters, so this must stay bit-identical to serving.
    """
    h, w = image.shape[:2]
    ratio = w / float(h)
    padded = int(IMG_H * max(MIN_W / IMG_H, ratio))
    if padded > MAX_W:
        return MAX_W
    wanted = int(math.ceil(IMG_H * ratio))
    return padded if wanted > padded else max(1, wanted)


def padded_width(resized: int) -> int:
    """PaddleX single-image pad target for a crop of the given resized width."""
    return max(MIN_W, resized)


def normalize(image: np.ndarray, width: int) -> np.ndarray:
    """PaddleX-identical resize + normalise into a (3, 48, width) float32 array (unpadded)."""
    resized = cv2.resize(image, (width, IMG_H)).astype(np.float32)
    return (resized.transpose(2, 0, 1) / 255.0 - 0.5) / 0.5


def batch_tensor(images: list[np.ndarray], widths: list[int]) -> torch.Tensor:
    """Pad a list of normalised images to one width (at least MIN_W), like PaddleX."""
    target = max(MIN_W, max(widths))
    out = np.zeros((len(images), 3, IMG_H, target), dtype=np.float32)
    for index, (image, width) in enumerate(zip(images, widths, strict=True)):
        out[index, :, :, :width] = normalize(image, width)
    return torch.from_numpy(out)


def logits(model: PPOCRV5MobileRecForTextRecognition, pixel_values: torch.Tensor) -> torch.Tensor:
    """Raw pre-softmax logits (batch, T, classes); the HF head applies softmax itself."""
    features = model.model(pixel_values).last_hidden_state
    encoded = model.head.encoder(features).last_hidden_state
    return model.head.head(encoded)


def ctc_greedy_decode(probs: torch.Tensor, chars: list[str]) -> list[tuple[str, float]]:
    """PaddleX CTCLabelDecode equivalent: argmax, collapse repeats, drop blanks."""
    scores, indices = probs.max(dim=2)
    results: list[tuple[str, float]] = []
    for seq, conf in zip(indices.tolist(), scores.tolist(), strict=True):
        text, kept = [], []
        prev = -1
        for position, idx in enumerate(seq):
            if idx != prev and idx != 0:
                text.append(chars[idx])
                kept.append(conf[position])
            prev = idx
        results.append(("".join(text), float(np.mean(kept)) if kept else 0.0))
    return results


def encode_label(text: str, char_to_index: dict[str, int]) -> list[int] | None:
    ids = []
    for ch in text:
        idx = char_to_index.get(ch)
        if idx is None:
            return None
        ids.append(idx)
    return ids


def ctc_loss(raw_logits: torch.Tensor, targets: list[list[int]]) -> torch.Tensor:
    log_probs = F.log_softmax(raw_logits.float(), dim=2).transpose(0, 1)  # (T, B, C)
    if log_probs.device.type == "mps":  # MPS has no CTC kernel; CUDA and CPU compute in place
        log_probs = log_probs.cpu()
    device = log_probs.device
    t = log_probs.shape[0]
    input_lengths = torch.full((log_probs.shape[1],), t, dtype=torch.long, device=device)
    target_lengths = torch.tensor([len(x) for x in targets], dtype=torch.long, device=device)
    flat = torch.tensor([i for x in targets for i in x], dtype=torch.long, device=device)
    return F.ctc_loss(log_probs, flat, input_lengths, target_lengths, blank=0,
                      reduction="mean", zero_infinity=True)