readme.md 16.7 KB
Newer Older
M
MissPenguin 已提交
1 2 3
# Mobile deployment based on Paddle-Lite

- [1. Preparation](#1-preparation)
M
update  
MissPenguin 已提交
4 5 6
    - [Preparation environment](#preparation-environment)
    - [1.1 Prepare the cross-compilation environment](#11-prepare-the-cross-compilation-environment)
    - [1.2 Prepare Paddle-Lite library](#12-prepare-paddle-lite-library)
M
MissPenguin 已提交
7
- [2. Run](#2-run)
M
update  
MissPenguin 已提交
8 9
    - [2.1 Inference Model Optimization](#21-inference-model-optimization)
    - [2.2 Run optimized model on Phone](#22-run-optimized-model-on-phone)
M
MissPenguin 已提交
10
- [FAQ](#faq)
W
WenmuZhou 已提交
11 12


M
MissPenguin 已提交
13
This tutorial will introduce how to use [Paddle-Lite](https://github.com/PaddlePaddle/Paddle-Lite) to deploy PaddleOCR ultra-lightweight Chinese and English detection models on mobile phones.
W
WenmuZhou 已提交
14

M
MissPenguin 已提交
15
Paddle-Lite is a lightweight inference engine for PaddlePaddle. It provides efficient inference capabilities for mobile phones and IoT, and extensively integrates cross-platform hardware to provide lightweight deployment solutions for end-side deployment issues.
W
WenmuZhou 已提交
16

M
update  
MissPenguin 已提交
17
## 1. Preparation
W
WenmuZhou 已提交
18

M
update  
MissPenguin 已提交
19
### Preparation environment
W
WenmuZhou 已提交
20

M
update  
MissPenguin 已提交
21 22 23 24 25 26 27 28
- Computer (for Compiling Paddle Lite)
- Mobile phone (arm7 or arm8)

### 1.1 Prepare the cross-compilation environment
The cross-compilation environment is used to compile C++ demos of Paddle Lite and PaddleOCR.
Supports multiple development environments.

For the compilation process of different development environments, please refer to the corresponding documents.
W
WenmuZhou 已提交
29 30 31 32 33

1. [Docker](https://paddle-lite.readthedocs.io/zh/latest/source_compile/compile_env.html#docker)
2. [Linux](https://paddle-lite.readthedocs.io/zh/latest/source_compile/compile_env.html#linux)
3. [MAC OS](https://paddle-lite.readthedocs.io/zh/latest/source_compile/compile_env.html#mac-os)

M
update  
MissPenguin 已提交
34
### 1.2 Prepare Paddle-Lite library
W
WenmuZhou 已提交
35

M
update  
MissPenguin 已提交
36
There are two ways to obtain the Paddle-Lite library:
L
LDOUBLEV 已提交
37
- 1. [Recommended] Download directly, the download link of the Paddle-Lite library is as follows:
W
WenmuZhou 已提交
38

M
update  
MissPenguin 已提交
39
      | Platform | Paddle-Lite library download link |
W
WenmuZhou 已提交
40
      |---|---|
W
WenmuZhou 已提交
41 42
      |Android|[arm7](https://github.com/PaddlePaddle/Paddle-Lite/releases/download/v2.10/inference_lite_lib.android.armv7.gcc.c++_shared.with_extra.with_cv.tar.gz) / [arm8](https://github.com/PaddlePaddle/Paddle-Lite/releases/download/v2.10/inference_lite_lib.android.armv8.gcc.c++_shared.with_extra.with_cv.tar.gz)|
      |IOS|[arm7](https://github.com/PaddlePaddle/Paddle-Lite/releases/download/v2.10/inference_lite_lib.ios.armv7.with_cv.with_extra.with_log.tiny_publish.tar.gz) / [arm8](https://github.com/PaddlePaddle/Paddle-Lite/releases/download/v2.10/inference_lite_lib.ios.armv8.with_cv.with_extra.with_log.tiny_publish.tar.gz)|
W
WenmuZhou 已提交
43

M
update  
MissPenguin 已提交
44
      Note: 1. The above Paddle-Lite library is compiled from the Paddle-Lite 2.10 branch. For more information about Paddle-Lite 2.10, please refer to [link](https://github.com/PaddlePaddle/Paddle-Lite/releases/tag/v2.10).
W
WenmuZhou 已提交
45

L
LDOUBLEV 已提交
46 47 48
    **Note: It is recommended to use paddlelite>=2.10 version of the prediction library, other prediction library versions [download link](https://github.com/PaddlePaddle/Paddle-Lite/tags)**

- 2. Compile Paddle-Lite to get the prediction library. The compilation method of Paddle-Lite is as follows:
W
WenmuZhou 已提交
49 50 51
```
git clone https://github.com/PaddlePaddle/Paddle-Lite.git
cd Paddle-Lite
M
update  
MissPenguin 已提交
52
# Switch to Paddle-Lite release/v2.10 stable branch
W
WenmuZhou 已提交
53
git checkout release/v2.10
W
WenmuZhou 已提交
54 55 56
./lite/tools/build_android.sh  --arch=armv8  --with_cv=ON --with_extra=ON
```

M
update  
MissPenguin 已提交
57 58 59
Note: When compiling Paddle-Lite to obtain the Paddle-Lite library, you need to turn on the two options `--with_cv=ON --with_extra=ON`, `--arch` means the `arm` version, here is designated as armv8,

More compilation commands refer to the introduction [link](https://paddle-lite.readthedocs.io/zh/release-v2.10_a/source_compile/linux_x86_compile_android.html)
W
WenmuZhou 已提交
60

M
update  
MissPenguin 已提交
61 62 63 64
After directly downloading the Paddle-Lite library and decompressing it, you can get the `inference_lite_lib.android.armv8/` folder, and the Paddle-Lite library obtained by compiling Paddle-Lite is located
`Paddle-Lite/build.lite.android.armv8.gcc/inference_lite_lib.android.armv8/` folder.

The structure of the prediction library is as follows:
W
WenmuZhou 已提交
65 66
```
inference_lite_lib.android.armv8/
M
update  
MissPenguin 已提交
67 68
|-- cxx                                        C++ prebuild library
|   |-- include                                C++
W
WenmuZhou 已提交
69 70 71 72 73 74 75
|   |   |-- paddle_api.h
|   |   |-- paddle_image_preprocess.h
|   |   |-- paddle_lite_factory_helper.h
|   |   |-- paddle_place.h
|   |   |-- paddle_use_kernels.h
|   |   |-- paddle_use_ops.h
|   |   `-- paddle_use_passes.h
M
update  
MissPenguin 已提交
76 77 78 79
|   `-- lib                                           C++ library
|       |-- libpaddle_api_light_bundled.a             C++ static library
|       `-- libpaddle_light_api_shared.so             C++ dynamic library
|-- java                                     Java library
W
WenmuZhou 已提交
80 81 82 83 84
|   |-- jar
|   |   `-- PaddlePredictor.jar
|   |-- so
|   |   `-- libpaddle_lite_jni.so
|   `-- src
M
update  
MissPenguin 已提交
85 86 87
|-- demo                                     C++ and Java demo
|   |-- cxx                                  C++ demo
|   `-- java                                 Java demo
W
WenmuZhou 已提交
88 89
```

M
update  
MissPenguin 已提交
90
## 2 Run
W
WenmuZhou 已提交
91

M
update  
MissPenguin 已提交
92
### 2.1 Inference Model Optimization
W
WenmuZhou 已提交
93

M
update  
MissPenguin 已提交
94
Paddle Lite provides a variety of strategies to automatically optimize the original training model, including quantization, sub-graph fusion, hybrid scheduling, Kernel optimization and so on. In order to make the optimization process more convenient and easy to use, Paddle Lite provide opt tools to automatically complete the optimization steps and output a lightweight, optimal executable model.
W
WenmuZhou 已提交
95

M
update  
MissPenguin 已提交
96
If you have prepared the model file ending in .nb, you can skip this step.
W
WenmuZhou 已提交
97

M
update  
MissPenguin 已提交
98
The following table also provides a series of models that can be deployed on mobile phones to recognize Chinese. You can directly download the optimized model.
W
WenmuZhou 已提交
99

M
update  
MissPenguin 已提交
100
|Version|Introduction|Model size|Detection model|Text Direction model|Recognition model|Paddle-Lite branch|
W
WenmuZhou 已提交
101
|---|---|---|---|---|---|---|
M
update  
MissPenguin 已提交
102 103
|PP-OCRv2|extra-lightweight chinese OCR optimized model|11M|[download link](https://paddleocr.bj.bcebos.com/PP-OCRv2/lite/ch_PP-OCRv2_det_infer_opt.nb)|[download link](https://paddleocr.bj.bcebos.com/PP-OCRv2/lite/ch_ppocr_mobile_v2.0_cls_infer_opt.nb)|[download link](https://paddleocr.bj.bcebos.com/PP-OCRv2/lite/ch_PP-OCRv2_rec_infer_opt.nb)|v2.10|
|PP-OCRv2(slim)|extra-lightweight chinese OCR optimized model|4.6M|[download link](https://paddleocr.bj.bcebos.com/PP-OCRv2/lite/ch_PP-OCRv2_det_slim_opt.nb)|[download link](https://paddleocr.bj.bcebos.com/PP-OCRv2/lite/ch_ppocr_mobile_v2.0_cls_slim_opt.nb)|[download link](https://paddleocr.bj.bcebos.com/PP-OCRv2/lite/ch_PP-OCRv2_rec_slim_opt.nb)|v2.10|
W
WenmuZhou 已提交
104

M
update  
MissPenguin 已提交
105
If you directly use the model in the above table for deployment, you can skip the following steps and directly read [Section 2.2](#2.2-Run-optimized-model-on-Phone).
W
WenmuZhou 已提交
106

M
update  
MissPenguin 已提交
107
If the model to be deployed is not in the above table, you need to follow the steps below to obtain the optimized model.
W
WenmuZhou 已提交
108

L
LDOUBLEV 已提交
109
- Step 1: Refer to [document](https://www.paddlepaddle.org.cn/lite/v2.10/user_guides/opt/opt_python.html) to install paddlelite, which is used to convert paddle inference model to paddlelite required for running nb model
W
WenmuZhou 已提交
110
```
L
LDOUBLEV 已提交
111
pip install paddlelite==2.10 # The paddlelite version should be the same as the prediction library version
W
WenmuZhou 已提交
112
```
L
LDOUBLEV 已提交
113
After installation, the following commands can view the help information
W
WenmuZhou 已提交
114
```
L
LDOUBLEV 已提交
115
paddle_lite_opt
W
WenmuZhou 已提交
116 117
```

L
LDOUBLEV 已提交
118 119
Introduction to paddle_lite_opt parameters:

M
update  
MissPenguin 已提交
120
|Options|Description|
W
WenmuZhou 已提交
121
|---|---|
M
update  
MissPenguin 已提交
122 123 124 125 126 127 128
|--model_dir|The path of the PaddlePaddle model to be optimized (non-combined form)|
|--model_file|The network structure file path of the PaddlePaddle model (combined form) to be optimized|
|--param_file|The weight file path of the PaddlePaddle model (combined form) to be optimized|
|--optimize_out_type|Output model type, currently supports two types: protobuf and naive_buffer, among which naive_buffer is a more lightweight serialization/deserialization implementation. If you need to perform model prediction on the mobile side, please set this option to naive_buffer. The default is protobuf|
|--optimize_out|The output path of the optimized model|
|--valid_targets|The executable backend of the model, the default is arm. Currently it supports x86, arm, opencl, npu, xpu, multiple backends can be specified at the same time (separated by spaces), and Model Optimize Tool will automatically select the best method. If you need to support Huawei NPU (DaVinci architecture NPU equipped with Kirin 810/990 Soc), it should be set to npu, arm|
|--record_tailoring_info|When using the function of cutting library files according to the model, set this option to true to record the kernel and OP information contained in the optimized model. The default is false|
W
WenmuZhou 已提交
129

M
update  
MissPenguin 已提交
130
`--model_dir` is suitable for the non-combined mode of the model to be optimized, and the inference model of PaddleOCR is the combined mode, that is, the model structure and model parameters are stored in a single file.
W
WenmuZhou 已提交
131

L
LDOUBLEV 已提交
132 133
- Step 2: Use paddle_lite_opt to convert the inference model to the mobile model format.

M
update  
MissPenguin 已提交
134
The following takes the ultra-lightweight Chinese model of PaddleOCR as an example to introduce the use of the compiled opt file to complete the conversion of the inference model to the Paddle-Lite optimized model
W
WenmuZhou 已提交
135 136

```
M
update  
MissPenguin 已提交
137
# 【[Recommendation] Download the Chinese and English inference model of PP-OCRv2
W
WenmuZhou 已提交
138 139
wget  https://paddleocr.bj.bcebos.com/PP-OCRv2/chinese/ch_PP-OCRv2_det_slim_quant_infer.tar && tar xf  ch_PP-OCRv2_det_slim_quant_infer.tar
wget  https://paddleocr.bj.bcebos.com/PP-OCRv2/chinese/ch_PP-OCRv2_rec_slim_quant_infer.tar && tar xf  ch_PP-OCRv2_rec_slim_quant_infer.tar
W
WenmuZhou 已提交
140
wget  https://paddleocr.bj.bcebos.com/dygraph_v2.0/slim/ch_ppocr_mobile_v2.0_cls_slim_infer.tar && tar xf  ch_ppocr_mobile_v2.0_cls_slim_infer.tar
M
update  
MissPenguin 已提交
141
# Convert detection model
W
WenmuZhou 已提交
142
./opt --model_file=./ch_PP-OCRv2_det_slim_quant_infer/inference.pdmodel  --param_file=./ch_PP-OCRv2_det_slim_quant_infer/inference.pdiparams  --optimize_out=./ch_PP-OCRv2_det_slim_opt --valid_targets=arm  --optimize_out_type=naive_buffer
M
update  
MissPenguin 已提交
143
# Convert recognition model
W
WenmuZhou 已提交
144
./opt --model_file=./ch_PP-OCRv2_rec_slim_quant_infer/inference.pdmodel  --param_file=./ch_PP-OCRv2_rec_slim_quant_infer/inference.pdiparams  --optimize_out=./ch_PP-OCRv2_rec_slim_opt --valid_targets=arm  --optimize_out_type=naive_buffer
M
update  
MissPenguin 已提交
145
# Convert angle classifier model
W
WenmuZhou 已提交
146
./opt --model_file=./ch_ppocr_mobile_v2.0_cls_slim_infer/inference.pdmodel  --param_file=./ch_ppocr_mobile_v2.0_cls_slim_infer/inference.pdiparams  --optimize_out=./ch_ppocr_mobile_v2.0_cls_slim_opt --valid_targets=arm  --optimize_out_type=naive_buffer
W
WenmuZhou 已提交
147

W
WenmuZhou 已提交
148 149
```

M
update  
MissPenguin 已提交
150
After the conversion is successful, there will be more files ending with `.nb` in the inference model directory, which is the successfully converted model file.
W
WenmuZhou 已提交
151

M
update  
MissPenguin 已提交
152 153
<a name="2.2-Run-optimized-model-on-Phone"></a>
### 2.2 Run optimized model on Phone
W
WenmuZhou 已提交
154

M
update  
MissPenguin 已提交
155 156 157 158
Some preparatory work is required first.
 1. Prepare an Android phone with arm8. If the compiled prediction library and opt file are armv7, you need an arm7 phone and modify ARM_ABI = arm7 in the Makefile.
 2. Make sure the phone is connected to the computer, open the USB debugging option of the phone, and select the file transfer mode.
 3. Install the adb tool on the computer.
W
WenmuZhou 已提交
159

M
update  
MissPenguin 已提交
160
    3.1. Install ADB for MAC:
W
WenmuZhou 已提交
161 162 163
    ```
    brew cask install android-platform-tools
    ```
M
update  
MissPenguin 已提交
164
    3.2. Install ADB for Linux
W
WenmuZhou 已提交
165 166 167 168
    ```
    sudo apt update
    sudo apt install -y wget adb
    ```
M
update  
MissPenguin 已提交
169
    3.3. Install ADB for windows
W
WenmuZhou 已提交
170

M
update  
MissPenguin 已提交
171
    To install on win, you need to go to Google's Android platform to download the adb package for installation:[link](https://developer.android.com/studio)
W
WenmuZhou 已提交
172

M
update  
MissPenguin 已提交
173 174
    Verify whether adb is installed successfully
     ```
W
WenmuZhou 已提交
175 176
    adb devices
    ```
M
update  
MissPenguin 已提交
177
    If there is device output, it means the installation is successful。
W
WenmuZhou 已提交
178 179 180 181 182
    ```
       List of devices attached
       744be294    device
    ```

M
update  
MissPenguin 已提交
183
 4. Prepare optimized models, prediction library files, test images and dictionary files used.
W
WenmuZhou 已提交
184 185 186
 ```
 git clone https://github.com/PaddlePaddle/PaddleOCR.git
 cd PaddleOCR/deploy/lite/
M
update  
MissPenguin 已提交
187
 # run prepare.sh
W
WenmuZhou 已提交
188 189
 sh prepare.sh /{lite prediction library path}/inference_lite_lib.android.armv8

M
update  
MissPenguin 已提交
190
 #
W
WenmuZhou 已提交
191 192
 cd /{lite prediction library path}/inference_lite_lib.android.armv8/
 cd demo/cxx/ocr/
M
update  
MissPenguin 已提交
193 194 195 196
 # copy paddle-lite C++ .so file to debug/ directory
 cp ../../../cxx/lib/libpaddle_light_api_shared.so ./debug/

 cd inference_lite_lib.android.armv8/demo/cxx/ocr/
W
WenmuZhou 已提交
197 198 199
 cp ../../../cxx/lib/libpaddle_light_api_shared.so ./debug/
 ```

M
update  
MissPenguin 已提交
200
Prepare the test image, taking PaddleOCR/doc/imgs/11.jpg as an example, copy the image file to the demo/cxx/ocr/debug/ folder. Prepare the model files optimized by the lite opt tool, ch_det_mv3_db_opt.nb, ch_rec_mv3_crnn_opt.nb, and place them under the demo/cxx/ocr/debug/ folder.
W
WenmuZhou 已提交
201

M
update  
MissPenguin 已提交
202
The structure of the OCR demo is as follows after the above command is executed:
W
WenmuZhou 已提交
203 204 205 206

```
demo/cxx/ocr/
|-- debug/  
M
update  
MissPenguin 已提交
207 208 209 210 211 212 213 214 215
|   |--ch_PP-OCRv2_det_slim_opt.nb           Detection model
|   |--ch_PP-OCRv2_rec_slim_opt.nb           Recognition model
|   |--ch_ppocr_mobile_v2.0_cls_slim_opt.nb           Text direction classification model
|   |--11.jpg                           Image for OCR
|   |--ppocr_keys_v1.txt                Dictionary file
|   |--libpaddle_light_api_shared.so    C++ .so file
|   |--config.txt                       Config file
|-- config.txt                  Config file
|-- cls_process.cc              Pre-processing and post-processing files for the angle classifier
W
WenmuZhou 已提交
216
|-- cls_process.h
M
update  
MissPenguin 已提交
217
|-- crnn_process.cc             Pre-processing and post-processing files for the CRNN model
W
WenmuZhou 已提交
218
|-- crnn_process.h
M
update  
MissPenguin 已提交
219
|-- db_post_process.cc          Pre-processing and post-processing files for the DB model
W
WenmuZhou 已提交
220
|-- db_post_process.h
M
update  
MissPenguin 已提交
221 222
|-- Makefile  
|-- ocr_db_crnn.cc              C++ main code
W
WenmuZhou 已提交
223 224
```

225
**Note**:
M
update  
MissPenguin 已提交
226
1. `ppocr_keys_v1.txt` is a Chinese dictionary file. If the nb model is used for English recognition or other language recognition, dictionary file should be replaced with a dictionary of the corresponding language. PaddleOCR provides a variety of dictionaries under ppocr/utils/, including:
W
WenmuZhou 已提交
227
```
M
update  
MissPenguin 已提交
228 229 230 231 232 233
dict/french_dict.txt     # french
dict/german_dict.txt     # german
ic15_dict.txt       # english
dict/japan_dict.txt      # japan
dict/korean_dict.txt     # korean
ppocr_keys_v1.txt   # chinese
W
WenmuZhou 已提交
234 235
```

M
update  
MissPenguin 已提交
236
2.  `config.txt` of the detector and classifier, as shown below:
W
WenmuZhou 已提交
237
```
M
update  
MissPenguin 已提交
238 239 240 241 242
max_side_len  960         #  Limit the maximum image height and width to 960
det_db_thresh  0.3        # Used to filter the binarized image of DB prediction, setting 0.-0.3 has no obvious effect on the result
det_db_box_thresh  0.5    # DDB post-processing filter box threshold, if there is a missing box detected, it can be reduced as appropriate
det_db_unclip_ratio  1.6  # Indicates the compactness of the text box, the smaller the value, the closer the text box to the text
use_direction_classify  0  # Whether to use the direction classifier, 0 means not to use, 1 means to use
L
LDOUBLEV 已提交
243
rec_image_height  32      # The height of the input image of the recognition model, the PP-OCRv3 model needs to be set to 48, and the PP-OCRv2 model needs to be set to 32
W
WenmuZhou 已提交
244 245
```

M
update  
MissPenguin 已提交
246
 5. Run Model on phone
W
WenmuZhou 已提交
247

M
update  
MissPenguin 已提交
248
After the above steps are completed, you can use adb to push the file to the phone to run, the steps are as follows:
W
WenmuZhou 已提交
249 250

 ```
M
update  
MissPenguin 已提交
251 252
 # Execute the compilation and get the executable file ocr_db_crnn
 # The first execution of this command will download dependent libraries such as opencv. After the download is complete, you need to execute it again
W
WenmuZhou 已提交
253
 make -j
M
update  
MissPenguin 已提交
254
 # Move the compiled executable file to the debug folder
W
WenmuZhou 已提交
255
 mv ocr_db_crnn ./debug/
M
update  
MissPenguin 已提交
256
 # Push the debug folder to the phone
W
WenmuZhou 已提交
257 258 259 260
 adb push debug /data/local/tmp/
 adb shell
 cd /data/local/tmp/debug
 export LD_LIBRARY_PATH=${PWD}:$LD_LIBRARY_PATH
M
update  
MissPenguin 已提交
261
 # The use of ocr_db_crnn is:
L
LDOUBLEV 已提交
262 263 264
 # ./ocr_db_crnn Detection model file Orientation classifier model file Recognition model file  Hardware  Precision  Threads Batchsize  Test image path Dictionary file path
 ./ocr_db_crnn ch_PP-OCRv2_det_slim_opt.nb  ch_PP-OCRv2_rec_slim_opt.nb  ch_ppocr_mobile_v2.0_cls_slim_opt.nb  arm8 INT8 10 1  ./11.jpg  config.txt  ppocr_keys_v1.txt  True
# precision can be INT8 for quantitative model or FP32 for normal model.
W
WenmuZhou 已提交
265 266
 ```

M
update  
MissPenguin 已提交
267
If you modify the code, you need to recompile and push to the phone.
W
WenmuZhou 已提交
268

M
update  
MissPenguin 已提交
269
The outputs are as follows:
W
WenmuZhou 已提交
270 271

<div align="center">
W
WenmuZhou 已提交
272
    <img src="imgs/lite_demo.png" width="600">
W
WenmuZhou 已提交
273 274 275
</div>

## FAQ
W
WenmuZhou 已提交
276

M
update  
MissPenguin 已提交
277 278 279
Q1: What if I want to change the model, do I need to run it again according to the process?

A1: If you have performed the above steps, you only need to replace the .nb model file to complete the model replacement.
W
WenmuZhou 已提交
280

M
update  
MissPenguin 已提交
281
Q2: How to test with another picture?
W
WenmuZhou 已提交
282

M
update  
MissPenguin 已提交
283
A2: Replace the .jpg test image under ./debug with the image you want to test, and run adb push to push new image to the phone.
W
WenmuZhou 已提交
284

M
update  
MissPenguin 已提交
285
Q3: How to package it into the mobile APP?
W
WenmuZhou 已提交
286

M
update  
MissPenguin 已提交
287
A3: This demo aims to provide the core algorithm part that can run OCR on mobile phones. Further, PaddleOCR/deploy/android_demo is an example of encapsulating this demo into a mobile app for reference.
L
LDOUBLEV 已提交
288 289 290 291

Q4: When running the demo, an error is reported `Error: This model is not supported, because kernel for 'io_copy' is not supported by Paddle-Lite.`

A4: The problem is that the installed paddlelite version does not match the downloaded prediction library version. Make sure that the paddleliteopt tool matches your prediction library version, and try to switch to the nb model again.