inference.md 13.4 KB
Newer Older
1

2
# Inference based on prediction engine
3

4
5
6
7
inference model (model saved by fluid.io.save_inference_model)
It is generally the solidified model saved after the model training is completed, which is mostly used to predict deployment.
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.
Compared with the checkpoints model, the inference model will additionally save the structural information of the model. It has superior performance in predicting deployment and accelerating reasoning, is flexible and convenient, and is suitable for integration with actual systems. For more detailed introduction, please refer to the document [Classification prediction framework](https://paddleclas.readthedocs.io/zh_CN/latest/extension/paddle_inference.html).
LDOUBLEV's avatar
LDOUBLEV committed
8

9
Next, we first introduce how to convert the trained model into an inference model, and then we will introduce text detection, text recognition, and the connection of the two based on prediction engine inference.
LDOUBLEV's avatar
LDOUBLEV committed
10

11
12
## Training model to inference model
### Detection model to inference model
LDOUBLEV's avatar
LDOUBLEV committed
13

14
Download the super lightweight Chinese detection model:
LDOUBLEV's avatar
LDOUBLEV committed
15
```
dyning's avatar
dyning committed
16
wget -P ./ch_lite/ https://paddleocr.bj.bcebos.com/ch_models/ch_det_mv3_db.tar && tar xf ./ch_lite/ch_det_mv3_db.tar -C ./ch_lite/
LDOUBLEV's avatar
LDOUBLEV committed
17
```
18
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:
LDOUBLEV's avatar
LDOUBLEV committed
19
```
dyning's avatar
dyning committed
20
python3 tools/export_model.py -c configs/det/det_mv3_db.yml -o Global.checkpoints=./ch_lite/det_mv3_db/best_accuracy Global.save_inference_dir=./inference/det_db/
LDOUBLEV's avatar
LDOUBLEV committed
21
```
22
23
24
When transferring 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.checkpoints and Global.save_inference_dir parameters in the configuration file.
Global.checkpoints points to the model parameter file saved in training, and Global.save_inference_dir is the directory where the generated inference model is to be saved.
After the conversion is successful, there are two files in the `save_inference_dir` directory:
LDOUBLEV's avatar
LDOUBLEV committed
25
```
LDOUBLEV's avatar
LDOUBLEV committed
26
inference/det_db/
27
28
  └─  model     Check the program file of inference model
  └─  params    Check the parameter file of the inference model
LDOUBLEV's avatar
LDOUBLEV committed
29
30
```

31
### Recognition model to inference model
LDOUBLEV's avatar
LDOUBLEV committed
32

33
Download the ultra-lightweight Chinese recognition model:
LDOUBLEV's avatar
LDOUBLEV committed
34
```
dyning's avatar
dyning committed
35
wget -P ./ch_lite/ https://paddleocr.bj.bcebos.com/ch_models/ch_rec_mv3_crnn.tar && tar xf ./ch_lite/ch_rec_mv3_crnn.tar -C ./ch_lite/
LDOUBLEV's avatar
LDOUBLEV committed
36
37
```

38
The identification model is converted to the inference model in the same way as the detection, as follows:
LDOUBLEV's avatar
LDOUBLEV committed
39
```
LDOUBLEV's avatar
LDOUBLEV committed
40
41
python3 tools/export_model.py -c configs/rec/rec_chinese_lite_train.yml -o Global.checkpoints=./ch_lite/rec_mv3_crnn/best_accuracy \
        Global.save_inference_dir=./inference/rec_crnn/
LDOUBLEV's avatar
LDOUBLEV committed
42
```
LDOUBLEV's avatar
LDOUBLEV committed
43

44
If you are a model trained on your own data set and you have adjusted the dictionary file of Chinese characters, please pay attention to whether the character_dict_path in the configuration file is the required dictionary file.
LDOUBLEV's avatar
LDOUBLEV committed
45

46
After the conversion is successful, there are two files in the directory:
LDOUBLEV's avatar
LDOUBLEV committed
47
```
LDOUBLEV's avatar
LDOUBLEV committed
48
/inference/rec_crnn/
49
50
  └─  model     Identify the program file of the inference model
  └─  params    Identify the parameter file of the inference model
LDOUBLEV's avatar
LDOUBLEV committed
51
```
52

53
## Text detection model inference
54

55
The following will introduce the ultra-lightweight Chinese detection model reasoning, DB text detection model reasoning and EAST text detection model reasoning. 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.
dyning's avatar
dyning committed
56

57
### 1.Ultra-lightweight Chinese detection model inference
dyning's avatar
dyning committed
58

59
Super lightweight Chinese detection model inference, you can execute the following commands:
60
61

```
LDOUBLEV's avatar
LDOUBLEV committed
62
python3 tools/infer/predict_det.py --image_dir="./doc/imgs/2.jpg" --det_model_dir="./inference/det_db/"
63
64
```

65
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:
dyning's avatar
dyning committed
66
67

![](imgs_results/det_res_2.jpg)
68

69
By setting the size of the parameter det_max_side_len, the maximum value of picture normalization in the detection algorithm is changed. When the length and width of the picture are less than det_max_side_len, the original picture is used for prediction, otherwise the picture is scaled to the maximum value for prediction. This parameter is set to det_max_side_len=960 by default. If the resolution of the input picture is relatively large and you want to use a larger resolution for prediction, you can execute the following command:
70
71

```
LDOUBLEV's avatar
LDOUBLEV committed
72
python3 tools/infer/predict_det.py --image_dir="./doc/imgs/2.jpg" --det_model_dir="./inference/det_db/" --det_max_side_len=1200
dyning's avatar
dyning committed
73
74
```

75
If you want to use the CPU for prediction, execute the command as follows
dyning's avatar
dyning committed
76
```
LDOUBLEV's avatar
LDOUBLEV committed
77
python3 tools/infer/predict_det.py --image_dir="./doc/imgs/2.jpg" --det_model_dir="./inference/det_db/" --use_gpu=False
dyning's avatar
dyning committed
78
79
```

80
### 2.DB text detection model inference
dyning's avatar
dyning committed
81

82
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/det_r50_vd_db.tar)), you can use the following command to convert:
dyning's avatar
dyning committed
83

