recognition_en.md 15.5 KB
Newer Older
1
# Text Recognition
Khanh Tran's avatar
Khanh Tran committed
2

WenmuZhou's avatar
WenmuZhou committed
3
4
5
6
7
8
9
10
11
12
13
14
- [1. Data Preparation](#1-data-preparation)
  - [1.1 DataSet Preparation](#11-dataset-preparation)
  - [1.2 Dictionary](#12-dictionary)
  - [1.4 Add Space Category](#14-add-space-category)
- [2.Training](#2training)
  - [2.1 Data Augmentation](#21-data-augmentation)
  - [2.2 General Training](#22-general-training)
  - [2.3 Multi-language Training](#23-multi-language-training)
  - [2.4 Training with Knowledge Distillation](#24-training-with-knowledge-distillation)
- [3. Evalution](#3-evalution)
- [4. Prediction](#4-prediction)
- [5. Convert to Inference Model](#5-convert-to-inference-model)
WenmuZhou's avatar
WenmuZhou committed
15
16

<a name="DATA_PREPARATION"></a>
17
## 1. Data Preparation
Khanh Tran's avatar
Khanh Tran committed
18

WenmuZhou's avatar
WenmuZhou committed
19
### 1.1 DataSet Preparation
Khanh Tran's avatar
Khanh Tran committed
20

WenmuZhou's avatar
WenmuZhou committed
21
To prepare datasets, refer to [ocr_datasets](./dataset/ocr_datasets.md) .
WenmuZhou's avatar
WenmuZhou committed
22

23
24
If you want to reproduce the paper SAR, you need to download extra dataset [SynthAdd](https://pan.baidu.com/share/init?surl=uV0LtoNmcxbO-0YA7Ch4dg), extraction code: 627x. Besides, icdar2013, icdar2015, cocotext, IIIT5k datasets are also used to train. For specific details, please refer to the paper SAR.

WenmuZhou's avatar
WenmuZhou committed
25
<a name="Dictionary"></a>
WenmuZhou's avatar
WenmuZhou committed
26
### 1.2 Dictionary
Khanh Tran's avatar
Khanh Tran committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

Finally, a dictionary ({word_dict_name}.txt) needs to be provided so that when the model is trained, all the characters that appear can be mapped to the dictionary index.

Therefore, the dictionary needs to contain all the characters that you want to be recognized correctly. {word_dict_name}.txt needs to be written in the following format and saved in the `utf-8` encoding format:

```
l
d
a
d
r
n
```

In `word_dict.txt`, there is a single word in each line, which maps characters and numeric indexes together, e.g "and" will be mapped to [2 5 1]

WenmuZhou's avatar
WenmuZhou committed
43
44
PaddleOCR has built-in dictionaries, which can be used on demand.

Khanh Tran's avatar
Khanh Tran committed
45
46
`ppocr/utils/ppocr_keys_v1.txt` is a Chinese dictionary with 6623 characters.

WenmuZhou's avatar
WenmuZhou committed
47
48
49
50
`ppocr/utils/ic15_dict.txt` is an English dictionary with 63 characters

`ppocr/utils/dict/french_dict.txt` is a French dictionary with 118 characters

51
`ppocr/utils/dict/japan_dict.txt` is a Japanese dictionary with 4399 characters
WenmuZhou's avatar
WenmuZhou committed
52

tink2123's avatar
tink2123 committed
53
`ppocr/utils/dict/korean_dict.txt` is a Korean dictionary with 3636 characters
WenmuZhou's avatar
WenmuZhou committed
54

tink2123's avatar
tink2123 committed
55
56
`ppocr/utils/dict/german_dict.txt` is a German dictionary with 131 characters

tink2123's avatar
tink2123 committed
57
`ppocr/utils/en_dict.txt` is a English dictionary with 96 characters
WenmuZhou's avatar
WenmuZhou committed
58

59

WenmuZhou's avatar
WenmuZhou committed
60
The current multi-language model is still in the demo stage and will continue to optimize the model and add languages. **You are very welcome to provide us with dictionaries and fonts in other languages**,
littletomatodonkey's avatar
fix doc  
littletomatodonkey committed
61
If you like, you can submit the dictionary file to [dict](../../ppocr/utils/dict) and we will thank you in the Repo.
Khanh Tran's avatar
Khanh Tran committed
62
63


tink2123's avatar
tink2123 committed
64
To customize the dict file, please modify the `character_dict_path` field in `configs/rec/rec_icdar15_train.yml` .
Khanh Tran's avatar
Khanh Tran committed
65

tink2123's avatar
tink2123 committed
66
67
68
69
- Custom dictionary

If you need to customize dic file, please add character_dict_path field in configs/rec/rec_icdar15_train.yml to point to your dictionary path. And set character_type to ch.

WenmuZhou's avatar
WenmuZhou committed
70
<a name="Add_space_category"></a>
71
### 1.4 Add Space Category
tink2123's avatar
tink2123 committed
72

xmy0916's avatar
xmy0916 committed
73
If you want to support the recognition of the `space` category, please set the `use_space_char` field in the yml file to `True`.
tink2123's avatar
tink2123 committed
74

WenmuZhou's avatar
WenmuZhou committed
75
<a name="TRAINING"></a>
76
## 2.Training
Khanh Tran's avatar
Khanh Tran committed
77

tink2123's avatar
tink2123 committed
78
<a name="Data_Augmentation"></a>
tink2123's avatar
tink2123 committed
79
### 2.1 Data Augmentation
tink2123's avatar
tink2123 committed
80
81
82
83
84
85
86
87

PaddleOCR provides a variety of data augmentation methods. All the augmentation methods are enabled by default.

The default perturbation methods are: cvtColor, blur, jitter, Gasuss noise, random crop, perspective, color reverse, TIA augmentation.

Each disturbance method is selected with a 40% probability during the training process. For specific code implementation, please refer to: [rec_img_aug.py](../../ppocr/data/imaug/rec_img_aug.py)

<a name="Training"></a>
tink2123's avatar
tink2123 committed
88
### 2.2 General Training
tink2123's avatar
tink2123 committed
89

Khanh Tran's avatar
Khanh Tran committed
90
91
92
93
94
95
96
PaddleOCR provides training scripts, evaluation scripts, and prediction scripts. In this section, the CRNN recognition model will be used as an example:

First download the pretrain model, you can download the trained model to finetune on the icdar2015 data:

```
cd PaddleOCR/
# Download the pre-trained model of MobileNetV3
tink2123's avatar
tink2123 committed
97
wget -P ./pretrain_models/ https://paddleocr.bj.bcebos.com/dygraph_v2.0/en/rec_mv3_none_bilstm_ctc_v2.0_train.tar
Khanh Tran's avatar
Khanh Tran committed
98
99
# Decompress model parameters
cd pretrain_models
tink2123's avatar
tink2123 committed
100
tar -xf rec_mv3_none_bilstm_ctc_v2.0_train.tar && rm -rf rec_mv3_none_bilstm_ctc_v2.0_train.tar
Khanh Tran's avatar
Khanh Tran committed
101
102
103
104
105
```

Start training:

```
tink2123's avatar
tink2123 committed
106
# GPU training Support single card and multi-card training
tink2123's avatar
tink2123 committed
107
# Training icdar15 English data and The training log will be automatically saved as train.log under "{save_model_dir}"
tink2123's avatar
tink2123 committed
108
109
110
111

#specify the single card training(Long training time, not recommended)
python3 tools/train.py -c configs/rec/rec_icdar15_train.yml
#specify the card number through --gpus
xmy0916's avatar
xmy0916 committed
112
python3 -m paddle.distributed.launch --gpus '0,1,2,3'  tools/train.py -c configs/rec/rec_icdar15_train.yml
Khanh Tran's avatar
Khanh Tran committed
113
```
tink2123's avatar
tink2123 committed
114
115


Khanh Tran's avatar
Khanh Tran committed
116
117
118
119
120
121
122
123
124
PaddleOCR supports alternating training and evaluation. You can modify `eval_batch_step` in `configs/rec/rec_icdar15_train.yml` to set the evaluation frequency. By default, it is evaluated every 500 iter and the best acc model is saved under `output/rec_CRNN/best_accuracy` during the evaluation process.

If the evaluation set is large, the test will be time-consuming. It is recommended to reduce the number of evaluations, or evaluate after training.

* Tip: You can use the `-c` parameter to select multiple model configurations under the `configs/rec/` path for training. The recognition algorithms supported by PaddleOCR are:


| Configuration file |  Algorithm |   backbone |   trans   |   seq      |     pred     |
| :--------: |  :-------:   | :-------:  |   :-------:   |   :-----:   |  :-----:   |
xmy0916's avatar
xmy0916 committed
125
126
| [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  |
Khanh Tran's avatar
Khanh Tran committed
127
| rec_chinese_lite_train.yml |  CRNN |   Mobilenet_v3 small 0.5 |  None   |  BiLSTM |  ctc  |
WenmuZhou's avatar
WenmuZhou committed
128
| rec_chinese_common_train.yml |  CRNN |   ResNet34_vd |  None   |  BiLSTM |  ctc  |
Khanh Tran's avatar
Khanh Tran committed
129
130
131
132
133
| 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
134
135
| 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
136
| rec_r50fpn_vd_none_srn.yml    | SRN | Resnet50_fpn_vd    | None    | rnn | srn |
Topdu's avatar
Topdu committed
137
| rec_mtb_nrtr.yml    | NRTR | nrtr_mtb    | None    | transformer encoder | transformer decoder |
andyjpaddle's avatar
andyjpaddle committed
138
| rec_r31_sar.yml               | SAR | ResNet31 | None | LSTM encoder | LSTM decoder |
Khanh Tran's avatar
Khanh Tran committed
139
140


WenmuZhou's avatar
WenmuZhou committed
141
For training Chinese data, it is recommended to use
xmy0916's avatar
xmy0916 committed
142
[rec_chinese_lite_train_v2.0.yml](../../configs/rec/ch_ppocr_v2.0/rec_chinese_lite_train_v2.0.yml). If you want to try the result of other algorithms on the Chinese data set, please refer to the following instructions to modify the configuration file:
Khanh Tran's avatar
Khanh Tran committed
143
co
xmy0916's avatar
xmy0916 committed
144
Take `rec_chinese_lite_train_v2.0.yml` as an example:
Khanh Tran's avatar
Khanh Tran committed
145
146
147
```
Global:
  ...
xmy0916's avatar
xmy0916 committed
148
149
  # Add a custom dictionary, such as modify the dictionary, please point the path to the new dictionary
  character_dict_path: ppocr/utils/ppocr_keys_v1.txt
Khanh Tran's avatar
Khanh Tran committed
150
151
  # Modify character type
  ...
152
  # Whether to recognize spaces
xmy0916's avatar
xmy0916 committed
153
  use_space_char: True
Khanh Tran's avatar
Khanh Tran committed
154

155
156
157
158

Optimizer:
  ...
  # Add learning rate decay strategy
xmy0916's avatar
xmy0916 committed
159
160
161
162
163
164
165
166
167
  lr:
    name: Cosine
    learning_rate: 0.001
  ...

...

Train:
  dataset:
MissPenguin's avatar
MissPenguin committed
168
    # Type of dataset,we support LMDBDataSet and SimpleDataSet
xmy0916's avatar
xmy0916 committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
    name: SimpleDataSet
    # Path of dataset
    data_dir: ./train_data/
    # Path of train list
    label_file_list: ["./train_data/train_list.txt"]
    transforms:
      ...
      - RecResizeImg:
          # Modify image_shape to fit long text
          image_shape: [3, 32, 320]
      ...
  loader:
    ...
    # Train batch_size for Single card
    batch_size_per_card: 256
    ...

Eval:
  dataset:
MissPenguin's avatar
MissPenguin committed
188
    # Type of dataset,we support LMDBDataSet and SimpleDataSet
xmy0916's avatar
xmy0916 committed
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
    name: SimpleDataSet
    # Path of dataset
    data_dir: ./train_data
    # Path of eval list
    label_file_list: ["./train_data/val_list.txt"]
    transforms:
      ...
      - RecResizeImg:
          # Modify image_shape to fit long text
          image_shape: [3, 32, 320]
      ...
  loader:
    # Eval batch_size for Single card
    batch_size_per_card: 256
    ...
Khanh Tran's avatar
Khanh Tran committed
204
205
206
```
**Note that the configuration file for prediction/evaluation must be consistent with the training.**

WenmuZhou's avatar
WenmuZhou committed
207
<a name="Multi_language"></a>
tink2123's avatar
tink2123 committed
208
### 2.3 Multi-language Training
tink2123's avatar
tink2123 committed
209
210
211

Currently, the multi-language algorithms supported by PaddleOCR are:

tink2123's avatar
tink2123 committed
212
213
214
215
216
217
218
219
220
221
222
223
| Configuration file |  Algorithm name |   backbone |   trans   |   seq      |     pred     |  language |
| :--------: |  :-------:   | :-------:  |   :-------:   |   :-----:   |  :-----:   | :-----:  |
| rec_chinese_cht_lite_train.yml |  CRNN |   Mobilenet_v3 small 0.5 |  None   |  BiLSTM |  ctc  | chinese traditional  |
| rec_en_lite_train.yml |  CRNN |   Mobilenet_v3 small 0.5 |  None   |  BiLSTM |  ctc  | English(Case sensitive)   |
| 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  | Japanese |
| rec_korean_lite_train.yml |  CRNN |   Mobilenet_v3 small 0.5 |  None   |  BiLSTM |  ctc  | Korean  |
| 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  | arabic |
| 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  |
tink2123's avatar
tink2123 committed
224

tink2123's avatar
tink2123 committed
225
For more supported languages, please refer to : [Multi-language model](https://github.com/PaddlePaddle/PaddleOCR/blob/release/2.1/doc/doc_en/multi_languages_en.md#4-support-languages-and-abbreviations)
WenmuZhou's avatar
WenmuZhou committed
226
227
228
229
230
231
232
233
234


If you want to finetune on the basis of the existing model effect, please refer to the following instructions to modify the configuration file:

Take `rec_french_lite_train` as an example:

```
Global:
  ...
xmy0916's avatar
xmy0916 committed
235
  # Add a custom dictionary, such as modify the dictionary, please point the path to the new dictionary
WenmuZhou's avatar
WenmuZhou committed
236
237
  character_dict_path: ./ppocr/utils/dict/french_dict.txt
  ...
xmy0916's avatar
xmy0916 committed
238
  # Whether to recognize spaces
xmy0916's avatar
xmy0916 committed
239
  use_space_char: True
xmy0916's avatar
xmy0916 committed
240

WenmuZhou's avatar
WenmuZhou committed
241
...
xmy0916's avatar
xmy0916 committed
242
243
244

Train:
  dataset:
MissPenguin's avatar
MissPenguin committed
245
    # Type of dataset,we support LMDBDataSet and SimpleDataSet
xmy0916's avatar
xmy0916 committed
246
247
248
249
250
251
252
253
254
    name: SimpleDataSet
    # Path of dataset
    data_dir: ./train_data/
    # Path of train list
    label_file_list: ["./train_data/french_train.txt"]
    ...

Eval:
  dataset:
MissPenguin's avatar
MissPenguin committed
255
    # Type of dataset,we support LMDBDataSet and SimpleDataSet
xmy0916's avatar
xmy0916 committed
256
257
258
259
260
261
    name: SimpleDataSet
    # Path of dataset
    data_dir: ./train_data
    # Path of eval list
    label_file_list: ["./train_data/french_val.txt"]
    ...
WenmuZhou's avatar
WenmuZhou committed
262
```
Khanh Tran's avatar
Khanh Tran committed
263

264
265
266
267
268
269
<a name="kd"></a>

### 2.4 Training with Knowledge Distillation

Knowledge distillation is supported in PaddleOCR for text recognition training process. For more details, please refer to [doc](./knowledge_distillation_en.md).

WenmuZhou's avatar
WenmuZhou committed
270
<a name="EVALUATION"></a>
271

272
## 3. Evalution
Khanh Tran's avatar
Khanh Tran committed
273

WenmuZhou's avatar
WenmuZhou committed
274
The evaluation dataset can be set by modifying the `Eval.dataset.label_file_list` field in the `configs/rec/rec_icdar15_train.yml` file.
Khanh Tran's avatar
Khanh Tran committed
275
276
277

```
# GPU evaluation, Global.checkpoints is the weight to be tested
WenmuZhou's avatar
WenmuZhou committed
278
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
Khanh Tran's avatar
Khanh Tran committed
279
280
```

WenmuZhou's avatar
WenmuZhou committed
281
<a name="PREDICTION"></a>
282
## 4. Prediction
Khanh Tran's avatar
Khanh Tran committed
283
284
285
286


Using the model trained by paddleocr, you can quickly get prediction through the following script.

tink2123's avatar
tink2123 committed
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
The default prediction picture is stored in `infer_img`, and the trained weight is specified via `-o Global.checkpoints`:


According to the `save_model_dir` and `save_epoch_step` fields set in the configuration file, the following parameters will be saved:

```
output/rec/
├── 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
```

Among them, best_accuracy.* is the best model on the evaluation set; iter_epoch_x.* is the model saved at intervals of `save_epoch_step`; latest.* is the model of the last epoch.
Khanh Tran's avatar
Khanh Tran committed
308
309
310

```
# Predict English results
WenmuZhou's avatar
WenmuZhou committed
311
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/en/word_1.jpg
Khanh Tran's avatar
Khanh Tran committed
312
313
```

tink2123's avatar
tink2123 committed
314

Khanh Tran's avatar
Khanh Tran committed
315
316
Input image:

317
![](../imgs_words/en/word_1.png)
Khanh Tran's avatar
Khanh Tran committed
318
319
320
321
322

Get the prediction result of the input image:

```
infer_img: doc/imgs_words/en/word_1.png
tink2123's avatar
tink2123 committed
323
        result: ('joint', 0.9998967)
Khanh Tran's avatar
Khanh Tran committed
324
325
```

xmy0916's avatar
xmy0916 committed
326
The configuration file used for prediction must be consistent with the training. For example, you completed the training of the Chinese model with `python3 tools/train.py -c configs/rec/ch_ppocr_v2.0/rec_chinese_lite_train_v2.0.yml`, you can use the following command to predict the Chinese model:
Khanh Tran's avatar
Khanh Tran committed
327
328
329

```
# Predict Chinese results
WenmuZhou's avatar
WenmuZhou committed
330
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
Khanh Tran's avatar
Khanh Tran committed
331
332
333
334
```

Input image:

335
![](../imgs_words/ch/word_1.jpg)
Khanh Tran's avatar
Khanh Tran committed
336
337
338
339
340

Get the prediction result of the input image:

```
infer_img: doc/imgs_words/ch/word_1.jpg
tink2123's avatar
tink2123 committed
341
        result: ('韩国小馆', 0.997218)
Khanh Tran's avatar
Khanh Tran committed
342
```
343
344
345

<a name="Inference"></a>

346
## 5. Convert to Inference Model
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371

The recognition model is converted to the inference model in the same way as the detection, as follows:

```
# -c Set the training algorithm yml configuration file
# -o Set optional parameters
# Global.pretrained_model parameter Set the training model address to be converted without adding the file suffix .pdmodel, .pdopt or .pdparams.
# Global.save_inference_dir Set the address where the converted model will be saved.

python3 tools/export_model.py -c configs/rec/ch_ppocr_v2.0/rec_chinese_lite_train_v2.0.yml -o Global.pretrained_model=./ch_lite/ch_ppocr_mobile_v2.0_rec_train/best_accuracy  Global.save_inference_dir=./inference/rec_crnn/
```

If you have a model trained on your own dataset with a different dictionary file, please make sure that you modify the `character_dict_path` in the configuration file to your dictionary file path.

After the conversion is successful, there are three files in the model save directory:

```
inference/det_db/
    ├── inference.pdiparams         # The parameter file of recognition inference model
    ├── inference.pdiparams.info    # The parameter information of recognition inference model, which can be ignored
    └── inference.pdmodel           # The program file of recognition model
```

- Text recognition model Inference using custom characters dictionary

WenmuZhou's avatar
WenmuZhou committed
372
  If the text dictionary is modified during training, when using the inference model to predict, you need to specify the dictionary path used by `--rec_char_dict_path`
373
374

  ```
WenmuZhou's avatar
WenmuZhou committed
375
  python3 tools/infer/predict_rec.py --image_dir="./doc/imgs_words_en/word_336.png" --rec_model_dir="./your inference model" --rec_image_shape="3, 32, 100" --rec_char_dict_path="your text dict path"
376
  ```