Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / ocr / src / main / assets / README.md
| 1 | # `:library:ocr` bundled model assets |
| 2 | |
| 3 | ## `ppocr_det.maml` — text detection |
| 4 | |
| 5 | Apache-2.0, from https://huggingface.co/PaddlePaddle/PP-OCRv5_mobile_det_onnx |
| 6 | @ `e6f4fa85f00e168c862bc462aebca69eef9b3d3d` (`inference.onnx`, 4,826,518 B fp32, |
| 7 | SHA-256 `a4319856…597b6e61d`) — the official PaddleOCR export of PP-OCRv5 mobile |
| 8 | detection. The source ONNX SHA-256 is in the `.maml` header, so a shipped asset traces |
| 9 | back to this line. |
| 10 | |
| 11 | Rebuild with `./scripts/ml/fetch_and_convert.sh`. |
| 12 | |
| 13 | ### It is folded, not copied |
| 14 | |
| 15 | Unlike every other `.maml`, this one is **not** the ONNX's tensors verbatim. The export |
| 16 | is PP-HGNetV2 as `paddle2onnx` spells it, and every convolution arrives as a chain: |
| 17 | |
| 18 | Conv (no bias) → Add(bias) → Mul(scalar) → Add(scalar) |
| 19 | → HardSigmoid(α=1/6) → Mul(self) → Mul(scalar) → Add(scalar) |
| 20 | |
| 21 | threaded through 192 `Identity` nodes. That is 62 convolutions, 117 `Add`s, 86 `Mul`s and |
| 22 | 34 `HardSigmoid`s, of which only about a dozen `Add`/`Mul` pairs are real ops — the |
| 23 | feature-pyramid residuals and the squeeze-excite gates. |
| 24 | |
| 25 | `scripts/ml/ppocr_fold.py` folds the constant part ahead of time: the bias, the batch |
| 26 | norms, the affine blocks that precede an activation, and `HardSigmoid`'s α and β all go |
| 27 | into each convolution's weight and bias, and `HardSigmoid(1/6) → Mul(self)` is recognised |
| 28 | as the `HardSwish` the runtime already fuses. All of it is exact - an affine map composed |
| 29 | with a convolution is a convolution - and the result is 62 convolutions, 2 transposed |
| 30 | convolutions, 10 average pools, 10 channel multiplies, 11 adds, 6 nearest resizes, 1 |
| 31 | concatenation and 14 scalar affines. |
| 32 | The 14 are the affine blocks that could **not** be folded forward into the convolution they |
| 33 | feed. `conv(a * x + t)` is `a * conv(x) + t * sum(W)` only at an interior pixel: a padded |
| 34 | convolution reads zero outside the input rather than `t`, so at the border the constant's |
| 35 | contribution is `t` times the in-bounds taps alone. Twelve of the 14 feed a padded |
| 36 | depthwise and two feed a squeeze-excite's two branches. See `nets::Kind::Affine`. |
| 37 | |
| 38 | That op inventory matches ncnn's independent conversion of the same model layer for layer |
| 39 | (`Convolution` 48 + `ConvolutionDepthWise` 14 = 62, `Pooling` 10, `Interp` 6, |
| 40 | `Deconvolution` 2, `HardSwish` 24, `HardSigmoid` 10, `Concat` 1), which is the strongest |
| 41 | available check that the fold is right. |
| 42 | |
| 43 | ### Preprocessing and post-processing |
| 44 | |
| 45 | From the export's own `inference.yml`, not guessed: |
| 46 | |
| 47 | * **BGR**, not RGB (`DecodeImage: img_mode: BGR`). |
| 48 | * Long side resized to **960** (`DetResizeForTest: resize_long: 960`). |
| 49 | * ImageNet mean/std — the same constants `u2netp` uses. |
| 50 | * DBNet post-processing: `thresh: 0.3`, `box_thresh: 0.6`, `max_candidates: 1000`, |
| 51 | `unclip_ratio: 1.5`. |
| 52 | |
| 53 | ## `ppocr_keys.txt` — the recogniser's character dictionary |
| 54 | |
| 55 | 836 characters, one per line, UTF-8, LF. Extracted from the same export's |
| 56 | `inference.yml` (`PostProcess.character_dict`, `CTCLabelDecode`), so it carries the same |
| 57 | Apache-2.0 provenance as the weights and needs no separate pin. |
| 58 | |
| 59 | **Its order is the decode.** The recogniser's final layer emits 838 logits positionally: |
| 60 | |
| 61 | | label | meaning | |
| 62 | |---|---| |
| 63 | | 0 | the CTC blank | |
| 64 | | 1..=836 | this file's lines, in order | |
| 65 | | 837 | the space, which `use_space_char` appends | |
| 66 | |
| 67 | So a character is `line[label - 1]`, and a file that lost or reordered a line decodes to |
| 68 | fluent-looking nonsense rather than failing. `post::ctc` checks the length at load, and |
| 69 | `tests/assets.rs` checks that length against the model's logit count. |
| 70 | |
| 71 | The space is deliberately **not** in the file — no entry in it is whitespace — so that a |
| 72 | text editor or a line-ending conversion cannot silently eat it. `post::ctc::Dictionary` |
| 73 | appends it. |
| 74 | |
| 75 | ## `ppocr_rec.maml` - text recognition |
| 76 | Apache-2.0, from https://huggingface.co/PaddlePaddle/latin_PP-OCRv5_mobile_rec_onnx |
| 77 | @ `89d3a50e2c27e2e7cceeab0e944c25c807d5db4f` (`inference.onnx`, 8,042,023 B fp32, |
| 78 | SHA-256 `78881130.dd121498`) - the official export of the **latin** PP-OCRv5 mobile |
| 79 | recogniser, which is the variant whose 836-character dictionary is `ppocr_keys.txt`. |
| 80 | Folded by `scripts/ml/ppocr_fold.py` like detection, into 56 layers and 112 tensors. |
| 81 | From the export's own `inference.yml`: **BGR**, `image_shape [3, 48, 320]`, |
| 82 | `CTCLabelDecode`. Height is fixed at 48 and width must be a multiple of 8, so `T = W / 8` |
| 83 | and the standard 320-wide crop gives 40 timesteps. |
| 84 | ### A CNN into a transformer, with no permutes |
| 85 | The backbone takes `3 x 48 x W` to `480 x 3 x W/4`; one average pool with kernel and |
| 86 | stride `(3, 2)` collapses that to `480 x 1 x W/8`, which *is* a sequence. Two pre-norm |
| 87 | transformer blocks at `d_model` 120 in 8 heads follow, then a concat back onto the pooled |
| 88 | features and an 838-way classifier. |
| 89 | `nets::ppocr_rec` keeps the sequence as `[d_model, 1, T]` - the layout the backbone already |
| 90 | produces - so all nine `Transpose`s and 52 `Reshape`s in the export are no-ops and are not |
| 91 | transcribed. Every projection is a 1x1 convolution and splitting `d_model` into heads is a |
| 92 | reinterpretation rather than a copy. The export's fused `[120, 360]` QKV projection is |
| 93 | split into three by the fold, and its `Mul(q, 1/sqrt(15))` is dropped because |
| 94 | `Kind::AttnScores` derives the same scale from the head geometry. |
| 95 | The classifier's final `Softmax` is **not** transcribed either. `post::ctc::decode` reads |
| 96 | the raw class-major logits: the argmax is unchanged by a softmax, and the winner's |
| 97 | probability is `1 / sum(exp(x - peak))` from the logits directly. |
| 98 | ### Both folds are checked against onnxruntime |
| 99 | The layer table digest pins structure, not weight values, so a fold that computes the |
| 100 | wrong number passes it. `scripts/ml/onnx_parity.py` checks the numbers, and found two |
| 101 | bugs that everything else missed - an affine folded into a padded convolution, and a |
| 102 | silently zeroed QKV bias. At 64 wide the recogniser's logits agree with onnxruntime to |
| 103 | 0.073 against a 0.075 bar from fp16 weight quantisation alone. |