inference_en.md 25.1 KB
Newer Older
K
Khanh Tran 已提交
1

2
# Inference Based on Python Prediction Engine
K
Khanh Tran 已提交
3

W
WenmuZhou 已提交
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.
K
Khanh Tran 已提交
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.

L
LDOUBLEV 已提交
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.
L
LDOUBLEV 已提交
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).
K
Khanh Tran 已提交
10

W
WenmuZhou 已提交
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.
K
Khanh Tran 已提交
12

Z
zhoujun 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
- [Inference Based on Python Prediction Engine](#inference-based-on-python-prediction-engine)
  - [1. Convert Training Model to Inference Model](#1-convert-training-model-to-inference-model)
    - [1.1 Convert Detection Model to Inference Model](#11-convert-detection-model-to-inference-model)
    - [1.2 Convert Recognition Model to Inference Model](#12-convert-recognition-model-to-inference-model)
    - [1.3 Convert Angle Classification Model to Inference Model](#13-convert-angle-classification-model-to-inference-model)
  - [2. Text Detection Model Inference](#2-text-detection-model-inference)
    - [2.1 Lightweight Chinese Detection Model Inference](#21-lightweight-chinese-detection-model-inference)
    - [2.2 DB Text Detection Model Inference](#22-db-text-detection-model-inference)
    - [2.3 EAST TEXT DETECTION MODEL INFERENCE](#23-east-text-detection-model-inference)
    - [2.4 Sast Text Detection Model Inference](#24-sast-text-detection-model-inference)
      - [(1). Quadrangle text detection model (ICDAR2015)](#1-quadrangle-text-detection-model-icdar2015)
      - [(2). Curved text detection model (Total-Text)](#2-curved-text-detection-model-total-text)
  - [3. Text Recognition Model Inference](#3-text-recognition-model-inference)
    - [3.1 Lightweight Chinese Text Recognition Model Reference](#31-lightweight-chinese-text-recognition-model-reference)
    - [3.2 CTC-Based Text Recognition Model Inference](#32-ctc-based-text-recognition-model-inference)
    - [3.3 SRN-Based Text Recognition Model Inference](#33-srn-based-text-recognition-model-inference)
    - [3.4 Text Recognition Model Inference Using Custom Characters Dictionary](#34-text-recognition-model-inference-using-custom-characters-dictionary)
    - [3.5 Multilingual Model Inference](#35-multilingual-model-inference)
  - [4. Angle Classification Model Inference](#4-angle-classification-model-inference)
  - [5. Text Detection Angle Classification and Recognition Inference Concatenation](#5-text-detection-angle-classification-and-recognition-inference-concatenation)
    - [5.1 Lightweight Chinese Model](#51-lightweight-chinese-model)
    - [5.2 Other Models](#52-other-models)
W
WenmuZhou 已提交
35

L
licx 已提交
36
<a name="CONVERT"></a>
37
## 1. Convert Training Model to Inference Model
L
licx 已提交
38
<a name="Convert_detection_model"></a>
39 40

### 1.1 Convert Detection Model to Inference Model
K
Khanh Tran 已提交
41

X
xxxpsyduck 已提交
42
Download the lightweight Chinese detection model:
K
Khanh Tran 已提交
43
```
W
WenmuZhou 已提交
44
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/
K
Khanh Tran 已提交
45
```
W
WenmuZhou 已提交
46

K
Khanh Tran 已提交
47 48
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:
```
W
WenmuZhou 已提交
49 50
# -c Set the training algorithm yml configuration file
# -o Set optional parameters
W
WenmuZhou 已提交
51
# Global.pretrained_model parameter Set the training model address to be converted without adding the file suffix .pdmodel, .pdopt or .pdparams.
W
WenmuZhou 已提交
52
# Global.save_inference_dir Set the address where the converted model will be saved.
T
tink2123 已提交
53

54
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/
K
Khanh Tran 已提交
55
```
W
WenmuZhou 已提交
56

W
WenmuZhou 已提交
57
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.
W
WenmuZhou 已提交
58
After the conversion is successful, there are three files in the model save directory:
K
Khanh Tran 已提交
59 60
```
inference/det_db/
61 62 63
    ├── 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
K
Khanh Tran 已提交
64 65
```

L
licx 已提交
66
<a name="Convert_recognition_model"></a>
67
### 1.2 Convert Recognition Model to Inference Model
K
Khanh Tran 已提交
68

X
xxxpsyduck 已提交
69
Download the lightweight Chinese recognition model:
K
Khanh Tran 已提交
70
```
W
WenmuZhou 已提交
71
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/
K
Khanh Tran 已提交
72 73 74 75
```

The recognition model is converted to the inference model in the same way as the detection, as follows:
```
W
WenmuZhou 已提交
76 77
# -c Set the training algorithm yml configuration file
# -o Set optional parameters
W
WenmuZhou 已提交
78
# Global.pretrained_model parameter Set the training model address to be converted without adding the file suffix .pdmodel, .pdopt or .pdparams.
W
WenmuZhou 已提交
79
# Global.save_inference_dir Set the address where the converted model will be saved.
T
tink2123 已提交
80

81
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/
K
Khanh Tran 已提交
82 83 84 85
```

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.

W
WenmuZhou 已提交
86
After the conversion is successful, there are three files in the model save directory:
K
Khanh Tran 已提交
87
```
W
WenmuZhou 已提交
88
inference/det_db/
89 90 91
    ├── 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
K
Khanh Tran 已提交
92 93
```

W
WenmuZhou 已提交
94
<a name="Convert_angle_class_model"></a>
95
### 1.3 Convert Angle Classification Model to Inference Model
W
WenmuZhou 已提交
96 97 98

Download the angle classification model:
```
W
WenmuZhou 已提交
99
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/
W
WenmuZhou 已提交
100 101 102 103
```

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

109
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/
W
WenmuZhou 已提交
110 111 112 113
```

After the conversion is successful, there are two files in the directory:
```
W
WenmuZhou 已提交
114
inference/det_db/
115 116 117
    ├── 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
W
WenmuZhou 已提交
118 119 120
```


L
licx 已提交
121
<a name="DETECTION_MODEL_INFERENCE"></a>
122
## 2. Text Detection Model Inference
K
Khanh Tran 已提交
123

T
tink2123 已提交
124 125
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**.
K
Khanh Tran 已提交
126

L
licx 已提交
127
<a name="LIGHTWEIGHT_DETECTION"></a>
128
### 2.1 Lightweight Chinese Detection Model Inference
K
Khanh Tran 已提交
129

X
xxxpsyduck 已提交
130
For lightweight Chinese detection model inference, you can execute the following commands:
K
Khanh Tran 已提交
131 132

```
L
LDOUBLEV 已提交
133 134 135 136
# 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
L
LDOUBLEV 已提交
137
python3 tools/infer/predict_det.py --image_dir="./doc/imgs/00018069.jpg" --det_model_dir="./inference/det_db/"
K
Khanh Tran 已提交
138 139
```

fanruinet's avatar
fanruinet 已提交
140
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:
K
Khanh Tran 已提交
141

L
LDOUBLEV 已提交
142
![](../imgs_results/det_res_00018069.jpg)
K
Khanh Tran 已提交
143

L
LDOUBLEV 已提交
144
You can use the parameters `limit_type` and `det_limit_side_len` to limit the size of the input image,
M
MissPenguin 已提交
145
The optional parameters of `limit_type` are [`max`, `min`], and
L
LDOUBLEV 已提交
146
`det_limit_size_len` is a positive integer, generally set to a multiple of 32, such as 960.
K
Khanh Tran 已提交
147

L
LDOUBLEV 已提交
148 149 150 151 152
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:
K
Khanh Tran 已提交
153
```
W
WenmuZhou 已提交
154
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
K
Khanh Tran 已提交
155 156 157 158
```

If you want to use the CPU for prediction, execute the command as follows
```
W
WenmuZhou 已提交
159
python3 tools/infer/predict_det.py --image_dir="./doc/imgs/1.jpg" --det_model_dir="./inference/det_db/" --use_gpu=False
K
Khanh Tran 已提交
160 161
```

L
licx 已提交
162
<a name="DB_DETECTION"></a>
163
### 2.2 DB Text Detection Model Inference
K
Khanh Tran 已提交
164

W
WenmuZhou 已提交
165
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:
K
Khanh Tran 已提交
166 167

```
168
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
K
Khanh Tran 已提交
169 170 171 172 173 174 175 176 177 178
```

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:

179
![](../imgs_results/det_res_img_10_db.jpg)
K
Khanh Tran 已提交
180 181 182

**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.

L
licx 已提交
183
<a name="EAST_DETECTION"></a>
184
### 2.3 EAST TEXT DETECTION MODEL INFERENCE
K
Khanh Tran 已提交
185

M
MissPenguin 已提交
186
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:
K
Khanh Tran 已提交
187 188

```
189
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
K
Khanh Tran 已提交
190
```
L
licx 已提交
191
**For EAST text detection model inference, you need to set the parameter ``--det_algorithm="EAST"``**, run the following command:
K
Khanh Tran 已提交
192 193 194 195

```
python3 tools/infer/predict_det.py --image_dir="./doc/imgs_en/img_10.jpg" --det_model_dir="./inference/det_east/" --det_algorithm="EAST"
```
L
licx 已提交
196

K
Khanh Tran 已提交
197 198
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:

M
MissPenguin 已提交
199
![](../imgs_results/det_res_img_10_east.jpg)
K
Khanh Tran 已提交
200

L
licx 已提交
201 202 203 204
**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>
205
### 2.4 Sast Text Detection Model Inference
L
licx 已提交
206
#### (1). Quadrangle text detection model (ICDAR2015)  
M
MissPenguin 已提交
207
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:
L
licx 已提交
208 209

```
210
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
L
licx 已提交
211 212 213
```

**For SAST quadrangle text detection model inference, you need to set the parameter `--det_algorithm="SAST"`**, run the following command:
K
Khanh Tran 已提交
214

L
licx 已提交
215 216 217 218 219
```
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:
K
Khanh Tran 已提交
220

M
MissPenguin 已提交
221
![](../imgs_results/det_res_img_10_sast.jpg)
L
licx 已提交
222 223

#### (2). Curved text detection model (Total-Text)  
M
MissPenguin 已提交
224
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:
L
licx 已提交
225 226

```
227
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
L
licx 已提交
228 229
```

W
opt doc  
WenmuZhou 已提交
230
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:
L
licx 已提交
231 232 233 234 235 236 237

```
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:

M
MissPenguin 已提交
238
![](../imgs_results/det_res_img623_sast.jpg)
L
licx 已提交
239 240 241 242

**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>
243
## 3. Text Recognition Model Inference
K
Khanh Tran 已提交
244

fanruinet's avatar
fanruinet 已提交
245
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.
K
Khanh Tran 已提交
246 247


L
licx 已提交
248
<a name="LIGHTWEIGHT_RECOGNITION"></a>
249
### 3.1 Lightweight Chinese Text Recognition Model Reference
K
Khanh Tran 已提交
250

X
xxxpsyduck 已提交
251
For lightweight Chinese recognition model inference, you can execute the following commands:
K
Khanh Tran 已提交
252 253

```
W
WenmuZhou 已提交
254 255 256 257
# 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"
K
Khanh Tran 已提交
258 259
```

W
WenmuZhou 已提交
260
![](../imgs_words_en/word_10.png)
K
Khanh Tran 已提交
261 262 263

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

W
WenmuZhou 已提交
264
```bash
W
WenmuZhou 已提交
265
Predicts of ./doc/imgs_words_en/word_10.png:('PAIN', 0.9897658)
W
WenmuZhou 已提交
266
```
K
Khanh Tran 已提交
267

L
licx 已提交
268
<a name="CTC-BASED_RECOGNITION"></a>
269
### 3.2 CTC-Based Text Recognition Model Inference
K
Khanh Tran 已提交
270

W
WenmuZhou 已提交
271
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.
K
Khanh Tran 已提交
272

W
WenmuZhou 已提交
273
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:
K
Khanh Tran 已提交
274 275

```
276
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
K
Khanh Tran 已提交
277 278
```

W
WenmuZhou 已提交
279
For CRNN text recognition model inference, execute the following commands:
K
Khanh Tran 已提交
280 281

```
T
tink2123 已提交
282
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"
K
Khanh Tran 已提交
283
```
X
xxxpsyduck 已提交
284

W
WenmuZhou 已提交
285
![](../imgs_words_en/word_336.png)
K
Khanh Tran 已提交
286

W
WenmuZhou 已提交
287 288 289 290 291
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)
```
292

X
xxxpsyduck 已提交
293
**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:
K
Khanh Tran 已提交
294 295 296

- 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`.

文幕地方's avatar
文幕地方 已提交
297
- 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.
K
Khanh Tran 已提交
298 299 300 301 302 303

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

T
tink2123 已提交
304
<a name="SRN-BASED_RECOGNITION"></a>
305
### 3.3 SRN-Based Text Recognition Model Inference
T
tink2123 已提交
306 307 308 309 310 311 312 313 314

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" \
T
tink2123 已提交
315
                                    --rec_char_dict_path="./ppocr/utils/ic15_dict.txt" \
T
tink2123 已提交
316 317 318
                                    --rec_algorithm="SRN"
```

L
licx 已提交
319
<a name="USING_CUSTOM_CHARACTERS"></a>
320
### 3.4 Text Recognition Model Inference Using Custom Characters Dictionary
文幕地方's avatar
文幕地方 已提交
321
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`
L
LDOUBLEV 已提交
322 323

```
T
tink2123 已提交
324
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"
L
LDOUBLEV 已提交
325 326
```

W
WenmuZhou 已提交
327
<a name="MULTILINGUAL_MODEL_INFERENCE"></a>
328 329

### 3.5 Multilingual Model Inference
W
WenmuZhou 已提交
330
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,
T
tink2123 已提交
331
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:
W
WenmuZhou 已提交
332 333

```
T
tink2123 已提交
334
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"
W
WenmuZhou 已提交
335 336 337 338 339 340
```
![](../imgs_words/korean/1.jpg)

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

``` text
W
WenmuZhou 已提交
341
Predicts of ./doc/imgs_words/korean/1.jpg:('바탕으로', 0.9948904)
W
WenmuZhou 已提交
342 343 344
```

<a name="ANGLE_CLASSIFICATION_MODEL_INFERENCE"></a>
345
## 4. Angle Classification Model Inference
W
WenmuZhou 已提交
346 347 348 349

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

```
W
WenmuZhou 已提交
350
python3 tools/infer/predict_cls.py --image_dir="./doc/imgs_words_en/word_10.png" --cls_model_dir="./inference/cls/"
W
WenmuZhou 已提交
351
```
W
WenmuZhou 已提交
352 353 354 355 356 357
```
# 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"
```
W
WenmuZhou 已提交
358
![](../imgs_words_en/word_10.png)
W
WenmuZhou 已提交
359 360 361

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

W
WenmuZhou 已提交
362
```
W
WenmuZhou 已提交
363
 Predicts of ./doc/imgs_words_en/word_10.png:['0', 0.9999995]
W
WenmuZhou 已提交
364
```
W
WenmuZhou 已提交
365

L
licx 已提交
366
<a name="CONCATENATION"></a>
367
## 5. Text Detection Angle Classification and Recognition Inference Concatenation
K
Khanh Tran 已提交
368

L
licx 已提交
369
<a name="LIGHTWEIGHT_CHINESE_MODEL"></a>
370
### 5.1 Lightweight Chinese Model
K
Khanh Tran 已提交
371

Z
zhoujun 已提交
372
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(Paddle Inference is not thread-safe, it is recommended to use multi-process) . The visualized recognition results are saved to the `./inference_results` folder by default.
K
Khanh Tran 已提交
373

littletomatodonkey's avatar
littletomatodonkey 已提交
374
```shell
W
WenmuZhou 已提交
375
# use direction classifier
W
WenmuZhou 已提交
376
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
W
WenmuZhou 已提交
377 378

# not use use direction classifier
W
WenmuZhou 已提交
379
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 已提交
380 381 382 383

# 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
```
384

K
Khanh Tran 已提交
385 386 387

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

W
WenmuZhou 已提交
388
![](../imgs_results/system_res_00018069.jpg)
K
Khanh Tran 已提交
389

L
licx 已提交
390
<a name="OTHER_MODELS"></a>
391
### 5.2 Other Models
K
Khanh Tran 已提交
392

L
licx 已提交
393 394 395 396 397
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:
K
Khanh Tran 已提交
398 399

```
T
tink2123 已提交
400
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"
K
Khanh Tran 已提交
401 402 403 404
```

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

W
WenmuZhou 已提交
405
![](../imgs_results/img_10_east_starnet.jpg)