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

tink2123's avatar
tink2123 committed
3
本文提供了PaddleOCR文本识别任务的全流程指南,包括数据准备、模型训练、调优、评估、预测,各个阶段的详细说明:
WenmuZhou's avatar
WenmuZhou committed
4

WenmuZhou's avatar
WenmuZhou committed
5
6
7
8
9
- [1 数据准备](#数据准备)
    - [1.1 自定义数据集](#自定义数据集)
    - [1.2 数据下载](#数据下载)
    - [1.3 字典](#字典)  
    - [1.4 支持空格](#支持空格)
WenmuZhou's avatar
WenmuZhou committed
10

WenmuZhou's avatar
WenmuZhou committed
11
12
- [2 启动训练](#启动训练)
    - [2.1 数据增强](#数据增强)
tink2123's avatar
tink2123 committed
13
14
    - [2.2 通用模型训练](#通用模型训练)
    - [2.3 多语言模型训练](#多语言模型训练)
WenmuZhou's avatar
WenmuZhou committed
15

WenmuZhou's avatar
WenmuZhou committed
16
- [3 评估](#评估)
WenmuZhou's avatar
WenmuZhou committed
17

WenmuZhou's avatar
WenmuZhou committed
18
- [4 预测](#预测)
WenmuZhou's avatar
WenmuZhou committed
19
20
21


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


WenmuZhou's avatar
WenmuZhou committed
25
PaddleOCR 支持两种数据格式:
tink2123's avatar
tink2123 committed
26
27
 - `lmdb` 用于训练以lmdb格式存储的数据集(LMDBDataSet);
 - `通用数据` 用于训练以文本文件存储的数据集(SimpleDataSet);
tink2123's avatar
tink2123 committed
28
29
30
31

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

```
WenmuZhou's avatar
WenmuZhou committed
32
# linux and mac os
33
ln -sf <path/to/dataset> <path/to/paddle_ocr>/train_data/dataset
WenmuZhou's avatar
WenmuZhou committed
34
35
# windows
mklink /d <path/to/paddle_ocr>/train_data/dataset <path/to/dataset>
tink2123's avatar
tink2123 committed
36
37
```

WenmuZhou's avatar
WenmuZhou committed
38
<a name="准备数据集"></a>
tink2123's avatar
tink2123 committed
39
### 1.1 自定义数据集
WenmuZhou's avatar
WenmuZhou committed
40
下面以通用数据集为例, 介绍如何准备数据集:
tink2123's avatar
tink2123 committed
41

WenmuZhou's avatar
WenmuZhou committed
42
* 训练集
tink2123's avatar
tink2123 committed
43

WenmuZhou's avatar
WenmuZhou committed
44
建议将训练图片放入同一个文件夹,并用一个txt文件(rec_gt_train.txt)记录图片路径和标签,txt文件里的内容如下:
WenmuZhou's avatar
WenmuZhou committed
45

WenmuZhou's avatar
WenmuZhou committed
46
**注意:** txt文件中默认请将图片路径和图片标签用 \t 分割,如用其他方式分割将造成训练报错。
tink2123's avatar
tink2123 committed
47

WenmuZhou's avatar
WenmuZhou committed
48
49
```
" 图像文件名                 图像标注信息 "
tink2123's avatar
tink2123 committed
50

WenmuZhou's avatar
WenmuZhou committed
51
52
train_data/rec/train/word_001.jpg   简单可依赖
train_data/rec/train/word_002.jpg   用科技让复杂的世界更简单
WenmuZhou's avatar
WenmuZhou committed
53
54
...
```
tink2123's avatar
tink2123 committed
55

WenmuZhou's avatar
WenmuZhou committed
56
57
58
最终训练集应有如下文件结构:
```
|-train_data
WenmuZhou's avatar
WenmuZhou committed
59
  |-rec
WenmuZhou's avatar
WenmuZhou committed
60
61
62
63
64
65
    |- rec_gt_train.txt
    |- train
        |- word_001.png
        |- word_002.jpg
        |- word_003.jpg
        | ...
tink2123's avatar
tink2123 committed
66
67
```

WenmuZhou's avatar
WenmuZhou committed
68
69
70
71
72
73
- 测试集

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

```
|-train_data
WenmuZhou's avatar
WenmuZhou committed
74
  |-rec
WenmuZhou's avatar
WenmuZhou committed
75
76
77
78
79
80
    |- rec_gt_test.txt
    |- test
        |- word_001.jpg
        |- word_002.jpg
        |- word_003.jpg
        | ...
tink2123's avatar
tink2123 committed
81
```
WenmuZhou's avatar
WenmuZhou committed
82
83
84

<a name="数据下载"></a>

tink2123's avatar
tink2123 committed
85
### 1.2 数据下载
WenmuZhou's avatar
WenmuZhou committed
86

tink2123's avatar
tink2123 committed
87
- ICDAR2015
WenmuZhou's avatar
WenmuZhou committed
88

tink2123's avatar
tink2123 committed
89
若您本地没有数据集,可以在官网下载 [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
fix doc  
tink2123 committed
90

91
92
如果希望复现SAR的论文指标,需要下载[SynthAdd](https://pan.baidu.com/share/init?surl=uV0LtoNmcxbO-0YA7Ch4dg), 提取码:627x。此外,真实数据集icdar2013, icdar2015, cocotext, IIIT5也作为训练数据的一部分。具体数据细节可以参考论文SAR。

tink2123's avatar
tink2123 committed
93
如果你使用的是icdar2015的公开数据集,PaddleOCR 提供了一份用于训练 ICDAR2015 数据集的标签文件,通过以下方式下载:
94

tink2123's avatar
fix doc  
tink2123 committed
95
96
97
98
```
# 训练集标签
wget -P ./train_data/ic15_data  https://paddleocr.bj.bcebos.com/dataset/rec_gt_train.txt
# 测试集标签
tink2123's avatar
tink2123 committed
99
wget -P ./train_data/ic15_data  https://paddleocr.bj.bcebos.com/dataset/rec_gt_test.txt
tink2123's avatar
fix doc  
tink2123 committed
100
```
tink2123's avatar
tink2123 committed
101

tink2123's avatar
tink2123 committed
102
PaddleOCR 也提供了数据格式转换脚本,可以将ICDAR官网 label 转换为PaddleOCR支持的数据格式。 数据转换工具在 `ppocr/utils/gen_label.py`, 这里以训练集为例:
WenmuZhou's avatar
WenmuZhou committed
103
104
105
106
107
108

```
# 将官网下载的标签文件转换为 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
109
110
111
112
113
114
115
116
117
118
数据样式格式如下,(a)为原始图片,(b)为每张图片对应的 Ground Truth 文本文件:
![](../datasets/icdar_rec.png)

- 多语言数据集

多语言模型的训练数据集均为100w的合成数据,使用了开源合成工具 [text_renderer](https://github.com/Sanster/text_renderer) ,少量的字体可以通过下面两种方式下载。
* [百度网盘](https://pan.baidu.com/s/1bS_u207Rm7YbY33wOECKDA) 提取码:frgi
* [google drive](https://drive.google.com/file/d/18cSWX7wXSy4G0tbKJ0d9PuIaiwRLHpjA/view)


WenmuZhou's avatar
WenmuZhou committed
119
<a name="字典"></a>
tink2123's avatar
tink2123 committed
120
### 1.3 字典
tink2123's avatar
tink2123 committed
121
122
123

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

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

tink2123's avatar
tink2123 committed
126
127
```
l
tink2123's avatar
tink2123 committed
128
129
d
a
tink2123's avatar
tink2123 committed
130
131
d
r
tink2123's avatar
tink2123 committed
132
n
tink2123's avatar
tink2123 committed
133
```
tink2123's avatar
tink2123 committed
134
135
136

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

WenmuZhou's avatar
WenmuZhou committed
137
138
139
140
* 内置字典

PaddleOCR内置了一部分字典,可以按需使用。

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

tink2123's avatar
tink2123 committed
143
`ppocr/utils/ic15_dict.txt` 是一个包含36个字符的英文字典
WenmuZhou's avatar
WenmuZhou committed
144
145
146

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

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

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

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

tink2123's avatar
tink2123 committed
153
`ppocr/utils/en_dict.txt` 是一个包含96个字符的英文字典
tink2123's avatar
tink2123 committed
154

WenmuZhou's avatar
WenmuZhou committed
155

WenmuZhou's avatar
WenmuZhou committed
156

tink2123's avatar
tink2123 committed
157

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

tink2123's avatar
tink2123 committed
161
- 自定义字典
tink2123's avatar
tink2123 committed
162

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

WenmuZhou's avatar
WenmuZhou committed
166
<a name="支持空格"></a>
tink2123's avatar
tink2123 committed
167
### 1.4 添加空格类别
tink2123's avatar
tink2123 committed
168

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

tink2123's avatar
tink2123 committed
171

WenmuZhou's avatar
WenmuZhou committed
172
<a name="启动训练"></a>
tink2123's avatar
tink2123 committed
173
## 2. 启动训练
tink2123's avatar
tink2123 committed
174

tink2123's avatar
tink2123 committed
175
<a name="数据增强"></a>
tink2123's avatar
tink2123 committed
176
### 2.1 数据增强
tink2123's avatar
tink2123 committed
177
178
179
180
181
182
183
184
185
186

PaddleOCR提供了多种数据增强方式,默认配置文件中已经添加了数据增广。

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

训练过程中每种扰动方式以40%的概率被选择,具体代码实现请参考:[rec_img_aug.py](../../ppocr/data/imaug/rec_img_aug.py)

*由于OpenCV的兼容性问题,扰动操作暂时只支持Linux*

<a name="通用模型训练"></a>
tink2123's avatar
tink2123 committed
187
### 2.2 通用模型训练
tink2123's avatar
tink2123 committed
188

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

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

```
tink2123's avatar
tink2123 committed
194
195
cd PaddleOCR/
# 下载MobileNetV3的预训练模型
tink2123's avatar
tink2123 committed
196
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
197
198
# 解压模型参数
cd pretrain_models
tink2123's avatar
tink2123 committed
199
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
200
201
202
203
```

开始训练:

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

tink2123's avatar
tink2123 committed
206
```
tink2123's avatar
tink2123 committed
207
# GPU训练 支持单卡,多卡训练
tink2123's avatar
tink2123 committed
208
# 训练icdar15英文数据 训练日志会自动保存为 "{save_model_dir}" 下的train.log
tink2123's avatar
tink2123 committed
209

tink2123's avatar
tink2123 committed
210
211
#单卡训练(训练周期长,不建议)
python3 tools/train.py -c configs/rec/rec_icdar15_train.yml
tink2123's avatar
tink2123 committed
212

tink2123's avatar
tink2123 committed
213
214
215
#多卡训练,通过--gpus参数指定卡号
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
216
217


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

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

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


| 配置文件 |  算法名称 |   backbone |   trans   |   seq      |     pred     |
| :--------: |  :-------:   | :-------:  |   :-------:   |   :-----:   |  :-----:   |
xmy0916's avatar
xmy0916 committed
227
228
| [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
229
230
231
232
233
| 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  |
LDOUBLEV's avatar
LDOUBLEV committed
234
235
| rec_mv3_tps_bilstm_att.yml |  CRNN |   Mobilenet_v3 |  TPS   |  BiLSTM |  att  |
| rec_r34_vd_tps_bilstm_att.yml |  CRNN |   Resnet34_vd |  TPS   |  BiLSTM |  att  |
tink2123's avatar
tink2123 committed
236
| rec_r50fpn_vd_none_srn.yml    | SRN | Resnet50_fpn_vd    | None    | rnn | srn |
Topdu's avatar
Topdu committed
237
| rec_mtb_nrtr.yml    | NRTR | nrtr_mtb    | None    | transformer encoder | transformer decoder |
andyjpaddle's avatar
andyjpaddle committed
238
| rec_r31_sar.yml               | SAR | ResNet31 | None | LSTM encoder | LSTM decoder |
tink2123's avatar
tink2123 committed
239

xmy0916's avatar
xmy0916 committed
240
训练中文数据,推荐使用[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
241

xmy0916's avatar
xmy0916 committed
242
`rec_chinese_lite_train_v2.0.yml` 为例:
tink2123's avatar
tink2123 committed
243
244
245
```
Global:
  ...
xmy0916's avatar
xmy0916 committed
246
247
  # 添加自定义字典,如修改字典请将路径指向新字典
  character_dict_path: ppocr/utils/ppocr_keys_v1.txt
tink2123's avatar
tink2123 committed
248
249
250
  # 修改字符类型
  character_type: ch
  ...
xmy0916's avatar
xmy0916 committed
251
  # 识别空格
xmy0916's avatar
xmy0916 committed
252
  use_space_char: True
tink2123's avatar
tink2123 committed
253

254
255
256
257

Optimizer:
  ...
  # 添加学习率衰减策略
xmy0916's avatar
xmy0916 committed
258
259
260
261
262
263
264
265
266
  lr:
    name: Cosine
    learning_rate: 0.001
  ...

...

Train:
  dataset:
MissPenguin's avatar
MissPenguin committed
267
    # 数据集格式,支持LMDBDataSet以及SimpleDataSet
xmy0916's avatar
xmy0916 committed
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
    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:
MissPenguin's avatar
MissPenguin committed
287
    # 数据集格式,支持LMDBDataSet以及SimpleDataSet
xmy0916's avatar
xmy0916 committed
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
    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
303
```
tink2123's avatar
tink2123 committed
304
**注意,预测/评估时的配置文件请务必与训练一致。**
tink2123's avatar
tink2123 committed
305

tink2123's avatar
tink2123 committed
306
<a name="多语言模型训练"></a>
tink2123's avatar
tink2123 committed
307
### 2.3 多语言模型训练
WenmuZhou's avatar
WenmuZhou committed
308

tink2123's avatar
tink2123 committed
309
PaddleOCR目前已支持80种(除中文外)语种识别,`configs/rec/multi_languages` 路径下提供了一个多语言的配置文件模版: [rec_multi_language_lite_train.yml](../../configs/rec/multi_language/rec_multi_language_lite_train.yml)
tink2123's avatar
tink2123 committed
310

tink2123's avatar
tink2123 committed
311
按语系划分,目前PaddleOCR支持的语种有:
tink2123's avatar
tink2123 committed
312
313
314
315

| 配置文件 |  算法名称 |   backbone |   trans   |   seq      |     pred     |  language | character_type |
| :--------: |  :-------:   | :-------:  |   :-------:   |   :-----:   |  :-----:   | :-----:  | :-----:  |
| rec_chinese_cht_lite_train.yml |  CRNN |   Mobilenet_v3 small 0.5 |  None   |  BiLSTM |  ctc  | 中文繁体  | chinese_cht|
tink2123's avatar
tink2123 committed
316
| rec_en_lite_train.yml |  CRNN |   Mobilenet_v3 small 0.5 |  None   |  BiLSTM |  ctc  | 英语(区分大小写)   | EN |
tink2123's avatar
tink2123 committed
317
318
319
320
| rec_french_lite_train.yml |  CRNN |   Mobilenet_v3 small 0.5 |  None   |  BiLSTM |  ctc  | 法语 |  french |
| rec_ger_lite_train.yml |  CRNN |   Mobilenet_v3 small 0.5 |  None   |  BiLSTM |  ctc  | 德语   | german |
| rec_japan_lite_train.yml |  CRNN |   Mobilenet_v3 small 0.5 |  None   |  BiLSTM |  ctc  | 日语  | japan |
| rec_korean_lite_train.yml |  CRNN |   Mobilenet_v3 small 0.5 |  None   |  BiLSTM |  ctc  | 韩语  | korean |
tink2123's avatar
tink2123 committed
321
322
323
324
325
326
| rec_latin_lite_train.yml |  CRNN |   Mobilenet_v3 small 0.5 |  None   |  BiLSTM |  ctc  | 拉丁字母  | latin |
| rec_arabic_lite_train.yml |  CRNN |   Mobilenet_v3 small 0.5 |  None   |  BiLSTM |  ctc  | 阿拉伯字母 |  ar |
| rec_cyrillic_lite_train.yml |  CRNN |   Mobilenet_v3 small 0.5 |  None   |  BiLSTM |  ctc  | 斯拉夫字母  | cyrillic |
| rec_devanagari_lite_train.yml |  CRNN |   Mobilenet_v3 small 0.5 |  None   |  BiLSTM |  ctc  | 梵文字母  | devanagari |

更多支持语种请参考: [多语言模型](https://github.com/PaddlePaddle/PaddleOCR/blob/release/2.1/doc/doc_ch/multi_languages.md#%E8%AF%AD%E7%A7%8D%E7%BC%A9%E5%86%99)
WenmuZhou's avatar
WenmuZhou committed
327
328
329
330
331
332
333
334
335
336

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

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

...
xmy0916's avatar
xmy0916 committed
341
342
343

Train:
  dataset:
MissPenguin's avatar
MissPenguin committed
344
    # 数据集格式,支持LMDBDataSet以及SimpleDataSet
xmy0916's avatar
xmy0916 committed
345
346
347
348
349
350
351
352
353
    name: SimpleDataSet
    # 数据集路径
    data_dir: ./train_data/
    # 训练集标签文件
    label_file_list: ["./train_data/french_train.txt"]
    ...

Eval:
  dataset:
MissPenguin's avatar
MissPenguin committed
354
    # 数据集格式,支持LMDBDataSet以及SimpleDataSet
xmy0916's avatar
xmy0916 committed
355
356
357
358
359
360
    name: SimpleDataSet
    # 数据集路径
    data_dir: ./train_data
    # 验证集标签文件
    label_file_list: ["./train_data/french_val.txt"]
    ...
WenmuZhou's avatar
WenmuZhou committed
361
362
```
<a name="评估"></a>
tink2123's avatar
tink2123 committed
363
## 3 评估
tink2123's avatar
tink2123 committed
364

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

```
tink2123's avatar
tink2123 committed
368
# GPU 评估, Global.checkpoints 为待测权重
xmy0916's avatar
xmy0916 committed
369
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
370
371
```

WenmuZhou's avatar
WenmuZhou committed
372
<a name="预测"></a>
tink2123's avatar
tink2123 committed
373
## 4 预测
tink2123's avatar
tink2123 committed
374

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

tink2123's avatar
tink2123 committed
377
378
379
380
381
默认预测图片存储在 `infer_img` 里,通过 `-o Global.checkpoints` 加载训练好的参数文件:

根据配置文件中设置的的 `save_model_dir``save_epoch_step` 字段,会有以下几种参数被保存下来:

```
tink2123's avatar
tink2123 committed
382
output/rec/
tink2123's avatar
tink2123 committed
383
384
385
386
387
388
389
390
391
392
393
394
395
├── best_accuracy.pdopt  
├── best_accuracy.pdparams  
├── best_accuracy.states  
├── config.yml  
├── iter_epoch_3.pdopt  
├── iter_epoch_3.pdparams  
├── iter_epoch_3.states  
├── latest.pdopt  
├── latest.pdparams  
├── latest.states  
└── train.log
```
其中 best_accuracy.* 是评估集上的最优模型;iter_epoch_x.* 是以 `save_epoch_step` 为间隔保存下来的模型;latest.* 是最后一个epoch的模型。
tink2123's avatar
tink2123 committed
396
397

```
tink2123's avatar
tink2123 committed
398
# 预测英文结果
WenmuZhou's avatar
WenmuZhou committed
399
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
400
```
tink2123's avatar
tink2123 committed
401
402
403

预测图片:

404
![](../imgs_words/en/word_1.png)
tink2123's avatar
tink2123 committed
405
406
407
408

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

```
tink2123's avatar
tink2123 committed
409
infer_img: doc/imgs_words/en/word_1.png
tink2123's avatar
tink2123 committed
410
        result: ('joint', 0.9998967)
tink2123's avatar
tink2123 committed
411
412
```

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

```
# 预测中文结果
WenmuZhou's avatar
WenmuZhou committed
418
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
419
420
```

tink2123's avatar
tink2123 committed
421
预测图片:
tink2123's avatar
tink2123 committed
422

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

tink2123's avatar
tink2123 committed
425
426
427
得到输入图像的预测结果:

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