README.md 11.8 KB
Newer Older
B
Bin Lu 已提交
1
English | [简体中文](README_ch.md)
littletomatodonkey's avatar
littletomatodonkey 已提交
2

文幕地方's avatar
文幕地方 已提交
3 4 5 6 7 8 9 10 11 12
- [1 Introduction](#1-introduction)
- [2. Performance](#2-performance)
- [3. Effect demo](#3-effect-demo)
  - [3.1 SER](#31-ser)
  - [3.2 RE](#32-re)
- [4. Install](#4-install)
  - [4.1 Install dependencies](#41-install-dependencies)
  - [5.3 RE](#53-re)
- [6. Reference Links](#6-reference-links)
- [License](#license)
M
update  
MissPenguin 已提交
13

B
Bin Lu 已提交
14
# Document Visual Question Answering
M
update  
MissPenguin 已提交
15

B
Bin Lu 已提交
16
## 1 Introduction
M
update  
MissPenguin 已提交
17

B
Bin Lu 已提交
18
VQA refers to visual question answering, which mainly asks and answers image content. DOC-VQA is one of the VQA tasks. DOC-VQA mainly asks questions about the text content of text images.
文幕地方's avatar
文幕地方 已提交
19

B
Bin Lu 已提交
20
The DOC-VQA algorithm in PP-Structure is developed based on the PaddleNLP natural language processing algorithm library.
文幕地方's avatar
add re  
文幕地方 已提交
21

B
Bin Lu 已提交
22
The main features are as follows:
文幕地方's avatar
add re  
文幕地方 已提交
23

B
Bin Lu 已提交
24 25 26 27 28
- Integrate [LayoutXLM](https://arxiv.org/pdf/2104.08836.pdf) model and PP-OCR prediction engine.
- Supports Semantic Entity Recognition (SER) and Relation Extraction (RE) tasks based on multimodal methods. Based on the SER task, the text recognition and classification in the image can be completed; based on the RE task, the relationship extraction of the text content in the image can be completed, such as judging the problem pair (pair).
- Supports custom training for SER tasks and RE tasks.
- Supports end-to-end system prediction and evaluation of OCR+SER.
- Supports end-to-end system prediction of OCR+SER+RE.
littletomatodonkey's avatar
littletomatodonkey 已提交
29 30


B
Bin Lu 已提交
31 32
This project is an open source implementation of [LayoutXLM: Multimodal Pre-training for Multilingual Visually-rich Document Understanding](https://arxiv.org/pdf/2104.08836.pdf) on Paddle 2.2,
Included fine-tuning code on [XFUND dataset](https://github.com/doc-analysis/XFUND).
littletomatodonkey's avatar
littletomatodonkey 已提交
33

B
Bin Lu 已提交
34
## 2. Performance
littletomatodonkey's avatar
littletomatodonkey 已提交
35

B
Bin Lu 已提交
36
We evaluate the algorithm on the Chinese dataset of [XFUND](https://github.com/doc-analysis/XFUND), and the performance is as follows
文幕地方's avatar
add re  
文幕地方 已提交
37

B
Bin Lu 已提交
38
| Model | Task | hmean | Model download address |
文幕地方's avatar
文幕地方 已提交
39
|:---:|:---:|:---:| :---:|
B
Bin Lu 已提交
40 41 42 43 44
| LayoutXLM | SER | 0.9038 | [link](https://paddleocr.bj.bcebos.com/pplayout/ser_LayoutXLM_xfun_zh.tar) |
| LayoutXLM | RE | 0.7483 | [link](https://paddleocr.bj.bcebos.com/pplayout/re_LayoutXLM_xfun_zh.tar) |
| LayoutLMv2 | SER | 0.8544 | [link](https://paddleocr.bj.bcebos.com/pplayout/ser_LayoutLMv2_xfun_zh.tar)
| LayoutLMv2 | RE | 0.6777 | [link](https://paddleocr.bj.bcebos.com/pplayout/re_LayoutLMv2_xfun_zh.tar) |
| LayoutLM | SER | 0.7731 | [link](https://paddleocr.bj.bcebos.com/pplayout/ser_LayoutLM_xfun_zh.tar) |
文幕地方's avatar
add re  
文幕地方 已提交
45

B
Bin Lu 已提交
46
## 3. Effect demo
littletomatodonkey's avatar
littletomatodonkey 已提交
47

B
Bin Lu 已提交
48
**Note:** The test images are from the XFUND dataset.
littletomatodonkey's avatar
littletomatodonkey 已提交
49

M
update  
MissPenguin 已提交
50
<a name="31"></a>
文幕地方's avatar
文幕地方 已提交
51
### 3.1 SER
littletomatodonkey's avatar
littletomatodonkey 已提交
52

M
update  
MissPenguin 已提交
53
![](../docs/vqa/result_ser/zh_val_0_ser.jpg) | ![](../docs/vqa/result_ser/zh_val_42_ser.jpg)
文幕地方's avatar
add re  
文幕地方 已提交
54
---|---
littletomatodonkey's avatar
littletomatodonkey 已提交
55

B
Bin Lu 已提交
56
Boxes with different colors in the figure represent different categories. For the XFUND dataset, there are 3 categories: `QUESTION`, `ANSWER`, `HEADER`
littletomatodonkey's avatar
littletomatodonkey 已提交
57

B
Bin Lu 已提交
58 59 60
* Dark purple: HEADER
* Light purple: QUESTION
* Army Green: ANSWER
littletomatodonkey's avatar
littletomatodonkey 已提交
61

B
Bin Lu 已提交
62
The corresponding categories and OCR recognition results are also marked on the upper left of the OCR detection frame.
littletomatodonkey's avatar
littletomatodonkey 已提交
63

M
update  
MissPenguin 已提交
64
<a name="32"></a>
文幕地方's avatar
文幕地方 已提交
65
### 3.2 RE
littletomatodonkey's avatar
littletomatodonkey 已提交
66

M
update  
MissPenguin 已提交
67
![](../docs/vqa/result_re/zh_val_21_re.jpg) | ![](../docs/vqa/result_re/zh_val_40_re.jpg)
文幕地方's avatar
add re  
文幕地方 已提交
68
---|---
littletomatodonkey's avatar
littletomatodonkey 已提交
69 70


B
Bin Lu 已提交
71
The red box in the figure represents the question, the blue box represents the answer, and the question and the answer are connected by a green line. The corresponding categories and OCR recognition results are also marked on the upper left of the OCR detection frame.
littletomatodonkey's avatar
littletomatodonkey 已提交
72

B
Bin Lu 已提交
73
## 4. Install
文幕地方's avatar
add re  
文幕地方 已提交
74

B
Bin Lu 已提交
75
### 4.1 Install dependencies
littletomatodonkey's avatar
littletomatodonkey 已提交
76

B
Bin Lu 已提交
77
- **(1) Install PaddlePaddle**
littletomatodonkey's avatar
littletomatodonkey 已提交
78 79

```bash
文幕地方's avatar
文幕地方 已提交
80
python3 -m pip install --upgrade pip
littletomatodonkey's avatar
littletomatodonkey 已提交
81

B
Bin Lu 已提交
82
# GPU installation
83
python3 -m pip install "paddlepaddle-gpu>=2.2" -i https://mirror.baidu.com/pypi/simple
littletomatodonkey's avatar
littletomatodonkey 已提交
84

B
Bin Lu 已提交
85
# CPU installation
86
python3 -m pip install "paddlepaddle>=2.2" -i https://mirror.baidu.com/pypi/simple
littletomatodonkey's avatar
littletomatodonkey 已提交
87

B
Bin Lu 已提交
88 89
````
For more requirements, please refer to the instructions in [Installation Documentation](https://www.paddlepaddle.org.cn/install/quick).
littletomatodonkey's avatar
littletomatodonkey 已提交
90

B
Bin Lu 已提交
91
### 4.2 Install PaddleOCR
littletomatodonkey's avatar
littletomatodonkey 已提交
92

B
Bin Lu 已提交
93
- **(1) pip install PaddleOCR whl package quickly (prediction only)**
littletomatodonkey's avatar
littletomatodonkey 已提交
94 95

```bash
96
python3 -m pip install paddleocr
B
Bin Lu 已提交
97
````
littletomatodonkey's avatar
littletomatodonkey 已提交
98

B
Bin Lu 已提交
99
- **(2) Download VQA source code (prediction + training)**
littletomatodonkey's avatar
littletomatodonkey 已提交
100 101

```bash
B
Bin Lu 已提交
102
[Recommended] git clone https://github.com/PaddlePaddle/PaddleOCR
littletomatodonkey's avatar
littletomatodonkey 已提交
103

B
Bin Lu 已提交
104
# If the pull cannot be successful due to network problems, you can also choose to use the hosting on the code cloud:
littletomatodonkey's avatar
littletomatodonkey 已提交
105 106
git clone https://gitee.com/paddlepaddle/PaddleOCR

B
Bin Lu 已提交
107 108
# Note: Code cloud hosting code may not be able to synchronize the update of this github project in real time, there is a delay of 3 to 5 days, please use the recommended method first.
````
littletomatodonkey's avatar
littletomatodonkey 已提交
109

B
Bin Lu 已提交
110
- **(3) Install VQA's `requirements`**
littletomatodonkey's avatar
littletomatodonkey 已提交
111 112

```bash
113
python3 -m pip install -r ppstructure/vqa/requirements.txt
B
Bin Lu 已提交
114
````
littletomatodonkey's avatar
littletomatodonkey 已提交
115

B
Bin Lu 已提交
116
## 5. Usage
littletomatodonkey's avatar
littletomatodonkey 已提交
117

B
Bin Lu 已提交
118
### 5.1 Data and Model Preparation
littletomatodonkey's avatar
littletomatodonkey 已提交
119

B
Bin Lu 已提交
120
If you want to experience the prediction process directly, you can download the pre-training model provided by us, skip the training process, and just predict directly.
121

B
Bin Lu 已提交
122
* Download the processed dataset
123

124
The download address of the processed XFUND Chinese dataset: [link](https://paddleocr.bj.bcebos.com/ppstructure/dataset/XFUND.tar).
littletomatodonkey's avatar
littletomatodonkey 已提交
125 126


B
Bin Lu 已提交
127
Download and unzip the dataset, and place the dataset in the current directory after unzipping.
littletomatodonkey's avatar
littletomatodonkey 已提交
128 129

```shell
130
wget https://paddleocr.bj.bcebos.com/ppstructure/dataset/XFUND.tar
B
Bin Lu 已提交
131
````
littletomatodonkey's avatar
littletomatodonkey 已提交
132

B
Bin Lu 已提交
133
* Convert the dataset
littletomatodonkey's avatar
littletomatodonkey 已提交
134

B
Bin Lu 已提交
135
If you need to train other XFUND datasets, you can use the following commands to convert the datasets
littletomatodonkey's avatar
littletomatodonkey 已提交
136

B
Bin Lu 已提交
137 138 139
```bash
python3 ppstructure/vqa/tools/trans_xfun_data.py --ori_gt_path=path/to/json_path --output_path=path/to/save_path
````
littletomatodonkey's avatar
littletomatodonkey 已提交
140

B
Bin Lu 已提交
141
* Download the pretrained models
142
```bash
B
Bin Lu 已提交
143 144 145 146 147 148 149
mkdir pretrain && cd pretrain
#download the SER model
wget https://paddleocr.bj.bcebos.com/pplayout/ser_LayoutXLM_xfun_zh.tar && tar -xvf ser_LayoutXLM_xfun_zh.tar
#download the RE model
wget https://paddleocr.bj.bcebos.com/pplayout/re_LayoutXLM_xfun_zh.tar && tar -xvf re_LayoutXLM_xfun_zh.tar
cd ../
````
littletomatodonkey's avatar
littletomatodonkey 已提交
150

M
update  
MissPenguin 已提交
151
<a name="52"></a>
文幕地方's avatar
文幕地方 已提交
152
### 5.2 SER
littletomatodonkey's avatar
littletomatodonkey 已提交
153

B
Bin Lu 已提交
154
Before starting training, you need to modify the following four fields
155

B
Bin Lu 已提交
156 157 158 159
1. `Train.dataset.data_dir`: point to the directory where the training set images are stored
2. `Train.dataset.label_file_list`: point to the training set label file
3. `Eval.dataset.data_dir`: refers to the directory where the validation set images are stored
4. `Eval.dataset.label_file_list`: point to the validation set label file
littletomatodonkey's avatar
littletomatodonkey 已提交
160

B
Bin Lu 已提交
161
* start training
littletomatodonkey's avatar
littletomatodonkey 已提交
162
```shell
163
CUDA_VISIBLE_DEVICES=0 python3 tools/train.py -c configs/vqa/ser/layoutxlm.yml
B
Bin Lu 已提交
164
````
littletomatodonkey's avatar
littletomatodonkey 已提交
165

B
Bin Lu 已提交
166 167
Finally, `precision`, `recall`, `hmean` and other indicators will be printed.
In the `./output/ser_layoutxlm/` folder will save the training log, the optimal model and the model for the latest epoch.
littletomatodonkey's avatar
littletomatodonkey 已提交
168

B
Bin Lu 已提交
169
* resume training
Z
zhoujun 已提交
170

B
Bin Lu 已提交
171
To resume training, assign the folder path of the previously trained model to the `Architecture.Backbone.checkpoints` field.
172

Z
zhoujun 已提交
173
```shell
174
CUDA_VISIBLE_DEVICES=0 python3 tools/train.py -c configs/vqa/ser/layoutxlm.yml -o Architecture.Backbone.checkpoints=path/to/model_dir
B
Bin Lu 已提交
175
````
Z
zhoujun 已提交
176

B
Bin Lu 已提交
177
* evaluate
Z
zhoujun 已提交
178

B
Bin Lu 已提交
179
Evaluation requires assigning the folder path of the model to be evaluated to the `Architecture.Backbone.checkpoints` field.
littletomatodonkey's avatar
littletomatodonkey 已提交
180 181

```shell
182
CUDA_VISIBLE_DEVICES=0 python3 tools/eval.py -c configs/vqa/ser/layoutxlm.yml -o Architecture.Backbone.checkpoints=path/to/model_dir
B
Bin Lu 已提交
183 184
````
Finally, `precision`, `recall`, `hmean` and other indicators will be printed
littletomatodonkey's avatar
littletomatodonkey 已提交
185

186
* `OCR + SER` tandem prediction based on training engine
littletomatodonkey's avatar
littletomatodonkey 已提交
187

188
Use the following command to complete the series prediction of `OCR engine + SER`, taking the SER model based on LayoutXLM as an example::
littletomatodonkey's avatar
littletomatodonkey 已提交
189 190

```shell
191
python3.7 tools/export_model.py -c configs/vqa/ser/layoutxlm.yml -o Architecture.Backbone.checkpoints=pretrain/ser_LayoutXLM_xfun_zh/ Global.save_inference_dir=output/ser/infer
B
Bin Lu 已提交
192
````
littletomatodonkey's avatar
littletomatodonkey 已提交
193

B
Bin Lu 已提交
194
Finally, the prediction result visualization image and the prediction result text file will be saved in the directory configured by the `config.Global.save_res_path` field. The prediction result text file is named `infer_results.txt`.
195

196
* End-to-end evaluation of `OCR + SER` prediction system
littletomatodonkey's avatar
littletomatodonkey 已提交
197

B
Bin Lu 已提交
198
First use the `tools/infer_vqa_token_ser.py` script to complete the prediction of the dataset, then use the following command to evaluate.
199

littletomatodonkey's avatar
littletomatodonkey 已提交
200 201
```shell
export CUDA_VISIBLE_DEVICES=0
B
Bin Lu 已提交
202 203
python3 tools/eval_with_label_end2end.py --gt_json_path XFUND/zh_val/xfun_normalize_val.json --pred_json_path output_res/infer_results.txt
````
204 205 206 207 208 209 210 211 212 213
* export model

Use the following command to complete the model export of the SER model, taking the SER model based on LayoutXLM as an example:

```shell
python3.7 tools/export_model.py -c configs/vqa/ser/layoutxlm.yml -o Architecture.Backbone.checkpoints=pretrain/ser_LayoutXLM_xfun_zh/ Global.save_inference_dir=output/ser/infer
```
The converted model will be stored in the directory specified by the `Global.save_inference_dir` field.

* `OCR + SER` tandem prediction based on prediction engine
文幕地方's avatar
文幕地方 已提交
214

215 216 217 218 219 220 221
Use the following command to complete the tandem prediction of `OCR + SER` based on the prediction engine, taking the SER model based on LayoutXLM as an example:

```shell
cd ppstructure
CUDA_VISIBLE_DEVICES=0 python3.7 vqa/predict_vqa_token_ser.py --vqa_algorithm=LayoutXLM --ser_model_dir=../output/ser/infer --ser_dict_path=../train_data/XFUND/class_list_xfun.txt --image_dir=docs/vqa/input/zh_val_42.jpg --output=output
```
After the prediction is successful, the visualization images and results will be saved in the directory specified by the `output` field
littletomatodonkey's avatar
littletomatodonkey 已提交
222

M
update  
MissPenguin 已提交
223
<a name="53"></a>
文幕地方's avatar
文幕地方 已提交
224
### 5.3 RE
littletomatodonkey's avatar
littletomatodonkey 已提交
225

B
Bin Lu 已提交
226
* start training
littletomatodonkey's avatar
littletomatodonkey 已提交
227

B
Bin Lu 已提交
228
Before starting training, you need to modify the following four fields
文幕地方's avatar
add re  
文幕地方 已提交
229

B
Bin Lu 已提交
230 231 232 233
1. `Train.dataset.data_dir`: point to the directory where the training set images are stored
2. `Train.dataset.label_file_list`: point to the training set label file
3. `Eval.dataset.data_dir`: refers to the directory where the validation set images are stored
4. `Eval.dataset.label_file_list`: point to the validation set label file
Z
zhoujun 已提交
234 235

```shell
236
CUDA_VISIBLE_DEVICES=0 python3 tools/train.py -c configs/vqa/re/layoutxlm.yml
B
Bin Lu 已提交
237
````
Z
zhoujun 已提交
238

B
Bin Lu 已提交
239 240
Finally, `precision`, `recall`, `hmean` and other indicators will be printed.
In the `./output/re_layoutxlm/` folder will save the training log, the optimal model and the model for the latest epoch.
241

B
Bin Lu 已提交
242
* resume training
243

B
Bin Lu 已提交
244
To resume training, assign the folder path of the previously trained model to the `Architecture.Backbone.checkpoints` field.
文幕地方's avatar
add re  
文幕地方 已提交
245

Z
zhoujun 已提交
246
```shell
247
CUDA_VISIBLE_DEVICES=0 python3 tools/train.py -c configs/vqa/re/layoutxlm.yml -o Architecture.Backbone.checkpoints=path/to/model_dir
B
Bin Lu 已提交
248
````
Z
zhoujun 已提交
249

B
Bin Lu 已提交
250
* evaluate
Z
zhoujun 已提交
251

B
Bin Lu 已提交
252
Evaluation requires assigning the folder path of the model to be evaluated to the `Architecture.Backbone.checkpoints` field.
文幕地方's avatar
add re  
文幕地方 已提交
253 254

```shell
255
CUDA_VISIBLE_DEVICES=0 python3 tools/eval.py -c configs/vqa/re/layoutxlm.yml -o Architecture.Backbone.checkpoints=path/to/model_dir
B
Bin Lu 已提交
256 257
````
Finally, `precision`, `recall`, `hmean` and other indicators will be printed
文幕地方's avatar
add re  
文幕地方 已提交
258

B
Bin Lu 已提交
259
* Use `OCR engine + SER + RE` tandem prediction
文幕地方's avatar
add re  
文幕地方 已提交
260

B
Bin Lu 已提交
261
Use the following command to complete the series prediction of `OCR engine + SER + RE`, taking the pretrained SER and RE models as an example:
文幕地方's avatar
add re  
文幕地方 已提交
262 263
```shell
export CUDA_VISIBLE_DEVICES=0
264
python3 tools/infer_vqa_token_ser_re.py -c configs/vqa/re/layoutxlm.yml -o Architecture.Backbone.checkpoints=pretrain/re_LayoutXLM_xfun_zh/Global.infer_img=ppstructure/docs/vqa/input/zh_val_21.jpg -c_ser configs/vqa/ser/layoutxlm. yml -o_ser Architecture.Backbone.checkpoints=pretrain/ser_LayoutXLM_xfun_zh/
B
Bin Lu 已提交
265
````
littletomatodonkey's avatar
littletomatodonkey 已提交
266

B
Bin Lu 已提交
267
Finally, the prediction result visualization image and the prediction result text file will be saved in the directory configured by the `config.Global.save_res_path` field. The prediction result text file is named `infer_results.txt`.
268

269
* export model
文幕地方's avatar
文幕地方 已提交
270

271 272 273 274 275 276
cooming soon

* `OCR + SER + RE` tandem prediction based on prediction engine

cooming soon

B
Bin Lu 已提交
277
## 6. Reference Links
littletomatodonkey's avatar
littletomatodonkey 已提交
278 279 280 281

- LayoutXLM: Multimodal Pre-training for Multilingual Visually-rich Document Understanding, https://arxiv.org/pdf/2104.08836.pdf
- microsoft/unilm/layoutxlm, https://github.com/microsoft/unilm/tree/master/layoutxlm
- XFUND dataset, https://github.com/doc-analysis/XFUND
M
MissPenguin 已提交
282 283 284 285

## License

The content of this project itself is licensed under the [Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)](https://creativecommons.org/licenses/by-nc-sa/4.0/)