"""Fine-tune the official devanagari_PP-OCRv5_mobile_rec with PyTorch (CUDA on the T4, MPS on k2). Paddle's CPU training on macOS arm64 runs at ~2 images/s (the PPLCNetV3 depthwise convs have no fast CPU kernel there), so training uses the Hugging Face torch port of the same network, initialised from PaddlePaddle/devanagari_PP-OCRv5_mobile_rec_safetensors, with CTC loss. Sources are ``name:root:labels:weight:aug`` where ``weight`` is the per-epoch sampling multiplier (0.5 = a random half each epoch, 2 = every line twice) and ``aug`` the probability of photometric/geometric augmentation. Batches are bucketed by resized width and padded exactly like PaddleX inference, so there is no train/serve squeeze mismatch. Model selection uses ``--dev`` sets only; test sets are never read here. ``--guard-dev NAME`` turns a dev set into a no-regression guard: a checkpoint is eligible as "best" only while every guard set's codepoint CER stays within ``--guard-tol`` (absolute) of its step-0 value, and eligible checkpoints are ranked by the mean CER of the non-guard sets. Without guards, a 5-point handwriting gain outweighs a 1-point print loss in the plain mean. """ from __future__ import annotations import argparse import json import math import random import sys import time import unicodedata from pathlib import Path import cv2 import numpy as np import torch from PIL import Image from torch.utils.data import DataLoader, Dataset, Sampler from common import ( IMG_H, MIN_W, DEVICE_CHOICES, batch_tensor, ctc_greedy_decode, ctc_loss, encode_label, load_character_list, load_model, logits, pick_device, read_bgr, resized_width, ) from eval_metrics import evaluate from selection import guard_regressions, split_dev_sets, target_score MAX_TRAIN_W = 960 def augment(image: np.ndarray, rng: random.Random) -> np.ndarray: """Conservative capture degradations that never crop glyphs.""" img = image if rng.random() < 0.3: # extra margin, replicated border top, bottom = rng.randint(0, 4), rng.randint(0, 4) left, right = rng.randint(0, 8), rng.randint(0, 8) img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_REPLICATE) if rng.random() < 0.25: h, w = img.shape[:2] m = cv2.getRotationMatrix2D((w / 2, h / 2), rng.uniform(-2.0, 2.0), 1.0) img = cv2.warpAffine(img, m, (w, h), borderMode=cv2.BORDER_REPLICATE) if rng.random() < 0.15: kernel = np.ones((2, 2), np.uint8) img = cv2.erode(img, kernel) if rng.random() < 0.5 else cv2.dilate(img, kernel) if rng.random() < 0.3: # low-resolution capture h, w = img.shape[:2] scale = rng.uniform(0.4, 0.8) small = cv2.resize(img, (max(4, int(w * scale)), max(4, int(h * scale))), interpolation=cv2.INTER_AREA) img = cv2.resize(small, (w, h), interpolation=cv2.INTER_LINEAR) if rng.random() < 0.25: img = cv2.GaussianBlur(img, (3, 3) if rng.random() < 0.7 else (5, 5), 0) if rng.random() < 0.4: alpha, beta = rng.uniform(0.6, 1.3), rng.uniform(-30, 30) img = cv2.convertScaleAbs(img, alpha=alpha, beta=beta) if rng.random() < 0.2: tint = np.array([rng.uniform(0.8, 1.0) for _ in range(3)], dtype=np.float32) img = np.clip(img.astype(np.float32) * tint, 0, 255).astype(np.uint8) if rng.random() < 0.3: noise = np.random.default_rng(rng.randint(0, 2**31)).normal(0, rng.uniform(2, 10), img.shape) img = np.clip(img.astype(np.float32) + noise, 0, 255).astype(np.uint8) if rng.random() < 0.3: ok, buf = cv2.imencode(".jpg", img, [cv2.IMWRITE_JPEG_QUALITY, rng.randint(30, 85)]) decoded = cv2.imdecode(buf, cv2.IMREAD_COLOR) if ok else None if decoded is not None: img = decoded return img class LineSet(Dataset): def __init__(self, items: list[dict], train: bool, seed: int) -> None: self.items = items self.train = train self.seed = seed def __len__(self) -> int: return len(self.items) def __getitem__(self, index: int): item = self.items[index] image = read_bgr(Path(item["path"])) if self.train and item["aug"] > 0: rng = random.Random((self.seed * 1_000_003 + index * 7919 + int(time.time_ns() % 1_000_000)) % (2**31)) if rng.random() < item["aug"]: image = augment(image, rng) width = min(resized_width(image), MAX_TRAIN_W if self.train else 3200) return image, width, item["ids"], item["text"] def collate(batch): images, widths, ids, texts = zip(*batch) return batch_tensor(list(images), list(widths)), list(ids), list(texts) class BucketSampler(Sampler[list[int]]): """Width-bucketed batches; a fresh weighted epoch sample every iteration.""" def __init__(self, groups: list[tuple[list[int], float]], widths: list[int], batch_size: int, seed: int) -> None: self.groups, self.widths, self.batch_size, self.seed = groups, widths, batch_size, seed self.epoch = 0 def epoch_indices(self) -> list[int]: rng = random.Random(self.seed + self.epoch) out: list[int] = [] for indices, weight in self.groups: whole, frac = int(weight), weight - int(weight) out += indices * whole out += rng.sample(indices, int(round(frac * len(indices)))) rng.shuffle(out) return out def __iter__(self): rng = random.Random(self.seed * 31 + self.epoch) order = self.epoch_indices() window = self.batch_size * 64 batches: list[list[int]] = [] for start in range(0, len(order), window): chunk = sorted(order[start:start + window], key=lambda i: self.widths[i]) batches += [chunk[i:i + self.batch_size] for i in range(0, len(chunk), self.batch_size)] rng.shuffle(batches) self.epoch += 1 return iter(b for b in batches if len(b) == self.batch_size) def __len__(self) -> int: return len(self.epoch_indices()) // self.batch_size def load_source(spec: str, char_to_index: dict[str, int], max_len: int, stats: dict) -> tuple[list[dict], float]: name, root, labels, weight, aug = spec.split(":") root_path = Path(root) items = [] for line in Path(labels).read_text(encoding="utf-8").splitlines(): if "\t" not in line: continue rel, text = line.split("\t", 1) text = unicodedata.normalize("NFC", text).strip() if not text or len(text) > max_len: stats[f"{name}_skipped_length"] = stats.get(f"{name}_skipped_length", 0) + 1 continue ids = encode_label(text, char_to_index) if ids is None: stats[f"{name}_skipped_oov"] = stats.get(f"{name}_skipped_oov", 0) + 1 continue items.append({"path": str(root_path / rel), "text": text, "ids": ids, "aug": float(aug), "source": name}) stats[f"{name}_kept"] = len(items) return items, float(weight) def image_widths(items: list[dict]) -> list[int]: widths = [] for item in items: with Image.open(item["path"]) as im: w, h = im.size widths.append(min(MAX_TRAIN_W, max(MIN_W, int(math.ceil(IMG_H * w / float(h)))))) return widths def set_train_mode(model: torch.nn.Module) -> None: """Train mode with BatchNorm running statistics frozen at their pretrained values. Width-bucketed batches have very different padding fractions, so live BN statistics (momentum 0.1, i.e. the last ~10 batches) swing with whatever bucket came last and made the step-4000 v1b eval 3x worse on the synthetic dev set. Affine BN weights still train. """ model.train() for module in model.modules(): if isinstance(module, torch.nn.modules.batchnorm._BatchNorm): module.eval() @torch.no_grad() def run_eval(model, loader, chars, device) -> dict: model.eval() pairs = [] for pixel_values, _, texts in loader: probs = model(pixel_values.to(device)).last_hidden_state preds = ctc_greedy_decode(probs.float().cpu(), chars) pairs += [(p, t) for (p, _), t in zip(preds, texts, strict=True)] set_train_mode(model) return evaluate(pairs) def main() -> int: p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) p.add_argument("--model-dir", type=Path, required=True) p.add_argument("--out", type=Path, required=True) p.add_argument("--train", action="append", required=True, help="name:root:labels:weight:aug") p.add_argument("--dev", action="append", required=True, help="name:root:labels (max rows via --dev-max)") p.add_argument("--dev-max", type=int, default=2000) p.add_argument("--guard-dev", action="append", default=[], help="dev set name that must not regress beyond --guard-tol vs step 0 (repeatable)") p.add_argument("--guard-tol", type=float, default=0.002, help="allowed absolute cp CER increase on guard sets") p.add_argument("--epochs", type=int, default=4) p.add_argument("--batch-size", type=int, default=64) p.add_argument("--grad-accum", type=int, default=1, help="split each batch into this many micro-batches (same gradient, less GPU memory)") p.add_argument("--lr", type=float, default=1e-4) p.add_argument("--weight-decay", type=float, default=1e-4) p.add_argument("--warmup-steps", type=int, default=500) p.add_argument("--eval-every", type=int, default=1500) p.add_argument("--max-len", type=int, default=25) p.add_argument("--workers", type=int, default=6) p.add_argument("--seed", type=int, default=20260924) p.add_argument("--device", choices=DEVICE_CHOICES, default="auto") args = p.parse_args() torch.manual_seed(args.seed) device = pick_device(args.device) if not 1 <= args.grad_accum <= args.batch_size: raise SystemExit(f"--grad-accum must be between 1 and --batch-size, got {args.grad_accum}") chars = load_character_list(args.model_dir) char_to_index = {c: i for i, c in enumerate(chars) if i > 0} args.out.mkdir(parents=True, exist_ok=True) stats: dict = {} items: list[dict] = [] groups: list[tuple[list[int], float]] = [] for spec in args.train: source_items, weight = load_source(spec, char_to_index, args.max_len, stats) groups.append((list(range(len(items), len(items) + len(source_items))), weight)) items += source_items dev_sets = {} for spec in args.dev: name, root, labels = spec.split(":") dev_items, _ = load_source(f"{name}:{root}:{labels}:1:0", char_to_index, 10_000, stats) dev_items = random.Random(args.seed).sample(dev_items, min(args.dev_max, len(dev_items))) dev_sets[name] = DataLoader(LineSet(dev_items, train=False, seed=args.seed), batch_size=64, shuffle=False, num_workers=2, collate_fn=collate) print(json.dumps(stats, indent=1), flush=True) try: target_sets = split_dev_sets(list(dev_sets), args.guard_dev) except ValueError as exc: raise SystemExit(f"--guard-dev: {exc}") from exc print("reading image sizes...", flush=True) widths = image_widths(items) sampler = BucketSampler(groups, widths, args.batch_size, args.seed) loader = DataLoader(LineSet(items, train=True, seed=args.seed), batch_sampler=sampler, num_workers=args.workers, collate_fn=collate, persistent_workers=True, prefetch_factor=4) steps_per_epoch = len(sampler) total_steps = steps_per_epoch * args.epochs model = load_model(args.model_dir, device) set_train_mode(model) optimizer = torch.optim.AdamW(model.parameters(), lr=args.lr, weight_decay=args.weight_decay) def lr_at(step: int) -> float: if step < args.warmup_steps: return args.lr * (step + 1) / args.warmup_steps progress = (step - args.warmup_steps) / max(1, total_steps - args.warmup_steps) return args.lr * 0.5 * (1 + math.cos(math.pi * min(1.0, progress))) log = (args.out / "train_log.jsonl").open("a", encoding="utf-8") guard_baseline: dict[str, float] = {} def evaluate_and_log(step: int, best: float | None) -> float | None: results = {name: run_eval(model, dl, chars, device) for name, dl in dev_sets.items()} cer = {name: r["cer_codepoint"] for name, r in results.items()} score = target_score(cer, target_sets) if not guard_baseline: guard_baseline.update({name: cer[name] for name in args.guard_dev}) regressions = guard_regressions(cer, guard_baseline, args.guard_tol) record = {"step": step, "dev_mean_cp_cer": float(np.mean([r["cer_codepoint"] for r in results.values()])), "target_mean_cp_cer": score, "eligible": not regressions, "guard_regressions": regressions, **{f"{k}_{m}": v[m] for k, v in results.items() for m in ("cer_codepoint", "cer_grapheme", "exact_line_acc")}} log.write(json.dumps(record) + "\n"); log.flush() print(json.dumps(record), flush=True) if regressions: print(f"step {step}: not eligible as best, guard sets regressed: {regressions}", flush=True) return best if best is None or score < best: model.save_pretrained(args.out / "best") (args.out / "best" / "preprocessor_config.json").write_text( (args.model_dir / "preprocessor_config.json").read_text(encoding="utf-8"), encoding="utf-8") (args.out / "best" / "selected.json").write_text(json.dumps(record, indent=1), encoding="utf-8") return score return best print(f"device={device} train_items={len(items)} steps/epoch={steps_per_epoch} total={total_steps}", flush=True) best = evaluate_and_log(0, None) # the unmodified starting point, so "best" can only improve on it step = 0 started = time.time() for epoch in range(args.epochs): for pixel_values, ids, _ in loader: for group in optimizer.param_groups: group["lr"] = lr_at(step) optimizer.zero_grad(set_to_none=True) # Micro-batches weighted by their share of the batch: with mean-reduced CTC and frozen # BatchNorm statistics the summed gradient equals the full-batch gradient exactly. loss_value, finite = 0.0, True for part in torch.arange(len(ids)).chunk(args.grad_accum): rows = part.tolist() part_loss = ctc_loss(logits(model, pixel_values[part].to(device)), [ids[i] for i in rows]) if not torch.isfinite(part_loss): finite = False break (part_loss * (len(rows) / len(ids))).backward() loss_value += part_loss.item() * len(rows) / len(ids) if not finite: print(f"step {step}: non-finite loss, batch skipped", flush=True) optimizer.zero_grad(set_to_none=True) step += 1 continue torch.nn.utils.clip_grad_norm_(model.parameters(), 5.0) optimizer.step() step += 1 if step % 50 == 0: rate = step * args.batch_size / (time.time() - started) print(f"epoch {epoch} step {step}/{total_steps} loss {loss_value:.4f} lr {lr_at(step):.2e} {rate:.0f} img/s", flush=True) if step % args.eval_every == 0: best = evaluate_and_log(step, best) model.save_pretrained(args.out / "last") best = evaluate_and_log(step, best) print(f"done: best target mean cp CER {best:.4f} (targets {target_sets}, guards {args.guard_dev})", flush=True) return 0 if __name__ == "__main__": sys.exit(main())