inference_en.md 24.5 KB
Newer Older
Khanh Tran's avatar
Khanh Tran committed
1

2
# Inference Based on Python Prediction Engine
Khanh Tran's avatar
Khanh Tran committed
3

WenmuZhou's avatar
WenmuZhou committed
4
The inference model (the model saved by `paddle.jit.save`) is generally a solidified model saved after the model training is completed, and is mostly used to give prediction in deployment.
Khanh Tran's avatar
Khanh Tran committed
5
6
7

The model saved during the training process is the checkpoints model, which saves the parameters of the model and is mostly used to resume training.

LDOUBLEV's avatar
LDOUBLEV committed
8
Compared with the checkpoints model, the inference model will additionally save the structural information of the model. Therefore, it is easier to deploy because the model structure and model parameters are already solidified in the inference model file, and is suitable for integration with actual systems.
LDOUBLEV's avatar
LDOUBLEV committed
9
For more details, please refer to the document [Classification Framework](https://github.com/PaddlePaddle/PaddleClas/blob/release%2F2.0/docs/zh_CN/extension/paddle_mobile_inference.md).
Khanh Tran's avatar
Khanh Tran committed
10

WenmuZhou's avatar
WenmuZhou committed
11
Next, we first introduce how to convert a trained model into an inference model, and then we will introduce text detection, text recognition, angle class, and the concatenation of them based on inference model.
Khanh Tran's avatar
Khanh Tran committed
12

13
14
15
16
- [1. Convert Training Model to Inference Model](#CONVERT)
    - [1.1 Convert Detection Model to Inference Model](#Convert_detection_model)
    - [1.2 Convert Recognition Model to Inference Model](#Convert_recognition_model)
    - [1.3 Convert Angle Classification Model to Inference Model](#Convert_angle_class_model)
WenmuZhou's avatar
WenmuZhou committed
17
18


19
20
21
22
23
- [2. Text Detection Model Inference](#DETECTION_MODEL_INFERENCE)
    - [2.1 Lightweight Chinese Detection Model Inference](#LIGHTWEIGHT_DETECTION)
    - [2.2 DB Text Detection Model Inference](#DB_DETECTION)
    - [2.3 East Text Detection Model Inference](#EAST_DETECTION)
    - [2.4 Sast Text Detection Model Inference](#SAST_DETECTION)
tink2123's avatar
tink2123 committed
24

25
26
27
28
29
30
- [3. Text Recognition Model Inference](#RECOGNITION_MODEL_INFERENCE)
    - [3.1 Lightweight Chinese Text Recognition Model Reference](#LIGHTWEIGHT_RECOGNITION)
    - [3.2 CTC-Based Text Recognition Model Inference](#CTC-BASED_RECOGNITION)
    - [3.3 SRN-Based Text Recognition Model Inference](#SRN-BASED_RECOGNITION)
    - [3.4 Text Recognition Model Inference Using Custom Characters Dictionary](#USING_CUSTOM_CHARACTERS)
    - [3.5 Multilingual Model Inference](#MULTILINGUAL_MODEL_INFERENCE)
WenmuZhou's avatar
WenmuZhou committed
31

32
- [4. Angle Classification Model Inference](#ANGLE_CLASS_MODEL_INFERENCE)
WenmuZhou's avatar
WenmuZhou committed
33

34
35
36
- [5. Text Detection Angle Classification And Recognition Inference Concatenation](#CONCATENATION)
    - [5.1 Lightweight Chinese Model](#LIGHTWEIGHT_CHINESE_MODEL)
    - [5.2 Other Models](#OTHER_MODELS)
WenmuZhou's avatar
WenmuZhou committed
37

licx's avatar
licx committed
38
<a name="CONVERT"></a>
39
## 1. Convert Training Model to Inference Model
licx's avatar
licx committed
40
<a name="Convert_detection_model"></a>
41
42

### 1.1 Convert Detection Model to Inference Model
Khanh Tran's avatar
Khanh Tran committed
43

xxxpsyduck's avatar
xxxpsyduck committed
44
Download the lightweight Chinese detection model:
Khanh Tran's avatar
Khanh Tran committed
45
```
WenmuZhou's avatar
WenmuZhou committed
46
wget -P ./ch_lite/ https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_det_train.tar && tar xf ./ch_lite/ch_ppocr_mobile_v2.0_det_train.tar -C ./ch_lite/
Khanh Tran's avatar
Khanh Tran committed
47
```
WenmuZhou's avatar
WenmuZhou committed
48

Khanh Tran's avatar
Khanh Tran committed
49
50
The above model is a DB algorithm trained with MobileNetV3 as the backbone. To convert the trained model into an inference model, just run the following command:
```
WenmuZhou's avatar
WenmuZhou committed
51
52
# -c Set the training algorithm yml configuration file
# -o Set optional parameters
WenmuZhou's avatar
WenmuZhou committed
53
# Global.pretrained_model parameter Set the training model address to be converted without adding the file suffix .pdmodel, .pdopt or .pdparams.
WenmuZhou's avatar
WenmuZhou committed
54
# Global.save_inference_dir Set the address where the converted model will be saved.
tink2123's avatar
tink2123 committed
55

56
python3 tools/export_model.py -c configs/det/ch_ppocr_v2.0/ch_det_mv3_db_v2.0.yml -o Global.pretrained_model=./ch_lite/ch_ppocr_mobile_v2.0_det_train/best_accuracy  Global.save_inference_dir=./inference/det_db/
Khanh Tran's avatar
Khanh Tran committed
57
```
WenmuZhou's avatar
WenmuZhou committed
58

WenmuZhou's avatar
WenmuZhou committed
59
When converting to an inference model, the configuration file used is the same as the configuration file used during training. In addition, you also need to set the `Global.pretrained_model` parameter in the configuration file.
WenmuZhou's avatar
WenmuZhou committed
60
After the conversion is successful, there are three files in the model save directory:
Khanh Tran's avatar
Khanh Tran committed
61
62
```
inference/det_db/
63
64
65
    ├── inference.pdiparams         # The parameter file of detection inference model
    ├── inference.pdiparams.info    # The parameter information of detection inference model, which can be ignored
    └── inference.pdmodel           # The program file of detection inference model
Khanh Tran's avatar
Khanh Tran committed
66
67
```

licx's avatar
licx committed
68
<a name="Convert_recognition_model"></a>
69
### 1.2 Convert Recognition Model to Inference Model
Khanh Tran's avatar
Khanh Tran committed
70

xxxpsyduck's avatar
xxxpsyduck committed
71
Download the lightweight Chinese recognition model:
Khanh Tran's avatar
Khanh Tran committed
72
```
WenmuZhou's avatar
WenmuZhou committed
73
wget -P ./ch_lite/ https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_rec_train.tar && tar xf ./ch_lite/ch_ppocr_mobile_v2.0_rec_train.tar -C ./ch_lite/
Khanh Tran's avatar
Khanh Tran committed
74
75
76
77
```

The recognition model is converted to the inference model in the same way as the detection, as follows:
```
WenmuZhou's avatar
WenmuZhou committed
78
79
# -c Set the training algorithm yml configuration file
# -o Set optional parameters
WenmuZhou's avatar
WenmuZhou committed
80
# Global.pretrained_model parameter Set the training model address to be converted without adding the file suffix .pdmodel, .pdopt or .pdparams.
WenmuZhou's avatar
WenmuZhou committed
81
# Global.save_inference_dir Set the address where the converted model will be saved.
tink2123's avatar
tink2123 committed
82

83
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/
Khanh Tran's avatar
Khanh Tran committed
84
85
86
87
```

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.

WenmuZhou's avatar
WenmuZhou committed
88
After the conversion is successful, there are three files in the model save directory:
Khanh Tran's avatar
Khanh Tran committed
89
```
WenmuZhou's avatar
WenmuZhou committed
90
inference/det_db/
91
92
93
    ├── 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
Khanh Tran's avatar
Khanh Tran committed
94
95
```

WenmuZhou's avatar
WenmuZhou committed
96
<a name="Convert_angle_class_model"></a>
97
### 1.3 Convert Angle Classification Model to Inference Model
WenmuZhou's avatar
WenmuZhou committed
98
99
100

Download the angle classification model:
```
WenmuZhou's avatar
WenmuZhou committed
101
wget -P ./ch_lite/ https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_train.tar && tar xf ./ch_lite/ch_ppocr_mobile_v2.0_cls_train.tar -C ./ch_lite/
WenmuZhou's avatar
WenmuZhou committed
102
103
104
105
```

The angle classification model is converted to the inference model in the same way as the detection, as follows:
```
WenmuZhou's avatar
WenmuZhou committed
106
107
# -c Set the training algorithm yml configuration file
# -o Set optional parameters
WenmuZhou's avatar
WenmuZhou committed
108
# Global.pretrained_model parameter Set the training model address to be converted without adding the file suffix .pdmodel, .pdopt or .pdparams.
WenmuZhou's avatar
WenmuZhou committed
109
# Global.save_inference_dir Set the address where the converted model will be saved.
WenmuZhou's avatar
WenmuZhou committed
110

111
python3 tools/export_model.py -c configs/cls/cls_mv3.yml -o Global.pretrained_model=./ch_lite/ch_ppocr_mobile_v2.0_cls_train/best_accuracy  Global.save_inference_dir=./inference/cls/
WenmuZhou's avatar
WenmuZhou committed
112
113
114
115
```

After the conversion is successful, there are two files in the directory:
```
WenmuZhou's avatar
WenmuZhou committed
116
inference/det_db/
117
118
119
    ├── inference.pdiparams         # The parameter file of angle class inference model
    ├── inference.pdiparams.info    # The parameter information of  angle class inference model, which can be ignored
    └── inference.pdmodel           # The program file of angle class model
WenmuZhou's avatar
WenmuZhou committed
120
121
122
```


licx's avatar
licx committed
123
<a name="DETECTION_MODEL_INFERENCE"></a>
124
## 2. Text Detection Model Inference
Khanh Tran's avatar
Khanh Tran committed
125

tink2123's avatar
tink2123 committed
126
127
The following will introduce the lightweight Chinese detection model inference, DB text detection model inference and EAST text detection model inference. The default configuration is based on the inference setting of the DB text detection model.
Because EAST and DB algorithms are very different, when inference, it is necessary to **adapt the EAST text detection algorithm by passing in corresponding parameters**.
Khanh Tran's avatar
Khanh Tran committed
128

licx's avatar
licx committed
129
<a name="LIGHTWEIGHT_DETECTION"></a>
130
### 2.1 Lightweight Chinese Detection Model Inference
Khanh Tran's avatar
Khanh Tran committed
131

xxxpsyduck's avatar
xxxpsyduck committed
132
For lightweight Chinese detection model inference, you can execute the following commands:
Khanh Tran's avatar
Khanh Tran committed
133
134

```
LDOUBLEV's avatar
LDOUBLEV committed
135
136
137
138
# download DB text detection inference model
wget  https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_det_infer.tar
tar xf ch_ppocr_mobile_v2.0_det_infer.tar
# predict
139
python3 tools/infer/predict_det.py --image_dir="./doc/imgs/00018069.jpg" --det_model_dir="./inference/det_db/"
Khanh Tran's avatar
Khanh Tran committed
140
141
```

fanruinet's avatar
fanruinet committed
142
The visual text detection results are saved to the ./inference_results folder by default, and the name of the result file is prefixed with 'det_res'. Examples of results are as follows:
Khanh Tran's avatar
Khanh Tran committed
143

144
![](../imgs_results/det_res_00018069.jpg)
Khanh Tran's avatar
Khanh Tran committed
145

LDOUBLEV's avatar
LDOUBLEV committed
146
You can use the parameters `limit_type` and `det_limit_side_len` to limit the size of the input image,
MissPenguin's avatar
MissPenguin committed
147
The optional parameters of `limit_type` are [`max`, `min`], and
LDOUBLEV's avatar
LDOUBLEV committed
148
`det_limit_size_len` is a positive integer, generally set to a multiple of 32, such as 960.
Khanh Tran's avatar
Khanh Tran committed
149

LDOUBLEV's avatar
LDOUBLEV committed
150
151
152
153
154
The default setting of the parameters is `limit_type='max', det_limit_side_len=960`. Indicates that the longest side of the network input image cannot exceed 960,
If this value is exceeded, the image will be resized with the same width ratio to ensure that the longest side is `det_limit_side_len`.
Set as `limit_type='min', det_limit_side_len=960`, it means that the shortest side of the image is limited to 960.

If the resolution of the input picture is relatively large and you want to use a larger resolution prediction, you can set det_limit_side_len to the desired value, such as 1216:
Khanh Tran's avatar
Khanh Tran committed
155
```
WenmuZhou's avatar
WenmuZhou committed
156
python3 tools/infer/predict_det.py --image_dir="./doc/imgs/1.jpg" --det_model_dir="./inference/det_db/" --det_limit_type=max --det_limit_side_len=1216
Khanh Tran's avatar
Khanh Tran committed
157
158
159
160
```

If you want to use the CPU for prediction, execute the command as follows
```
WenmuZhou's avatar
WenmuZhou committed
161
python3 tools/infer/predict_det.py --image_dir="./doc/imgs/1.jpg" --det_model_dir="./inference/det_db/" --use_gpu=False
Khanh Tran's avatar
Khanh Tran committed
162
163
```

licx's avatar
licx committed
164
<a name="DB_DETECTION"></a>
165
### 2.2 DB Text Detection Model Inference
Khanh Tran's avatar
Khanh Tran committed
166

WenmuZhou's avatar
WenmuZhou committed
167
First, convert the model saved in the DB text detection training process into an inference model. Taking the model based on the Resnet50_vd backbone network and trained on the ICDAR2015 English dataset as an example ([model download link](https://paddleocr.bj.bcebos.com/dygraph_v2.0/en/det_r50_vd_db_v2.0_train.tar)), you can use the following command to convert:
Khanh Tran's avatar
Khanh Tran committed
168
169

```
170
python3 tools/export_model.py -c configs/det/det_r50_vd_db.yml -o Global.pretrained_model=./det_r50_vd_db_v2.0_train/best_accuracy  Global.save_inference_dir=./inference/det_db
Khanh Tran's avatar
Khanh Tran committed
171
172
173
174
175
176
177
178
179
180
```

DB text detection model inference, you can execute the following command:

```
python3 tools/infer/predict_det.py --image_dir="./doc/imgs_en/img_10.jpg" --det_model_dir="./inference/det_db/"
```

The visualized text detection results are saved to the `./inference_results` folder by default, and the name of the result file is prefixed with 'det_res'. Examples of results are as follows:

181
![](../imgs_results/det_res_img_10_db.jpg)
Khanh Tran's avatar
Khanh Tran committed
182
183
184

**Note**: Since the ICDAR2015 dataset has only 1,000 training images, mainly for English scenes, the above model has very poor detection result on Chinese text images.

licx's avatar
licx committed
185
<a name="EAST_DETECTION"></a>
186
### 2.3 EAST TEXT DETECTION MODEL INFERENCE
Khanh Tran's avatar
Khanh Tran committed
187

MissPenguin's avatar
MissPenguin committed
188
First, convert the model saved in the EAST text detection training process into an inference model. Taking the model based on the Resnet50_vd backbone network and trained on the ICDAR2015 English dataset as an example ([model download link](https://paddleocr.bj.bcebos.com/dygraph_v2.0/en/det_r50_vd_east_v2.0_train.tar)), you can use the following command to convert:
Khanh Tran's avatar
Khanh Tran committed
189
190

```
191
python3 tools/export_model.py -c configs/det/det_r50_vd_east.yml -o Global.pretrained_model=./det_r50_vd_east_v2.0_train/best_accuracy  Global.save_inference_dir=./inference/det_east
Khanh Tran's avatar
Khanh Tran committed
192
```
licx's avatar
licx committed
193
**For EAST text detection model inference, you need to set the parameter ``--det_algorithm="EAST"``**, run the following command:
Khanh Tran's avatar
Khanh Tran committed
194
195
196
197

```
python3 tools/infer/predict_det.py --image_dir="./doc/imgs_en/img_10.jpg" --det_model_dir="./inference/det_east/" --det_algorithm="EAST"
```
licx's avatar
licx committed
198

Khanh Tran's avatar
Khanh Tran committed
199
200
The visualized text detection results are saved to the `./inference_results` folder by default, and the name of the result file is prefixed with 'det_res'. Examples of results are as follows:

MissPenguin's avatar
MissPenguin committed
201
![](../imgs_results/det_res_img_10_east.jpg)
Khanh Tran's avatar
Khanh Tran committed
202

licx's avatar
licx committed
203
204
205
206
**Note**: EAST post-processing locality aware NMS has two versions: Python and C++. The speed of C++ version is obviously faster than that of Python version. Due to the compilation version problem of NMS of C++ version, C++ version NMS will be called only in Python 3.5 environment, and python version NMS will be called in other cases.


<a name="SAST_DETECTION"></a>
207
### 2.4 Sast Text Detection Model Inference
licx's avatar
licx committed
208
#### (1). Quadrangle text detection model (ICDAR2015)  
MissPenguin's avatar
MissPenguin committed
209
First, convert the model saved in the SAST text detection training process into an inference model. Taking the model based on the Resnet50_vd backbone network and trained on the ICDAR2015 English dataset as an example ([model download link](https://paddleocr.bj.bcebos.com/dygraph_v2.0/en/det_r50_vd_sast_icdar15_v2.0_train.tar)), you can use the following command to convert:
licx's avatar
licx committed
210
211

```
212
python3 tools/export_model.py -c configs/det/det_r50_vd_sast_icdar15.yml -o Global.pretrained_model=./det_r50_vd_sast_icdar15_v2.0_train/best_accuracy  Global.save_inference_dir=./inference/det_sast_ic15
licx's avatar
licx committed
213
214
215
```

**For SAST quadrangle text detection model inference, you need to set the parameter `--det_algorithm="SAST"`**, run the following command:
Khanh Tran's avatar
Khanh Tran committed
216

licx's avatar
licx committed
217
218
219
220
221
```
python3 tools/infer/predict_det.py --det_algorithm="SAST" --image_dir="./doc/imgs_en/img_10.jpg" --det_model_dir="./inference/det_sast_ic15/"
```

The visualized text detection results are saved to the `./inference_results` folder by default, and the name of the result file is prefixed with 'det_res'. Examples of results are as follows:
Khanh Tran's avatar
Khanh Tran committed
222

MissPenguin's avatar
MissPenguin committed
223
![](../imgs_results/det_res_img_10_sast.jpg)
licx's avatar
licx committed
224
225

#### (2). Curved text detection model (Total-Text)  
MissPenguin's avatar
MissPenguin committed
226
First, convert the model saved in the SAST text detection training process into an inference model. Taking the model based on the Resnet50_vd backbone network and trained on the Total-Text English dataset as an example ([model download link](https://paddleocr.bj.bcebos.com/dygraph_v2.0/en/det_r50_vd_sast_totaltext_v2.0_train.tar)), you can use the following command to convert:
licx's avatar
licx committed
227
228

```
229
python3 tools/export_model.py -c configs/det/det_r50_vd_sast_totaltext.yml -o Global.pretrained_model=./det_r50_vd_sast_totaltext_v2.0_train/best_accuracy  Global.save_inference_dir=./inference/det_sast_tt
licx's avatar
licx committed
230
231
```

WenmuZhou's avatar
opt doc  
WenmuZhou committed
232
For SAST curved text detection model inference, you need to set the parameter `--det_algorithm="SAST"` and `--det_sast_polygon=True`, run the following command:
licx's avatar
licx committed
233
234
235
236
237
238
239

```
python3 tools/infer/predict_det.py --det_algorithm="SAST" --image_dir="./doc/imgs_en/img623.jpg" --det_model_dir="./inference/det_sast_tt/" --det_sast_polygon=True
```

The visualized text detection results are saved to the `./inference_results` folder by default, and the name of the result file is prefixed with 'det_res'. Examples of results are as follows:

MissPenguin's avatar
MissPenguin committed
240
![](../imgs_results/det_res_img623_sast.jpg)
licx's avatar
licx committed
241
242
243
244

**Note**: SAST post-processing locality aware NMS has two versions: Python and C++. The speed of C++ version is obviously faster than that of Python version. Due to the compilation version problem of NMS of C++ version, C++ version NMS will be called only in Python 3.5 environment, and python version NMS will be called in other cases.

<a name="RECOGNITION_MODEL_INFERENCE"></a>
245
## 3. Text Recognition Model Inference
Khanh Tran's avatar
Khanh Tran committed
246

fanruinet's avatar
fanruinet committed
247
The following will introduce the lightweight Chinese recognition model inference, other CTC-based and Attention-based text recognition models inference. For Chinese text recognition, it is recommended to choose the recognition model based on CTC loss. In practice, it is also found that the result of the model based on Attention loss is not as good as the one based on CTC loss. In addition, if the characters dictionary is modified during training, make sure that you use the same characters set during inference. Please check below for details.
Khanh Tran's avatar
Khanh Tran committed
248
249


licx's avatar
licx committed
250
<a name="LIGHTWEIGHT_RECOGNITION"></a>
251
### 3.1 Lightweight Chinese Text Recognition Model Reference
Khanh Tran's avatar
Khanh Tran committed
252

xxxpsyduck's avatar
xxxpsyduck committed
253
For lightweight Chinese recognition model inference, you can execute the following commands:
Khanh Tran's avatar
Khanh Tran committed
254
255

```
WenmuZhou's avatar
WenmuZhou committed
256
257
258
259
# download CRNN text recognition inference model
wget  https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_rec_infer.tar
tar xf ch_ppocr_mobile_v2.0_rec_infer.tar
python3 tools/infer/predict_rec.py --image_dir="./doc/imgs_words_en/word_10.png" --rec_model_dir="ch_ppocr_mobile_v2.0_rec_infer"
Khanh Tran's avatar
Khanh Tran committed
260
261
```

WenmuZhou's avatar
WenmuZhou committed
262
![](../imgs_words_en/word_10.png)
Khanh Tran's avatar
Khanh Tran committed
263
264
265

After executing the command, the prediction results (recognized text and score) of the above image will be printed on the screen.

WenmuZhou's avatar
WenmuZhou committed
266
```bash
WenmuZhou's avatar
WenmuZhou committed
267
Predicts of ./doc/imgs_words_en/word_10.png:('PAIN', 0.9897658)
WenmuZhou's avatar
WenmuZhou committed
268
```
Khanh Tran's avatar
Khanh Tran committed
269

licx's avatar
licx committed
270
<a name="CTC-BASED_RECOGNITION"></a>
271
### 3.2 CTC-Based Text Recognition Model Inference
Khanh Tran's avatar
Khanh Tran committed
272

WenmuZhou's avatar
WenmuZhou committed
273
Taking CRNN as an example, we introduce the recognition model inference based on CTC loss. Rosetta and Star-Net are used in a similar way, No need to set the recognition algorithm parameter rec_algorithm.
Khanh Tran's avatar
Khanh Tran committed
274

WenmuZhou's avatar
WenmuZhou committed
275
First, convert the model saved in the CRNN text recognition training process into an inference model. Taking the model based on Resnet34_vd backbone network, using MJSynth and SynthText (two English text recognition synthetic datasets) for training, as an example ([model download address](https://paddleocr.bj.bcebos.com/dygraph_v2.0/en/rec_r34_vd_none_bilstm_ctc_v2.0_train.tar)). It can be converted as follow:
Khanh Tran's avatar
Khanh Tran committed
276
277

```
278
python3 tools/export_model.py -c configs/det/rec_r34_vd_none_bilstm_ctc.yml -o Global.pretrained_model=./rec_r34_vd_none_bilstm_ctc_v2.0_train/best_accuracy  Global.save_inference_dir=./inference/rec_crnn
Khanh Tran's avatar
Khanh Tran committed
279
280
```

WenmuZhou's avatar
WenmuZhou committed
281
For CRNN text recognition model inference, execute the following commands:
Khanh Tran's avatar
Khanh Tran committed
282
283

```
tink2123's avatar
tink2123 committed
284
python3 tools/infer/predict_rec.py --image_dir="./doc/imgs_words_en/word_336.png" --rec_model_dir="./inference/starnet/" --rec_image_shape="3, 32, 100" --rec_char_dict_path="./ppocr/utils/ic15_dict.txt"
Khanh Tran's avatar
Khanh Tran committed
285
```
xxxpsyduck's avatar
xxxpsyduck committed
286

WenmuZhou's avatar
WenmuZhou committed
287
![](../imgs_words_en/word_336.png)
Khanh Tran's avatar
Khanh Tran committed
288

WenmuZhou's avatar
WenmuZhou committed
289
290
291
292
293
After executing the command, the recognition result of the above image is as follows:

```bash
Predicts of ./doc/imgs_words_en/word_336.png:('super', 0.9999073)
```
294

xxxpsyduck's avatar
xxxpsyduck committed
295
**Note**:Since the above model refers to [DTRB](https://arxiv.org/abs/1904.01906) text recognition training and evaluation process, it is different from the training of lightweight Chinese recognition model in two aspects:
Khanh Tran's avatar
Khanh Tran committed
296
297
298
299
300
301
302
303
304
305

- The image resolution used in training is different: the image resolution used in training the above model is [3,32,100], while during our Chinese model training, in order to ensure the recognition effect of long text, the image resolution used in training is [3, 32, 320]. The default shape parameter of the inference stage is the image resolution used in training phase, that is [3, 32, 320]. Therefore, when running inference of the above English model here, you need to set the shape of the recognition image through the parameter `rec_image_shape`.

- Character list: the experiment in the DTRB paper is only for 26 lowercase English characters and 10 numbers, a total of 36 characters. All upper and lower case characters are converted to lower case characters, and characters not in the above list are ignored and considered as spaces. Therefore, no characters dictionary file is used here, but a dictionary is generated by the below command. Therefore, the parameter `rec_char_type` needs to be set during inference, which is specified as "en" in English.

```
self.character_str = "0123456789abcdefghijklmnopqrstuvwxyz"
dict_character = list(self.character_str)
```

tink2123's avatar
tink2123 committed
306
<a name="SRN-BASED_RECOGNITION"></a>
307
### 3.3 SRN-Based Text Recognition Model Inference
tink2123's avatar
tink2123 committed
308
309
310
311
312
313
314
315
316

The recognition model based on SRN requires additional setting of the recognition algorithm parameter
--rec_algorithm="SRN". At the same time, it is necessary to ensure that the predicted shape is consistent
with the training, such as: --rec_image_shape="1, 64, 256"

```
python3 tools/infer/predict_rec.py --image_dir="./doc/imgs_words_en/word_336.png" \
                                    --rec_model_dir="./inference/srn/" \
                                    --rec_image_shape="1, 64, 256" \
tink2123's avatar
tink2123 committed
317
                                    --rec_char_dict_path="./ppocr/utils/ic15_dict.txt" \
tink2123's avatar
tink2123 committed
318
319
320
                                    --rec_algorithm="SRN"
```

licx's avatar
licx committed
321
<a name="USING_CUSTOM_CHARACTERS"></a>
322
### 3.4 Text Recognition Model Inference Using Custom Characters Dictionary
WenmuZhou's avatar
WenmuZhou committed
323
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`, and set `rec_char_type=ch`
LDOUBLEV's avatar
LDOUBLEV committed
324
325

```
tink2123's avatar
tink2123 committed
326
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"
LDOUBLEV's avatar
LDOUBLEV committed
327
328
```

WenmuZhou's avatar
WenmuZhou committed
329
<a name="MULTILINGUAL_MODEL_INFERENCE"></a>
330
331

### 3.5 Multilingual Model Inference
WenmuZhou's avatar
WenmuZhou committed
332
If you need to predict other language models, when using inference model prediction, you need to specify the dictionary path used by `--rec_char_dict_path`. At the same time, in order to get the correct visualization results,
tink2123's avatar
tink2123 committed
333
You need to specify the visual font path through `--vis_font_path`. There are small language fonts provided by default under the `doc/fonts` path, such as Korean recognition:
WenmuZhou's avatar
WenmuZhou committed
334
335

```
tink2123's avatar
tink2123 committed
336
python3 tools/infer/predict_rec.py --image_dir="./doc/imgs_words/korean/1.jpg" --rec_model_dir="./your inference model"  --rec_char_dict_path="ppocr/utils/dict/korean_dict.txt" --vis_font_path="doc/fonts/korean.ttf"
WenmuZhou's avatar
WenmuZhou committed
337
338
339
340
341
342
```
![](../imgs_words/korean/1.jpg)

After executing the command, the prediction result of the above figure is:

``` text
WenmuZhou's avatar
WenmuZhou committed
343
Predicts of ./doc/imgs_words/korean/1.jpg:('바탕으로', 0.9948904)
WenmuZhou's avatar
WenmuZhou committed
344
345
346
```

<a name="ANGLE_CLASSIFICATION_MODEL_INFERENCE"></a>
347
## 4. Angle Classification Model Inference
WenmuZhou's avatar
WenmuZhou committed
348
349
350
351

For angle classification model inference, you can execute the following commands:

```
WenmuZhou's avatar
WenmuZhou committed
352
python3 tools/infer/predict_cls.py --image_dir="./doc/imgs_words_en/word_10.png" --cls_model_dir="./inference/cls/"
WenmuZhou's avatar
WenmuZhou committed
353
```
WenmuZhou's avatar
WenmuZhou committed
354
355
356
357
358
359
```
# download text angle class inference model:
wget  https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar
tar xf ch_ppocr_mobile_v2.0_cls_infer.tar
python3 tools/infer/predict_cls.py --image_dir="./doc/imgs_words_en/word_10.png" --cls_model_dir="ch_ppocr_mobile_v2.0_cls_infer"
```
WenmuZhou's avatar
WenmuZhou committed
360
![](../imgs_words_en/word_10.png)
WenmuZhou's avatar
WenmuZhou committed
361
362
363

After executing the command, the prediction results (classification angle and score) of the above image will be printed on the screen.

WenmuZhou's avatar
WenmuZhou committed
364
```
WenmuZhou's avatar
WenmuZhou committed
365
 Predicts of ./doc/imgs_words_en/word_10.png:['0', 0.9999995]
WenmuZhou's avatar
WenmuZhou committed
366
```
WenmuZhou's avatar
WenmuZhou committed
367

licx's avatar
licx committed
368
<a name="CONCATENATION"></a>
369
## 5. Text Detection Angle Classification and Recognition Inference Concatenation
Khanh Tran's avatar
Khanh Tran committed
370

licx's avatar
licx committed
371
<a name="LIGHTWEIGHT_CHINESE_MODEL"></a>
372
### 5.1 Lightweight Chinese Model
Khanh Tran's avatar
Khanh Tran committed
373

littletomatodonkey's avatar
littletomatodonkey committed
374
When performing prediction, you need to specify the path of a single image or a folder of images through the parameter `image_dir`, the parameter `det_model_dir` specifies the path to detect the inference model, the parameter `cls_model_dir` specifies the path to angle classification inference model and the parameter `rec_model_dir` specifies the path to identify the inference model. The parameter `use_angle_cls` is used to control whether to enable the angle classification model. The parameter `use_mp` specifies whether to use multi-process to infer `total_process_num` specifies process number when using multi-process. The parameter . The visualized recognition results are saved to the `./inference_results` folder by default.
Khanh Tran's avatar
Khanh Tran committed
375

littletomatodonkey's avatar
littletomatodonkey committed
376
```shell
WenmuZhou's avatar
WenmuZhou committed
377
# use direction classifier
WenmuZhou's avatar
WenmuZhou committed
378
python3 tools/infer/predict_system.py --image_dir="./doc/imgs/00018069.jpg" --det_model_dir="./inference/det_db/" --cls_model_dir="./inference/cls/" --rec_model_dir="./inference/rec_crnn/" --use_angle_cls=true
WenmuZhou's avatar
WenmuZhou committed
379
380

# not use use direction classifier
WenmuZhou's avatar
WenmuZhou committed
381
python3 tools/infer/predict_system.py --image_dir="./doc/imgs/00018069.jpg" --det_model_dir="./inference/det_db/" --rec_model_dir="./inference/rec_crnn/"
littletomatodonkey's avatar
littletomatodonkey committed
382
383
384
385

# use multi-process
python3 tools/infer/predict_system.py --image_dir="./doc/imgs/00018069.jpg" --det_model_dir="./inference/det_db/" --rec_model_dir="./inference/rec_crnn/" --use_angle_cls=false --use_mp=True --total_process_num=6
```
386

Khanh Tran's avatar
Khanh Tran committed
387
388
389

After executing the command, the recognition result image is as follows:

WenmuZhou's avatar
WenmuZhou committed
390
![](../imgs_results/system_res_00018069.jpg)
Khanh Tran's avatar
Khanh Tran committed
391

licx's avatar
licx committed
392
<a name="OTHER_MODELS"></a>
393
### 5.2 Other Models
Khanh Tran's avatar
Khanh Tran committed
394

licx's avatar
licx committed
395
396
397
398
399
If you want to try other detection algorithms or recognition algorithms, please refer to the above text detection model inference and text recognition model inference, update the corresponding configuration and model.

**Note: due to the limitation of rotation logic of detected box, SAST curved text detection model (using the parameter `det_sast_polygon=True`) is not supported for model combination yet.**

The following command uses the combination of the EAST text detection and STAR-Net text recognition:
Khanh Tran's avatar
Khanh Tran committed
400
401

```
tink2123's avatar
tink2123 committed
402
python3 tools/infer/predict_system.py --image_dir="./doc/imgs_en/img_10.jpg" --det_model_dir="./inference/det_east/" --det_algorithm="EAST" --rec_model_dir="./inference/starnet/" --rec_image_shape="3, 32, 100" --rec_char_dict_path="./ppocr/utils/ic15_dict.txt"
Khanh Tran's avatar
Khanh Tran committed
403
404
405
406
```

After executing the command, the recognition result image is as follows:

WenmuZhou's avatar
WenmuZhou committed
407
![](../imgs_results/img_10_east_starnet.jpg)