【学习笔记】从零开始的OCR-2——PP-OCR

xuancx 2026-09-14 15:18 1

摘要:精读PP-OCR以及里面的一些技术。


论文:《PP-OCR: A Practical Ultra Lightweight OCR System》


PP-OCR 由 text detection, detected boxes rectification and text recognition组成,下图为整体框架图。



Text Detection




  • 总体上使用 Differentiable Binarization 作为 text detector,将其中的Backbone换成了MobileNetV3 large x0.5。为什么用MobileNetV3?



    理由是,在figure6中,MobileNetV3 can achieve higher accuracy when the predict time are same.



    ​综合了性能(HMean指标后文会说)和速度选择了large x0.5




  • Backbone是很重要的,这几乎决定了整个Text Detection的速度,所以要找到很好的模型架构。



    The size of backbone has dominant effect on the model size of a text detector. Therefore, light backbones should be selected for building the ultra lightweight models





  • Head 选择了FPN-like 的架构,同时 fuse the feature maps of the different scales 为了提升 small text regions detection 的表现。在fuse different scales 的 feature maps 时,通常使用一个1*1 convolution(简写为​inner_channels​)。经过实验发现,​inner_channels​对 model size 有很大的影响,但是减少他会降低正确率。



    When inner channels is reduced from 256 to 96, the model size is reduced from 7M to 4.1M, but the accuracy declines slightly.



    从下表可以看到,减少​inner_channels​的维度,直接time减半了




  • 删除了MobileNetV3中大量的SE结构,因为发现输入分辨率较大的时候(640*640),很难estimate the channel-wise feature responses with the SE block。同时开销很大。所以去掉了。



    However, when the input resolution is large, such as 640 × 640, it is hard to estimate the channel-wise feature responses with the SE block. The accuracy improvement is limited, but the time cost is very high.





  • Cosine Learning Rate Decay,关于学习率的调整方法,在figure8中有介绍;Learning Rate Warm-up 在文本分类有效,本文发现在文本检测也是有效的。这两个策略从表中看出可以是相当有效了。




  • FPGM Pruner,采取原生方法,加上每层不同的剪枝敏感度





Detected boxes rectification


为了把检测框转化为水平矩形(并且里面的文本是正的)。利用图像分类的技术,训练了一个 classifier




  • Backbone使用MobileNetV3 small x0.35



    • 在之前的一些工作中,ShuffleNetV2被用于训练方向分类器。从表中来看,无论是精度、模型大小还是推理时间,ShuffleNetV2都不是一个很好的选择。




  • Data Augmentation:



    • 基础数据增强 BDA:包含轻微旋转;透视变形;运动模糊;高斯噪声。

    • 通用分类增强:又测试了CutMix;Mixup;Cutout;Hide-and-Seek;GridMask;Random Erasing;AutoAugment;RandAugment。

    • 测试结果在Table 9,工程化的采用了BDA + RandAugment




  • Input Resolution:以前的一般是 32 * 100,现在的是48 * 192.



    In general, when the input resolution of a normalized image is increased, accuracy will also be improved.





  • PACT Quantization



    Quantization allows the neural network model to have lower latency, smaller volume and lower computational power consumption.





    • 传统 PACT 是专门针对 ReLU 设计的,但是 MobileNetV3 里面还有 hard swish ,所以修改了 PACT 中的函数,让他也可以处理负数的情况:



      y=PACT(x)=
      \begin{cases}
      -\alpha,&x<-\alpha\\
      x,&-\alpha\le x<\alpha\\
      \alpha,&x\ge\alpha
      \end{cases}

      即:







\boxed{
y=clip(x,-\alpha,\alpha)
}



  • 还加入 L2 regularization 约束 PACT 参数稳定。可能的原因是,模型可能会发现无限增大 \alpha 会减少cliping带来的误差,这样 clip 就失去了效果




  • PACT 和前面的 FPGM Pruner 都是基于 PaddleSlim 这个工具包实现的




Text recognition


使用 CRNN 的架构




  • Backbone使用MobileNetV3 small x0.5(或者x1.0)




  • Data Augmentation:除了之前提到的BDA,还加入了TIA,




  • Cosine Learning Rate Decay:关于学习率的调整方法,同Text Detection




  • Feature Map Resolution:如图12所示,把一部分的下采样幅度减少。为了让特征图分辨率更大,准确率更高。




  • Regularization Parameters:添加了 L2 decay




  • Learning Rate Warm-up:也是关于学习率的,同Text Detection




  • Light Head:sequence features指的是时间步 * 特征的形式,这里的每个特征维度(即Feature Map Resolution后的输出的维度)的大小对模型大小有很大影响。因为这后面有一个全连接层(d→N,d为特征维度,N为字符个数,这里有d∗N的参数量,一般N在6000左右),如果d过大,参数量会暴增,所以设置为48.



    A full connection layer is used to encode the sequence features to the predicted characters in the ordinary. The dimension of the sequence features have an impact on the model size of a text recognizer, especially for Chinese recognition whose characters are more than 6 thousands. Meanwhile, it is not that the higher of the dimension, the stronger of the ability of the sequence features representation. In PP-OCR, the dimension of the sequence features is set to 48 empirically.








  • Pre-trained Model:如果数据量少,可以利用一个在 ImageNet 预训练的模型来fine-tune。



    If the training data is fewer, fine tune the existing networks, which are trained on a large data set such as ImageNet, to achieve fast convergence and better accuracy.




    • 同时要用很多的合成数据,因为真实数据很少,用很多的合成数据训练也能达到很好的效果。




  • PACT Quantization:同Detected boxes rectification,除了LSTM layers被跳过了



    Those layers will not be quantified at present since the complexity of LSTM quantization.





数据集


构建了一个数据集,详细可以查看原论文3.1节。重点需要关注以下三点:




  • 不同任务使用完全不同的数据策略




  • synthetic data 不是简单造数据,而是针对 failure case 设计(例如对于方向分类的,就专门设计反向的。)




  • 真实数据主要承担 domain adaptation,而 synthetic 承担 diversity(即针对真实数据中少见的一些分布,比如Text Detection中少见long text, multi direction text and table text.)




  • 基于 TextRender 来构建的数据集




训练细节




  • Adam optimizer




  • cosine learning rate decay




  • 先训练一个完整模型,然后经过PACT + FGPM




  • Evaluation metrics:




    • Text Detection:HMean指标。这个指标同时考虑了精确率(Precision)和召回率(Recall)这两个指标




      • 精确率(Precision) 是指模型预测出的文本区域中,真正对应文本的比例。




      • 召回率(Recall) 所有真实文本区域中,被模型正确检测出的比例。



        \text{HMean} = \frac{2*\text{Precision}*\text{Recall}}{\text{Precision}+\text{Recall}}



      • 当Precision或Recall中有任何一项接近于零时,Hmean会接近于零,这反映了模型性能的一个平衡点。Hmean的值范围从0到1,值越高表示模型性能越好。




      • 在文本检测任务中,使用Hmean作为性能指标可以帮助研究者了解其模型在不同数据集上的整体表现,并且鼓励模型在保证高精度的同时也要有良好的召回率,即不仅要找到尽可能多的真实文本区域,而且这些识别出来的区域还应该是正确的。






    • Direction Classification:Accuracy




    • Text Recognition:F-score




      • 这里是一个完整的系统,因为他必须要检测到正确位置(一般是IoU > 0.5)并且 识别文字正确,才会计算F-score。



        IoU=
        \frac{
        Area(pred\cap gt)
        }
        {
        Area(pred\cup gt)
        }







OCR system




  • FPGM + PACT 对整个 OCR 系统的影响




  • 大规模OCR system的区别:使用Res18_vd作为Text detection的主干网络,使用Res34_vd作为Text Recognition主干网络



最新回复 (1)
  • Leon824 09-15 12:00
    1

    技术细节写的很详细,mark一下,感谢佬分享

* 帖子来源Linux.do
返回