未验证 提交 63c643cc 编写于 作者: E Evezerest 提交者: GitHub

Merge pull request #5744 from Evezerest/release2.4

Unified file name
...@@ -100,7 +100,7 @@ PaddleOCR旨在打造一套丰富、领先、且实用的OCR工具库,助力 ...@@ -100,7 +100,7 @@ PaddleOCR旨在打造一套丰富、领先、且实用的OCR工具库,助力
- [版面分析](./ppstructure/layout/README_ch.md) - [版面分析](./ppstructure/layout/README_ch.md)
- [表格识别](./ppstructure/table/README_ch.md) - [表格识别](./ppstructure/table/README_ch.md)
- [DocVQA](./ppstructure/vqa/README_ch.md) - [DocVQA](./ppstructure/vqa/README_ch.md)
- [关键信息提取](./ppstructure/docs/kie.md) - [关键信息提取](./ppstructure/docs/kie_ch.md)
- OCR学术圈 - OCR学术圈
- [两阶段模型介绍与下载](./doc/doc_ch/algorithm_overview.md) - [两阶段模型介绍与下载](./doc/doc_ch/algorithm_overview.md)
- [端到端PGNet算法](./doc/doc_ch/pgnet.md) - [端到端PGNet算法](./doc/doc_ch/pgnet.md)
......
# 关键信息提取(Key Information Extraction) # Key Information Extraction(KIE)
本节介绍PaddleOCR中关键信息提取SDMGR方法的快速使用和训练方法。 This section provides a tutorial example on how to quickly use, train, and evaluate a key information extraction(KIE) model, [SDMGR](https://arxiv.org/abs/2103.14470), in PaddleOCR.
SDMGR是一个关键信息提取算法,将每个检测到的文本区域分类为预定义的类别,如订单ID、发票号码,金额等。 [SDMGR(Spatial Dual-Modality Graph Reasoning)](https://arxiv.org/abs/2103.14470) is a KIE algorithm that classifies each detected text region into predefined categories, such as order ID, invoice number, amount, and etc.
* [1. 快速使用](#1-----) * [1. Quick Use](#1-----)
* [2. 执行训练](#2-----) * [2. Model Training](#2-----)
* [3. 执行评估](#3-----) * [3. Model Evaluation](#3-----)
<a name="1-----"></a> <a name="1-----"></a>
## 1. 快速使用
训练和测试的数据采用wildreceipt数据集,通过如下指令下载数据集: ## 1. Quick Use
``` [Wildreceipt dataset](https://paperswithcode.com/dataset/wildreceipt) is used for this tutorial. It contains 1765 photos, with 25 classes, and 50000 text boxes, which can be downloaded by wget:
```shell
wget https://paddleocr.bj.bcebos.com/dygraph_v2.1/kie/wildreceipt.tar && tar xf wildreceipt.tar wget https://paddleocr.bj.bcebos.com/dygraph_v2.1/kie/wildreceipt.tar && tar xf wildreceipt.tar
``` ```
执行预测: Download the pretrained model and predict the result:
``` ```shell
cd PaddleOCR/ cd PaddleOCR/
wget https://paddleocr.bj.bcebos.com/dygraph_v2.1/kie/kie_vgg16.tar && tar xf kie_vgg16.tar wget https://paddleocr.bj.bcebos.com/dygraph_v2.1/kie/kie_vgg16.tar && tar xf kie_vgg16.tar
python3.7 tools/infer_kie.py -c configs/kie/kie_unet_sdmgr.yml -o Global.checkpoints=kie_vgg16/best_accuracy Global.infer_img=../wildreceipt/1.txt python3.7 tools/infer_kie.py -c configs/kie/kie_unet_sdmgr.yml -o Global.checkpoints=kie_vgg16/best_accuracy Global.infer_img=../wildreceipt/1.txt
``` ```
执行预测后的结果保存在`./output/sdmgr_kie/predicts_kie.txt`文件中,可视化结果保存在`/output/sdmgr_kie/kie_results/`目录下。 The prediction result is saved as `./output/sdmgr_kie/predicts_kie.txt`, and the visualization results are saved in the folder`/output/sdmgr_kie/kie_results/`.
可视化结果如下图所示: The visualization results are shown in the figure below:
<div align="center"> <div align="center">
<img src="./imgs/0.png" width="800"> <img src="./imgs/0.png" width="800">
</div> </div>
<a name="2-----"></a> <a name="2-----"></a>
## 2. 执行训练 ## 2. Model Training
创建数据集软链到PaddleOCR/train_data目录下: Create a softlink to the folder, `PaddleOCR/train_data`:
``` ```shell
cd PaddleOCR/ && mkdir train_data && cd train_data cd PaddleOCR/ && mkdir train_data && cd train_data
ln -s ../../wildreceipt ./ ln -s ../../wildreceipt ./
``` ```
训练采用的配置文件是configs/kie/kie_unet_sdmgr.yml,配置文件中默认训练数据路径是`train_data/wildreceipt`,准备好数据后,可以通过如下指令执行训练: The configuration file used for training is `configs/kie/kie_unet_sdmgr.yml`. The default training data path in the configuration file is `train_data/wildreceipt`. After preparing the data, you can execute the model training with the following command:
``` ```shell
python3.7 tools/train.py -c configs/kie/kie_unet_sdmgr.yml -o Global.save_model_dir=./output/kie/ python3.7 tools/train.py -c configs/kie/kie_unet_sdmgr.yml -o Global.save_model_dir=./output/kie/
``` ```
<a name="3-----"></a> <a name="3-----"></a>
## 3. 执行评估
``` ## 3. Model Evaluation
After training, you can execute the model evaluation with the following command:
```shell
python3.7 tools/eval.py -c configs/kie/kie_unet_sdmgr.yml -o Global.checkpoints=./output/kie/best_accuracy python3.7 tools/eval.py -c configs/kie/kie_unet_sdmgr.yml -o Global.checkpoints=./output/kie/best_accuracy
``` ```
**Reference:**
**参考文献:**
<!-- [ALGORITHM] --> <!-- [ALGORITHM] -->
......
# Key Information Extraction(KIE) # 关键信息提取(Key Information Extraction)
This section provides a tutorial example on how to quickly use, train, and evaluate a key information extraction(KIE) model, [SDMGR](https://arxiv.org/abs/2103.14470), in PaddleOCR. 本节介绍PaddleOCR中关键信息提取SDMGR方法的快速使用和训练方法。
[SDMGR(Spatial Dual-Modality Graph Reasoning)](https://arxiv.org/abs/2103.14470) is a KIE algorithm that classifies each detected text region into predefined categories, such as order ID, invoice number, amount, and etc. SDMGR是一个关键信息提取算法,将每个检测到的文本区域分类为预定义的类别,如订单ID、发票号码,金额等。
* [1. Quick Use](#1-----) * [1. 快速使用](#1-----)
* [2. Model Training](#2-----) * [2. 执行训练](#2-----)
* [3. Model Evaluation](#3-----) * [3. 执行评估](#3-----)
<a name="1-----"></a> <a name="1-----"></a>
## 1. 快速使用
## 1. Quick Use 训练和测试的数据采用wildreceipt数据集,通过如下指令下载数据集:
[Wildreceipt dataset](https://paperswithcode.com/dataset/wildreceipt) is used for this tutorial. It contains 1765 photos, with 25 classes, and 50000 text boxes, which can be downloaded by wget: ```
```shell
wget https://paddleocr.bj.bcebos.com/dygraph_v2.1/kie/wildreceipt.tar && tar xf wildreceipt.tar wget https://paddleocr.bj.bcebos.com/dygraph_v2.1/kie/wildreceipt.tar && tar xf wildreceipt.tar
``` ```
Download the pretrained model and predict the result: 执行预测:
```shell ```
cd PaddleOCR/ cd PaddleOCR/
wget https://paddleocr.bj.bcebos.com/dygraph_v2.1/kie/kie_vgg16.tar && tar xf kie_vgg16.tar wget https://paddleocr.bj.bcebos.com/dygraph_v2.1/kie/kie_vgg16.tar && tar xf kie_vgg16.tar
python3.7 tools/infer_kie.py -c configs/kie/kie_unet_sdmgr.yml -o Global.checkpoints=kie_vgg16/best_accuracy Global.infer_img=../wildreceipt/1.txt python3.7 tools/infer_kie.py -c configs/kie/kie_unet_sdmgr.yml -o Global.checkpoints=kie_vgg16/best_accuracy Global.infer_img=../wildreceipt/1.txt
``` ```
The prediction result is saved as `./output/sdmgr_kie/predicts_kie.txt`, and the visualization results are saved in the folder`/output/sdmgr_kie/kie_results/`. 执行预测后的结果保存在`./output/sdmgr_kie/predicts_kie.txt`文件中,可视化结果保存在`/output/sdmgr_kie/kie_results/`目录下。
The visualization results are shown in the figure below: 可视化结果如下图所示:
<div align="center"> <div align="center">
<img src="./imgs/0.png" width="800"> <img src="./imgs/0.png" width="800">
</div> </div>
<a name="2-----"></a> <a name="2-----"></a>
## 2. Model Training ## 2. 执行训练
Create a softlink to the folder, `PaddleOCR/train_data`: 创建数据集软链到PaddleOCR/train_data目录下:
```shell ```
cd PaddleOCR/ && mkdir train_data && cd train_data cd PaddleOCR/ && mkdir train_data && cd train_data
ln -s ../../wildreceipt ./ ln -s ../../wildreceipt ./
``` ```
The configuration file used for training is `configs/kie/kie_unet_sdmgr.yml`. The default training data path in the configuration file is `train_data/wildreceipt`. After preparing the data, you can execute the model training with the following command: 训练采用的配置文件是configs/kie/kie_unet_sdmgr.yml,配置文件中默认训练数据路径是`train_data/wildreceipt`,准备好数据后,可以通过如下指令执行训练:
```shell ```
python3.7 tools/train.py -c configs/kie/kie_unet_sdmgr.yml -o Global.save_model_dir=./output/kie/ python3.7 tools/train.py -c configs/kie/kie_unet_sdmgr.yml -o Global.save_model_dir=./output/kie/
``` ```
<a name="3-----"></a> <a name="3-----"></a>
## 3. 执行评估
## 3. Model Evaluation ```
After training, you can execute the model evaluation with the following command:
```shell
python3.7 tools/eval.py -c configs/kie/kie_unet_sdmgr.yml -o Global.checkpoints=./output/kie/best_accuracy python3.7 tools/eval.py -c configs/kie/kie_unet_sdmgr.yml -o Global.checkpoints=./output/kie/best_accuracy
``` ```
**Reference:**
**参考文献:**
<!-- [ALGORITHM] --> <!-- [ALGORITHM] -->
......
# 文档视觉问答(DOC-VQA) # Document Visual Q&A(DOC-VQA)
VQA指视觉问答,主要针对图像内容进行提问和回答,DOC-VQA是VQA任务中的一种,DOC-VQA主要针对文本图像的文字内容提出问题。 Document Visual Q&A, mainly for the image content of the question and answer, DOC-VQA is a type of VQA task, DOC-VQA mainly asks questions about the textual content of text images.
PP-Structure 里的 DOC-VQA算法基于PaddleNLP自然语言处理算法库进行开发。 The DOC-VQA algorithm in PP-Structure is developed based on PaddleNLP natural language processing algorithm library.
主要特性如下: The main features are as follows:
- 集成[LayoutXLM](https://arxiv.org/pdf/2104.08836.pdf)模型以及PP-OCR预测引擎。 - Integrated LayoutXLM model and PP-OCR prediction engine.
- 支持基于多模态方法的语义实体识别 (Semantic Entity Recognition, SER) 以及关系抽取 (Relation Extraction, RE) 任务。基于 SER 任务,可以完成对图像中的文本识别与分类;基于 RE 任务,可以完成对图象中的文本内容的关系提取,如判断问题对(pair)。 - Support Semantic Entity Recognition (SER) and Relation Extraction (RE) tasks based on multi-modal methods. Based on SER task, text recognition and classification in images can be completed. Based on THE RE task, we can extract the relation of the text content in the image, such as judge the problem pair.
- 支持SER任务和RE任务的自定义训练。
- 支持OCR+SER的端到端系统预测与评估。
- 支持OCR+SER+RE的端到端系统预测。
**Note**:本项目基于 [LayoutXLM](https://arxiv.org/pdf/2104.08836.pdf) 在Paddle 2.2上的开源实现,同时经过飞桨团队与**中国工商银行**在不动产证场景深入打磨,联合开源。 - Support custom training for SER and RE tasks.
- Support OCR+SER end-to-end system prediction and evaluation.
## 1.性能 - Support OCR+SER+RE end-to-end system prediction.
我们在 [XFUN](https://github.com/doc-analysis/XFUND) 的中文数据集上对算法进行了评估,性能如下 **Note**: This project is based on the open source implementation of [LayoutXLM](https://arxiv.org/pdf/2104.08836.pdf) on Paddle 2.2, and at the same time, after in-depth polishing by the flying Paddle team and the Industrial and **Commercial Bank of China** in the scene of real estate certificate, jointly open source.
| 模型 | 任务 | f1 | 模型下载地址 |
## 1.Performance
We evaluated the algorithm on [XFUN](https://github.com/doc-analysis/XFUND) 's Chinese data set, and the performance is as follows
| Model | Task | F1 | Model Download Link |
|:---:|:---:|:---:| :---:| |:---:|:---:|:---:| :---:|
| LayoutXLM | RE | 0.7113 | [链接](https://paddleocr.bj.bcebos.com/pplayout/PP-Layout_v1.0_re_pretrained.tar) | | LayoutXLM | RE | 0.7113 | [Link](https://paddleocr.bj.bcebos.com/pplayout/PP-Layout_v1.0_re_pretrained.tar) |
| LayoutXLM | SER | 0.9056 | [链接](https://paddleocr.bj.bcebos.com/pplayout/PP-Layout_v1.0_ser_pretrained.tar) | | LayoutXLM | SER | 0.9056 | [Link](https://paddleocr.bj.bcebos.com/pplayout/PP-Layout_v1.0_ser_pretrained.tar) |
| LayoutLM | SER | 0.78 | [链接](https://paddleocr.bj.bcebos.com/pplayout/LayoutLM_ser_pretrained.tar) | | LayoutLM | SER | 0.78 | [Link](https://paddleocr.bj.bcebos.com/pplayout/LayoutLM_ser_pretrained.tar) |
## 2.效果演示 ## 2.Demonstration
**注意:** 测试图片来源于XFUN数据集。 **Note**: the test images are from the xfun dataset.
### 2.1 SER ### 2.1 SER
![](./images/result_ser/zh_val_0_ser.jpg) | ![](./images/result_ser/zh_val_42_ser.jpg) ![](./images/result_ser/zh_val_0_ser.jpg) | ![](./images/result_ser/zh_val_42_ser.jpg)
---|--- ---|---
图中不同颜色的框表示不同的类别,对于XFUN数据集,有`QUESTION`, `ANSWER`, `HEADER` 3种类别 Different colored boxes in the figure represent different categories. For xfun dataset, there are three categories: query, answer and header:
* 深紫色:HEADER * Dark purple: header
* 浅紫色:QUESTION * Light purple: query
* 军绿色:ANSWER * Army green: answer
在OCR检测框的左上方也标出了对应的类别和OCR识别结果。 The corresponding category and OCR recognition results are also marked at the top left of the OCR detection box.
### 2.2 RE ### 2.2 RE
...@@ -51,79 +54,89 @@ PP-Structure 里的 DOC-VQA算法基于PaddleNLP自然语言处理算法库进 ...@@ -51,79 +54,89 @@ PP-Structure 里的 DOC-VQA算法基于PaddleNLP自然语言处理算法库进
---|--- ---|---
图中红色框表示问题,蓝色框表示答案,问题和答案之间使用绿色线连接。在OCR检测框的左上方也标出了对应的类别和OCR识别结果。 In the figure, the red box represents the question, the blue box represents the answer, and the question and answer are connected by green lines. The corresponding category and OCR recognition results are also marked at the top left of the OCR detection box.
## 3.安装 ## 3. Setup
### 3.1 安装依赖 ### 3.1 Installation dependency
- **(1) 安装PaddlePaddle** - **(1) Install PaddlePaddle**
```bash ```bash
python3 -m pip install --upgrade pip pip3 install --upgrade pip
# GPU安装 # GPU PaddlePaddle Install
python3 -m pip install paddlepaddle-gpu==2.2 -i https://mirror.baidu.com/pypi/simple python3 -m pip install paddlepaddle-gpu==2.2 -i https://mirror.baidu.com/pypi/simple
# CPU安装 # CPU PaddlePaddle Install
python3 -m pip install paddlepaddle==2.2 -i https://mirror.baidu.com/pypi/simple python3 -m pip install paddlepaddle==2.2 -i https://mirror.baidu.com/pypi/simple
``` ```
更多需求,请参照[安装文档](https://www.paddlepaddle.org.cn/install/quick)中的说明进行操作。 For more requirements, please refer to the [instructions](https://www.paddlepaddle.org.cn/install/quick) in the installation document.
### 3.2 安装PaddleOCR(包含 PP-OCR 和 VQA ) ### 3.2 Install PaddleOCR (including pp-ocr and VQA)
- **(1)pip快速安装PaddleOCR whl包(仅预测)** - **(1) PIP quick install paddleocr WHL package (forecast only)**
```bash ```bash
python3 -m pip install paddleocr pip install paddleocr
``` ```
- **(2)下载VQA源码(预测+训练)** - **(2) Download VQA source code (prediction + training)**
```bash ```bash
【推荐】git clone https://github.com/PaddlePaddle/PaddleOCR [recommended] git clone https://github.com/PaddlePaddle/PaddleOCR
# 如果因为网络问题无法pull成功,也可选择使用码云上的托管: # If you cannot pull successfully because of network problems, you can also choose to use the hosting on the code cloud:
git clone https://gitee.com/paddlepaddle/PaddleOCR git clone https://gitee.com/paddlepaddle/PaddleOCR
# 注:码云托管代码可能无法实时同步本github项目更新,存在3~5天延时,请优先使用推荐方式。 # Note: the code cloud hosting code may not be able to synchronize the update of this GitHub project in real time, with a delay of 3 ~ 5 days. Please give priority to the recommended method.
```
- **(3) Install PaddleNLP**
```bash
# You need to use the latest code version of paddlenlp for installation
git clone https://github.com/PaddlePaddle/PaddleNLP -b develop
cd PaddleNLP
pip3 install -e .
``` ```
- **(3)安装VQA的`requirements`**
- **(4) Install requirements for VQA**
```bash ```bash
cd ppstructure/vqa cd ppstructure/vqa
python3 -m pip install -r requirements.txt pip install -r requirements.txt
``` ```
## 4. 使用 ## 4.Usage
### 4.1 数据和预训练模型准备 ### 4.1 Data and pre training model preparation
处理好的XFUN中文数据集下载地址:[https://paddleocr.bj.bcebos.com/dataset/XFUND.tar](https://paddleocr.bj.bcebos.com/dataset/XFUND.tar) Download address of processed xfun Chinese dataset: [https://paddleocr.bj.bcebos.com/dataset/XFUND.tar](https://paddleocr.bj.bcebos.com/dataset/XFUND.tar)
下载并解压该数据集,解压后将数据集放置在当前目录下。 Download and unzip the dataset, and then place the dataset in the current directory.
```shell ```shell
wget https://paddleocr.bj.bcebos.com/dataset/XFUND.tar wget https://paddleocr.bj.bcebos.com/dataset/XFUND.tar
``` ```
如果希望转换XFUN中其他语言的数据集,可以参考[XFUN数据转换脚本](helper/trans_xfun_data.py) If you want to convert data sets in other languages in xfun, you can refer to [xfun data conversion script.](helper/trans_xfun_data.py))
如果希望直接体验预测过程,可以下载我们提供的预训练模型,跳过训练过程,直接预测即可。 If you want to experience the prediction process directly, you can download the pre training model provided by us, skip the training process and predict directly.
### 4.2 SER任务 ### 4.2 SER Task
* 启动训练 * Start training
```shell ```shell
python3 train_ser.py \ python3.7 train_ser.py \
--model_name_or_path "layoutxlm-base-uncased" \ --model_name_or_path "layoutxlm-base-uncased" \
--ser_model_type "LayoutXLM" \ --ser_model_type "LayoutXLM" \
--train_data_dir "XFUND/zh_train/image" \ --train_data_dir "XFUND/zh_train/image" \
...@@ -139,12 +152,12 @@ python3 train_ser.py \ ...@@ -139,12 +152,12 @@ python3 train_ser.py \
--seed 2048 --seed 2048
``` ```
最终会打印出`precision`, `recall`, `f1`等指标,模型和训练日志会保存在`./output/ser/`文件夹中。 Finally, Precision, Recall, F1 and other indicators will be printed, and the model and training log will be saved in/ In the output/Ser/ folder.
* 恢复训练 * Recovery training
```shell ```shell
python3 train_ser.py \ python3.7 train_ser.py \
--model_name_or_path "model_path" \ --model_name_or_path "model_path" \
--ser_model_type "LayoutXLM" \ --ser_model_type "LayoutXLM" \
--train_data_dir "XFUND/zh_train/image" \ --train_data_dir "XFUND/zh_train/image" \
...@@ -162,7 +175,7 @@ python3 train_ser.py \ ...@@ -162,7 +175,7 @@ python3 train_ser.py \
--resume --resume
``` ```
* 评估 * Evaluation
```shell ```shell
export CUDA_VISIBLE_DEVICES=0 export CUDA_VISIBLE_DEVICES=0
python3 eval_ser.py \ python3 eval_ser.py \
...@@ -175,13 +188,13 @@ python3 eval_ser.py \ ...@@ -175,13 +188,13 @@ python3 eval_ser.py \
--output_dir "output/ser/" \ --output_dir "output/ser/" \
--seed 2048 --seed 2048
``` ```
最终会打印出`precision`, `recall`, `f1`等指标 Finally, Precision, Recall, F1 and other indicators will be printed
* 使用评估集合中提供的OCR识别结果进行预测 * The OCR recognition results provided in the evaluation set are used for prediction
```shell ```shell
export CUDA_VISIBLE_DEVICES=0 export CUDA_VISIBLE_DEVICES=0
python3 infer_ser.py \ python3.7 infer_ser.py \
--model_name_or_path "PP-Layout_v1.0_ser_pretrained/" \ --model_name_or_path "PP-Layout_v1.0_ser_pretrained/" \
--ser_model_type "LayoutXLM" \ --ser_model_type "LayoutXLM" \
--output_dir "output/ser/" \ --output_dir "output/ser/" \
...@@ -189,13 +202,13 @@ python3 infer_ser.py \ ...@@ -189,13 +202,13 @@ python3 infer_ser.py \
--ocr_json_path "XFUND/zh_val/xfun_normalize_val.json" --ocr_json_path "XFUND/zh_val/xfun_normalize_val.json"
``` ```
最终会在`output_res`目录下保存预测结果可视化图像以及预测结果文本文件,文件名为`infer_results.txt` It will end up in output_res The visual image of the prediction result and the text file of the prediction result are saved in the res directory. The file name is infer_ results.txt.
* 使用`OCR引擎 + SER`串联结果 * Using OCR engine + SER concatenation results
```shell ```shell
export CUDA_VISIBLE_DEVICES=0 export CUDA_VISIBLE_DEVICES=0
python3 infer_ser_e2e.py \ python3.7 infer_ser_e2e.py \
--model_name_or_path "PP-Layout_v1.0_ser_pretrained/" \ --model_name_or_path "PP-Layout_v1.0_ser_pretrained/" \
--ser_model_type "LayoutXLM" \ --ser_model_type "LayoutXLM" \
--max_seq_length 512 \ --max_seq_length 512 \
...@@ -203,17 +216,17 @@ python3 infer_ser_e2e.py \ ...@@ -203,17 +216,17 @@ python3 infer_ser_e2e.py \
--infer_imgs "images/input/zh_val_0.jpg" --infer_imgs "images/input/zh_val_0.jpg"
``` ```
* `OCR引擎 + SER`预测系统进行端到端评估 * End-to-end evaluation of OCR engine + SER prediction system
```shell ```shell
export CUDA_VISIBLE_DEVICES=0 export CUDA_VISIBLE_DEVICES=0
python3 helper/eval_with_label_end2end.py --gt_json_path XFUND/zh_val/xfun_normalize_val.json --pred_json_path output_res/infer_results.txt python3.7 helper/eval_with_label_end2end.py --gt_json_path XFUND/zh_val/xfun_normalize_val.json --pred_json_path output_res/infer_results.txt
``` ```
### 4.3 RE任务 ### 4.3 RE Task
* 启动训练 * Start training
```shell ```shell
export CUDA_VISIBLE_DEVICES=0 export CUDA_VISIBLE_DEVICES=0
...@@ -237,7 +250,7 @@ python3 train_re.py \ ...@@ -237,7 +250,7 @@ python3 train_re.py \
``` ```
* 恢复训练 * Resume training
```shell ```shell
export CUDA_VISIBLE_DEVICES=0 export CUDA_VISIBLE_DEVICES=0
...@@ -262,9 +275,9 @@ python3 train_re.py \ ...@@ -262,9 +275,9 @@ python3 train_re.py \
``` ```
最终会打印出`precision`, `recall`, `f1`等指标,模型和训练日志会保存在`./output/re/`文件夹中。 Finally, Precision, Recall, F1 and other indicators will be printed, and the model and training log will be saved in the output/RE file folder.
* 评估 * Evaluation
```shell ```shell
export CUDA_VISIBLE_DEVICES=0 export CUDA_VISIBLE_DEVICES=0
python3 eval_re.py \ python3 eval_re.py \
...@@ -278,10 +291,10 @@ python3 eval_re.py \ ...@@ -278,10 +291,10 @@ python3 eval_re.py \
--num_workers 8 \ --num_workers 8 \
--seed 2048 --seed 2048
``` ```
最终会打印出`precision`, `recall`, `f1`等指标 Finally, Precision, Recall, F1 and other indicators will be printed
* 使用评估集合中提供的OCR识别结果进行预测 * The OCR recognition results provided in the evaluation set are used for prediction
```shell ```shell
export CUDA_VISIBLE_DEVICES=0 export CUDA_VISIBLE_DEVICES=0
...@@ -296,13 +309,13 @@ python3 infer_re.py \ ...@@ -296,13 +309,13 @@ python3 infer_re.py \
--seed 2048 --seed 2048
``` ```
最终会在`output_res`目录下保存预测结果可视化图像以及预测结果文本文件,文件名为`infer_results.txt` The visual image of the prediction result and the text file of the prediction result are saved in the output_res file folder, the file name is`infer_results.txt`
* 使用`OCR引擎 + SER + RE`串联结果 * Concatenation results using OCR engine + SER+ RE
```shell ```shell
export CUDA_VISIBLE_DEVICES=0 export CUDA_VISIBLE_DEVICES=0
python3 infer_ser_re_e2e.py \ python3.7 infer_ser_re_e2e.py \
--model_name_or_path "PP-Layout_v1.0_ser_pretrained/" \ --model_name_or_path "PP-Layout_v1.0_ser_pretrained/" \
--re_model_name_or_path "PP-Layout_v1.0_re_pretrained/" \ --re_model_name_or_path "PP-Layout_v1.0_re_pretrained/" \
--ser_model_type "LayoutXLM" \ --ser_model_type "LayoutXLM" \
...@@ -311,7 +324,7 @@ python3 infer_ser_re_e2e.py \ ...@@ -311,7 +324,7 @@ python3 infer_ser_re_e2e.py \
--infer_imgs "images/input/zh_val_21.jpg" --infer_imgs "images/input/zh_val_21.jpg"
``` ```
## 参考链接 ## Reference
- LayoutXLM: Multimodal Pre-training for Multilingual Visually-rich Document Understanding, https://arxiv.org/pdf/2104.08836.pdf - 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 - microsoft/unilm/layoutxlm, https://github.com/microsoft/unilm/tree/master/layoutxlm
......
# Document Visual Q&A(DOC-VQA) # 文档视觉问答(DOC-VQA)
Document Visual Q&A, mainly for the image content of the question and answer, DOC-VQA is a type of VQA task, DOC-VQA mainly asks questions about the textual content of text images. VQA指视觉问答,主要针对图像内容进行提问和回答,DOC-VQA是VQA任务中的一种,DOC-VQA主要针对文本图像的文字内容提出问题。
The DOC-VQA algorithm in PP-Structure is developed based on PaddleNLP natural language processing algorithm library. PP-Structure 里的 DOC-VQA算法基于PaddleNLP自然语言处理算法库进行开发。
The main features are as follows: 主要特性如下:
- Integrated LayoutXLM model and PP-OCR prediction engine. - 集成[LayoutXLM](https://arxiv.org/pdf/2104.08836.pdf)模型以及PP-OCR预测引擎。
- Support Semantic Entity Recognition (SER) and Relation Extraction (RE) tasks based on multi-modal methods. Based on SER task, text recognition and classification in images can be completed. Based on THE RE task, we can extract the relation of the text content in the image, such as judge the problem pair. - 支持基于多模态方法的语义实体识别 (Semantic Entity Recognition, SER) 以及关系抽取 (Relation Extraction, RE) 任务。基于 SER 任务,可以完成对图像中的文本识别与分类;基于 RE 任务,可以完成对图象中的文本内容的关系提取,如判断问题对(pair)。
- 支持SER任务和RE任务的自定义训练。
- 支持OCR+SER的端到端系统预测与评估。
- 支持OCR+SER+RE的端到端系统预测。
- Support custom training for SER and RE tasks. **Note**:本项目基于 [LayoutXLM](https://arxiv.org/pdf/2104.08836.pdf) 在Paddle 2.2上的开源实现,同时经过飞桨团队与**中国工商银行**在不动产证场景深入打磨,联合开源。
- Support OCR+SER end-to-end system prediction and evaluation.
- Support OCR+SER+RE end-to-end system prediction. ## 1.性能
**Note**: This project is based on the open source implementation of [LayoutXLM](https://arxiv.org/pdf/2104.08836.pdf) on Paddle 2.2, and at the same time, after in-depth polishing by the flying Paddle team and the Industrial and **Commercial Bank of China** in the scene of real estate certificate, jointly open source. 我们在 [XFUN](https://github.com/doc-analysis/XFUND) 的中文数据集上对算法进行了评估,性能如下
| 模型 | 任务 | f1 | 模型下载地址 |
## 1.Performance
We evaluated the algorithm on [XFUN](https://github.com/doc-analysis/XFUND) 's Chinese data set, and the performance is as follows
| Model | Task | F1 | Model Download Link |
|:---:|:---:|:---:| :---:| |:---:|:---:|:---:| :---:|
| LayoutXLM | RE | 0.7113 | [Link](https://paddleocr.bj.bcebos.com/pplayout/PP-Layout_v1.0_re_pretrained.tar) | | LayoutXLM | RE | 0.7113 | [链接](https://paddleocr.bj.bcebos.com/pplayout/PP-Layout_v1.0_re_pretrained.tar) |
| LayoutXLM | SER | 0.9056 | [Link](https://paddleocr.bj.bcebos.com/pplayout/PP-Layout_v1.0_ser_pretrained.tar) | | LayoutXLM | SER | 0.9056 | [链接](https://paddleocr.bj.bcebos.com/pplayout/PP-Layout_v1.0_ser_pretrained.tar) |
| LayoutLM | SER | 0.78 | [Link](https://paddleocr.bj.bcebos.com/pplayout/LayoutLM_ser_pretrained.tar) | | LayoutLM | SER | 0.78 | [链接](https://paddleocr.bj.bcebos.com/pplayout/LayoutLM_ser_pretrained.tar) |
## 2.Demonstration ## 2.效果演示
**Note**: the test images are from the xfun dataset. **注意:** 测试图片来源于XFUN数据集。
### 2.1 SER ### 2.1 SER
![](./images/result_ser/zh_val_0_ser.jpg) | ![](./images/result_ser/zh_val_42_ser.jpg) ![](./images/result_ser/zh_val_0_ser.jpg) | ![](./images/result_ser/zh_val_42_ser.jpg)
---|--- ---|---
Different colored boxes in the figure represent different categories. For xfun dataset, there are three categories: query, answer and header: 图中不同颜色的框表示不同的类别,对于XFUN数据集,有`QUESTION`, `ANSWER`, `HEADER` 3种类别
* Dark purple: header * 深紫色:HEADER
* Light purple: query * 浅紫色:QUESTION
* Army green: answer * 军绿色:ANSWER
The corresponding category and OCR recognition results are also marked at the top left of the OCR detection box. 在OCR检测框的左上方也标出了对应的类别和OCR识别结果。
### 2.2 RE ### 2.2 RE
...@@ -54,89 +51,79 @@ The corresponding category and OCR recognition results are also marked at the to ...@@ -54,89 +51,79 @@ The corresponding category and OCR recognition results are also marked at the to
---|--- ---|---
In the figure, the red box represents the question, the blue box represents the answer, and the question and answer are connected by green lines. The corresponding category and OCR recognition results are also marked at the top left of the OCR detection box. 图中红色框表示问题,蓝色框表示答案,问题和答案之间使用绿色线连接。在OCR检测框的左上方也标出了对应的类别和OCR识别结果。
## 3. Setup ## 3.安装
### 3.1 Installation dependency ### 3.1 安装依赖
- **(1) Install PaddlePaddle** - **(1) 安装PaddlePaddle**
```bash ```bash
pip3 install --upgrade pip python3 -m pip install --upgrade pip
# GPU PaddlePaddle Install # GPU安装
python3 -m pip install paddlepaddle-gpu==2.2 -i https://mirror.baidu.com/pypi/simple python3 -m pip install paddlepaddle-gpu==2.2 -i https://mirror.baidu.com/pypi/simple
# CPU PaddlePaddle Install # CPU安装
python3 -m pip install paddlepaddle==2.2 -i https://mirror.baidu.com/pypi/simple python3 -m pip install paddlepaddle==2.2 -i https://mirror.baidu.com/pypi/simple
``` ```
For more requirements, please refer to the [instructions](https://www.paddlepaddle.org.cn/install/quick) in the installation document. 更多需求,请参照[安装文档](https://www.paddlepaddle.org.cn/install/quick)中的说明进行操作。
### 3.2 Install PaddleOCR (including pp-ocr and VQA) ### 3.2 安装PaddleOCR(包含 PP-OCR 和 VQA )
- **(1) PIP quick install paddleocr WHL package (forecast only)** - **(1)pip快速安装PaddleOCR whl包(仅预测)**
```bash ```bash
pip install paddleocr python3 -m pip install paddleocr
``` ```
- **(2) Download VQA source code (prediction + training)** - **(2)下载VQA源码(预测+训练)**
```bash ```bash
[recommended] git clone https://github.com/PaddlePaddle/PaddleOCR 【推荐】git clone https://github.com/PaddlePaddle/PaddleOCR
# If you cannot pull successfully because of network problems, you can also choose to use the hosting on the code cloud: # 如果因为网络问题无法pull成功,也可选择使用码云上的托管:
git clone https://gitee.com/paddlepaddle/PaddleOCR git clone https://gitee.com/paddlepaddle/PaddleOCR
# Note: the code cloud hosting code may not be able to synchronize the update of this GitHub project in real time, with a delay of 3 ~ 5 days. Please give priority to the recommended method. # 注:码云托管代码可能无法实时同步本github项目更新,存在3~5天延时,请优先使用推荐方式。
```
- **(3) Install PaddleNLP**
```bash
# You need to use the latest code version of paddlenlp for installation
git clone https://github.com/PaddlePaddle/PaddleNLP -b develop
cd PaddleNLP
pip3 install -e .
``` ```
- **(3)安装VQA的`requirements`**
- **(4) Install requirements for VQA**
```bash ```bash
cd ppstructure/vqa cd ppstructure/vqa
pip install -r requirements.txt python3 -m pip install -r requirements.txt
``` ```
## 4.Usage ## 4. 使用
### 4.1 Data and pre training model preparation ### 4.1 数据和预训练模型准备
Download address of processed xfun Chinese dataset: [https://paddleocr.bj.bcebos.com/dataset/XFUND.tar](https://paddleocr.bj.bcebos.com/dataset/XFUND.tar) 处理好的XFUN中文数据集下载地址:[https://paddleocr.bj.bcebos.com/dataset/XFUND.tar](https://paddleocr.bj.bcebos.com/dataset/XFUND.tar)
Download and unzip the dataset, and then place the dataset in the current directory. 下载并解压该数据集,解压后将数据集放置在当前目录下。
```shell ```shell
wget https://paddleocr.bj.bcebos.com/dataset/XFUND.tar wget https://paddleocr.bj.bcebos.com/dataset/XFUND.tar
``` ```
If you want to convert data sets in other languages in xfun, you can refer to [xfun data conversion script.](helper/trans_xfun_data.py)) 如果希望转换XFUN中其他语言的数据集,可以参考[XFUN数据转换脚本](helper/trans_xfun_data.py)
If you want to experience the prediction process directly, you can download the pre training model provided by us, skip the training process and predict directly. 如果希望直接体验预测过程,可以下载我们提供的预训练模型,跳过训练过程,直接预测即可。
### 4.2 SER Task ### 4.2 SER任务
* Start training * 启动训练
```shell ```shell
python3.7 train_ser.py \ python3 train_ser.py \
--model_name_or_path "layoutxlm-base-uncased" \ --model_name_or_path "layoutxlm-base-uncased" \
--ser_model_type "LayoutXLM" \ --ser_model_type "LayoutXLM" \
--train_data_dir "XFUND/zh_train/image" \ --train_data_dir "XFUND/zh_train/image" \
...@@ -152,12 +139,12 @@ python3.7 train_ser.py \ ...@@ -152,12 +139,12 @@ python3.7 train_ser.py \
--seed 2048 --seed 2048
``` ```
Finally, Precision, Recall, F1 and other indicators will be printed, and the model and training log will be saved in/ In the output/Ser/ folder. 最终会打印出`precision`, `recall`, `f1`等指标,模型和训练日志会保存在`./output/ser/`文件夹中。
* Recovery training * 恢复训练
```shell ```shell
python3.7 train_ser.py \ python3 train_ser.py \
--model_name_or_path "model_path" \ --model_name_or_path "model_path" \
--ser_model_type "LayoutXLM" \ --ser_model_type "LayoutXLM" \
--train_data_dir "XFUND/zh_train/image" \ --train_data_dir "XFUND/zh_train/image" \
...@@ -175,7 +162,7 @@ python3.7 train_ser.py \ ...@@ -175,7 +162,7 @@ python3.7 train_ser.py \
--resume --resume
``` ```
* Evaluation * 评估
```shell ```shell
export CUDA_VISIBLE_DEVICES=0 export CUDA_VISIBLE_DEVICES=0
python3 eval_ser.py \ python3 eval_ser.py \
...@@ -188,13 +175,13 @@ python3 eval_ser.py \ ...@@ -188,13 +175,13 @@ python3 eval_ser.py \
--output_dir "output/ser/" \ --output_dir "output/ser/" \
--seed 2048 --seed 2048
``` ```
Finally, Precision, Recall, F1 and other indicators will be printed 最终会打印出`precision`, `recall`, `f1`等指标
* The OCR recognition results provided in the evaluation set are used for prediction * 使用评估集合中提供的OCR识别结果进行预测
```shell ```shell
export CUDA_VISIBLE_DEVICES=0 export CUDA_VISIBLE_DEVICES=0
python3.7 infer_ser.py \ python3 infer_ser.py \
--model_name_or_path "PP-Layout_v1.0_ser_pretrained/" \ --model_name_or_path "PP-Layout_v1.0_ser_pretrained/" \
--ser_model_type "LayoutXLM" \ --ser_model_type "LayoutXLM" \
--output_dir "output/ser/" \ --output_dir "output/ser/" \
...@@ -202,13 +189,13 @@ python3.7 infer_ser.py \ ...@@ -202,13 +189,13 @@ python3.7 infer_ser.py \
--ocr_json_path "XFUND/zh_val/xfun_normalize_val.json" --ocr_json_path "XFUND/zh_val/xfun_normalize_val.json"
``` ```
It will end up in output_res The visual image of the prediction result and the text file of the prediction result are saved in the res directory. The file name is infer_ results.txt. 最终会在`output_res`目录下保存预测结果可视化图像以及预测结果文本文件,文件名为`infer_results.txt`
* Using OCR engine + SER concatenation results * 使用`OCR引擎 + SER`串联结果
```shell ```shell
export CUDA_VISIBLE_DEVICES=0 export CUDA_VISIBLE_DEVICES=0
python3.7 infer_ser_e2e.py \ python3 infer_ser_e2e.py \
--model_name_or_path "PP-Layout_v1.0_ser_pretrained/" \ --model_name_or_path "PP-Layout_v1.0_ser_pretrained/" \
--ser_model_type "LayoutXLM" \ --ser_model_type "LayoutXLM" \
--max_seq_length 512 \ --max_seq_length 512 \
...@@ -216,17 +203,17 @@ python3.7 infer_ser_e2e.py \ ...@@ -216,17 +203,17 @@ python3.7 infer_ser_e2e.py \
--infer_imgs "images/input/zh_val_0.jpg" --infer_imgs "images/input/zh_val_0.jpg"
``` ```
* End-to-end evaluation of OCR engine + SER prediction system * `OCR引擎 + SER`预测系统进行端到端评估
```shell ```shell
export CUDA_VISIBLE_DEVICES=0 export CUDA_VISIBLE_DEVICES=0
python3.7 helper/eval_with_label_end2end.py --gt_json_path XFUND/zh_val/xfun_normalize_val.json --pred_json_path output_res/infer_results.txt python3 helper/eval_with_label_end2end.py --gt_json_path XFUND/zh_val/xfun_normalize_val.json --pred_json_path output_res/infer_results.txt
``` ```
### 4.3 RE Task ### 4.3 RE任务
* Start training * 启动训练
```shell ```shell
export CUDA_VISIBLE_DEVICES=0 export CUDA_VISIBLE_DEVICES=0
...@@ -250,7 +237,7 @@ python3 train_re.py \ ...@@ -250,7 +237,7 @@ python3 train_re.py \
``` ```
* Resume training * 恢复训练
```shell ```shell
export CUDA_VISIBLE_DEVICES=0 export CUDA_VISIBLE_DEVICES=0
...@@ -275,9 +262,9 @@ python3 train_re.py \ ...@@ -275,9 +262,9 @@ python3 train_re.py \
``` ```
Finally, Precision, Recall, F1 and other indicators will be printed, and the model and training log will be saved in the output/RE file folder. 最终会打印出`precision`, `recall`, `f1`等指标,模型和训练日志会保存在`./output/re/`文件夹中。
* Evaluation * 评估
```shell ```shell
export CUDA_VISIBLE_DEVICES=0 export CUDA_VISIBLE_DEVICES=0
python3 eval_re.py \ python3 eval_re.py \
...@@ -291,10 +278,10 @@ python3 eval_re.py \ ...@@ -291,10 +278,10 @@ python3 eval_re.py \
--num_workers 8 \ --num_workers 8 \
--seed 2048 --seed 2048
``` ```
Finally, Precision, Recall, F1 and other indicators will be printed 最终会打印出`precision`, `recall`, `f1`等指标
* The OCR recognition results provided in the evaluation set are used for prediction * 使用评估集合中提供的OCR识别结果进行预测
```shell ```shell
export CUDA_VISIBLE_DEVICES=0 export CUDA_VISIBLE_DEVICES=0
...@@ -309,13 +296,13 @@ python3 infer_re.py \ ...@@ -309,13 +296,13 @@ python3 infer_re.py \
--seed 2048 --seed 2048
``` ```
The visual image of the prediction result and the text file of the prediction result are saved in the output_res file folder, the file name is`infer_results.txt` 最终会在`output_res`目录下保存预测结果可视化图像以及预测结果文本文件,文件名为`infer_results.txt`
* Concatenation results using OCR engine + SER+ RE * 使用`OCR引擎 + SER + RE`串联结果
```shell ```shell
export CUDA_VISIBLE_DEVICES=0 export CUDA_VISIBLE_DEVICES=0
python3.7 infer_ser_re_e2e.py \ python3 infer_ser_re_e2e.py \
--model_name_or_path "PP-Layout_v1.0_ser_pretrained/" \ --model_name_or_path "PP-Layout_v1.0_ser_pretrained/" \
--re_model_name_or_path "PP-Layout_v1.0_re_pretrained/" \ --re_model_name_or_path "PP-Layout_v1.0_re_pretrained/" \
--ser_model_type "LayoutXLM" \ --ser_model_type "LayoutXLM" \
...@@ -324,7 +311,7 @@ python3.7 infer_ser_re_e2e.py \ ...@@ -324,7 +311,7 @@ python3.7 infer_ser_re_e2e.py \
--infer_imgs "images/input/zh_val_21.jpg" --infer_imgs "images/input/zh_val_21.jpg"
``` ```
## Reference ## 参考链接
- LayoutXLM: Multimodal Pre-training for Multilingual Visually-rich Document Understanding, https://arxiv.org/pdf/2104.08836.pdf - 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 - microsoft/unilm/layoutxlm, https://github.com/microsoft/unilm/tree/master/layoutxlm
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册