recognition.md 15.5 KB
Newer Older
tink2123's avatar
tink2123 committed
1
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>
WenmuZhou's avatar
WenmuZhou 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
39
40
<a name="准备数据集"></a>
#### 1.1 自定义数据集
下面以通用数据集为例, 介绍如何准备数据集:
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

tink2123's avatar
tink2123 committed
91
如果你使用的是icdar2015的公开数据集,PaddleOCR 提供了一份用于训练 ICDAR2015 数据集的标签文件,通过以下方式下载:
tink2123's avatar
fix doc  
tink2123 committed
92
93
94
95
```
# 训练集标签
wget -P ./train_data/ic15_data  https://paddleocr.bj.bcebos.com/dataset/rec_gt_train.txt
# 测试集标签
tink2123's avatar
tink2123 committed
96
wget -P ./train_data/ic15_data  https://paddleocr.bj.bcebos.com/dataset/rec_gt_test.txt
tink2123's avatar
fix doc  
tink2123 committed
97
```
tink2123's avatar
tink2123 committed
98

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

```
# 将官网下载的标签文件转换为 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
106
107
108
109
110
111
112
113
114
115
数据样式格式如下,(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
116
<a name="字典"></a>
tink2123's avatar
tink2123 committed
117
#### 1.3 字典
tink2123's avatar
tink2123 committed
118
119
120

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

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

tink2123's avatar
tink2123 committed
123
124
```
l
tink2123's avatar
tink2123 committed
125
126
d
a
tink2123's avatar
tink2123 committed
127
128
d
r
tink2123's avatar
tink2123 committed
129
n
tink2123's avatar
tink2123 committed
130
```
tink2123's avatar
tink2123 committed
131
132
133

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

WenmuZhou's avatar
WenmuZhou committed
134
135
136
137
* 内置字典

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

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

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

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

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

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

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

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

WenmuZhou's avatar
WenmuZhou committed
152

WenmuZhou's avatar
WenmuZhou committed
153

tink2123's avatar
tink2123 committed
154

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

tink2123's avatar
tink2123 committed
158
- 自定义字典
tink2123's avatar
tink2123 committed
159

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

WenmuZhou's avatar
WenmuZhou committed
163
<a name="支持空格"></a>
tink2123's avatar
tink2123 committed
164
#### 1.4 添加空格类别
tink2123's avatar
tink2123 committed
165

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

tink2123's avatar
tink2123 committed
168

WenmuZhou's avatar
WenmuZhou committed
169
<a name="启动训练"></a>
WenmuZhou's avatar
WenmuZhou committed
170
### 2. 启动训练
tink2123's avatar
tink2123 committed
171

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

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>
#### 2.2 通用模型训练

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

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

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

开始训练:

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

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

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

tink2123's avatar
tink2123 committed
210
211
212
#多卡训练,通过--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
213
214


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

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

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


| 配置文件 |  算法名称 |   backbone |   trans   |   seq      |     pred     |
| :--------: |  :-------:   | :-------:  |   :-------:   |   :-----:   |  :-----:   |
xmy0916's avatar
xmy0916 committed
224
225
| [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
226
227
228
229
230
| 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
231
232
| 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
233
| rec_r50fpn_vd_none_srn.yml    | SRN | Resnet50_fpn_vd    | None    | rnn | srn |
Topdu's avatar
Topdu committed
234
| rec_mtb_nrtr.yml    | NRTR | nrtr_mtb    | None    | transformer encoder | transformer decoder |
tink2123's avatar
tink2123 committed
235

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

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

250
251
252
253

Optimizer:
  ...
  # 添加学习率衰减策略
xmy0916's avatar
xmy0916 committed
254
255
256
257
258
259
260
261
262
  lr:
    name: Cosine
    learning_rate: 0.001
  ...

...

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

tink2123's avatar
tink2123 committed
302
303
<a name="多语言模型训练"></a>
#### 2.3 多语言模型训练
WenmuZhou's avatar
WenmuZhou committed
304

tink2123's avatar
tink2123 committed
305
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
306

tink2123's avatar
tink2123 committed
307
按语系划分,目前PaddleOCR支持的语种有:
tink2123's avatar
tink2123 committed
308
309
310
311

| 配置文件 |  算法名称 |   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
312
| rec_en_lite_train.yml |  CRNN |   Mobilenet_v3 small 0.5 |  None   |  BiLSTM |  ctc  | 英语(区分大小写)   | EN |
tink2123's avatar
tink2123 committed
313
314
315
316
| 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
317
318
319
320
321
322
| 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
323
324
325
326
327
328
329
330
331
332

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

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

...
xmy0916's avatar
xmy0916 committed
337
338
339

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

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

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

```
tink2123's avatar
tink2123 committed
364
# GPU 评估, Global.checkpoints 为待测权重
xmy0916's avatar
xmy0916 committed
365
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
366
367
```

WenmuZhou's avatar
WenmuZhou committed
368
<a name="预测"></a>
WenmuZhou's avatar
WenmuZhou committed
369
### 4 预测
tink2123's avatar
tink2123 committed
370

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

tink2123's avatar
tink2123 committed
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
默认预测图片存储在 `infer_img` 里,通过 `-o Global.checkpoints` 加载训练好的参数文件:

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

```
seed_ch/  
├── 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
392
393

```
tink2123's avatar
tink2123 committed
394
# 预测英文结果
WenmuZhou's avatar
WenmuZhou committed
395
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
396
```
tink2123's avatar
tink2123 committed
397
398
399

预测图片:

400
![](../imgs_words/en/word_1.png)
tink2123's avatar
tink2123 committed
401
402
403
404

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

```
tink2123's avatar
tink2123 committed
405
infer_img: doc/imgs_words/en/word_1.png
tink2123's avatar
tink2123 committed
406
        result: ('joint', 0.9998967)
tink2123's avatar
tink2123 committed
407
408
```

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

```
# 预测中文结果
WenmuZhou's avatar
WenmuZhou committed
414
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
415
416
```

tink2123's avatar
tink2123 committed
417
预测图片:
tink2123's avatar
tink2123 committed
418

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

tink2123's avatar
tink2123 committed
421
422
423
得到输入图像的预测结果:

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