Skip to content

[Feature] Rec: width-proportional CTC length in training (train_seq_len) for PPLCNetV3/V4, PPHGNetV2, PPHGNet_small - #18420

Open
ErshovVE wants to merge 2 commits into
PaddlePaddle:mainfrom
ErshovVE:feat/rec-train-seq-len
Open

ErshovVE wants to merge 2 commits into
PaddlePaddle:mainfrom
ErshovVE:feat/rec-train-seq-len

Conversation

@ErshovVE

Copy link
Copy Markdown

Problem

The PP-LCNet / PP-HGNet recognition backbones pool the feature map to a fixed 40 time steps in training
(adaptive_avg_pool2d(x, [1, 40])), but to W / 8 steps at inference (avg_pool2d(x, [3, 2])). The backbones
are PPLCNetV3 (PP-OCRv4/v5 mobile rec), PPLCNetV4 (PP-OCRv6), PPHGNetV2 (PP-OCRv5 server rec) and PPHGNet_small
(PP-OCRv4 server rec).

CTC needs at least as many steps as the label has characters, plus one per repeated pair. For a label that does not
fit, paddle.nn.functional.ctc_loss silently returns zero loss and zero gradient. As a result:

  • text lines longer than ~35–40 characters never train the CTC head;
  • they still count in the batch, so they dilute the loss;
  • the training accuracy looks far worse than the test accuracy.

On our Russian/English document lines (many 60–95 characters long), the training line accuracy was ~0.5 while the
test accuracy was ~0.9, and the CTC head had never seen a long line.

Repro (on this branch, CPU):

import numpy as np, paddle
from ppocr.losses.rec_ctc_loss import CTCLoss
from ppocr.modeling.backbones.rec_lcnetv3 import PPLCNetV3

for train_seq_len in (40, -1):
    backbone = PPLCNetV3(scale=0.95, train_seq_len=train_seq_len)
    backbone.train()
    T = backbone(paddle.rand([1, 3, 48, 960])).shape[3]   # a 960 px wide line
    logits = paddle.randn([1, T, 100])
    label = paddle.randint(1, 100, [1, 60], dtype="int32")  # 60 characters
    loss = CTCLoss()(logits, [None, label, paddle.to_tensor([60], dtype="int64")])["loss"]
    print(train_seq_len, T, float(loss))
# 40  40   0.0       <- the line is silently ignored
# -1  120  ~460      (random logits)

Change

  • New backbone option train_seq_len (default 40: unchanged behaviour, existing configs and weights are not
    affected). With -1, the training sequence length is W / 8 (adaptive_avg_pool2d(x, [1, W_feat // 2])), the
    same as at inference.
  • The shared logic is in ppocr/modeling/backbones/rec_seq_pool.py (rec_seq_pool, check_train_seq_len). The
    four backbones call it; the eval and export paths are unchanged, so an exported model is identical.
  • Invalid values (0, < -1) fail at construction. -1 needs a known feature width at train time, so with
    Global.to_static it asserts with an explicit message.

Usage:

Architecture:
  Backbone:
    name: PPLCNetV3          # or PPLCNetV4 / PPHGNetV2_B4 / PPHGNet_small
    train_seq_len: -1        # CTC length W / 8 at training time, the same as inference

With -1, batches of different widths give different sequence lengths. That is fine for CTC, and it works with
MultiScaleSampler, whose batches already have different widths.

Tests

tests/ppocr/test_rec_seq_pool.py:

  • rec_seq_pool itself: fixed and proportional lengths, input heights 32 / 48 / 64, a narrow input, the eval path,
    the height check;
  • every backbone: the default is still 40 in training; -1 gives W / 8; eval is unchanged with both settings;
    invalid values are rejected.

PPHGNetV2_B4 and PPHGNet_small are marked resource_intensive.

pytest tests/ppocr/test_rec_seq_pool.py -m ""      # 43 passed

Results

We trained PP-OCRv6 small (PPLCNetV4) on 4.1 M Russian/English lines of 1–250 characters with train_seq_len: -1.
Line accuracy on our validation (4 241 real document lines, exact match, spaces count), by label length:

Length 0–15 16–40 41–60 61+
Line accuracy 0.936 0.970 0.974 0.964
Lines 1 246 1 024 1 257 714

Lines over 40 characters read as well as the short ones.

This is the result of the whole recipe; we did not run an ablation that changes only train_seq_len. The direct
evidence for the option is the zero loss above: with 40 steps those lines give no gradient at all.

…aining

PPLCNetV4, PPLCNetV3 and PPHGNetV2 pool rec features to a fixed 40 steps
at train time, while inference uses avg_pool [3, 2] (T = W / 8). Labels
longer than ~40 chars do not fit CTC and Paddle silently zeroes their loss.

New backbone option train_seq_len (default 40, unchanged behaviour);
-1 makes the train-time sequence length W / 8, the same as inference.
The shared logic lives in rec_seq_pool.py. Eval/export paths are unchanged.
PPHGNet_small (PP-OCRv4 server rec) pools rec features to a fixed 40 steps in
training like PPLCNetV3/V4 and PPHGNetV2, so labels longer than ~40 chars got
a zero CTC loss. It now uses rec_seq_pool with the same train_seq_len option
(default 40, unchanged behaviour).
@CLAassistant

CLAassistant commented Oct 26, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants