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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "markdown",
|
5 |
+
"metadata": {},
|
6 |
+
"source": [
|
7 |
+
"## 1. PP-OCRv3模型简介\n",
|
8 |
+
"\n",
|
9 |
+
"PP-OCRv3在PP-OCRv2的基础上进一步升级。整体的框架图保持了与PP-OCRv2相同的pipeline,针对检测模型和识别模型进行了优化。其中,检测模块仍基于DB算法优化,而识别模块不再采用CRNN,换成了IJCAI 2022最新收录的文本识别算法SVTR,并对其进行产业适配。PP-OCRv3系统框图如下所示(粉色框中为PP-OCRv3新增策略):\n",
|
10 |
+
"\n",
|
11 |
+
"<div align=\"center\">\n",
|
12 |
+
"<img src=\"https://raw.githubusercontent.com/PaddlePaddle/PaddleOCR/release/2.6/doc/ppocrv3_framework.png\" width = \"80%\" />\n",
|
13 |
+
"</div>\n",
|
14 |
+
"\n",
|
15 |
+
"\n",
|
16 |
+
"从算法改进思路上看,分别针对检测和识别模型,进行了共9个方面的改进:\n",
|
17 |
+
"\n",
|
18 |
+
"- 检测模块:\n",
|
19 |
+
" - LK-PAN:大感受野的PAN结构;\n",
|
20 |
+
" - DML:教师模型互学习策略;\n",
|
21 |
+
" - RSE-FPN:残差注意力机制的FPN结构;\n",
|
22 |
+
"\n",
|
23 |
+
"\n",
|
24 |
+
"- 识别模块:\n",
|
25 |
+
" - SVTR_LCNet:轻量级文本识别网络;\n",
|
26 |
+
" - GTC:Attention指导CTC训练策略;\n",
|
27 |
+
" - TextConAug:挖掘文字上下文信息的数据增广策略;\n",
|
28 |
+
" - TextRotNet:自监督的预训练模型;\n",
|
29 |
+
" - UDML:联合互学习策略;\n",
|
30 |
+
" - UIM:无标注数据挖掘方案。\n",
|
31 |
+
"\n",
|
32 |
+
"从效果上看,速度可比情况下,多种场景精度均有大幅提升:\n",
|
33 |
+
"- 中文场景,相对于PP-OCRv2中文模型提升超5%;\n",
|
34 |
+
"- 英文数字场景,相比于PP-OCRv2英文模型提升11%;\n",
|
35 |
+
"- 多语言场景,优化80+语种识别效果,平均准确率提升超5%。\n",
|
36 |
+
"\n",
|
37 |
+
"\n",
|
38 |
+
"更详细的优化细节可参考技术报告:https://arxiv.org/abs/2206.03001 。\n",
|
39 |
+
"\n",
|
40 |
+
"更多关于PaddleOCR的内容,可以点击 https://github.com/PaddlePaddle/PaddleOCR 进行了解。\n",
|
41 |
+
"\n",
|
42 |
+
"\n"
|
43 |
+
]
|
44 |
+
},
|
45 |
+
{
|
46 |
+
"cell_type": "markdown",
|
47 |
+
"metadata": {},
|
48 |
+
"source": [
|
49 |
+
"## 2. 模型效果\n",
|
50 |
+
"\n",
|
51 |
+
"PP-OCRv3的效果如下:\n",
|
52 |
+
"\n",
|
53 |
+
"<div align=\"center\">\n",
|
54 |
+
"<img src=\"https://user-images.githubusercontent.com/12406017/200261622-1b928d93-93ab-4575-8c60-214bcc03eda1.png\" width = \"80%\" />\n",
|
55 |
+
"</div>\n",
|
56 |
+
"<div align=\"center\">\n",
|
57 |
+
"<img src=\"https://user-images.githubusercontent.com/12406017/200261711-9f18bb04-3736-4f51-892c-de801db9ab9e.png\" width = \"80%\" />\n",
|
58 |
+
"</div>\n",
|
59 |
+
"\n",
|
60 |
+
"\n",
|
61 |
+
"\n"
|
62 |
+
]
|
63 |
+
},
|
64 |
+
{
|
65 |
+
"cell_type": "markdown",
|
66 |
+
"metadata": {},
|
67 |
+
"source": [
|
68 |
+
"## 3. 模型如何使用\n",
|
69 |
+
"\n",
|
70 |
+
"### 3.1 模型推理\n",
|
71 |
+
"* 安装PaddleOCR whl包"
|
72 |
+
]
|
73 |
+
},
|
74 |
+
{
|
75 |
+
"cell_type": "code",
|
76 |
+
"execution_count": null,
|
77 |
+
"metadata": {
|
78 |
+
"collapsed": false,
|
79 |
+
"jupyter": {
|
80 |
+
"outputs_hidden": false
|
81 |
+
},
|
82 |
+
"scrolled": true,
|
83 |
+
"tags": []
|
84 |
+
},
|
85 |
+
"outputs": [],
|
86 |
+
"source": [
|
87 |
+
"! pip install paddleocr --user"
|
88 |
+
]
|
89 |
+
},
|
90 |
+
{
|
91 |
+
"cell_type": "markdown",
|
92 |
+
"metadata": {},
|
93 |
+
"source": [
|
94 |
+
"* 快速体验"
|
95 |
+
]
|
96 |
+
},
|
97 |
+
{
|
98 |
+
"cell_type": "code",
|
99 |
+
"execution_count": 1,
|
100 |
+
"metadata": {
|
101 |
+
"scrolled": true,
|
102 |
+
"tags": []
|
103 |
+
},
|
104 |
+
"outputs": [],
|
105 |
+
"source": [
|
106 |
+
"# 命令行使用\n",
|
107 |
+
"! wget https://raw.githubusercontent.com/PaddlePaddle/PaddleOCR/dygraph/doc/imgs/11.jpg\n",
|
108 |
+
"! paddleocr --image_dir 11.jpg --use_angle_cls true"
|
109 |
+
]
|
110 |
+
},
|
111 |
+
{
|
112 |
+
"cell_type": "markdown",
|
113 |
+
"metadata": {},
|
114 |
+
"source": [
|
115 |
+
"运行完成后,会在终端输出如下结果:\n",
|
116 |
+
"```log\n",
|
117 |
+
"[[[28.0, 37.0], [302.0, 39.0], [302.0, 72.0], [27.0, 70.0]], ('纯臻营养护发素', 0.96588134765625)]\n",
|
118 |
+
"[[[26.0, 81.0], [172.0, 83.0], [172.0, 104.0], [25.0, 101.0]], ('产品信息/参数', 0.9113278985023499)]\n",
|
119 |
+
"[[[28.0, 115.0], [330.0, 115.0], [330.0, 132.0], [28.0, 132.0]], ('(45元/每公斤,100公斤起订)', 0.8843421936035156)]\n",
|
120 |
+
"......\n",
|
121 |
+
"```\n",
|
122 |
+
"\n",
|
123 |
+
"\n"
|
124 |
+
]
|
125 |
+
},
|
126 |
+
{
|
127 |
+
"cell_type": "markdown",
|
128 |
+
"metadata": {},
|
129 |
+
"source": [
|
130 |
+
"### 3.2 模型训练\n",
|
131 |
+
"PP-OCR系统由文本检测模型、方向分类器和文本识别模型构成,三个模型训练教程可参考如下文档:\n",
|
132 |
+
"1. 文本检测模型:[文本检测训练教程](https://github.com/PaddlePaddle/PaddleOCR/blob/release%2F2.6/doc/doc_ch/detection.md)\n",
|
133 |
+
"1. 方向分类器: [方向分类器训练教程](https://github.com/PaddlePaddle/PaddleOCR/blob/release%2F2.6/doc/doc_ch/angle_class.md)\n",
|
134 |
+
"1. 文本识别模型:[文本识别训练教程](https://github.com/PaddlePaddle/PaddleOCR/blob/release%2F2.6/doc/doc_ch/recognition.md)\n",
|
135 |
+
"\n",
|
136 |
+
"模型训练完成后,可以通过指定模型路径的方式串联使用\n",
|
137 |
+
"命令参考如下:\n",
|
138 |
+
"```python\n",
|
139 |
+
"paddleocr --image_dir 11.jpg --use_angle_cls true --det_model_dir=/path/to/det_inference_model --cls_model_dir=/path/to/cls_inference_model --rec_model_dir=/path/to/rec_inference_model\n",
|
140 |
+
"```"
|
141 |
+
]
|
142 |
+
},
|
143 |
+
{
|
144 |
+
"cell_type": "markdown",
|
145 |
+
"metadata": {},
|
146 |
+
"source": [
|
147 |
+
"## 4. 原理\n",
|
148 |
+
"\n",
|
149 |
+
"优化思路具体如下\n",
|
150 |
+
"\n",
|
151 |
+
"1. 检测模型优化\n",
|
152 |
+
"- LK-PAN:大感受野的PAN结构。\n",
|
153 |
+
" \n",
|
154 |
+
"LK-PAN (Large Kernel PAN) 是一个具有更大感受野的轻量级PAN结构,核心是将PAN结构的path augmentation中卷积核从3*3改为9*9。通过增大卷积核,提升特征图每个位置覆盖的感受野,更容易检测大字体的文字以及极端长宽比的文字。使用LK-PAN结构,可以将教师模型的hmean从83.2%提升到85.0%。\n",
|
155 |
+
" <div align=\"center\">\n",
|
156 |
+
" <img src=\"https://raw.githubusercontent.com/PaddlePaddle/PaddleOCR/release/2.6/doc/ppocr_v3/LKPAN.png\" width = \"60%\" />\n",
|
157 |
+
" </div>\n",
|
158 |
+
"\n",
|
159 |
+
"- DML:教师模型互学习策略\n",
|
160 |
+
"\n",
|
161 |
+
"[DML](https://arxiv.org/abs/1706.00384) (Deep Mutual Learning)互学习蒸馏方法,如下图所示,通过两个结构相同的模型互相学习,可以有效提升文本检测模型的精度。教师模型采用DML策略,hmean从85%提升到86%。将PP-OCRv2中CML的教师模型更新为上述更高精度的教师模型,学生模型的hmean可以进一步从83.2%提升到84.3%。\n",
|
162 |
+
" <div align=\"center\">\n",
|
163 |
+
" <img src=\"https://raw.githubusercontent.com/PaddlePaddle/PaddleOCR/release/2.6/doc/ppocr_v3/teacher_dml.png\" width = \"60%\" />\n",
|
164 |
+
" </div>\n",
|
165 |
+
"\n",
|
166 |
+
"- RSE-FPN:残差注意力机制的FPN结构\n",
|
167 |
+
"\n",
|
168 |
+
"RSE-FPN(Residual Squeeze-and-Excitation FPN)如下图所示,引入残差结构和通道注意力结构,将FPN中的卷积层更换为通道注意力结构的RSEConv层,进一步提升特征图的表征能力。考虑到PP-OCRv2的检测模型中FPN通道数非常小,仅为96,如果直接用SEblock代替FPN中卷积会导致某些通道的特征被抑制,精度会下降。RSEConv引入残差结构会缓解上述问题,提升文本检测效果。进一步将PP-OCRv2中CML的学生模型的FPN结构更新为RSE-FPN,学生模型的hmean可以进一步从84.3%提升到85.4%。\n",
|
169 |
+
"\n",
|
170 |
+
"<div align=\"center\">\n",
|
171 |
+
"<img src=\"https://raw.githubusercontent.com/PaddlePaddle/PaddleOCR/release/2.6/doc/ppocr_v3/RSEFPN.png\" width = \"60%\" />\n",
|
172 |
+
"</div>\n",
|
173 |
+
"\n",
|
174 |
+
"1. 识别模型优化\n",
|
175 |
+
"- SVTR_LCNet:轻量级文本识别网络\n",
|
176 |
+
"\n",
|
177 |
+
"SVTR_LCNet是针对文本识别任务,将基于Transformer的SVTR网络和轻量级CNN网络PP-LCNet 融合的一种轻量级文本识别网络。使用该网络,预测速度优于PP-OCRv2的识别模型20%,但是由于没有采用蒸馏策略,该识别模型效果略差。此外,进一步将输入图片规范化高度从32提升到48,预测速度稍微变慢,但是模型效果大幅提升,识别准确率达到73.98%(+2.08%),接近PP-OCRv2采用蒸馏策略的识别模型效果。\n",
|
178 |
+
"\n",
|
179 |
+
"- GTC:Attention指导CTC训练策略\n",
|
180 |
+
" \n",
|
181 |
+
"[GTC](https://arxiv.org/pdf/2002.01276.pdf)(Guided Training of CTC),利用Attention模块CTC训练,融合多种文本特征的表达,是一种有效的提升文本识别的策略。使用该策略,预测时完全去除 Attention 模块,在推理阶段不增加任何耗时,识别模型的准确率进一步提升到75.8%(+1.82%)。训练流程如下所示:\n",
|
182 |
+
"\n",
|
183 |
+
"<div align=\"center\">\n",
|
184 |
+
"<img src=\"https://user-images.githubusercontent.com/12406017/200265540-1bbb730f-35d4-4d72-8e00-70856bb932ee.png\" width = \"60%\" />\n",
|
185 |
+
"</div>\n",
|
186 |
+
"\n",
|
187 |
+
"- TextConAug:挖掘文字上下文信息的数据增广策略\n",
|
188 |
+
"\n",
|
189 |
+
"TextConAug是一种挖掘文字上下文信息的数据增广策略,主要思想来源于论文[ConCLR](https://www.cse.cuhk.edu.hk/~byu/papers/C139-AAAI2022-ConCLR.pdf),作者提出ConAug数据增广,在一个batch内对2张不同的图像进行联结,组成新的图像并进行自监督对比学习。PP-OCRv3将此方法应用到有监督的学习任务中,设计了TextConAug数据增强方法,可以丰富训练数据上下文信息,提升训练数据多样性。使用该策略,识别模型的准确率进一步提升到76.3%(+0.5%)。TextConAug示意图如下所示:\n",
|
190 |
+
"\n",
|
191 |
+
"<div align=\"center\">\n",
|
192 |
+
"<img src=\"https://user-images.githubusercontent.com/12406017/200265540-1bbb730f-35d4-4d72-8e00-70856bb932ee.png\" width = \"60%\" />\n",
|
193 |
+
"</div>\n",
|
194 |
+
"\n",
|
195 |
+
"- TextRotNet:自监督的预训练模型\n",
|
196 |
+
"\n",
|
197 |
+
"TextRotNet是使用大量无标注的文本行数据,通过自监督方式训练的预训练模型,参考于论文[STR-Fewer-Labels](https://github.com/ku21fan/STR-Fewer-Labels)。该模型可以初始化SVTR_LCNet的初始权重,从而帮助文本识别模型收敛到更��位置。使用该策略,识别模型的准确率进一步提升到76.9%(+0.6%)。TextRotNet训练流程如下图所示:\n",
|
198 |
+
"\n",
|
199 |
+
"<div align=\"center\">\n",
|
200 |
+
"<img src=\"https://raw.githubusercontent.com/PaddlePaddle/PaddleOCR/release/2.6/doc/ppocr_v3/SSL.png\" width = \"60%\" />\n",
|
201 |
+
"</div>\n",
|
202 |
+
"\n",
|
203 |
+
"- UDML:联合互学习策略\n",
|
204 |
+
"\n",
|
205 |
+
"UDML(Unified-Deep Mutual Learning)联合互学习是PP-OCRv2中就采用的对于文本识别非常有效的提升模型效果的策略。在PP-OCRv3中,针对两个不同的SVTR_LCNet和Attention结构,对他们之间的PP-LCNet的特征图、SVTR模块的输出和Attention模块的输出同时进行监督训练。使用该策略,识别模型的准确率进一步提升到78.4%(+1.5%)。\n",
|
206 |
+
"\n",
|
207 |
+
"- UDML:联合互学习策略\n",
|
208 |
+
"\n",
|
209 |
+
"UIM(Unlabeled Images Mining)是一种非常简单的无标注数据挖掘方案。核心思想是利用高精度的文本识别大模型对无标注数据进行预测,获取伪标签,并且选择预测置信度高的样本作为训练数据,用于训练小模型。使用该策略,识别模型的准确率进一步提升到79.4%(+1%)。\n",
|
210 |
+
"\n",
|
211 |
+
"<div align=\"center\">\n",
|
212 |
+
"<img src=\"https://raw.githubusercontent.com/PaddlePaddle/PaddleOCR/release/2.6/doc/ppocr_v3/UIM.png\" width = \"60%\" />\n",
|
213 |
+
"</div>"
|
214 |
+
]
|
215 |
+
},
|
216 |
+
{
|
217 |
+
"cell_type": "markdown",
|
218 |
+
"metadata": {},
|
219 |
+
"source": [
|
220 |
+
"## 5. 注意事项\n",
|
221 |
+
"\n",
|
222 |
+
"PP-OCR系列模型训练过程中均使用通用数据,如在实际场景中表现不满意,可标注少量数据进行finetune。"
|
223 |
+
]
|
224 |
+
},
|
225 |
+
{
|
226 |
+
"cell_type": "markdown",
|
227 |
+
"metadata": {},
|
228 |
+
"source": [
|
229 |
+
"## 6. 相关论文以及引用信息\n",
|
230 |
+
"```\n",
|
231 |
+
"@article{li2022pp,\n",
|
232 |
+
" title={PP-OCRv3: More Attempts for the Improvement of Ultra Lightweight OCR System},\n",
|
233 |
+
" author={Li, Chenxia and Liu, Weiwei and Guo, Ruoyu and Yin, Xiaoting and Jiang, Kaitao and Du, Yongkun and Du, Yuning and Zhu, Lingfeng and Lai, Baohua and Hu, Xiaoguang and others},\n",
|
234 |
+
" journal={arXiv preprint arXiv:2206.03001},\n",
|
235 |
+
" year={2022}\n",
|
236 |
+
"}\n",
|
237 |
+
"```\n"
|
238 |
+
]
|
239 |
+
}
|
240 |
+
],
|
241 |
+
"metadata": {
|
242 |
+
"kernelspec": {
|
243 |
+
"display_name": "Python 3.8.13 ('py38')",
|
244 |
+
"language": "python",
|
245 |
+
"name": "python3"
|
246 |
+
},
|
247 |
+
"language_info": {
|
248 |
+
"codemirror_mode": {
|
249 |
+
"name": "ipython",
|
250 |
+
"version": 3
|
251 |
+
},
|
252 |
+
"file_extension": ".py",
|
253 |
+
"mimetype": "text/x-python",
|
254 |
+
"name": "python",
|
255 |
+
"nbconvert_exporter": "python",
|
256 |
+
"pygments_lexer": "ipython3",
|
257 |
+
"version": "3.8.13"
|
258 |
+
},
|
259 |
+
"vscode": {
|
260 |
+
"interpreter": {
|
261 |
+
"hash": "58fd1890da6594cebec461cf98c6cb9764024814357f166387d10d267624ecd6"
|
262 |
+
}
|
263 |
+
}
|
264 |
+
},
|
265 |
+
"nbformat": 4,
|
266 |
+
"nbformat_minor": 4
|
267 |
+
}
|