Repository navigation
Conversation
…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).
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 toW / 8steps at inference (avg_pool2d(x, [3, 2])). The backbonesare 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_losssilently returns zero loss and zero gradient. As a result: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):
Change
train_seq_len(default40: unchanged behaviour, existing configs and weights are notaffected). With
-1, the training sequence length isW / 8(adaptive_avg_pool2d(x, [1, W_feat // 2])), thesame as at inference.
ppocr/modeling/backbones/rec_seq_pool.py(rec_seq_pool,check_train_seq_len). Thefour backbones call it; the eval and export paths are unchanged, so an exported model is identical.
0,< -1) fail at construction.-1needs a known feature width at train time, so withGlobal.to_staticit asserts with an explicit message.Usage:
With
-1, batches of different widths give different sequence lengths. That is fine for CTC, and it works withMultiScaleSampler, whose batches already have different widths.Tests
tests/ppocr/test_rec_seq_pool.py:rec_seq_poolitself: fixed and proportional lengths, input heights 32 / 48 / 64, a narrow input, the eval path,the height check;
-1givesW / 8; eval is unchanged with both settings;invalid values are rejected.
PPHGNetV2_B4 and PPHGNet_small are marked
resource_intensive.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:
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 directevidence for the option is the zero loss above: with 40 steps those lines give no gradient at all.