84
```
85
86
87
# Set the yml configuration file of the training algorithm after -c
# The Global.checkpoints parameter sets the address of the training model to be converted without adding the file suffix .pdmodel, .pdopt or .pdparams.
# The Global.save_inference_dir parameter sets the address where the converted model will be saved.
dyning's avatar
dyning committed
88

dyning's avatar
dyning committed
89
python3 tools/export_model.py -c configs/det/det_r50_vd_db.yml -o Global.checkpoints="./models/det_r50_vd_db/best_accuracy" Global.save_inference_dir="./inference/det_db"
dyning's avatar
dyning committed
90
91
```

92
DB text detection model inference, you can execute the following command:
dyning's avatar
dyning committed
93
94
95
96
97

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

98
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:
dyning's avatar
dyning committed
99
100
101

![](imgs_results/det_res_img_10_db.jpg)

102
**Note**: Since the ICDAR2015 dataset has only 1,000 training images, mainly for English scenes, the above model has very poor detection effect on Chinese text images.
dyning's avatar
dyning committed
103

104
### 3.EAST text detection model inference
dyning's avatar
dyning committed
105

106
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 data set as an example ([model download link](https://paddleocr.bj.bcebos.com/det_r50_vd_east.tar)), you can use the following command to convert:
dyning's avatar
dyning committed
107
108

```
109
110
111
# Set the yml configuration file of the training algorithm after -c
# The Global.checkpoints parameter sets the address of the training model to be converted without adding the file suffix .pdmodel, .pdopt or .pdparams.
# The Global.save_inference_dir parameter sets the address where the converted model will be saved.
dyning's avatar
dyning committed
112

dyning's avatar
dyning committed
113
python3 tools/export_model.py -c configs/det/det_r50_vd_east.yml -o Global.checkpoints="./models/det_r50_vd_east/best_accuracy" Global.save_inference_dir="./inference/det_east"
dyning's avatar
dyning committed
114
115
```

116
EAST text detection model inference, you need to set the parameter det_algorithm, specify the detection algorithm type as EAST, you can execute the following command:
dyning's avatar
dyning committed
117
118
119
120

```
python3 tools/infer/predict_det.py --image_dir="./doc/imgs_en/img_10.jpg" --det_model_dir="./inference/det_east/" --det_algorithm="EAST"
```
121
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:
dyning's avatar
dyning committed
122
123
124

![](imgs_results/det_res_img_10_east.jpg)

125
**Note**: The Python version of NMS used in EAST post-processing in this codebase, so the prediction speed is time-consuming. If you use the C++ version, there will be a significant speedup.
126
127


128
## Text recognition model inference
129

130
The following will introduce the ultra-lightweight Chinese recognition model reasoning and CTC loss-based recognition model reasoning. **The recognition model reasoning based on Attention loss is still being debugged**. For Chinese text recognition, it is recommended to prefer the recognition model based on CTC loss. In practice, it is also found that the effect based on Attention loss is not as good as the recognition model based on CTC loss.
dyning's avatar
dyning committed
131
132


133
### 1.Ultra-lightweight Chinese recognition model inference
dyning's avatar
dyning committed
134

135
Super lightweight Chinese recognition model inference, you can execute the following commands:
dyning's avatar
dyning committed
136
137

```
LDOUBLEV's avatar
LDOUBLEV committed
138
python3 tools/infer/predict_rec.py --image_dir="./doc/imgs_words/ch/word_4.jpg" --rec_model_dir="./inference/rec_crnn/"
dyning's avatar
dyning committed
139
140
```

tink2123's avatar
tink2123 committed
141
![](imgs_words/ch/word_4.jpg)
dyning's avatar
dyning committed
142

143
After executing the command, the prediction results (recognized text and score) of the above image will be printed on the screen.
dyning's avatar
dyning committed
144

tink2123's avatar
tink2123 committed
145
Predicts of ./doc/imgs_words/ch/word_4.jpg:['实力活力', 0.89552695]
dyning's avatar
dyning committed
146
147


148
### 2.Identification model reasoning based on CTC loss
dyning's avatar
dyning committed
149

150
Taking STAR-Net as an example, we introduce the identification model reasoning based on CTC loss. CRNN and Rosetta are used in a similar way, without setting the recognition algorithm parameter rec_algorithm.
dyning's avatar
dyning committed
151

152
153
First, convert the model saved in the STAR-Net text recognition training process into an inference model. Based on Resnet34_vd backbone network, using MJSynth and SynthText two English text recognition synthetic data set training
The example of the model ([model download address](https://paddleocr.bj.bcebos.com/rec_r34_vd_tps_bilstm_ctc.tar))
dyning's avatar
dyning committed
154
155

```
156
157
158
# Set the yml configuration file of the training algorithm after -c
# The Global.checkpoints parameter sets the address of the training model to be converted without adding the file suffix .pdmodel, .pdopt or .pdparams.
# The Global.save_inference_dir parameter sets the address where the converted model will be saved.
dyning's avatar
dyning committed
159
160
161
162

python3 tools/export_model.py -c configs/rec/rec_r34_vd_tps_bilstm_ctc.yml -o Global.checkpoints="./models/rec_r34_vd_tps_bilstm_ctc/best_accuracy" Global.save_inference_dir="./inference/starnet"
```

163
STAR-Net text recognition model inference can execute the following commands:
164
165

```
dyning's avatar
dyning committed
166
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_type="en"
167
```
dyning's avatar
dyning committed
168
![](imgs_words_en/word_336.png)
dyning's avatar
dyning committed
169

170
After executing the command, the recognition result of the above image is as follows:
dyning's avatar
dyning committed
171

dyning's avatar
dyning committed
172
Predicts of ./doc/imgs_words_en/word_336.png:['super', 0.9999555]
dyning's avatar
dyning committed
173

174
**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 ultra-lightweight Chinese recognition model in two aspects:
175

176
- The image resolution used in training is different, and the image resolution used in training the above model is [3,32,100], While the 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 predictive inference program is the image resolution used in training Chinese, that is [3, 32, 320]. Therefore, when reasoning the above English model here, you need to set the shape of the recognition image through the parameter rec_image_shape.
177

178
- Character list, the experiment in the DTRB paper is only for 26 lowercase English mothers 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 character dictionary is entered here, but a dictionary is generated by the following command. Therefore, the parameter rec_char_type needs to be set during inference, which is specified as "en" in English.
179
180

```
dyning's avatar
dyning committed
181
182
self.character_str = "0123456789abcdefghijklmnopqrstuvwxyz"
dict_character = list(self.character_str)
183
184
```

185
## Text detection, recognition tandem reasoning
186

187
### 1.Ultra-lightweight Chinese OCR model reasoning
dyning's avatar
dyning committed
188

189
When performing prediction, you need to specify the path of a single image or a collection of images through the parameter image_dir, the parameter det_model_dir specifies the path to detect the inference model, and the parameter rec_model_dir specifies the path to identify the inference model. The visual recognition results are saved to the ./inference_results folder by default.
dyning's avatar
dyning committed
190

191
```
LDOUBLEV's avatar
LDOUBLEV committed
192
python3 tools/infer/predict_system.py --image_dir="./doc/imgs/2.jpg" --det_model_dir="./inference/det_db/"  --rec_model_dir="./inference/rec_crnn/"
193
194
```

195
After executing the command, the recognition result image is as follows:
dyning's avatar
dyning committed
196
197
198

![](imgs_results/2.jpg)

199
### 2.Other model reasoning
dyning's avatar
dyning committed
200

201
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, the following gives the EAST text detection and STAR-Net text recognition execution commands:
202
203

```
dyning's avatar
dyning committed
204
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_type="en"
205
```
dyning's avatar
dyning committed
206

207
After executing the command, the recognition result image is as follows:
dyning's avatar
dyning committed
208

dyning's avatar
dyning committed
209
![](imgs_results/img_10.jpg)