"""Export a fine-tuned torch recognizer to a PaddleX-servable ONNX model directory. Usage: export_onnx.py ``reference_onnx_dir`` is PaddlePaddle/devanagari_PP-OCRv5_mobile_rec_onnx: its ``inference.yml`` (pre/post-processing and character dictionary) is copied unchanged, and the exported graph keeps its I/O contract: input ``x`` [N, 3, 48, W] float32, output ``fetch_name_0`` [N, T, 570] softmax probabilities. The export is verified against the torch model on random inputs of several widths before the directory is considered valid. """ from __future__ import annotations import shutil import sys from pathlib import Path import numpy as np import onnxruntime as ort import torch from common import load_model class Probs(torch.nn.Module): def __init__(self, model: torch.nn.Module) -> None: super().__init__() self.model = model def forward(self, x: torch.Tensor) -> torch.Tensor: return self.model(x).last_hidden_state def main() -> int: model_dir, reference_dir, out_dir = Path(sys.argv[1]), Path(sys.argv[2]), Path(sys.argv[3]) out_dir.mkdir(parents=True, exist_ok=True) model = Probs(load_model(model_dir, torch.device("cpu")).eval()).eval() onnx_path = out_dir / "inference.onnx" torch.onnx.export( model, (torch.randn(1, 3, 48, 320),), str(onnx_path), input_names=["x"], output_names=["fetch_name_0"], dynamic_axes={"x": {0: "batch", 3: "width"}, "fetch_name_0": {0: "batch", 1: "steps"}}, opset_version=17, dynamo=False, ) shutil.copyfile(reference_dir / "inference.yml", out_dir / "inference.yml") session = ort.InferenceSession(str(onnx_path), providers=["CPUExecutionProvider"]) worst = 0.0 for batch, width in ((1, 320), (4, 480), (2, 1200)): x = torch.randn(batch, 3, 48, width) with torch.no_grad(): ref = model(x).numpy() got = np.asarray(session.run(["fetch_name_0"], {"x": x.numpy()})[0]) if got.shape != ref.shape: raise RuntimeError(f"shape mismatch at width {width}: onnx {got.shape} vs torch {ref.shape}") diff = float(np.abs(got - ref).max()) argmax_agree = float((got.argmax(-1) == ref.argmax(-1)).mean()) print(f"batch {batch} width {width}: T={got.shape[1]} max|diff|={diff:.2e} argmax agree={argmax_agree:.4f}") worst = max(worst, diff) if worst > 1e-3: raise RuntimeError(f"ONNX export deviates from torch (max |diff| {worst:.2e})") print(f"exported and verified: {out_dir}") return 0 if __name__ == "__main__": sys.exit(main())