diff --git a/ppocr/modeling/backbones/rec_hgnet.py b/ppocr/modeling/backbones/rec_hgnet.py index d33e1dc79d0..1b5d00fe89c 100644 --- a/ppocr/modeling/backbones/rec_hgnet.py +++ b/ppocr/modeling/backbones/rec_hgnet.py @@ -14,12 +14,13 @@ import paddle import paddle.nn as nn -import paddle.nn.functional as F from paddle.nn.initializer import KaimingNormal, Constant from paddle.nn import Conv2D, BatchNorm2D, ReLU, AdaptiveAvgPool2D, MaxPool2D from paddle.regularizer import L2Decay from paddle import ParamAttr +from ppocr.modeling.backbones.rec_seq_pool import check_train_seq_len, rec_seq_pool + kaiming_normal_ = KaimingNormal() zeros_ = Constant(value=0.0) ones_ = Constant(value=1.0) @@ -203,6 +204,8 @@ class PPHGNet(nn.Layer): class_expand: int=2048. Number of channels for the last 1x1 convolutional layer. dropout_prob: float. Parameters of dropout, 0.0 means dropout is not used. class_num: int=1000. The number of classes. + train_seq_len: int=40. Rec: CTC sequence length in training; -1 makes it + proportional to the width, as in inference (see rec_seq_pool). Returns: model: nn.Layer. Specific PPHGNet model depends on args. """ @@ -215,8 +218,11 @@ def __init__( in_channels=3, det=False, out_indices=None, + train_seq_len=40, ): super().__init__() + check_train_seq_len(train_seq_len) + self.train_seq_len = train_seq_len self.det = det self.out_indices = out_indices if out_indices is not None else [0, 1, 2, 3] @@ -290,10 +296,7 @@ def forward(self, x): if self.det: return out - if self.training: - x = F.adaptive_avg_pool2d(x, [1, 40]) - else: - x = F.avg_pool2d(x, [3, 2]) + x = rec_seq_pool(x, self.train_seq_len, self.training) return x diff --git a/ppocr/modeling/backbones/rec_lcnetv3.py b/ppocr/modeling/backbones/rec_lcnetv3.py index 387c76108bf..e6d748f9deb 100644 --- a/ppocr/modeling/backbones/rec_lcnetv3.py +++ b/ppocr/modeling/backbones/rec_lcnetv3.py @@ -34,6 +34,7 @@ ) from paddle.regularizer import L2Decay from ppocr.modeling.backbones.rec_hgnet import MeanPool2D +from ppocr.modeling.backbones.rec_seq_pool import check_train_seq_len, rec_seq_pool NET_CONFIG_det = { "blocks2": @@ -408,9 +409,12 @@ def __init__( lr_mult_list=[1.0, 1.0, 1.0, 1.0, 1.0, 1.0], lab_lr=0.1, det=False, + train_seq_len=40, **kwargs, ): super().__init__() + check_train_seq_len(train_seq_len) + self.train_seq_len = train_seq_len self.scale = scale self.lr_mult_list = lr_mult_list self.det = det @@ -560,8 +564,5 @@ def forward(self, x): out_list[3] = self.layer_list[3](out_list[3]) return out_list - if self.training: - x = F.adaptive_avg_pool2d(x, [1, 40]) - else: - x = F.avg_pool2d(x, [3, 2]) + x = rec_seq_pool(x, self.train_seq_len, self.training) return x diff --git a/ppocr/modeling/backbones/rec_lcnetv4.py b/ppocr/modeling/backbones/rec_lcnetv4.py index 405da0489ec..b26212975b3 100644 --- a/ppocr/modeling/backbones/rec_lcnetv4.py +++ b/ppocr/modeling/backbones/rec_lcnetv4.py @@ -31,6 +31,8 @@ ) from paddle.regularizer import L2Decay +from .rec_seq_pool import check_train_seq_len, rec_seq_pool + NET_CONFIG_DET = { "tiny": { @@ -536,11 +538,14 @@ def __init__( model_size="small", in_channels=3, lr_mult_list=[1.0, 1.0, 1.0, 1.0, 1.0, 1.0], + train_seq_len=40, **kwargs, ): super().__init__() self.det = det self.is_repped = False + check_train_seq_len(train_seq_len) + self.train_seq_len = train_seq_len if det: assert ( @@ -634,11 +639,7 @@ def forward(self, x): x = self.blocks4(x) x = self.blocks5(x) x = self.blocks6(x) - if self.training: - x = F.adaptive_avg_pool2d(x, [1, 40]) - else: - assert x.shape[2] >= 3, f"Feature height {x.shape[2]} < pool kernel 3." - x = F.avg_pool2d(x, [3, 2]) + x = rec_seq_pool(x, self.train_seq_len, self.training, check_height=True) return x def rep(self, fuse_lab=None): diff --git a/ppocr/modeling/backbones/rec_pphgnetv2.py b/ppocr/modeling/backbones/rec_pphgnetv2.py index a969f5220f2..18ef624240c 100644 --- a/ppocr/modeling/backbones/rec_pphgnetv2.py +++ b/ppocr/modeling/backbones/rec_pphgnetv2.py @@ -30,6 +30,7 @@ from typing import Tuple, List, Dict, Union, Callable, Any from ppocr.modeling.backbones.rec_donut_swin import DonutSwinModelOutput +from ppocr.modeling.backbones.rec_seq_pool import check_train_seq_len, rec_seq_pool class IdentityBasedConv1x1(nn.Conv2D): @@ -1308,9 +1309,12 @@ def __init__( det=False, text_rec=False, out_indices=None, + train_seq_len=40, **kwargs, ): super().__init__() + check_train_seq_len(train_seq_len) + self.train_seq_len = train_seq_len self.det = det self.text_rec = text_rec self.use_lab = use_lab @@ -1410,10 +1414,7 @@ def forward(self, x): return out if self.text_rec: - if self.training: - x = F.adaptive_avg_pool2d(x, [1, 40]) - else: - x = F.avg_pool2d(x, [3, 2]) + x = rec_seq_pool(x, self.train_seq_len, self.training) return x diff --git a/ppocr/modeling/backbones/rec_seq_pool.py b/ppocr/modeling/backbones/rec_seq_pool.py new file mode 100644 index 00000000000..f0d09ef307a --- /dev/null +++ b/ppocr/modeling/backbones/rec_seq_pool.py @@ -0,0 +1,48 @@ +# Copyright (c) 2026 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import paddle.nn.functional as F + +__all__ = ["rec_seq_pool", "check_train_seq_len"] + + +def check_train_seq_len(train_seq_len): + assert ( + train_seq_len == -1 or train_seq_len > 0 + ), "train_seq_len must be -1 (proportional to width) or > 0, got {}".format( + train_seq_len + ) + + +def rec_seq_pool(x, train_seq_len=40, training=True, check_height=False): + """Pool [B, C, H, W] recognition features to the [B, C, 1, T] CTC sequence. + + Inference: avg_pool [3, 2], so T = W // 2 (H must be 3, i.e. 48 px input). + Training: + train_seq_len > 0 -> fixed T = train_seq_len (legacy behaviour, 40); + train_seq_len == -1 -> T = W // 2, the same as inference, so long text + lines still fit into CTC (it needs T >= label length). + """ + if not training: + if check_height: + assert x.shape[2] >= 3, f"Feature height {x.shape[2]} < pool kernel 3." + return F.avg_pool2d(x, [3, 2]) + if train_seq_len == -1: + width = x.shape[3] + assert width > 0, ( + "train_seq_len=-1 needs a known feature width at train time, got {}; " + "it is not supported with Global.to_static".format(width) + ) + return F.adaptive_avg_pool2d(x, [1, max(1, width // 2)]) + return F.adaptive_avg_pool2d(x, [1, train_seq_len]) diff --git a/tests/ppocr/test_rec_seq_pool.py b/tests/ppocr/test_rec_seq_pool.py new file mode 100644 index 00000000000..f4125e6c45e --- /dev/null +++ b/tests/ppocr/test_rec_seq_pool.py @@ -0,0 +1,99 @@ +# Copyright (c) 2026 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import numpy as np +import paddle +import pytest + +from ppocr.modeling.backbones.rec_hgnet import PPHGNet_small +from ppocr.modeling.backbones.rec_lcnetv3 import PPLCNetV3 +from ppocr.modeling.backbones.rec_lcnetv4 import PPLCNetV4 +from ppocr.modeling.backbones.rec_pphgnetv2 import PPHGNetV2_B4 +from ppocr.modeling.backbones.rec_seq_pool import rec_seq_pool + +np.random.seed(42) + + +def _feat(h, w, c=8): + return paddle.to_tensor(np.random.rand(1, c, h, w).astype("float32")) + + +class TestRecSeqPool: + def test_train_default_is_fixed_40(self): + assert rec_seq_pool(_feat(3, 240), 40, training=True).shape == [1, 8, 1, 40] + + def test_train_proportional_matches_inference(self): + assert rec_seq_pool(_feat(3, 240), -1, training=True).shape == [1, 8, 1, 120] + + @pytest.mark.parametrize("h", [2, 3, 4]) + def test_train_proportional_any_height(self, h): + assert rec_seq_pool(_feat(h, 160), -1, training=True).shape == [1, 8, 1, 80] + + def test_train_proportional_narrow_input(self): + assert rec_seq_pool(_feat(3, 1), -1, training=True).shape == [1, 8, 1, 1] + + def test_eval_halves_width(self): + assert rec_seq_pool(_feat(3, 240), 40, training=False).shape == [1, 8, 1, 120] + + def test_eval_height_check(self): + with pytest.raises(AssertionError, match="Feature height"): + rec_seq_pool(_feat(2, 240), -1, training=False, check_height=True) + + +def _seq_len(model, h=48, w=960, training=True): + model.train() if training else model.eval() + x = paddle.to_tensor(np.random.rand(1, 3, h, w).astype("float32")) + with paddle.no_grad(): + out = model(x) + assert out.shape[2] == 1 + return out.shape[3] + + +BACKBONES = [ + pytest.param(lambda **kw: PPLCNetV4(model_size="small", **kw), id="v4_small"), + pytest.param(lambda **kw: PPLCNetV4(model_size="tiny", **kw), id="v4_tiny"), + pytest.param(lambda **kw: PPLCNetV3(scale=0.95, **kw), id="v3"), + pytest.param( + lambda **kw: PPHGNetV2_B4(text_rec=True, **kw), + id="hgnetv2_b4", + marks=pytest.mark.resource_intensive, + ), + pytest.param( + lambda **kw: PPHGNet_small(**kw), + id="hgnet_small", + marks=pytest.mark.resource_intensive, + ), +] + + +@pytest.mark.parametrize("make", BACKBONES) +class TestBackboneTrainSeqLen: + def test_default_train_is_40(self, make): + assert _seq_len(make(), training=True) == 40 + + def test_proportional_train(self, make): + assert _seq_len(make(train_seq_len=-1), training=True) == 120 + + def test_eval_unchanged(self, make): + assert _seq_len(make(), training=False) == 120 + assert _seq_len(make(train_seq_len=-1), training=False) == 120 + + @pytest.mark.parametrize("h", [32, 64]) + def test_proportional_other_heights(self, make, h): + assert _seq_len(make(train_seq_len=-1), h=h, w=640, training=True) == 80 + + @pytest.mark.parametrize("bad", [0, -2]) + def test_invalid_train_seq_len(self, make, bad): + with pytest.raises(AssertionError, match="train_seq_len"): + make(train_seq_len=bad)