recognition.md 13 KB
Newer Older
tink2123's avatar
tink2123 committed
1
2
## 文字识别

WenmuZhou's avatar
WenmuZhou committed
3
4
5
6
7
8
9

- [一、数据准备](#数据准备)
    - [数据下载](#数据下载)
    - [自定义数据集](#自定义数据集)  
    - [字典](#字典)  
    - [支持空格](#支持空格)

TingquanGao's avatar
TingquanGao committed
10
- [二、启动训练](#启动训练)
WenmuZhou's avatar
WenmuZhou committed
11
12
13
14
15
16
17
18
19
20
21
    - [1. 数据增强](#数据增强)
    - [2. 训练](#训练)
    - [3. 小语种](#小语种)

- [三、评估](#评估)

- [四、预测](#预测)
    - [1. 训练引擎预测](#训练引擎预测)


<a name="数据准备"></a>
tink2123's avatar
tink2123 committed
22
23
24
### 数据准备


tink2123's avatar
tink2123 committed
25
PaddleOCR 支持两种数据格式: `lmdb` 用于训练公开数据,调试算法; `通用数据` 训练自己的数据:
tink2123's avatar
tink2123 committed
26
27

请按如下步骤设置数据集:
tink2123's avatar
tink2123 committed
28
29
30
31

训练数据的默认存储路径是 `PaddleOCR/train_data`,如果您的磁盘上已有数据集,只需创建软链接至数据集目录:

```
32
ln -sf <path/to/dataset> <path/to/paddle_ocr>/train_data/dataset
tink2123's avatar
tink2123 committed
33
34
```

WenmuZhou's avatar
WenmuZhou committed
35
<a name="数据下载"></a>
tink2123's avatar
tink2123 committed
36
37
* 数据下载

tink2123's avatar
tink2123 committed
38
若您本地没有数据集,可以在官网下载 [icdar2015](http://rrc.cvc.uab.es/?ch=4&com=downloads) 数据,用于快速验证。也可以参考[DTRB](https://github.com/clovaai/deep-text-recognition-benchmark#download-lmdb-dataset-for-traininig-and-evaluation-from-here),下载 benchmark 所需的lmdb格式数据集。
tink2123's avatar
tink2123 committed
39

WenmuZhou's avatar
WenmuZhou committed
40
41
<a name="自定义数据集"></a>
* 使用自己数据集
tink2123's avatar
tink2123 committed
42
43

若您希望使用自己的数据进行训练,请参考下文组织您的数据。
WenmuZhou's avatar
WenmuZhou committed
44

tink2123's avatar
tink2123 committed
45
46
47
48
- 训练集

首先请将训练图片放入同一个文件夹(train_images),并用一个txt文件(rec_gt_train.txt)记录图片路径和标签。

MissPenguin's avatar
MissPenguin committed
49
**注意:** 默认请将图片路径和图片标签用 \t 分割,如用其他方式分割将造成训练报错
tink2123's avatar
tink2123 committed
50
51
52
53
54
55
56

```
" 图像文件名                 图像标注信息 "

train_data/train_0001.jpg   简单可依赖
train_data/train_0002.jpg   用科技让复杂的世界更简单
```
tink2123's avatar
fix doc  
tink2123 committed
57
58
59
60
61
62
PaddleOCR 提供了一份用于训练 icdar2015 数据集的标签文件,通过以下方式下载:

```
# 训练集标签
wget -P ./train_data/ic15_data  https://paddleocr.bj.bcebos.com/dataset/rec_gt_train.txt
# 测试集标签
tink2123's avatar
tink2123 committed
63
wget -P ./train_data/ic15_data  https://paddleocr.bj.bcebos.com/dataset/rec_gt_test.txt
tink2123's avatar
fix doc  
tink2123 committed
64
```
tink2123's avatar
tink2123 committed
65

tink2123's avatar
tink2123 committed
66
PaddleOCR 也提供了数据格式转换脚本,可以将官网 label 转换支持的数据格式。 数据转换工具在 `ppocr/utils/gen_label.py`, 这里以训练集为例:
WenmuZhou's avatar
WenmuZhou committed
67
68
69
70
71
72

```
# 将官网下载的标签文件转换为 rec_gt_label.txt
python gen_label.py --mode="rec" --input_path="{path/of/origin/label}" --output_label="rec_gt_label.txt"
```

tink2123's avatar
tink2123 committed
73
最终训练集应有如下文件结构:
tink2123's avatar
tink2123 committed
74
```
tink2123's avatar
tink2123 committed
75
|-train_data
tink2123's avatar
tink2123 committed
76
77
    |-ic15_data
        |- rec_gt_train.txt
tink2123's avatar
fix doc  
tink2123 committed
78
79
80
81
        |- train
            |- word_001.png
            |- word_002.jpg
            |- word_003.jpg
tink2123's avatar
tink2123 committed
82
            | ...
tink2123's avatar
tink2123 committed
83
```
tink2123's avatar
tink2123 committed
84

tink2123's avatar
fix doc  
tink2123 committed
85
- 测试集
tink2123's avatar
tink2123 committed
86

tink2123's avatar
fix doc  
tink2123 committed
87
同训练集类似,测试集也需要提供一个包含所有图片的文件夹(test)和一个rec_gt_test.txt,测试集的结构如下所示:
tink2123's avatar
tink2123 committed
88

tink2123's avatar
tink2123 committed
89
```
tink2123's avatar
tink2123 committed
90
|-train_data
tink2123's avatar
tink2123 committed
91
    |-ic15_data
tink2123's avatar
fix doc  
tink2123 committed
92
93
94
95
96
        |- rec_gt_test.txt
        |- test
            |- word_001.jpg
            |- word_002.jpg
            |- word_003.jpg
tink2123's avatar
tink2123 committed
97
            | ...
tink2123's avatar
tink2123 committed
98
```
WenmuZhou's avatar
WenmuZhou committed
99
<a name="字典"></a>
tink2123's avatar
tink2123 committed
100
101
102
103
- 字典

最后需要提供一个字典({word_dict_name}.txt),使模型在训练时,可以将所有出现的字符映射为字典的索引。

tink2123's avatar
tink2123 committed
104
因此字典需要包含所有希望被正确识别的字符,{word_dict_name}.txt需要写成如下格式,并以 `utf-8` 编码格式保存:
tink2123's avatar
tink2123 committed
105

tink2123's avatar
tink2123 committed
106
107
```
l
tink2123's avatar
tink2123 committed
108
109
d
a
tink2123's avatar
tink2123 committed
110
111
d
r
tink2123's avatar
tink2123 committed
112
n
tink2123's avatar
tink2123 committed
113
```
tink2123's avatar
tink2123 committed
114
115
116

word_dict.txt 每行有一个单字,将字符与数字索引映射在一起,“and” 将被映射成 [2 5 1]

tink2123's avatar
tink2123 committed
117
`ppocr/utils/ppocr_keys_v1.txt` 是一个包含6623个字符的中文字典
WenmuZhou's avatar
WenmuZhou committed
118

tink2123's avatar
tink2123 committed
119
`ppocr/utils/ic15_dict.txt` 是一个包含36个字符的英文字典
WenmuZhou's avatar
WenmuZhou committed
120
121
122

`ppocr/utils/dict/french_dict.txt` 是一个包含118个字符的法文字典

123
`ppocr/utils/dict/japan_dict.txt` 是一个包含4399个字符的日文字典
WenmuZhou's avatar
WenmuZhou committed
124

125
`ppocr/utils/dict/korean_dict.txt` 是一个包含3636个字符的韩文字典
WenmuZhou's avatar
WenmuZhou committed
126

127
`ppocr/utils/dict/german_dict.txt` 是一个包含131个字符的德文字典
WenmuZhou's avatar
WenmuZhou committed
128

tink2123's avatar
tink2123 committed
129
130
`ppocr/utils/dict/en_dict.txt` 是一个包含63个字符的英文字典

WenmuZhou's avatar
WenmuZhou committed
131

tink2123's avatar
fix doc  
tink2123 committed
132
您可以按需使用。
tink2123's avatar
tink2123 committed
133

WenmuZhou's avatar
WenmuZhou committed
134
135
136
目前的多语言模型仍处在demo阶段,会持续优化模型并补充语种,**非常欢迎您为我们提供其他语言的字典和字体**
如您愿意可将字典文件提交至 [dict](../../ppocr/utils/dict) 将语料文件提交至[corpus](../../ppocr/utils/corpus),我们会在Repo中感谢您。

tink2123's avatar
tink2123 committed
137
- 自定义字典
tink2123's avatar
tink2123 committed
138

tink2123's avatar
tink2123 committed
139
140
141
如需自定义dic文件,请在 `configs/rec/rec_icdar15_train.yml` 中添加 `character_dict_path` 字段, 指向您的字典路径。
并将 `character_type` 设置为 `ch`

WenmuZhou's avatar
WenmuZhou committed
142
<a name="支持空格"></a>
tink2123's avatar
tink2123 committed
143
144
- 添加空格类别

xmy0916's avatar
xmy0916 committed
145
如果希望支持识别"空格"类别, 请将yml文件中的 `use_space_char` 字段设置为 `True`
tink2123's avatar
tink2123 committed
146

tink2123's avatar
tink2123 committed
147

WenmuZhou's avatar
WenmuZhou committed
148
<a name="启动训练"></a>
tink2123's avatar
tink2123 committed
149
150
### 启动训练

tink2123's avatar
tink2123 committed
151
PaddleOCR提供了训练脚本、评估脚本和预测脚本,本节将以 CRNN 识别模型为例:
tink2123's avatar
tink2123 committed
152

tink2123's avatar
tink2123 committed
153
首先下载pretrain model,您可以下载训练好的模型在 icdar2015 数据上进行finetune
tink2123's avatar
tink2123 committed
154
155

```
tink2123's avatar
tink2123 committed
156
157
cd PaddleOCR/
# 下载MobileNetV3的预训练模型
tink2123's avatar
tink2123 committed
158
wget -P ./pretrain_models/ https://paddleocr.bj.bcebos.com/dygraph_v2.0/en/rec_mv3_none_bilstm_ctc_v2.0_train.tar
tink2123's avatar
tink2123 committed
159
160
# 解压模型参数
cd pretrain_models
tink2123's avatar
tink2123 committed
161
tar -xf rec_mv3_none_bilstm_ctc_v2.0_train.tar && rm -rf rec_mv3_none_bilstm_ctc_v2.0_train.tar
tink2123's avatar
tink2123 committed
162
163
164
165
```

开始训练:

tink2123's avatar
tink2123 committed
166
167
*如果您安装的是cpu版本,请将配置文件中的 `use_gpu` 字段修改为false*

tink2123's avatar
tink2123 committed
168
```
xmy0916's avatar
xmy0916 committed
169
# GPU训练 支持单卡,多卡训练,通过--gpus参数指定卡号
tink2123's avatar
tink2123 committed
170
# 训练icdar15英文数据 训练日志会自动保存为 "{save_model_dir}" 下的train.log
xmy0916's avatar
xmy0916 committed
171
python3 -m paddle.distributed.launch --gpus '0,1,2,3'  tools/train.py -c configs/rec/rec_icdar15_train.yml
tink2123's avatar
tink2123 committed
172
```
WenmuZhou's avatar
WenmuZhou committed
173
<a name="数据增强"></a>
tink2123's avatar
tink2123 committed
174
175
176
177
178
179
180
181
- 数据增强

PaddleOCR提供了多种数据增强方式,如果您希望在训练时加入扰动,请在配置文件中设置 `distort: true`

默认的扰动方式有:颜色空间转换(cvtColor)、模糊(blur)、抖动(jitter)、噪声(Gasuss noise)、随机切割(random crop)、透视(perspective)、颜色反转(reverse)。

训练过程中每种扰动方式以50%的概率被选择,具体代码实现请参考:[img_tools.py](https://github.com/PaddlePaddle/PaddleOCR/blob/develop/ppocr/data/rec/img_tools.py)

WenmuZhou's avatar
WenmuZhou committed
182
*由于OpenCV的兼容性问题,扰动操作暂时只支持Linux*
tink2123's avatar
tink2123 committed
183

WenmuZhou's avatar
WenmuZhou committed
184
<a name="训练"></a>
tink2123's avatar
tink2123 committed
185
186
- 训练

tink2123's avatar
tink2123 committed
187
PaddleOCR支持训练和评估交替进行, 可以在 `configs/rec/rec_icdar15_train.yml` 中修改 `eval_batch_step` 设置评估频率,默认每500个iter评估一次。评估过程中默认将最佳acc模型,保存为 `output/rec_CRNN/best_accuracy`
tink2123's avatar
tink2123 committed
188
189
190

如果验证集很大,测试将会比较耗时,建议减少评估次数,或训练完再进行评估。

MissPenguin's avatar
MissPenguin committed
191
**提示:** 可通过 -c 参数选择 `configs/rec/` 路径下的多种模型配置进行训练,PaddleOCR支持的识别算法有:
tink2123's avatar
tink2123 committed
192
193
194
195


| 配置文件 |  算法名称 |   backbone |   trans   |   seq      |     pred     |
| :--------: |  :-------:   | :-------:  |   :-------:   |   :-----:   |  :-----:   |
xmy0916's avatar
xmy0916 committed
196
197
| [rec_chinese_lite_train_v2.0.yml](../../configs/rec/ch_ppocr_v2.0/rec_chinese_lite_train_v2.0.yml) |  CRNN |   Mobilenet_v3 small 0.5 |  None   |  BiLSTM |  ctc  |
| [rec_chinese_common_train_v2.0.yml](../../configs/rec/ch_ppocr_v2.0/rec_chinese_common_train_v2.0.yml) |  CRNN | ResNet34_vd |  None   |  BiLSTM |  ctc  |
tink2123's avatar
tink2123 committed
198
| rec_chinese_lite_train.yml |  CRNN |   Mobilenet_v3 small 0.5 |  None   |  BiLSTM |  ctc  |
WenmuZhou's avatar
WenmuZhou committed
199
| rec_chinese_common_train.yml |  CRNN |   ResNet34_vd |  None   |  BiLSTM |  ctc  |
tink2123's avatar
tink2123 committed
200
201
202
203
204
205
| rec_icdar15_train.yml |  CRNN |   Mobilenet_v3 large 0.5 |  None   |  BiLSTM |  ctc  |
| rec_mv3_none_bilstm_ctc.yml |  CRNN |   Mobilenet_v3 large 0.5 |  None   |  BiLSTM |  ctc  |
| rec_mv3_none_none_ctc.yml |  Rosetta |   Mobilenet_v3 large 0.5 |  None   |  None |  ctc  |
| rec_r34_vd_none_bilstm_ctc.yml |  CRNN |   Resnet34_vd |  None   |  BiLSTM |  ctc  |
| rec_r34_vd_none_none_ctc.yml |  Rosetta |   Resnet34_vd |  None   |  None |  ctc  |

xmy0916's avatar
xmy0916 committed
206
训练中文数据,推荐使用[rec_chinese_lite_train_v2.0.yml](../../configs/rec/ch_ppocr_v2.0/rec_chinese_lite_train_v2.0.yml),如您希望尝试其他算法在中文数据集上的效果,请参考下列说明修改配置文件:
tink2123's avatar
tink2123 committed
207

xmy0916's avatar
xmy0916 committed
208
`rec_chinese_lite_train_v2.0.yml` 为例:
tink2123's avatar
tink2123 committed
209
210
211
```
Global:
  ...
xmy0916's avatar
xmy0916 committed
212
213
  # 添加自定义字典,如修改字典请将路径指向新字典
  character_dict_path: ppocr/utils/ppocr_keys_v1.txt
tink2123's avatar
tink2123 committed
214
215
216
  # 修改字符类型
  character_type: ch
  ...
xmy0916's avatar
xmy0916 committed
217
  # 识别空格
xmy0916's avatar
xmy0916 committed
218
  use_space_char: True
tink2123's avatar
tink2123 committed
219

220
221
222
223

Optimizer:
  ...
  # 添加学习率衰减策略
xmy0916's avatar
xmy0916 committed
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
  lr:
    name: Cosine
    learning_rate: 0.001
  ...

...

Train:
  dataset:
    # 数据集格式,支持LMDBDateSet以及SimpleDataSet
    name: SimpleDataSet
    # 数据集路径
    data_dir: ./train_data/
    # 训练集标签文件
    label_file_list: ["./train_data/train_list.txt"]
    transforms:
      ...
      - RecResizeImg:
          # 修改 image_shape 以适应长文本
          image_shape: [3, 32, 320]
      ...
  loader:
    ...
    # 单卡训练的batch_size
    batch_size_per_card: 256
    ...

Eval:
  dataset:
    # 数据集格式,支持LMDBDateSet以及SimpleDataSet
    name: SimpleDataSet
    # 数据集路径
    data_dir: ./train_data
    # 验证集标签文件
    label_file_list: ["./train_data/val_list.txt"]
    transforms:
      ...
      - RecResizeImg:
          # 修改 image_shape 以适应长文本
          image_shape: [3, 32, 320]
      ...
  loader:
    # 单卡验证的batch_size
    batch_size_per_card: 256
    ...
tink2123's avatar
tink2123 committed
269
```
tink2123's avatar
tink2123 committed
270
**注意,预测/评估时的配置文件请务必与训练一致。**
tink2123's avatar
tink2123 committed
271

WenmuZhou's avatar
WenmuZhou committed
272
273
274
275
<a name="小语种"></a>
- 小语种

PaddleOCR也提供了多语言的, `configs/rec/multi_languages` 路径下的提供了多语言的配置文件,目前PaddleOCR支持的多语言算法有:
tink2123's avatar
tink2123 committed
276

WenmuZhou's avatar
WenmuZhou committed
277
278
279
280
281
282
283
| 配置文件 |  算法名称 |   backbone |   trans   |   seq      |     pred     |  language |
| :--------: |  :-------:   | :-------:  |   :-------:   |   :-----:   |  :-----:   | :-----:  |
| rec_en_lite_train.yml |  CRNN |   Mobilenet_v3 small 0.5 |  None   |  BiLSTM |  ctc  | 英语   |
| rec_french_lite_train.yml |  CRNN |   Mobilenet_v3 small 0.5 |  None   |  BiLSTM |  ctc  | 法语 |  
| rec_ger_lite_train.yml |  CRNN |   Mobilenet_v3 small 0.5 |  None   |  BiLSTM |  ctc  | 德语   |
| rec_japan_lite_train.yml |  CRNN |   Mobilenet_v3 small 0.5 |  None   |  BiLSTM |  ctc  | 日语  |
| rec_korean_lite_train.yml |  CRNN |   Mobilenet_v3 small 0.5 |  None   |  BiLSTM |  ctc  | 韩语  |
tink2123's avatar
tink2123 committed
284

WenmuZhou's avatar
WenmuZhou committed
285
286
287
288
289
290
291
292
293
294
295
296

多语言模型训练方式与中文模型一致,训练数据集均为100w的合成数据,少量的字体可以在 [百度网盘](https://pan.baidu.com/s/1bS_u207Rm7YbY33wOECKDA) 上下载,提取码:frgi。

如您希望在现有模型效果的基础上调优,请参考下列说明修改配置文件:

`rec_french_lite_train` 为例:
```
Global:
  ...
  # 添加自定义字典,如修改字典请将路径指向新字典
  character_dict_path: ./ppocr/utils/dict/french_dict.txt
  ...
xmy0916's avatar
xmy0916 committed
297
  # 识别空格
xmy0916's avatar
xmy0916 committed
298
  use_space_char: True
WenmuZhou's avatar
WenmuZhou committed
299
300

...
xmy0916's avatar
xmy0916 committed
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320

Train:
  dataset:
    # 数据集格式,支持LMDBDateSet以及SimpleDataSet
    name: SimpleDataSet
    # 数据集路径
    data_dir: ./train_data/
    # 训练集标签文件
    label_file_list: ["./train_data/french_train.txt"]
    ...

Eval:
  dataset:
    # 数据集格式,支持LMDBDateSet以及SimpleDataSet
    name: SimpleDataSet
    # 数据集路径
    data_dir: ./train_data
    # 验证集标签文件
    label_file_list: ["./train_data/french_val.txt"]
    ...
WenmuZhou's avatar
WenmuZhou committed
321
322
```
<a name="评估"></a>
tink2123's avatar
tink2123 committed
323
324
### 评估

xmy0916's avatar
xmy0916 committed
325
评估数据集可以通过 `configs/rec/rec_icdar15_train.yml`  修改Eval中的 `label_file_path` 设置。
tink2123's avatar
tink2123 committed
326
327

```
tink2123's avatar
tink2123 committed
328
# GPU 评估, Global.checkpoints 为待测权重
xmy0916's avatar
xmy0916 committed
329
python3 -m paddle.distributed.launch --gpus '0' tools/eval.py -c configs/rec/rec_icdar15_train.yml -o Global.checkpoints={path/to/weights}/best_accuracy
tink2123's avatar
tink2123 committed
330
331
```

WenmuZhou's avatar
WenmuZhou committed
332
<a name="预测"></a>
tink2123's avatar
tink2123 committed
333
### 预测
tink2123's avatar
tink2123 committed
334

WenmuZhou's avatar
WenmuZhou committed
335
<a name="训练引擎预测"></a>
tink2123's avatar
tink2123 committed
336
337
* 训练引擎的预测

tink2123's avatar
tink2123 committed
338
使用 PaddleOCR 训练好的模型,可以通过以下脚本进行快速预测。
tink2123's avatar
tink2123 committed
339

tink2123's avatar
tink2123 committed
340
默认预测图片存储在 `infer_img` 里,通过 `-o Global.checkpoints` 指定权重:
tink2123's avatar
tink2123 committed
341
342

```
tink2123's avatar
tink2123 committed
343
# 预测英文结果
WenmuZhou's avatar
WenmuZhou committed
344
python3 tools/infer_rec.py -c configs/rec/rec_icdar15_train.yml -o Global.pretrained_model={path/to/weights}/best_accuracy Global.load_static_weights=false Global.infer_img=doc/imgs_words/en/word_1.png
tink2123's avatar
tink2123 committed
345
```
tink2123's avatar
tink2123 committed
346
347
348

预测图片:

349
![](../imgs_words/en/word_1.png)
tink2123's avatar
tink2123 committed
350
351
352
353

得到输入图像的预测结果:

```
tink2123's avatar
tink2123 committed
354
infer_img: doc/imgs_words/en/word_1.png
tink2123's avatar
tink2123 committed
355
        result: ('joint', 0.9998967)
tink2123's avatar
tink2123 committed
356
357
```

xmy0916's avatar
xmy0916 committed
358
预测使用的配置文件必须与训练一致,如您通过 `python3 tools/train.py -c configs/rec/ch_ppocr_v2.0/rec_chinese_lite_train_v2.0.yml` 完成了中文模型的训练,
tink2123's avatar
tink2123 committed
359
360
361
362
您可以使用如下命令进行中文模型预测。

```
# 预测中文结果
WenmuZhou's avatar
WenmuZhou committed
363
python3 tools/infer_rec.py -c configs/rec/ch_ppocr_v2.0/rec_chinese_lite_train_v2.0.yml -o Global.pretrained_model={path/to/weights}/best_accuracy Global.load_static_weights=false Global.infer_img=doc/imgs_words/ch/word_1.jpg
tink2123's avatar
tink2123 committed
364
365
```

tink2123's avatar
tink2123 committed
366
预测图片:
tink2123's avatar
tink2123 committed
367

368
![](../imgs_words/ch/word_1.jpg)
xiaoting's avatar
xiaoting committed
369

tink2123's avatar
tink2123 committed
370
371
372
得到输入图像的预测结果:

```
tink2123's avatar
tink2123 committed
373
infer_img: doc/imgs_words/ch/word_1.jpg
tink2123's avatar
tink2123 committed
374
        result: ('韩国小馆', 0.997218)
tink2123's avatar
tink2123 committed
375
```