Merge branch 'dygraph' of https://github.com/PaddlePaddle/PaddleOCR into table_pr
After Width: | Height: | Size: 181 KiB |
After Width: | Height: | Size: 175 KiB |
|
@ -0,0 +1,172 @@
|
|||
# 关键信息抽取算法-LayoutXLM
|
||||
|
||||
- [1. 算法简介](#1-算法简介)
|
||||
- [2. 环境配置](#2-环境配置)
|
||||
- [3. 模型训练、评估、预测](#3-模型训练评估预测)
|
||||
- [4. 推理部署](#4-推理部署)
|
||||
- [4.1 Python推理](#41-python推理)
|
||||
- [4.2 C++推理部署](#42-推理部署)
|
||||
- [4.3 Serving服务化部署](#43-serving服务化部署)
|
||||
- [4.4 更多推理部署](#44-更多推理部署)
|
||||
- [5. FAQ](#5-faq)
|
||||
- [引用](#引用)
|
||||
|
||||
|
||||
<a name="1"></a>
|
||||
|
||||
## 1. 算法简介
|
||||
|
||||
|
||||
论文信息:
|
||||
|
||||
> [LayoutXLM: Multimodal Pre-training for Multilingual Visually-rich Document Understanding](https://arxiv.org/abs/2104.08836)
|
||||
>
|
||||
> Yiheng Xu, Tengchao Lv, Lei Cui, Guoxin Wang, Yijuan Lu, Dinei Florencio, Cha Zhang, Furu Wei
|
||||
>
|
||||
> 2021
|
||||
|
||||
在XFUND_zh数据集上,算法复现效果如下:
|
||||
|
||||
|模型|骨干网络|任务|配置文件|hmean|下载链接|
|
||||
| --- | --- |--|--- | --- | --- |
|
||||
|LayoutXLM|LayoutXLM-base|SER |[ser_layoutxlm_xfund_zh.yml](../../configs/kie/layoutlm_series/ser_layoutxlm_xfund_zh.yml)|90.38%|[训练模型](https://paddleocr.bj.bcebos.com/pplayout/ser_LayoutXLM_xfun_zh.tar)/[推理模型](https://paddleocr.bj.bcebos.com/pplayout/ser_LayoutXLM_xfun_zh_infer.tar)|
|
||||
|LayoutXLM|LayoutXLM-base|RE | [re_layoutxlm_xfund_zh.yml](../../configs/kie/layoutlm_series/re_layoutxlm_xfund_zh.yml)|74.83%|[训练模型](https://paddleocr.bj.bcebos.com/pplayout/re_LayoutXLM_xfun_zh.tar)/[推理模型(coming soon)]()|
|
||||
|
||||
<a name="2"></a>
|
||||
|
||||
## 2. 环境配置
|
||||
请先参考[《运行环境准备》](./environment.md)配置PaddleOCR运行环境,参考[《项目克隆》](./clone.md)克隆项目代码。
|
||||
|
||||
|
||||
<a name="3"></a>
|
||||
|
||||
## 3. 模型训练、评估、预测
|
||||
|
||||
请参考[关键信息抽取教程](./kie.md)。PaddleOCR对代码进行了模块化,训练不同的关键信息抽取模型只需要**更换配置文件**即可。
|
||||
|
||||
|
||||
<a name="4"></a>
|
||||
## 4. 推理部署
|
||||
|
||||
<a name="4-1"></a>
|
||||
|
||||
### 4.1 Python推理
|
||||
|
||||
**注:** 目前RE任务推理过程仍在适配中,下面以SER任务为例,介绍基于LayoutXLM模型的关键信息抽取过程。
|
||||
|
||||
首先将训练得到的模型转换成inference model。LayoutXLM模型在XFUND_zh数据集上训练的模型为例([模型下载地址](https://paddleocr.bj.bcebos.com/pplayout/ser_LayoutXLM_xfun_zh.tar)),可以使用下面的命令进行转换。
|
||||
|
||||
``` bash
|
||||
wget https://paddleocr.bj.bcebos.com/pplayout/ser_LayoutXLM_xfun_zh.tar
|
||||
tar -xf ser_LayoutXLM_xfun_zh.tar
|
||||
python3 tools/export_model.py -c configs/kie/layoutlm_series/ser_layoutxlm_xfund_zh.yml -o Architecture.Backbone.checkpoints=./ser_LayoutXLM_xfun_zh/best_accuracy Global.save_inference_dir=./inference/ser_layoutxlm
|
||||
```
|
||||
|
||||
LayoutXLM模型基于SER任务进行推理,可以执行如下命令:
|
||||
|
||||
```bash
|
||||
cd ppstructure
|
||||
python3 vqa/predict_vqa_token_ser.py \
|
||||
--vqa_algorithm=LayoutXLM \
|
||||
--ser_model_dir=../inference/ser_layoutxlm_infer \
|
||||
--image_dir=./docs/vqa/input/zh_val_42.jpg \
|
||||
--ser_dict_path=../train_data/XFUND/class_list_xfun.txt \
|
||||
--vis_font_path=../doc/fonts/simfang.ttf
|
||||
```
|
||||
|
||||
SER可视化结果默认保存到`./output`文件夹里面,结果示例如下:
|
||||
|
||||
<div align="center">
|
||||
<img src="../../ppstructure/docs/vqa/result_ser/zh_val_42_ser.jpg" width="800">
|
||||
</div>
|
||||
|
||||
|
||||
<a name="4-2"></a>
|
||||
### 4.2 C++推理部署
|
||||
|
||||
暂不支持
|
||||
|
||||
<a name="4-3"></a>
|
||||
### 4.3 Serving服务化部署
|
||||
|
||||
暂不支持
|
||||
|
||||
<a name="4-4"></a>
|
||||
### 4.4 更多推理部署
|
||||
|
||||
暂不支持
|
||||
|
||||
<a name="5"></a>
|
||||
|
||||
## 5. FAQ
|
||||
|
||||
## 引用
|
||||
|
||||
|
||||
```bibtex
|
||||
@article{DBLP:journals/corr/abs-2104-08836,
|
||||
author = {Yiheng Xu and
|
||||
Tengchao Lv and
|
||||
Lei Cui and
|
||||
Guoxin Wang and
|
||||
Yijuan Lu and
|
||||
Dinei Flor{\^{e}}ncio and
|
||||
Cha Zhang and
|
||||
Furu Wei},
|
||||
title = {LayoutXLM: Multimodal Pre-training for Multilingual Visually-rich
|
||||
Document Understanding},
|
||||
journal = {CoRR},
|
||||
volume = {abs/2104.08836},
|
||||
year = {2021},
|
||||
url = {https://arxiv.org/abs/2104.08836},
|
||||
eprinttype = {arXiv},
|
||||
eprint = {2104.08836},
|
||||
timestamp = {Thu, 14 Oct 2021 09:17:23 +0200},
|
||||
biburl = {https://dblp.org/rec/journals/corr/abs-2104-08836.bib},
|
||||
bibsource = {dblp computer science bibliography, https://dblp.org}
|
||||
}
|
||||
|
||||
@article{DBLP:journals/corr/abs-1912-13318,
|
||||
author = {Yiheng Xu and
|
||||
Minghao Li and
|
||||
Lei Cui and
|
||||
Shaohan Huang and
|
||||
Furu Wei and
|
||||
Ming Zhou},
|
||||
title = {LayoutLM: Pre-training of Text and Layout for Document Image Understanding},
|
||||
journal = {CoRR},
|
||||
volume = {abs/1912.13318},
|
||||
year = {2019},
|
||||
url = {http://arxiv.org/abs/1912.13318},
|
||||
eprinttype = {arXiv},
|
||||
eprint = {1912.13318},
|
||||
timestamp = {Mon, 01 Jun 2020 16:20:46 +0200},
|
||||
biburl = {https://dblp.org/rec/journals/corr/abs-1912-13318.bib},
|
||||
bibsource = {dblp computer science bibliography, https://dblp.org}
|
||||
}
|
||||
|
||||
@article{DBLP:journals/corr/abs-2012-14740,
|
||||
author = {Yang Xu and
|
||||
Yiheng Xu and
|
||||
Tengchao Lv and
|
||||
Lei Cui and
|
||||
Furu Wei and
|
||||
Guoxin Wang and
|
||||
Yijuan Lu and
|
||||
Dinei A. F. Flor{\^{e}}ncio and
|
||||
Cha Zhang and
|
||||
Wanxiang Che and
|
||||
Min Zhang and
|
||||
Lidong Zhou},
|
||||
title = {LayoutLMv2: Multi-modal Pre-training for Visually-Rich Document Understanding},
|
||||
journal = {CoRR},
|
||||
volume = {abs/2012.14740},
|
||||
year = {2020},
|
||||
url = {https://arxiv.org/abs/2012.14740},
|
||||
eprinttype = {arXiv},
|
||||
eprint = {2012.14740},
|
||||
timestamp = {Tue, 27 Jul 2021 09:53:52 +0200},
|
||||
biburl = {https://dblp.org/rec/journals/corr/abs-2012-14740.bib},
|
||||
bibsource = {dblp computer science bibliography, https://dblp.org}
|
||||
}
|
||||
```
|
|
@ -0,0 +1,144 @@
|
|||
|
||||
# 关键信息抽取算法-SDMGR
|
||||
|
||||
- [1. 算法简介](#1-算法简介)
|
||||
- [2. 环境配置](#2-环境配置)
|
||||
- [3. 模型训练、评估、预测](#3-模型训练评估预测)
|
||||
- [3.1 模型训练](#31-模型训练)
|
||||
- [3.2 模型评估](#32-模型评估)
|
||||
- [3.3 模型预测](#33-模型预测)
|
||||
- [4. 推理部署](#4-推理部署)
|
||||
- [4.1 Python推理](#41-python推理)
|
||||
- [4.2 C++推理部署](#42-c推理部署)
|
||||
- [4.3 Serving服务化部署](#43-serving服务化部署)
|
||||
- [4.4 更多推理部署](#44-更多推理部署)
|
||||
- [5. FAQ](#5-faq)
|
||||
- [引用](#引用)
|
||||
|
||||
|
||||
<a name="1"></a>
|
||||
|
||||
## 1. 算法简介
|
||||
|
||||
|
||||
论文信息:
|
||||
|
||||
> [Spatial Dual-Modality Graph Reasoning for Key Information Extraction](https://arxiv.org/abs/2103.14470)
|
||||
>
|
||||
> Hongbin Sun and Zhanghui Kuang and Xiaoyu Yue and Chenhao Lin and Wayne Zhang
|
||||
>
|
||||
> 2021
|
||||
|
||||
在wildreceipt发票公开数据集上,算法复现效果如下:
|
||||
|
||||
|模型|骨干网络|配置文件|hmean|下载链接|
|
||||
| --- | --- | --- | --- | --- |
|
||||
|SDMGR|VGG6|[configs/kie/sdmgr/kie_unet_sdmgr.yml](../../configs/kie/sdmgr/kie_unet_sdmgr.yml)|86.7%|[训练模型]( https://paddleocr.bj.bcebos.com/dygraph_v2.1/kie/kie_vgg16.tar)/[推理模型(coming soon)]()|
|
||||
|
||||
|
||||
<a name="2"></a>
|
||||
|
||||
## 2. 环境配置
|
||||
请先参考[《运行环境准备》](./environment.md)配置PaddleOCR运行环境,参考[《项目克隆》](./clone.md)克隆项目代码。
|
||||
|
||||
|
||||
<a name="3"></a>
|
||||
|
||||
## 3. 模型训练、评估、预测
|
||||
|
||||
SDMGR是一个关键信息提取算法,将每个检测到的文本区域分类为预定义的类别,如订单ID、发票号码,金额等。
|
||||
|
||||
训练和测试的数据采用wildreceipt数据集,通过如下指令下载数据集:
|
||||
|
||||
```bash
|
||||
wget https://paddleocr.bj.bcebos.com/ppstructure/dataset/wildreceipt.tar && tar xf wildreceipt.tar
|
||||
```
|
||||
|
||||
创建数据集软链到PaddleOCR/train_data目录下:
|
||||
```
|
||||
cd PaddleOCR/ && mkdir train_data && cd train_data
|
||||
|
||||
ln -s ../../wildreceipt ./
|
||||
```
|
||||
|
||||
|
||||
### 3.1 模型训练
|
||||
|
||||
训练采用的配置文件是`configs/kie/sdmgr/kie_unet_sdmgr.yml`,配置文件中默认训练数据路径是`train_data/wildreceipt`,准备好数据后,可以通过如下指令执行训练:
|
||||
|
||||
```
|
||||
python3 tools/train.py -c configs/kie/sdmgr/kie_unet_sdmgr.yml -o Global.save_model_dir=./output/kie/
|
||||
```
|
||||
|
||||
### 3.2 模型评估
|
||||
|
||||
执行下面的命令进行模型评估
|
||||
|
||||
```bash
|
||||
python3 tools/eval.py -c configs/kie/sdmgr/kie_unet_sdmgr.yml -o Global.checkpoints=./output/kie/best_accuracy
|
||||
```
|
||||
|
||||
输出信息示例如下所示。
|
||||
|
||||
```py
|
||||
[2022/08/10 05:22:23] ppocr INFO: metric eval ***************
|
||||
[2022/08/10 05:22:23] ppocr INFO: hmean:0.8670120239257812
|
||||
[2022/08/10 05:22:23] ppocr INFO: fps:10.18816520530961
|
||||
```
|
||||
|
||||
### 3.3 模型预测
|
||||
|
||||
执行下面的命令进行模型预测,预测的时候需要预先加载存储图片路径以及OCR信息的文本文件,使用`Global.infer_img`进行指定。
|
||||
|
||||
```bash
|
||||
python3 tools/infer_kie.py -c configs/kie/kie_unet_sdmgr.yml -o Global.checkpoints=kie_vgg16/best_accuracy Global.infer_img=./train_data/wildreceipt/1.txt
|
||||
```
|
||||
|
||||
执行预测后的结果保存在`./output/sdmgr_kie/predicts_kie.txt`文件中,可视化结果保存在`/output/sdmgr_kie/kie_results/`目录下。
|
||||
|
||||
可视化结果如下图所示:
|
||||
|
||||
<div align="center">
|
||||
<img src="../../ppstructure/docs/imgs/sdmgr_result.png" width="800">
|
||||
</div>
|
||||
|
||||
<a name="4"></a>
|
||||
## 4. 推理部署
|
||||
|
||||
<a name="4-1"></a>
|
||||
### 4.1 Python推理
|
||||
|
||||
暂不支持
|
||||
|
||||
<a name="4-2"></a>
|
||||
### 4.2 C++推理部署
|
||||
|
||||
暂不支持
|
||||
|
||||
<a name="4-3"></a>
|
||||
### 4.3 Serving服务化部署
|
||||
|
||||
暂不支持
|
||||
|
||||
<a name="4-4"></a>
|
||||
### 4.4 更多推理部署
|
||||
|
||||
暂不支持
|
||||
|
||||
<a name="5"></a>
|
||||
|
||||
## 5. FAQ
|
||||
|
||||
## 引用
|
||||
|
||||
|
||||
```bibtex
|
||||
@misc{sun2021spatial,
|
||||
title={Spatial Dual-Modality Graph Reasoning for Key Information Extraction},
|
||||
author={Hongbin Sun and Zhanghui Kuang and Xiaoyu Yue and Chenhao Lin and Wayne Zhang},
|
||||
year={2021},
|
||||
eprint={2103.14470},
|
||||
archivePrefix={arXiv},
|
||||
primaryClass={cs.CV}
|
||||
}
|
||||
```
|
|
@ -0,0 +1,166 @@
|
|||
# 关键信息抽取算法-VI-LayoutXLM
|
||||
|
||||
- [1. 算法简介](#1-算法简介)
|
||||
- [2. 环境配置](#2-环境配置)
|
||||
- [3. 模型训练、评估、预测](#3-模型训练评估预测)
|
||||
- [4. 推理部署](#4-推理部署)
|
||||
- [4.1 Python推理](#41-python推理)
|
||||
- [4.2 C++推理部署](#42-c推理部署)
|
||||
- [4.3 Serving服务化部署](#43-serving服务化部署)
|
||||
- [4.4 更多推理部署](#44-更多推理部署)
|
||||
- [5. FAQ](#5-faq)
|
||||
- [引用](#引用)
|
||||
|
||||
|
||||
<a name="1"></a>
|
||||
|
||||
## 1. 算法简介
|
||||
|
||||
VI-LayoutXLM基于LayoutXLM进行改进,在下游任务训练过程中,去除视觉骨干网络模块,最终精度基本无损的情况下,模型推理速度进一步提升。
|
||||
|
||||
在XFUND_zh数据集上,算法复现效果如下:
|
||||
|
||||
|模型|骨干网络|任务|配置文件|hmean|下载链接|
|
||||
| --- | --- |---| --- | --- | --- |
|
||||
|VI-LayoutXLM |VI-LayoutXLM-base | SER |[ser_vi_layoutxlm_xfund_zh_udml.yml](../../configs/kie/vi_layoutxlm/ser_vi_layoutxlm_xfund_zh_udml.yml)|93.19%|[训练模型](https://paddleocr.bj.bcebos.com/ppstructure/models/vi_layoutxlm/ser_vi_layoutxlm_xfund_pretrained.tar)/[推理模型](https://paddleocr.bj.bcebos.com/ppstructure/models/vi_layoutxlm/ser_vi_layoutxlm_xfund_infer.tar)|
|
||||
|VI-LayoutXLM |VI-LayoutXLM-base |RE | [re_vi_layoutxlm_xfund_zh_udml.yml](../../configs/kie/vi_layoutxlm/re_vi_layoutxlm_xfund_zh_udml.yml)|83.92%|[训练模型](https://paddleocr.bj.bcebos.com/ppstructure/models/vi_layoutxlm/re_vi_layoutxlm_xfund_pretrained.tar)/[推理模型(coming soon)]()|
|
||||
|
||||
<a name="2"></a>
|
||||
|
||||
## 2. 环境配置
|
||||
请先参考[《运行环境准备》](./environment.md)配置PaddleOCR运行环境,参考[《项目克隆》](./clone.md)克隆项目代码。
|
||||
|
||||
|
||||
<a name="3"></a>
|
||||
|
||||
## 3. 模型训练、评估、预测
|
||||
|
||||
请参考[关键信息抽取教程](./kie.md)。PaddleOCR对代码进行了模块化,训练不同的关键信息抽取模型只需要**更换配置文件**即可。
|
||||
|
||||
|
||||
<a name="4"></a>
|
||||
## 4. 推理部署
|
||||
|
||||
<a name="4-1"></a>
|
||||
|
||||
### 4.1 Python推理
|
||||
|
||||
**注:** 目前RE任务推理过程仍在适配中,下面以SER任务为例,介绍基于VI-LayoutXLM模型的关键信息抽取过程。
|
||||
|
||||
首先将训练得到的模型转换成inference model。以VI-LayoutXLM模型在XFUND_zh数据集上训练的模型为例([模型下载地址](https://paddleocr.bj.bcebos.com/ppstructure/models/vi_layoutxlm/ser_vi_layoutxlm_xfund_pretrained.tar)),可以使用下面的命令进行转换。
|
||||
|
||||
``` bash
|
||||
wget https://paddleocr.bj.bcebos.com/ppstructure/models/vi_layoutxlm/ser_vi_layoutxlm_xfund_pretrained.tar
|
||||
tar -xf ser_vi_layoutxlm_xfund_pretrained.tar
|
||||
python3 tools/export_model.py -c configs/kie/vi_layoutxlm/ser_vi_layoutxlm_xfund_zh.yml -o Architecture.Backbone.checkpoints=./ser_vi_layoutxlm_xfund_pretrained/best_accuracy Global.save_inference_dir=./inference/ser_vi_layoutxlm_infer
|
||||
```
|
||||
|
||||
VI-LayoutXLM模型基于SER任务进行推理,可以执行如下命令:
|
||||
|
||||
```bash
|
||||
cd ppstructure
|
||||
python3 vqa/predict_vqa_token_ser.py \
|
||||
--vqa_algorithm=LayoutXLM \
|
||||
--ser_model_dir=../inference/ser_vi_layoutxlm_infer \
|
||||
--image_dir=./docs/vqa/input/zh_val_42.jpg \
|
||||
--ser_dict_path=../train_data/XFUND/class_list_xfun.txt \
|
||||
--vis_font_path=../doc/fonts/simfang.ttf \
|
||||
--ocr_order_method="tb-yx"
|
||||
```
|
||||
|
||||
SER可视化结果默认保存到`./output`文件夹里面,结果示例如下:
|
||||
|
||||
<div align="center">
|
||||
<img src="../../ppstructure/docs/vqa/result_ser/zh_val_42_ser.jpg" width="800">
|
||||
</div>
|
||||
|
||||
|
||||
<a name="4-2"></a>
|
||||
### 4.2 C++推理部署
|
||||
|
||||
暂不支持
|
||||
|
||||
<a name="4-3"></a>
|
||||
### 4.3 Serving服务化部署
|
||||
|
||||
暂不支持
|
||||
|
||||
<a name="4-4"></a>
|
||||
### 4.4 更多推理部署
|
||||
|
||||
暂不支持
|
||||
|
||||
<a name="5"></a>
|
||||
|
||||
## 5. FAQ
|
||||
|
||||
## 引用
|
||||
|
||||
|
||||
```bibtex
|
||||
@article{DBLP:journals/corr/abs-2104-08836,
|
||||
author = {Yiheng Xu and
|
||||
Tengchao Lv and
|
||||
Lei Cui and
|
||||
Guoxin Wang and
|
||||
Yijuan Lu and
|
||||
Dinei Flor{\^{e}}ncio and
|
||||
Cha Zhang and
|
||||
Furu Wei},
|
||||
title = {LayoutXLM: Multimodal Pre-training for Multilingual Visually-rich
|
||||
Document Understanding},
|
||||
journal = {CoRR},
|
||||
volume = {abs/2104.08836},
|
||||
year = {2021},
|
||||
url = {https://arxiv.org/abs/2104.08836},
|
||||
eprinttype = {arXiv},
|
||||
eprint = {2104.08836},
|
||||
timestamp = {Thu, 14 Oct 2021 09:17:23 +0200},
|
||||
biburl = {https://dblp.org/rec/journals/corr/abs-2104-08836.bib},
|
||||
bibsource = {dblp computer science bibliography, https://dblp.org}
|
||||
}
|
||||
|
||||
@article{DBLP:journals/corr/abs-1912-13318,
|
||||
author = {Yiheng Xu and
|
||||
Minghao Li and
|
||||
Lei Cui and
|
||||
Shaohan Huang and
|
||||
Furu Wei and
|
||||
Ming Zhou},
|
||||
title = {LayoutLM: Pre-training of Text and Layout for Document Image Understanding},
|
||||
journal = {CoRR},
|
||||
volume = {abs/1912.13318},
|
||||
year = {2019},
|
||||
url = {http://arxiv.org/abs/1912.13318},
|
||||
eprinttype = {arXiv},
|
||||
eprint = {1912.13318},
|
||||
timestamp = {Mon, 01 Jun 2020 16:20:46 +0200},
|
||||
biburl = {https://dblp.org/rec/journals/corr/abs-1912-13318.bib},
|
||||
bibsource = {dblp computer science bibliography, https://dblp.org}
|
||||
}
|
||||
|
||||
@article{DBLP:journals/corr/abs-2012-14740,
|
||||
author = {Yang Xu and
|
||||
Yiheng Xu and
|
||||
Tengchao Lv and
|
||||
Lei Cui and
|
||||
Furu Wei and
|
||||
Guoxin Wang and
|
||||
Yijuan Lu and
|
||||
Dinei A. F. Flor{\^{e}}ncio and
|
||||
Cha Zhang and
|
||||
Wanxiang Che and
|
||||
Min Zhang and
|
||||
Lidong Zhou},
|
||||
title = {LayoutLMv2: Multi-modal Pre-training for Visually-Rich Document Understanding},
|
||||
journal = {CoRR},
|
||||
volume = {abs/2012.14740},
|
||||
year = {2020},
|
||||
url = {https://arxiv.org/abs/2012.14740},
|
||||
eprinttype = {arXiv},
|
||||
eprint = {2012.14740},
|
||||
timestamp = {Tue, 27 Jul 2021 09:53:52 +0200},
|
||||
biburl = {https://dblp.org/rec/journals/corr/abs-2012-14740.bib},
|
||||
bibsource = {dblp computer science bibliography, https://dblp.org}
|
||||
}
|
||||
```
|
|
@ -1,10 +1,11 @@
|
|||
# OCR算法
|
||||
# 算法汇总
|
||||
|
||||
- [1. 两阶段算法](#1)
|
||||
- [1. 两阶段OCR算法](#1)
|
||||
- [1.1 文本检测算法](#11)
|
||||
- [1.2 文本识别算法](#12)
|
||||
- [2. 端到端算法](#2)
|
||||
- [2. 端到端OCR算法](#2)
|
||||
- [3. 表格识别算法](#3)
|
||||
- [4. 关键信息抽取算法](#4)
|
||||
|
||||
|
||||
本文给出了PaddleOCR已支持的OCR算法列表,以及每个算法在**英文公开数据集**上的模型和指标,主要用于算法简介和算法性能对比,更多包括中文在内的其他数据集上的模型请参考[PP-OCR v2.0 系列模型下载](./models_list.md)。
|
||||
|
@ -114,3 +115,34 @@
|
|||
|模型|骨干网络|配置文件|acc|下载链接|
|
||||
|---|---|---|---|---|
|
||||
|TableMaster|TableResNetExtra|[configs/table/table_master.yml](../../configs/table/table_master.yml)|77.47%|[训练模型](https://paddleocr.bj.bcebos.com/ppstructure/models/tablemaster/table_structure_tablemaster_train.tar) / [推理模型](https://paddleocr.bj.bcebos.com/ppstructure/models/tablemaster/table_structure_tablemaster_infer.tar)|
|
||||
|
||||
|
||||
|
||||
## 4. 关键信息抽取算法
|
||||
|
||||
已支持的关键信息抽取算法列表(戳链接获取使用教程):
|
||||
|
||||
- [x] [VI-LayoutXLM](./algorithm_kie_vi_laoutxlm.md)
|
||||
- [x] [LayoutLM](./algorithm_kie_laoutxlm.md)
|
||||
- [x] [LayoutLMv2](./algorithm_kie_laoutxlm.md)
|
||||
- [x] [LayoutXLM](./algorithm_kie_laoutxlm.md)
|
||||
- [x] [SDMGR](././algorithm_kie_sdmgr.md)
|
||||
|
||||
在wildreceipt发票公开数据集上,算法复现效果如下:
|
||||
|
||||
|模型|骨干网络|配置文件|hmean|下载链接|
|
||||
| --- | --- | --- | --- | --- |
|
||||
|SDMGR|VGG6|[configs/kie/sdmgr/kie_unet_sdmgr.yml](../../configs/kie/sdmgr/kie_unet_sdmgr.yml)|86.7%|[训练模型](https://paddleocr.bj.bcebos.com/dygraph_v2.1/kie/kie_vgg16.tar)|
|
||||
|
||||
|
||||
在XFUND_zh公开数据集上,算法效果如下:
|
||||
|
||||
|模型|骨干网络|任务|配置文件|hmean|下载链接|
|
||||
| --- | --- | --- | --- | --- | --- |
|
||||
|VI-LayoutXLM| VI-LayoutXLM-base | SER | [ser_vi_layoutxlm_xfund_zh_udml.yml](../../configs/kie/vi_layoutxlm/ser_vi_layoutxlm_xfund_zh_udml.yml)|**93.19%**|[训练模型](https://paddleocr.bj.bcebos.com/ppstructure/models/vi_layoutxlm/ser_vi_layoutxlm_xfund_pretrained.tar)|
|
||||
|LayoutXLM| LayoutXLM-base | SER | [ser_layoutxlm_xfund_zh.yml](../../configs/kie/layoutlm_series/ser_layoutxlm_xfund_zh.yml)|90.38%|[训练模型](https://paddleocr.bj.bcebos.com/pplayout/ser_LayoutXLM_xfun_zh.tar)|
|
||||
|LayoutLM| LayoutLM-base | SER | [ser_layoutlm_xfund_zh.yml](../../configs/kie/layoutlm_series/ser_layoutlm_xfund_zh.yml)|77.31%|[训练模型](https://paddleocr.bj.bcebos.com/pplayout/ser_LayoutLM_xfun_zh.tar)|
|
||||
|LayoutLMv2| LayoutLMv2-base | SER | [ser_layoutlmv2_xfund_zh.yml](../../configs/kie/layoutlm_series/ser_layoutlmv2_xfund_zh.yml)|85.44%|[训练模型](https://paddleocr.bj.bcebos.com/pplayout/ser_LayoutLMv2_xfun_zh.tar)|
|
||||
|VI-LayoutXLM| VI-LayoutXLM-base | RE | [re_vi_layoutxlm_xfund_zh_udml.yml](../../configs/kie/vi_layoutxlm/re_vi_layoutxlm_xfund_zh_udml.yml)|**83.92%**|[训练模型](https://paddleocr.bj.bcebos.com/ppstructure/models/vi_layoutxlm/re_vi_layoutxlm_xfund_pretrained.tar)|
|
||||
|LayoutXLM| LayoutXLM-base | RE | [re_layoutxlm_xfund_zh.yml](../../configs/kie/layoutlm_series/re_layoutxlm_xfund_zh.yml)|74.83%|[训练模型](https://paddleocr.bj.bcebos.com/pplayout/re_LayoutXLM_xfun_zh.tar)|
|
||||
|LayoutLMv2| LayoutLMv2-base | RE | [re_layoutlmv2_xfund_zh.yml](../../configs/kie/layoutlm_series/re_layoutlmv2_xfund_zh.yml)|67.77%|[训练模型](https://paddleocr.bj.bcebos.com/pplayout/re_LayoutLMv2_xfun_zh.tar)|
|
||||
|
|
|
@ -101,7 +101,7 @@ python3 tools/export_model.py -c configs/rec/rec_r45_visionlan.yml -o Global.pre
|
|||
执行如下命令进行模型推理:
|
||||
|
||||
```shell
|
||||
python3 tools/infer/predict_rec.py --image_dir='./doc/imgs_words/en/word_2.png' --rec_model_dir='./inference/rec_r45_visionlan/' --rec_algorithm='VisionLAN' --rec_image_shape='3,64,256' --rec_char_dict_path='./ppocr/utils/dict36.txt'
|
||||
python3 tools/infer/predict_rec.py --image_dir='./doc/imgs_words/en/word_2.png' --rec_model_dir='./inference/rec_r45_visionlan/' --rec_algorithm='VisionLAN' --rec_image_shape='3,64,256' --rec_char_dict_path='./ppocr/utils/ic15_dict.txt' --use_space_char=False
|
||||
# 预测文件夹下所有图像时,可修改image_dir为文件夹,如 --image_dir='./doc/imgs_words_en/'。
|
||||
```
|
||||
|
||||
|
@ -110,7 +110,7 @@ python3 tools/infer/predict_rec.py --image_dir='./doc/imgs_words/en/word_2.png'
|
|||
执行命令后,上面图像的预测结果(识别的文本和得分)会打印到屏幕上,示例如下:
|
||||
结果如下:
|
||||
```shell
|
||||
Predicts of ./doc/imgs_words/en/word_2.png:('yourself', 0.97076982)
|
||||
Predicts of ./doc/imgs_words/en/word_2.png:('yourself', 0.9999493)
|
||||
```
|
||||
|
||||
**注意**:
|
||||
|
|
|
@ -1,10 +1,14 @@
|
|||
## DocVQA数据集
|
||||
# 信息抽取数据集
|
||||
|
||||
这里整理了常见的DocVQA数据集,持续更新中,欢迎各位小伙伴贡献数据集~
|
||||
- [FUNSD数据集](#funsd)
|
||||
- [XFUND数据集](#xfund)
|
||||
- [wildreceipt数据集](#wildreceipt)
|
||||
|
||||
<a name="funsd"></a>
|
||||
#### 1、FUNSD数据集
|
||||
|
||||
## 1. FUNSD数据集
|
||||
|
||||
- **数据来源**:https://guillaumejaume.github.io/FUNSD/
|
||||
- **数据简介**:FUNSD数据集是一个用于表单理解的数据集,它包含199张真实的、完全标注的扫描版图片,类型包括市场报告、广告以及学术报告等,并分为149张训练集以及50张测试集。FUNSD数据集适用于多种类型的DocVQA任务,如字段级实体分类、字段级实体连接等。部分图像以及标注框可视化如下所示:
|
||||
<div align="center">
|
||||
|
@ -16,12 +20,33 @@
|
|||
- **下载地址**:https://guillaumejaume.github.io/FUNSD/download/
|
||||
|
||||
<a name="xfund"></a>
|
||||
#### 2、XFUND数据集
|
||||
|
||||
## 2. XFUND数据集
|
||||
- **数据来源**:https://github.com/doc-analysis/XFUND
|
||||
- **数据简介**:XFUND是一个多语种表单理解数据集,它包含7种不同语种的表单数据,并且全部用人工进行了键-值对形式的标注。其中每个语种的数据都包含了199张表单数据,并分为149张训练集以及50张测试集。部分图像以及标注框可视化如下所示:
|
||||
|
||||
<div align="center">
|
||||
<img src="../../datasets/xfund_demo/gt_zh_train_0.jpg" width="500">
|
||||
<img src="../../datasets/xfund_demo/gt_zh_train_1.jpg" width="500">
|
||||
</div>
|
||||
|
||||
- **下载地址**:https://github.com/doc-analysis/XFUND/releases/tag/v1.0
|
||||
|
||||
|
||||
<a name="wildreceipt"></a>
|
||||
|
||||
## 3. wildreceipt数据集
|
||||
|
||||
- **数据来源**:https://arxiv.org/abs/2103.14470
|
||||
- **数据简介**:wildreceipt数据集是英文发票数据集,包含26个类别(此处类别体系包含`Ignore`类别),共标注了50000个文本框。其中训练集包含1267张图片,测试集包含472张图片。部分图像以及标注框可视化如下所示:
|
||||
|
||||
<div align="center">
|
||||
<img src="../../datasets/wildreceipt_demo/2769.jpeg" width="500">
|
||||
<img src="../../datasets/wildreceipt_demo/1bbe854b8817dedb8585e0732089fd1f752d2cec.jpeg" width="500">
|
||||
</div>
|
||||
|
||||
**注:** 这里对于类别为`Ignore`或者`Others`的文本,没有进行可视化。
|
||||
|
||||
- **下载地址**:
|
||||
- 原始数据下载地址:[链接](https://download.openmmlab.com/mmocr/data/wildreceipt.tar)
|
||||
- 数据格式转换后适配于PaddleOCR训练的数据下载地址:[链接](https://paddleocr.bj.bcebos.com/ppstructure/dataset/wildreceipt.tar)
|
|
@ -0,0 +1,463 @@
|
|||
# 关键信息抽取
|
||||
|
||||
本文提供了PaddleOCR关键信息抽取的全流程指南,包括语义实体识别 (Semantic Entity Recognition) 以及关系抽取 (Relation Extraction, RE) 任务的数据准备、模型训练、调优、评估、预测,各个阶段的详细说明。
|
||||
|
||||
- [1. 数据准备](#1-数据准备)
|
||||
- [1.1. 准备数据集](#11-准备数据集)
|
||||
- [1.2. 自定义数据集](#12-自定义数据集)
|
||||
- [1.3. 数据下载](#13-数据下载)
|
||||
- [2. 开始训练](#2-开始训练)
|
||||
- [2.1. 启动训练](#21-启动训练)
|
||||
- [2.2. 断点训练](#22-断点训练)
|
||||
- [2.3. 混合精度训练](#24-混合精度训练)
|
||||
- [2.4. 分布式训练](#25-分布式训练)
|
||||
- [2.5. 知识蒸馏训练](#26-知识蒸馏训练)
|
||||
- [2.6. 其他训练环境](#27-其他训练环境)
|
||||
- [3. 模型评估与预测](#3-模型评估与预测)
|
||||
- [3.1. 指标评估](#31-指标评估)
|
||||
- [3.2. 测试信息抽取效果](#32-测试识别效果)
|
||||
- [4. 模型导出与预测](#4-模型导出与预测)
|
||||
- [5. FAQ](#5-faq)
|
||||
|
||||
# 1. 数据准备
|
||||
|
||||
## 1.1. 准备数据集
|
||||
|
||||
在训练信息抽取相关模型时,PaddleOCR支持以下数据格式。
|
||||
- `通用数据` 用于训练以文本文件存储的数据集(SimpleDataSet);
|
||||
|
||||
训练数据的默认存储路径是 `PaddleOCR/train_data`,如果您的磁盘上已有数据集,只需创建软链接至数据集目录:
|
||||
|
||||
```
|
||||
# linux and mac os
|
||||
ln -sf <path/to/dataset> <path/to/paddle_ocr>/train_data/dataset
|
||||
# windows
|
||||
mklink /d <path/to/paddle_ocr>/train_data/dataset <path/to/dataset>
|
||||
```
|
||||
|
||||
## 1.2. 自定义数据集
|
||||
|
||||
训练过程中一般包含训练集与验证集,二者数据格式相同,下面介绍如何自定义数据集。
|
||||
|
||||
**(1)训练集**
|
||||
|
||||
建议将训练图片放入同一个文件夹,并用一个文本文件记录图片路径和标签,文本文件里的内容如下:
|
||||
|
||||
```py
|
||||
" 图像文件名 图像标注信息 "
|
||||
zh_train_0.jpg [{"transcription": "汇丰晋信", "label": "other", "points": [[104, 114], [530, 114], [530, 175], [104, 175]], "id": 1, "linking": []}, {"transcription": "受理时间:", "label": "question", "points": [[126, 267], [266, 267], [266, 305], [126, 305]], "id": 7, "linking": [[7, 13]]}, {"transcription": "2020.6.15", "label": "answer", "points": [[321, 239], [537, 239], [537, 285], [321, 285]], "id": 13, "linking": [[7, 13]]}]
|
||||
zh_train_1.jpg [{"transcription": "中国人体器官捐献", "label": "other", "points": [[544, 459], [954, 459], [954, 517], [544, 517]], "id": 1, "linking": []}, {"transcription": ">编号:MC545715483585", "label": "other", "points": [[1462, 470], [2054, 470], [2054, 543], [1462, 543]], "id": 10, "linking": []}, {"transcription": "CHINAORGANDONATION", "label": "other", "points": [[543, 516], [958, 516], [958, 551], [543, 551]], "id": 14, "linking": []}, {"transcription": "中国人体器官捐献志愿登记表", "label": "header", "points": [[635, 793], [1892, 793], [1892, 904], [635, 904]], "id": 18, "linking": []}]
|
||||
...
|
||||
```
|
||||
|
||||
**注意:** 文本文件中默认请将图片路径和图片标签用 `\t` 分割,如用其他方式分割将造成训练报错。
|
||||
|
||||
其中图像标注信息字符串经过json解析之后可以得到一个列表信息,列表中每个元素是一个字典,存储了每个文本行的需要信息,各个字段的含义如下。
|
||||
|
||||
- transcription: 存储了文本行的文字内容
|
||||
- label: 该文本行内容所属的类别
|
||||
- points: 存储文本行的四点位置信息
|
||||
- id: 存储文本行的id信息,用于RE任务的训练
|
||||
- linking: 存储文本行的之间的连接信息,用于RE任务的训练
|
||||
|
||||
**(2)验证集**
|
||||
|
||||
验证集构建方式与训练集相同。
|
||||
|
||||
* 字典文件
|
||||
|
||||
训练集与验证集中的文本行包含标签信息,所有标签的列表存在字典文件中(如`class_list.txt`),字典文件中的每一行表示为一个类别名称。
|
||||
|
||||
以XFUND_zh数据为例,共包含4个类别,字典文件内容如下所示。
|
||||
|
||||
```
|
||||
OTHER
|
||||
QUESTION
|
||||
ANSWER
|
||||
HEADER
|
||||
```
|
||||
|
||||
在标注文件中,每个标注的文本行内容的`label`字段标注信息需要属于字典内容。
|
||||
|
||||
最终数据集应有如下文件结构:
|
||||
|
||||
```
|
||||
|-train_data
|
||||
|-data_name
|
||||
|- train.json
|
||||
|- train
|
||||
|- zh_train_0.png
|
||||
|- zh_train_1.jpg
|
||||
| ...
|
||||
|- val.json
|
||||
|- val
|
||||
|- zh_val_0.png
|
||||
|- zh_val_1.jpg
|
||||
| ...
|
||||
```
|
||||
|
||||
**注:**
|
||||
|
||||
- 标注文件中的类别信息不区分大小写,如`HEADER`与`header`会被解析为相同的类别id,因此在标注的时候,不能使用小写处理后相同的字符串表示不同的类别。
|
||||
- 在整理标注文件的时候,建议将other这个类别(其他,无需关注的文本行可以标注为other)放在第一行,在解析的时候,会将`other`类别的类别id解析为0,后续不会对该类进行可视化。
|
||||
|
||||
## 1.3. 数据下载
|
||||
|
||||
如果你没有本地数据集,可以从[XFUND](https://github.com/doc-analysis/XFUND)或者[FUNSD](https://guillaumejaume.github.io/FUNSD/)官网下载数据,然后使用XFUND与FUNSD的处理脚本([XFUND](../../ppstructure/vqa/tools/trans_xfun_data.py), [FUNSD](../../ppstructure/vqa/tools/trans_funsd_label.py)),生成用于PaddleOCR训练的数据格式,并使用公开数据集快速体验关键信息抽取的流程。
|
||||
|
||||
更多关于公开数据集的介绍,请参考[关键信息抽取数据集说明文档](./dataset/kie_datasets.md)。
|
||||
|
||||
PaddleOCR也支持了关键信息抽取模型的标注,具体使用方法请参考:[PPOCRLabel使用文档](../../PPOCRLabel/README_ch.md)。
|
||||
|
||||
|
||||
# 2. 开始训练
|
||||
|
||||
PaddleOCR提供了训练脚本、评估脚本和预测脚本,本节将以 VI-LayoutXLM 多模态预训练模型为例进行讲解。
|
||||
|
||||
> 如果希望使用基于SDMGR的关键信息抽取算法,请参考:[SDMGR使用](./algorithm_kie_sdmgr.md)。
|
||||
|
||||
## 2.1. 启动训练
|
||||
|
||||
如果你没有使用自定义数据集,可以使用PaddleOCR中已经处理好的XFUND_zh数据集进行快速体验。
|
||||
|
||||
```bash
|
||||
mkdir train_data
|
||||
cd train_data
|
||||
wget https://paddleocr.bj.bcebos.com/ppstructure/dataset/XFUND.tar && tar -xf XFUND.tar
|
||||
cd ..
|
||||
```
|
||||
|
||||
如果不希望训练,直接体验后面的模型评估、预测、动转静、推理的流程,可以下载PaddleOCR中提供的预训练模型,并跳过2.1部分。
|
||||
|
||||
使用下面的方法,下载基于XFUND数据的SER与RE任务预训练模型。
|
||||
|
||||
```bash
|
||||
mkdir pretrained_model
|
||||
cd pretrained_model
|
||||
# 下载并解压SER预训练模型
|
||||
wget https://paddleocr.bj.bcebos.com/ppstructure/models/vi_layoutxlm/ser_vi_layoutxlm_xfund_pretrained.tar & tar -xf ser_vi_layoutxlm_xfund_pretrained.tar
|
||||
|
||||
# 下载并解压RE预训练模型
|
||||
wget https://paddleocr.bj.bcebos.com/ppstructure/models/vi_layoutxlm/re_vi_layoutxlm_xfund_pretrained.tar & tar -xf re_vi_layoutxlm_xfund_pretrained.tar
|
||||
```
|
||||
|
||||
开始训练:
|
||||
|
||||
- 如果您安装的是cpu版本,请将配置文件中的 `use_gpu` 字段修改为false
|
||||
- PaddleOCR在训练时,会默认下载VI-LayoutXLM预训练模型,这里无需预先下载。
|
||||
|
||||
```bash
|
||||
# GPU训练 支持单卡,多卡训练
|
||||
# 训练日志会自动保存到 配置文件中"{Global.save_model_dir}" 下的train.log文件中
|
||||
|
||||
# SER单卡训练
|
||||
python3 tools/train.py -c configs/kie/vi_layoutxlm/ser_vi_layoutxlm_xfund_zh.yml
|
||||
|
||||
# SER多卡训练,通过--gpus参数指定卡号
|
||||
python3 -m paddle.distributed.launch --gpus '0,1,2,3' tools/train.py -c configs/kie/vi_layoutxlm/ser_vi_layoutxlm_xfund_zh.yml
|
||||
|
||||
# RE任务单卡训练
|
||||
python3 tools/train.py -c configs/kie/vi_layoutxlm/re_vi_layoutxlm_xfund_zh.yml
|
||||
```
|
||||
|
||||
以SER任务为例,正常启动训练后,会看到以下log输出:
|
||||
|
||||
```
|
||||
[2022/08/08 16:28:28] ppocr INFO: epoch: [1/200], global_step: 10, lr: 0.000006, loss: 1.871535, avg_reader_cost: 0.28200 s, avg_batch_cost: 0.82318 s, avg_samples: 8.0, ips: 9.71838 samples/s, eta: 0:51:59
|
||||
[2022/08/08 16:28:33] ppocr INFO: epoch: [1/200], global_step: 19, lr: 0.000018, loss: 1.461939, avg_reader_cost: 0.00042 s, avg_batch_cost: 0.32037 s, avg_samples: 6.9, ips: 21.53773 samples/s, eta: 0:37:55
|
||||
[2022/08/08 16:28:39] ppocr INFO: cur metric, precision: 0.11526348939743859, recall: 0.19776657060518732, hmean: 0.14564265817747712, fps: 34.008392345050055
|
||||
[2022/08/08 16:28:45] ppocr INFO: save best model is to ./output/ser_vi_layoutxlm_xfund_zh/best_accuracy
|
||||
[2022/08/08 16:28:45] ppocr INFO: best metric, hmean: 0.14564265817747712, precision: 0.11526348939743859, recall: 0.19776657060518732, fps: 34.008392345050055, best_epoch: 1
|
||||
[2022/08/08 16:28:51] ppocr INFO: save model in ./output/ser_vi_layoutxlm_xfund_zh/latest
|
||||
```
|
||||
|
||||
log 中自动打印如下信息:
|
||||
|
||||
| 字段 | 含义 |
|
||||
| :----: | :------: |
|
||||
| epoch | 当前迭代轮次 |
|
||||
| iter | 当前迭代次数 |
|
||||
| lr | 当前学习率 |
|
||||
| loss | 当前损失函数 |
|
||||
| reader_cost | 当前 batch 数据处理耗时 |
|
||||
| batch_cost | 当前 batch 总耗时 |
|
||||
| samples | 当前 batch 内的样本数 |
|
||||
| ips | 每秒处理图片的数量 |
|
||||
|
||||
|
||||
PaddleOCR支持训练和评估交替进行, 可以在 `configs/kie/vi_layoutxlm/ser_vi_layoutxlm_xfund_zh.yml` 中修改 `eval_batch_step` 设置评估频率,默认每19个iter评估一次。评估过程中默认将最佳hmean模型,保存为 `output/ser_vi_layoutxlm_xfund_zh/best_accuracy/` 。
|
||||
|
||||
如果验证集很大,测试将会比较耗时,建议减少评估次数,或训练完再进行评估。
|
||||
|
||||
**提示:** 可通过 -c 参数选择 `configs/kie/` 路径下的多种模型配置进行训练,PaddleOCR支持的信息抽取算法可以参考[前沿算法列表](./algorithm_overview.md)。
|
||||
|
||||
|
||||
如果你希望训练自己的数据集,需要修改配置文件中的数据配置、字典文件以及类别数。
|
||||
|
||||
|
||||
以 `configs/kie/vi_layoutxlm/ser_vi_layoutxlm_xfund_zh.yml` 为例,修改的内容如下所示。
|
||||
|
||||
```yaml
|
||||
|
||||
Architecture:
|
||||
# ...
|
||||
Backbone:
|
||||
name: LayoutXLMForSer
|
||||
pretrained: True
|
||||
mode: vi
|
||||
# 假设字典中包含n个字段(包含other),由于采用BIO标注,则类别数为2n-1
|
||||
num_classes: &num_classes 7
|
||||
|
||||
PostProcess:
|
||||
name: VQASerTokenLayoutLMPostProcess
|
||||
# 修改字典文件的路径为你自定义的数据集的字典路径
|
||||
class_path: &class_path train_data/XFUND/class_list_xfun.txt
|
||||
|
||||
Train:
|
||||
dataset:
|
||||
name: SimpleDataSet
|
||||
# 修改为你自己的训练数据目录
|
||||
data_dir: train_data/XFUND/zh_train/image
|
||||
# 修改为你自己的训练数据标签文件
|
||||
label_file_list:
|
||||
- train_data/XFUND/zh_train/train.json
|
||||
...
|
||||
loader:
|
||||
# 训练时的单卡batch_size
|
||||
batch_size_per_card: 8
|
||||
...
|
||||
|
||||
Eval:
|
||||
dataset:
|
||||
name: SimpleDataSet
|
||||
# 修改为你自己的验证数据目录
|
||||
data_dir: train_data/XFUND/zh_val/image
|
||||
# 修改为你自己的验证数据标签文件
|
||||
label_file_list:
|
||||
- train_data/XFUND/zh_val/val.json
|
||||
...
|
||||
loader:
|
||||
# 验证时的单卡batch_size
|
||||
batch_size_per_card: 8
|
||||
```
|
||||
|
||||
**注意,预测/评估时的配置文件请务必与训练一致。**
|
||||
|
||||
## 2.2. 断点训练
|
||||
|
||||
如果训练程序中断,如果希望加载训练中断的模型从而恢复训练,可以通过指定` Architecture.Backbone.checkpoints`指定要加载的模型路径:
|
||||
|
||||
```bash
|
||||
python3 tools/train.py -c configs/kie/vi_layoutxlm/ser_vi_layoutxlm_xfund_zh.yml -o Architecture.Backbone.checkpoints=./output/ser_vi_layoutxlm_xfund_zh/best_accuracy
|
||||
```
|
||||
|
||||
**注意**:
|
||||
|
||||
- `Architecture.Backbone.checkpoints`的优先级高于`Architecture.Backbone.pretrained`,需要加载之前训练好的训练模型进行模型微调、恢复训练、模型评估时,需要使用`Architecture.Backbone.checkpoints`指定模型参数路径;如果需要使用默认提供的通用预训练模型进行训练,则需要指定`Architecture.Backbone.pretrained`为`True`,同时指定`Architecture.Backbone.checkpoints`为空(`null`)。
|
||||
- LayoutXLM系列模型均是调用了PaddleNLP中的预训练模型,模型加载与保存的逻辑与PaddleNLP基本一致,因此在这里不需要指定`Global.pretrained_model`或者`Global.checkpoints`参数;此外,LayoutXLM系列模型的蒸馏训练目前不支持断点训练。
|
||||
|
||||
|
||||
## 2.3. 混合精度训练
|
||||
|
||||
coming soon!
|
||||
|
||||
## 2.4. 分布式训练
|
||||
|
||||
多机多卡训练时,通过 `--ips` 参数设置使用的机器IP地址,通过 `--gpus` 参数设置使用的GPU ID:
|
||||
|
||||
```bash
|
||||
python3 -m paddle.distributed.launch --ips="xx.xx.xx.xx,xx.xx.xx.xx" --gpus '0,1,2,3' tools/train.py -c configs/kie/vi_layoutxlm/ser_vi_layoutxlm_xfund_zh.yml
|
||||
```
|
||||
|
||||
**注意:** (1)采用多机多卡训练时,需要替换上面命令中的ips值为您机器的地址,机器之间需要能够相互ping通;(2)训练时需要在多个机器上分别启动命令。查看机器ip地址的命令为`ifconfig`;(3)更多关于分布式训练的性能优势等信息,请参考:[分布式训练教程](./distributed_training.md)。
|
||||
|
||||
## 2.5. 知识蒸馏训练
|
||||
|
||||
PaddleOCR支持了基于U-DML知识蒸馏的关键信息抽取模型训练过程,配置文件请参考:[ser_vi_layoutxlm_xfund_zh_udml.yml](../../configs/kie/vi_layoutxlm/ser_vi_layoutxlm_xfund_zh_udml.yml),更多关于知识蒸馏的说明文档请参考:[知识蒸馏说明文档](./knowledge_distillation.md)。
|
||||
|
||||
**注意**: PaddleOCR中LayoutXLM系列关键信息抽取模型的保存与加载逻辑与PaddleNLP保持一致,因此在蒸馏的过程中仅保存了学生模型的参数,如果希望使用保存的模型进行评估,需要使用学生模型的配置(上面的蒸馏文件对应的学生模型为[ser_vi_layoutxlm_xfund_zh.yml](../../configs/kie/vi_layoutxlm/ser_vi_layoutxlm_xfund_zh.yml))
|
||||
|
||||
|
||||
## 2.6. 其他训练环境
|
||||
|
||||
- Windows GPU/CPU
|
||||
在Windows平台上与Linux平台略有不同:
|
||||
Windows平台只支持`单卡`的训练与预测,指定GPU进行训练`set CUDA_VISIBLE_DEVICES=0`
|
||||
在Windows平台,DataLoader只支持单进程模式,因此需要设置 `num_workers` 为0;
|
||||
|
||||
- macOS
|
||||
不支持GPU模式,需要在配置文件中设置`use_gpu`为False,其余训练评估预测命令与Linux GPU完全相同。
|
||||
|
||||
- Linux DCU
|
||||
DCU设备上运行需要设置环境变量 `export HIP_VISIBLE_DEVICES=0,1,2,3`,其余训练评估预测命令与Linux GPU完全相同。
|
||||
|
||||
|
||||
# 3. 模型评估与预测
|
||||
|
||||
## 3.1. 指标评估
|
||||
|
||||
训练中模型参数默认保存在`Global.save_model_dir`目录下。在评估指标时,需要设置`Architecture.Backbone.checkpoints`指向保存的参数文件。评估数据集可以通过 `configs/kie/vi_layoutxlm/ser_vi_layoutxlm_xfund_zh.yml` 修改Eval中的 `label_file_path` 设置。
|
||||
|
||||
```bash
|
||||
# GPU 评估, Global.checkpoints 为待测权重
|
||||
python3 tools/eval.py -c configs/kie/vi_layoutxlm/ser_vi_layoutxlm_xfund_zh.yml -o Architecture.Backbone.checkpoints=./output/ser_vi_layoutxlm_xfund_zh/best_accuracy
|
||||
```
|
||||
|
||||
会输出以下信息,打印出precision、recall、hmean等信息。
|
||||
|
||||
```py
|
||||
[2022/08/09 07:59:28] ppocr INFO: metric eval ***************
|
||||
[2022/08/09 07:59:28] ppocr INFO: precision:0.697476609016161
|
||||
[2022/08/09 07:59:28] ppocr INFO: recall:0.8861671469740634
|
||||
[2022/08/09 07:59:28] ppocr INFO: hmean:0.7805806758686339
|
||||
[2022/08/09 07:59:28] ppocr INFO: fps:17.367364606899105
|
||||
```
|
||||
|
||||
|
||||
## 3.2. 测试信息抽取结果
|
||||
|
||||
使用 PaddleOCR 训练好的模型,可以通过以下脚本进行快速预测。
|
||||
|
||||
默认预测的图片存储在 `infer_img` 里,通过 `-o Architecture.Backbone.checkpoints` 加载训练好的参数文件:
|
||||
|
||||
根据配置文件中设置的 `save_model_dir` 和 `save_epoch_step` 字段,会有以下几种参数被保存下来:
|
||||
|
||||
```
|
||||
output/ser_vi_layoutxlm_xfund_zh/
|
||||
├── best_accuracy
|
||||
├── metric.states
|
||||
├── model_config.json
|
||||
├── model_state.pdparams
|
||||
├── best_accuracy.pdopt
|
||||
├── config.yml
|
||||
├── train.log
|
||||
├── latest
|
||||
├── metric.states
|
||||
├── model_config.json
|
||||
├── model_state.pdparams
|
||||
├── latest.pdopt
|
||||
```
|
||||
|
||||
其中 best_accuracy.* 是评估集上的最优模型;latest.* 是最新保存的一个模型。
|
||||
|
||||
|
||||
预测使用的配置文件必须与训练一致,如您通过 `python3 tools/train.py -c configs/kie/vi_layoutxlm/ser_vi_layoutxlm_xfund_zh.yml` 完成了模型的训练过程。
|
||||
|
||||
您可以使用如下命令进行中文模型预测。
|
||||
|
||||
|
||||
```bash
|
||||
python3 tools/infer_vqa_token_ser.py -c configs/kie/vi_layoutxlm/ser_vi_layoutxlm_xfund_zh.yml -o Architecture.Backbone.checkpoints=./output/ser_vi_layoutxlm_xfund_zh/best_accuracy Global.infer_img=./ppstructure/docs/vqa/input/zh_val_42.jpg
|
||||
```
|
||||
|
||||
预测图片如下所示,图片会存储在`Global.save_res_path`路径中。
|
||||
|
||||
<div align="center">
|
||||
<img src="../../ppstructure/docs/vqa/result_ser/zh_val_42_ser.jpg" width="800">
|
||||
</div>
|
||||
|
||||
预测过程中,默认会加载PP-OCRv3的检测识别模型,用于OCR的信息抽取,如果希望加载预先获取的OCR结果,可以使用下面的方式进行预测,指定`Global.infer_img`为标注文件,其中包含图片路径以及OCR信息,同时指定`Global.infer_mode`为False,表示此时不使用OCR预测引擎。
|
||||
|
||||
```bash
|
||||
python3 tools/infer_vqa_token_ser.py -c configs/kie/vi_layoutxlm/ser_vi_layoutxlm_xfund_zh.yml -o Architecture.Backbone.checkpoints=./output/ser_vi_layoutxlm_xfund_zh/best_accuracy Global.infer_img=./train_data/XFUND/zh_val/val.json Global.infer_mode=False
|
||||
```
|
||||
|
||||
对于上述图片,如果使用标注的OCR结果进行信息抽取,预测结果如下。
|
||||
|
||||
<div align="center">
|
||||
<img src="../../ppstructure/docs/vqa/result_ser_with_gt_ocr/zh_val_42_ser.jpg" width="800">
|
||||
</div>
|
||||
|
||||
可以看出,部分检测框信息更加准确,但是整体信息抽取识别结果基本一致。
|
||||
|
||||
|
||||
在RE任务模型预测时,需要先给出模型SER结果,因此需要同时加载SER的配置文件与模型权重,示例如下。
|
||||
|
||||
|
||||
```bash
|
||||
python3 ./tools/infer_vqa_token_ser_re.py -c configs/kie/vi_layoutxlm/re_vi_layoutxlm_xfund_zh.yml -o Architecture.Backbone.checkpoints=./pretrain_models/re_vi_layoutxlm_udml_xfund_zh/re_layoutxlm_xfund_zh_v4_udml/best_accuracy/ Global.infer_img=./train_data/XFUND/zh_val/image/ -c_ser configs/kie/vi_layoutxlm/ser_vi_layoutxlm_xfund_zh.yml -o_ser Architecture.Backbone.checkpoints=pretrain_models/ser_vi_layoutxlm_udml_xfund_zh/best_accuracy/
|
||||
```
|
||||
|
||||
预测结果如下所示。
|
||||
|
||||
<div align="center">
|
||||
<img src="../../ppstructure/docs/vqa/result_re/zh_val_42_re.jpg" width="800">
|
||||
</div>
|
||||
|
||||
|
||||
如果希望使用标注或者预先获取的OCR信息进行关键信息抽取,同上,可以指定`Global.infer_mode`为False,指定`Global.infer_img`为标注文件。
|
||||
|
||||
```bash
|
||||
python3 ./tools/infer_vqa_token_ser_re.py -c configs/kie/vi_layoutxlm/re_vi_layoutxlm_xfund_zh.yml -o Architecture.Backbone.checkpoints=./pretrain_models/re_vi_layoutxlm_udml_xfund_zh/re_layoutxlm_xfund_zh_v4_udml/best_accuracy/ Global.infer_img=./train_data/XFUND/zh_val/val.json Global.infer_mode=False -c_ser configs/kie/vi_layoutxlm/ser_vi_layoutxlm_xfund_zh.yml -o_ser Architecture.Backbone.checkpoints=pretrain_models/ser_vi_layoutxlm_udml_xfund_zh/best_accuracy/
|
||||
```
|
||||
|
||||
其中`c_ser`表示SER的配置文件,`o_ser` 后面需要加上待修改的SER模型与配置文件,如预训练权重等。
|
||||
|
||||
|
||||
预测结果如下所示。
|
||||
|
||||
<div align="center">
|
||||
<img src="../../ppstructure/docs/vqa/result_re_with_gt_ocr/zh_val_42_re.jpg" width="800">
|
||||
</div>
|
||||
|
||||
可以看出,直接使用标注的OCR结果的RE预测结果要更加准确一些。
|
||||
|
||||
# 4. 模型导出与预测
|
||||
|
||||
|
||||
## 4.1 模型导出
|
||||
|
||||
inference 模型(`paddle.jit.save`保存的模型)
|
||||
一般是模型训练,把模型结构和模型参数保存在文件中的固化模型,多用于预测部署场景。
|
||||
训练过程中保存的模型是checkpoints模型,保存的只有模型的参数,多用于恢复训练等。
|
||||
与checkpoints模型相比,inference 模型会额外保存模型的结构信息,在预测部署、加速推理上性能优越,灵活方便,适合于实际系统集成。
|
||||
|
||||
信息抽取模型中的SER任务转inference模型步骤如下:
|
||||
|
||||
```bash
|
||||
# -c 后面设置训练算法的yml配置文件
|
||||
# -o 配置可选参数
|
||||
# Global.pretrained_model 参数设置待转换的训练模型地址。
|
||||
# Global.save_inference_dir参数设置转换的模型将保存的地址。
|
||||
|
||||
python3 tools/export_model.py -c configs/kie/vi_layoutxlm/ser_vi_layoutxlm_xfund_zh.yml -o Architecture.Backbone.checkpoints=./output/ser_vi_layoutxlm_xfund_zh/best_accuracy Global.save_inference_dir=./inference/ser_vi_layoutxlm
|
||||
```
|
||||
|
||||
转换成功后,在目录下有三个文件:
|
||||
|
||||
```
|
||||
inference/ser_vi_layoutxlm/
|
||||
├── inference.pdiparams # inference模型的参数文件
|
||||
├── inference.pdiparams.info # inference模型的参数信息,可忽略
|
||||
└── inference.pdmodel # inference模型的模型结构文件
|
||||
```
|
||||
|
||||
RE任务的动转静过程适配中,敬请期待。
|
||||
|
||||
## 4.2 模型推理
|
||||
|
||||
VI-LayoutXLM模型基于SER任务进行推理,可以执行如下命令:
|
||||
|
||||
```bash
|
||||
cd ppstructure
|
||||
python3 vqa/predict_vqa_token_ser.py \
|
||||
--vqa_algorithm=LayoutXLM \
|
||||
--ser_model_dir=../inference/ser_vi_layoutxlm \
|
||||
--image_dir=./docs/vqa/input/zh_val_42.jpg \
|
||||
--ser_dict_path=../train_data/XFUND/class_list_xfun.txt \
|
||||
--vis_font_path=../doc/fonts/simfang.ttf \
|
||||
--ocr_order_method="tb-yx"
|
||||
```
|
||||
|
||||
可视化SER结果结果默认保存到`./output`文件夹里面。结果示例如下:
|
||||
|
||||
<div align="center">
|
||||
<img src="../../ppstructure/docs/vqa/result_ser/zh_val_42_ser.jpg" width="800">
|
||||
</div>
|
||||
|
||||
|
||||
# 5. FAQ
|
||||
|
||||
Q1: 训练模型转inference 模型之后预测效果不一致?
|
||||
|
||||
**A**:该问题多是trained model预测时候的预处理、后处理参数和inference model预测的时候的预处理、后处理参数不一致导致的。可以对比训练使用的配置文件中的预处理、后处理和预测时是否存在差异。
|
|
@ -90,7 +90,7 @@ After the conversion is successful, there are three files in the directory:
|
|||
For VisionLAN text recognition model inference, the following commands can be executed:
|
||||
|
||||
```
|
||||
python3 tools/infer/predict_rec.py --image_dir='./doc/imgs_words/en/word_2.png' --rec_model_dir='./inference/rec_r45_visionlan/' --rec_algorithm='VisionLAN' --rec_image_shape='3,64,256' --rec_char_dict_path='./ppocr/utils/dict36.txt'
|
||||
python3 tools/infer/predict_rec.py --image_dir='./doc/imgs_words/en/word_2.png' --rec_model_dir='./inference/rec_r45_visionlan/' --rec_algorithm='VisionLAN' --rec_image_shape='3,64,256' --rec_char_dict_path='./ppocr/utils/ic15_dict.txt' --use_space_char=False
|
||||
```
|
||||
|
||||

|
||||
|
@ -98,7 +98,7 @@ python3 tools/infer/predict_rec.py --image_dir='./doc/imgs_words/en/word_2.png'
|
|||
After executing the command, the prediction result (recognized text and score) of the image above is printed to the screen, an example is as follows:
|
||||
The result is as follows:
|
||||
```shell
|
||||
Predicts of ./doc/imgs_words/en/word_2.png:('yourself', 0.97076982)
|
||||
Predicts of ./doc/imgs_words/en/word_2.png:('yourself', 0.9999493)
|
||||
```
|
||||
|
||||
<a name="4-2"></a>
|
||||
|
|
|
@ -113,7 +113,6 @@ class LayoutLMv2ForSer(NLPBaseModel):
|
|||
pretrained,
|
||||
checkpoints,
|
||||
num_classes=num_classes)
|
||||
self.use_visual_backbone = True
|
||||
if hasattr(self.model.layoutlmv2, "use_visual_backbone"
|
||||
) and self.model.layoutlmv2.use_visual_backbone is False:
|
||||
self.use_visual_backbone = False
|
||||
|
@ -155,7 +154,9 @@ class LayoutXLMForSer(NLPBaseModel):
|
|||
pretrained,
|
||||
checkpoints,
|
||||
num_classes=num_classes)
|
||||
self.use_visual_backbone = True
|
||||
if hasattr(self.model.layoutxlm, "use_visual_backbone"
|
||||
) and self.model.layoutxlm.use_visual_backbone is False:
|
||||
self.use_visual_backbone = False
|
||||
|
||||
def forward(self, x):
|
||||
if self.use_visual_backbone is True:
|
||||
|
@ -185,6 +186,9 @@ class LayoutLMv2ForRe(NLPBaseModel):
|
|||
super(LayoutLMv2ForRe, self).__init__(
|
||||
LayoutLMv2Model, LayoutLMv2ForRelationExtraction, mode, "re",
|
||||
pretrained, checkpoints)
|
||||
if hasattr(self.model.layoutlmv2, "use_visual_backbone"
|
||||
) and self.model.layoutlmv2.use_visual_backbone is False:
|
||||
self.use_visual_backbone = False
|
||||
|
||||
def forward(self, x):
|
||||
x = self.model(
|
||||
|
@ -207,7 +211,6 @@ class LayoutXLMForRe(NLPBaseModel):
|
|||
super(LayoutXLMForRe, self).__init__(
|
||||
LayoutXLMModel, LayoutXLMForRelationExtraction, mode, "re",
|
||||
pretrained, checkpoints)
|
||||
self.use_visual_backbone = True
|
||||
if hasattr(self.model.layoutxlm, "use_visual_backbone"
|
||||
) and self.model.layoutxlm.use_visual_backbone is False:
|
||||
self.use_visual_backbone = False
|
||||
|
|
|
@ -780,7 +780,7 @@ class VLLabelDecode(BaseRecLabelDecode):
|
|||
) + length[i])].topk(1)[0][:, 0]
|
||||
preds_prob = paddle.exp(
|
||||
paddle.log(preds_prob).sum() / (preds_prob.shape[0] + 1e-6))
|
||||
text.append((preds_text, preds_prob))
|
||||
text.append((preds_text, preds_prob.numpy()[0]))
|
||||
if label is None:
|
||||
return text
|
||||
label = self.decode(label)
|
||||
|
|
|
@ -56,7 +56,7 @@ def load_model(config, model, optimizer=None, model_type='det'):
|
|||
is_float16 = False
|
||||
|
||||
if model_type == 'vqa':
|
||||
# NOTE: for vqa model, resume training is not supported now
|
||||
# NOTE: for vqa model dsitillation, resume training is not supported now
|
||||
if config["Architecture"]["algorithm"] in ["Distillation"]:
|
||||
return best_model_dict
|
||||
checkpoints = config['Architecture']['Backbone']['checkpoints']
|
||||
|
@ -191,8 +191,7 @@ def save_model(model,
|
|||
"""
|
||||
_mkdir_if_not_exist(model_path, logger)
|
||||
model_prefix = os.path.join(model_path, prefix)
|
||||
if config['Architecture']["model_type"] != 'vqa':
|
||||
paddle.save(optimizer.state_dict(), model_prefix + '.pdopt')
|
||||
paddle.save(optimizer.state_dict(), model_prefix + '.pdopt')
|
||||
if config['Architecture']["model_type"] != 'vqa':
|
||||
paddle.save(model.state_dict(), model_prefix + '.pdparams')
|
||||
metric_prefix = model_prefix
|
||||
|
|
Before Width: | Height: | Size: 208 KiB After Width: | Height: | Size: 208 KiB |
|
@ -1,71 +0,0 @@
|
|||
- [关键信息提取(Key Information Extraction)](#关键信息提取key-information-extraction)
|
||||
- [1. 快速使用](#1-快速使用)
|
||||
- [2. 执行训练](#2-执行训练)
|
||||
- [3. 执行评估](#3-执行评估)
|
||||
- [4. 参考文献](#4-参考文献)
|
||||
|
||||
# 关键信息提取(Key Information Extraction)
|
||||
|
||||
本节介绍PaddleOCR中关键信息提取SDMGR方法的快速使用和训练方法。
|
||||
|
||||
SDMGR是一个关键信息提取算法,将每个检测到的文本区域分类为预定义的类别,如订单ID、发票号码,金额等。
|
||||
|
||||
|
||||
## 1. 快速使用
|
||||
|
||||
训练和测试的数据采用wildreceipt数据集,通过如下指令下载数据集:
|
||||
|
||||
```
|
||||
wget https://paddleocr.bj.bcebos.com/ppstructure/dataset/wildreceipt.tar && tar xf wildreceipt.tar
|
||||
```
|
||||
|
||||
执行预测:
|
||||
|
||||
```
|
||||
cd PaddleOCR/
|
||||
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
|
||||
```
|
||||
|
||||
执行预测后的结果保存在`./output/sdmgr_kie/predicts_kie.txt`文件中,可视化结果保存在`/output/sdmgr_kie/kie_results/`目录下。
|
||||
|
||||
可视化结果如下图所示:
|
||||
|
||||
<div align="center">
|
||||
<img src="./imgs/0.png" width="800">
|
||||
</div>
|
||||
|
||||
## 2. 执行训练
|
||||
|
||||
创建数据集软链到PaddleOCR/train_data目录下:
|
||||
```
|
||||
cd PaddleOCR/ && mkdir train_data && cd train_data
|
||||
|
||||
ln -s ../../wildreceipt ./
|
||||
```
|
||||
|
||||
训练采用的配置文件是configs/kie/kie_unet_sdmgr.yml,配置文件中默认训练数据路径是`train_data/wildreceipt`,准备好数据后,可以通过如下指令执行训练:
|
||||
```
|
||||
python3.7 tools/train.py -c configs/kie/kie_unet_sdmgr.yml -o Global.save_model_dir=./output/kie/
|
||||
```
|
||||
## 3. 执行评估
|
||||
|
||||
```
|
||||
python3.7 tools/eval.py -c configs/kie/kie_unet_sdmgr.yml -o Global.checkpoints=./output/kie/best_accuracy
|
||||
```
|
||||
|
||||
|
||||
## 4. 参考文献
|
||||
|
||||
<!-- [ALGORITHM] -->
|
||||
|
||||
```bibtex
|
||||
@misc{sun2021spatial,
|
||||
title={Spatial Dual-Modality Graph Reasoning for Key Information Extraction},
|
||||
author={Hongbin Sun and Zhanghui Kuang and Xiaoyu Yue and Chenhao Lin and Wayne Zhang},
|
||||
year={2021},
|
||||
eprint={2103.14470},
|
||||
archivePrefix={arXiv},
|
||||
primaryClass={cs.CV}
|
||||
}
|
||||
```
|
|
@ -4,8 +4,7 @@
|
|||
- [2. OCR和表格识别模型](#2-ocr和表格识别模型)
|
||||
- [2.1 OCR](#21-ocr)
|
||||
- [2.2 表格识别模型](#22-表格识别模型)
|
||||
- [3. VQA模型](#3-vqa模型)
|
||||
- [4. KIE模型](#4-kie模型)
|
||||
- [3. KIE模型](#3-kie模型)
|
||||
|
||||
|
||||
<a name="1"></a>
|
||||
|
@ -38,19 +37,26 @@
|
|||
|en_ppocr_mobile_v2.0_table_structure|PubTabNet数据集训练的英文表格场景的表格结构预测|18.6M|[推理模型](https://paddleocr.bj.bcebos.com/dygraph_v2.0/table/en_ppocr_mobile_v2.0_table_structure_infer.tar) / [训练模型](https://paddleocr.bj.bcebos.com/dygraph_v2.1/table/en_ppocr_mobile_v2.0_table_structure_train.tar) |
|
||||
|
||||
<a name="3"></a>
|
||||
## 3. VQA模型
|
||||
|
||||
|模型名称|模型简介|推理模型大小|下载地址|
|
||||
| --- | --- | --- | --- |
|
||||
|ser_LayoutXLM_xfun_zh|基于LayoutXLM在xfun中文数据集上训练的SER模型|1.4G|[推理模型](https://paddleocr.bj.bcebos.com/pplayout/ser_LayoutXLM_xfun_zh_infer.tar) / [训练模型](https://paddleocr.bj.bcebos.com/pplayout/ser_LayoutXLM_xfun_zh.tar) |
|
||||
|re_LayoutXLM_xfun_zh|基于LayoutXLM在xfun中文数据集上训练的RE模型|1.4G|[推理模型 coming soon]() / [训练模型](https://paddleocr.bj.bcebos.com/pplayout/re_LayoutXLM_xfun_zh.tar) |
|
||||
|ser_LayoutLMv2_xfun_zh|基于LayoutLMv2在xfun中文数据集上训练的SER模型|778M|[推理模型](https://paddleocr.bj.bcebos.com/pplayout/ser_LayoutLMv2_xfun_zh_infer.tar) / [训练模型](https://paddleocr.bj.bcebos.com/pplayout/ser_LayoutLMv2_xfun_zh.tar) |
|
||||
|re_LayoutLMv2_xfun_zh|基于LayoutLMv2在xfun中文数据集上训练的RE模型|765M|[推理模型 coming soon]() / [训练模型](https://paddleocr.bj.bcebos.com/pplayout/re_LayoutLMv2_xfun_zh.tar) |
|
||||
|ser_LayoutLM_xfun_zh|基于LayoutLM在xfun中文数据集上训练的SER模型|430M|[推理模型](https://paddleocr.bj.bcebos.com/pplayout/ser_LayoutLM_xfun_zh_infer.tar) / [训练模型](https://paddleocr.bj.bcebos.com/pplayout/ser_LayoutLM_xfun_zh.tar) |
|
||||
## 3. KIE模型
|
||||
|
||||
<a name="4"></a>
|
||||
## 4. KIE模型
|
||||
在XFUND_zh数据集上,不同模型的精度与V100 GPU上速度信息如下所示。
|
||||
|
||||
|模型名称|模型简介|模型大小|下载地址|
|
||||
| --- | --- | --- | --- |
|
||||
|SDMGR|关键信息提取模型|78M|[推理模型 coming soon]() / [训练模型](https://paddleocr.bj.bcebos.com/dygraph_v2.1/kie/kie_vgg16.tar)|
|
||||
|模型名称|模型简介 | 推理模型大小| 精度(hmean) | 预测耗时(ms) | 下载地址|
|
||||
| --- | --- | --- |--- |--- | --- |
|
||||
|ser_VI-LayoutXLM_xfund_zh|基于VI-LayoutXLM在xfund中文数据集上训练的SER模型|1.1G| 93.19% | 15.49 | [推理模型](https://paddleocr.bj.bcebos.com/ppstructure/models/vi_layoutxlm/ser_vi_layoutxlm_xfund_infer.tar) / [训练模型](https://paddleocr.bj.bcebos.com/ppstructure/models/vi_layoutxlm/ser_vi_layoutxlm_xfund_pretrained.tar) |
|
||||
|re_VI-LayoutXLM_xfund_zh|基于VI-LayoutXLM在xfund中文数据集上训练的RE模型|1.1G| 83.92% | 15.49 |[推理模型 coming soon]() / [训练模型](https://paddleocr.bj.bcebos.com/ppstructure/models/vi_layoutxlm/re_vi_layoutxlm_xfund_pretrained.tar) |
|
||||
|ser_LayoutXLM_xfund_zh|基于LayoutXLM在xfund中文数据集上训练的SER模型|1.4G| 90.38% | 19.49 |[推理模型](https://paddleocr.bj.bcebos.com/pplayout/ser_LayoutXLM_xfun_zh_infer.tar) / [训练模型](https://paddleocr.bj.bcebos.com/pplayout/ser_LayoutXLM_xfun_zh.tar) |
|
||||
|re_LayoutXLM_xfund_zh|基于LayoutXLM在xfund中文数据集上训练的RE模型|1.4G| 74.83% | 19.49 |[推理模型 coming soon]() / [训练模型](https://paddleocr.bj.bcebos.com/pplayout/re_LayoutXLM_xfun_zh.tar) |
|
||||
|ser_LayoutLMv2_xfund_zh|基于LayoutLMv2在xfund中文数据集上训练的SER模型|778M| 85.44% | 31.46 |[推理模型](https://paddleocr.bj.bcebos.com/pplayout/ser_LayoutLMv2_xfun_zh_infer.tar) / [训练模型](https://paddleocr.bj.bcebos.com/pplayout/ser_LayoutLMv2_xfun_zh.tar) |
|
||||
|re_LayoutLMv2_xfund_zh|基于LayoutLMv2在xfun中文数据集上训练的RE模型|765M| 67.77% | 31.46 |[推理模型 coming soon]() / [训练模型](https://paddleocr.bj.bcebos.com/pplayout/re_LayoutLMv2_xfun_zh.tar) |
|
||||
|ser_LayoutLM_xfund_zh|基于LayoutLM在xfund中文数据集上训练的SER模型|430M| 77.31% | - |[推理模型](https://paddleocr.bj.bcebos.com/pplayout/ser_LayoutLM_xfun_zh_infer.tar) / [训练模型](https://paddleocr.bj.bcebos.com/pplayout/ser_LayoutLM_xfun_zh.tar) |
|
||||
|
||||
* 注:上述预测耗时信息仅包含了inference模型的推理耗时,没有统计预处理与后处理耗时,测试环境为`V100 GPU + CUDA 10.2 + CUDNN 8.1.1 + TRT 7.2.3.4`。
|
||||
|
||||
在wildreceipt数据集上,SDMGR模型精度与下载地址如下所示。
|
||||
|
||||
|
||||
|模型名称|模型简介|模型大小|精度|下载地址|
|
||||
| --- | --- | --- |--- | --- |
|
||||
|SDMGR|关键信息提取模型|78M| 86.70% | [推理模型 coming soon]() / [训练模型](https://paddleocr.bj.bcebos.com/dygraph_v2.1/kie/kie_vgg16.tar)|
|
||||
|
|
After Width: | Height: | Size: 1.6 MiB |
After Width: | Height: | Size: 1.6 MiB |
Before Width: | Height: | Size: 1.6 MiB After Width: | Height: | Size: 1.5 MiB |
After Width: | Height: | Size: 1.5 MiB |
|
@ -56,6 +56,8 @@ def init_args():
|
|||
"--ser_dict_path",
|
||||
type=str,
|
||||
default="../train_data/XFUND/class_list_xfun.txt")
|
||||
# need to be None or tb-yx
|
||||
parser.add_argument("--ocr_order_method", type=str, default=None)
|
||||
# params for inference
|
||||
parser.add_argument(
|
||||
"--mode",
|
||||
|
|
|
@ -0,0 +1,168 @@
|
|||
|
||||
# 怎样完成基于图像数据的信息抽取任务
|
||||
|
||||
- [1. 简介](#1-简介)
|
||||
- [1.1 背景](#11-背景)
|
||||
- [1.2 主流方法](#12-主流方法)
|
||||
- [2. 关键信息抽取任务流程](#2-关键信息抽取任务流程)
|
||||
- [2.1 训练OCR模型](#21-训练OCR模型)
|
||||
- [2.2 训练KIE模型](#22-训练KIE模型)
|
||||
- [3. 参考文献](#3-参考文献)
|
||||
|
||||
|
||||
## 1. 简介
|
||||
|
||||
### 1.1 背景
|
||||
|
||||
关键信息抽取 (Key Information Extraction, KIE)指的是是从文本或者图像中,抽取出关键的信息。针对文档图像的关键信息抽取任务作为OCR的下游任务,存在非常多的实际应用场景,如表单识别、车票信息抽取、身份证信息抽取等。然而,使用人力从这些文档图像中提取或者收集关键信息耗时费力,怎样自动化融合图像中的视觉、布局、文字等特征并完成关键信息抽取是一个价值与挑战并存的问题。
|
||||
|
||||
对于特定场景的文档图像,其中的关键信息位置、版式等较为固定,因此在研究早期有很多基于模板匹配的方法进行关键信息的抽取,考虑到其流程较为简单,该方法仍然被广泛应用在目前的很多场景中。但是这种基于模板匹配的方法在应用到不同的场景中时,需要耗费大量精力去调整与适配模板,迁移成本较高。
|
||||
|
||||
文档图像中的KIE一般包含2个子任务,示意图如下图所示。
|
||||
|
||||
* (1)SER: 语义实体识别 (Semantic Entity Recognition),对每一个检测到的文本进行分类,如将其分为姓名,身份证。如下图中的黑色框和红色框。
|
||||
* (2)RE: 关系抽取 (Relation Extraction),对每一个检测到的文本进行分类,如将其分为问题 (key) 和答案 (value) 。然后对每一个问题找到对应的答案,相当于完成key-value的匹配过程。如下图中的红色框和黑色框分别代表问题和答案,黄色线代表问题和答案之间的对应关系。
|
||||
|
||||
|
||||
<div align="center">
|
||||
<img src="https://user-images.githubusercontent.com/14270174/184588654-d87f54f3-13ab-42c4-afc0-da79bead3f14.png" width="800">
|
||||
</div>
|
||||
|
||||
|
||||
### 1.2 基于深度学习的主流方法
|
||||
|
||||
一般的KIE方法基于命名实体识别(Named Entity Recognition,NER)来展开研究,但是此类方法仅使用了文本信息而忽略了位置与视觉特征信息,因此精度受限。近几年大多学者开始融合多个模态的输入信息,进行特征融合,并对多模态信息进行处理,从而提升KIE的精度。主要方法有以下几种
|
||||
|
||||
* (1)基于Grid的方法:此类方法主要关注图像层面多模态信息的融合,文本大多大多为字符粒度,对文本与结构结构信息的嵌入方式较为简单,如Chargrid[1]等算法。
|
||||
* (2)基于Token的方法:此类方法参考NLP中的BERT等方法,将位置、视觉等特征信息共同编码到多模态模型中,并且在大规模数据集上进行预训练,从而在下游任务中,仅需要少量的标注数据便可以获得很好的效果。如LayoutLM[2], LayoutLMv2[3], LayoutXLM[4], StrucText[5]等算法。
|
||||
* (3)基于GCN的方法:此类方法尝试学习图像、文字之间的结构信息,从而可以解决开集信息抽取的问题(训练集中没有见过的模板),如GCN[6]、SDMGR[7]等算法。
|
||||
* (4)基于End-to-end的方法:此类方法将现有的OCR文字识别以及KIE信息抽取2个任务放在一个统一的网络中进行共同学习,并在学习过程中相互加强。如Trie[8]等算法。
|
||||
|
||||
更多关于该系列算法的详细介绍,请参考“动手学OCR·十讲”课程的课节六部分:[文档分析理论与实践](https://aistudio.baidu.com/aistudio/education/group/info/25207)。
|
||||
|
||||
## 2. 关键信息抽取任务流程
|
||||
|
||||
PaddleOCR中实现了LayoutXLM等算法(基于Token),同时,在PP-Structurev2中,对LayoutXLM多模态预训练模型的网络结构进行简化,去除了其中的Visual backbone部分,设计了视觉无关的VI-LayoutXLM模型,同时引入符合人类阅读顺序的排序逻辑以及UDML知识蒸馏策略,最终同时提升了关键信息抽取模型的精度与推理速度。
|
||||
|
||||
下面介绍怎样基于PaddleOCR完成关键信息抽取任务。
|
||||
|
||||
在非End-to-end的KIE方法中,完成关键信息抽取,至少需要**2个步骤**:首先使用OCR模型,完成文字位置与内容的提取,然后使用KIE模型,根据图像、文字位置以及文字内容,提取出其中的关键信息。
|
||||
|
||||
### 2.1 训练OCR模型
|
||||
|
||||
#### 2.1.1 文本检测
|
||||
|
||||
**(1)数据**
|
||||
|
||||
PaddleOCR中提供的模型大多数为通用模型,在进行文本检测的过程中,相邻文本行的检测一般是根据位置的远近进行区分,如上图,使用PP-OCRv3通用中英文检测模型进行文本检测时,容易将”民族“与“汉”这2个代表不同的字段检测到一起,从而增加后续KIE任务的难度。因此建议在做KIE任务的过程中,首先训练一个针对该文档数据集的检测模型。
|
||||
|
||||
在数据标注时,关键信息的标注需要隔开,比上图中的 “民族汉” 3个字相隔较近,此时需要将”民族“与”汉“标注为2个文本检测框,否则会增加后续KIE任务的难度。
|
||||
|
||||
对于下游任务,一般来说,`200~300`张的文本训练数据即可保证基本的训练效果,如果没有太多的先验知识,可以先标注 **`200~300`** 张图片,进行后续文本检测模型的训练。
|
||||
|
||||
|
||||
**(2)模型**
|
||||
|
||||
在模型选择方面,推荐使用PP-OCRv3_det,关于更多关于检测模型的训练方法介绍,请参考:[OCR文本检测模型训练教程](../../doc/doc_ch/detection.md)与[PP-OCRv3 文本检测模型训练教程](../../doc/doc_ch/PPOCRv3_det_train.md)。
|
||||
|
||||
#### 2.1.2 文本识别
|
||||
|
||||
相对自然场景,文档图像中的文本内容识别难度一般相对较低(背景相对不太复杂),因此**优先建议**尝试PaddleOCR中提供的PP-OCRv3通用文本识别模型([PP-OCRv3模型库链接](../../doc/doc_ch/models_list.md))。
|
||||
|
||||
**(1)数据**
|
||||
|
||||
然而,在部分文档场景中也会存在一些挑战,如身份证场景中存在着罕见字,在发票等场景中的字体比较特殊,这些问题都会增加文本识别的难度,此时如果希望保证或者进一步提升模型的精度,建议基于特定文档场景的文本识别数据集,加载PP-OCRv3模型进行微调。
|
||||
|
||||
在模型微调的过程中,建议准备至少`5000`张垂类场景的文本识别图像,可以保证基本的模型微调效果。如果希望提升模型的精度与泛化能力,可以合成更多与该场景类似的文本识别数据,从公开数据集中收集通用真实文本识别数据,一并添加到该场景的文本识别训练任务过程中。在训练过程中,建议每个epoch的真实垂类数据、合成数据、通用数据比例在`1:1:1`左右,这可以通过设置不同数据源的采样比例进行控制。如有3个训练文本文件,分别包含1W、2W、5W条数据,那么可以在配置文件中设置数据如下:
|
||||
|
||||
```yml
|
||||
Train:
|
||||
dataset:
|
||||
name: SimpleDataSet
|
||||
data_dir: ./train_data/
|
||||
label_file_list:
|
||||
- ./train_data/train_list_1W.txt
|
||||
- ./train_data/train_list_2W.txt
|
||||
- ./train_data/train_list_5W.txt
|
||||
ratio_list: [1.0, 0.5, 0.2]
|
||||
...
|
||||
```
|
||||
|
||||
**(2)模型**
|
||||
|
||||
在模型选择方面,推荐使用通用中英文文本识别模型PP-OCRv3_rec,关于更多关于文本识别模型的训练方法介绍,请参考:[OCR文本识别模型训练教程](../../doc/doc_ch/recognition.md)与[PP-OCRv3文本识别模型库与配置文件](../../doc/doc_ch/models_list.md)。
|
||||
|
||||
### 2.2 训练KIE模型
|
||||
|
||||
对于识别得到的文字进行关键信息抽取,有2种主要的方法。
|
||||
|
||||
(1)直接使用SER,获取关键信息的类别:如身份证场景中,将“姓名“与”张三“分别标记为`name_key`与`name_value`。最终识别得到的类别为`name_value`对应的**文本字段**即为我们所需要的关键信息。
|
||||
|
||||
(2)联合SER与RE进行使用:这种方法中,首先使用SER,获取图像文字内容中所有的key与value,然后使用RE方法,对所有的key与value进行配对,找到映射关系,从而完成关键信息的抽取。
|
||||
|
||||
#### 2.2.1 SER
|
||||
|
||||
以身份证场景为例, 关键信息一般包含`姓名`、`性别`、`民族`等,我们直接将对应的字段标注为特定的类别即可,如下图所示。
|
||||
|
||||
<div align="center">
|
||||
<img src="https://user-images.githubusercontent.com/14270174/184526682-8b810397-5a93-4395-93da-37b8b8494c41.png" width="500">
|
||||
</div>
|
||||
|
||||
**注意:**
|
||||
|
||||
- 标注过程中,对于无关于KIE关键信息的文本内容,均需要将其标注为`other`类别,相当于背景信息。如在身份证场景中,如果我们不关注性别信息,那么可以将“性别”与“男”这2个字段的类别均标注为`other`。
|
||||
- 标注过程中,需要以**文本行**为单位进行标注,无需标注单个字符的位置信息。
|
||||
|
||||
数据量方面,一般来说,对于比较固定的场景,**50张**左右的训练图片即可达到可以接受的效果,可以使用[PPOCRLabel](../../PPOCRLabel/README_ch.md)完成KIE的标注过程。
|
||||
|
||||
模型方面,推荐使用PP-Structurev2中提出的VI-LayoutXLM模型,它基于LayoutXLM模型进行改进,去除其中的视觉特征提取模块,在精度基本无损的情况下,进一步提升了模型推理速度。更多教程请参考:[VI-LayoutXLM算法介绍](../../doc/doc_ch/algorithm_kie_vi_layoutxlm.md)与[KIE关键信息抽取使用教程](../../doc/doc_ch/kie.md)。
|
||||
|
||||
|
||||
#### 2.2.2 SER + RE
|
||||
|
||||
该过程主要包含SER与RE 2个过程。SER阶段主要用于识别出文档图像中的所有key与value,RE阶段主要用于对所有的key与value进行匹配。
|
||||
|
||||
以身份证场景为例, 关键信息一般包含`姓名`、`性别`、`民族`等关键信息,在SER阶段,我们需要识别所有的question (key) 与answer (value) 。标注如下所示。每个字段的类别信息(`label`字段)可以是question、answer或者other(与待抽取的关键信息无关的字段)
|
||||
|
||||
<div align="center">
|
||||
<img src="https://user-images.githubusercontent.com/14270174/184526785-c3d2d310-cd57-4d31-b933-912716b29856.jpg" width="500">
|
||||
</div>
|
||||
|
||||
|
||||
在RE阶段,需要标注每个字段的的id与连接信息,如下图所示。
|
||||
|
||||
<div align="center">
|
||||
<img src="https://user-images.githubusercontent.com/14270174/184528728-626f77eb-fd9f-4709-a7dc-5411cc417dab.jpg" width="500">
|
||||
</div>
|
||||
|
||||
每个文本行字段中,需要添加`id`与`linking`字段信息,`id`记录该文本行的唯一标识,同一张图片中的不同文本内容不能重复,`linking`是一个列表,记录了不同文本之间的连接信息。如字段“出生”的id为0,字段“1996年1月11日”的id为1,那么它们均有[[0, 1]]的`linking`标注,表示该id=0与id=1的字段构成key-value的关系(姓名、性别等字段类似,此处不再一一赘述)。
|
||||
|
||||
|
||||
**注意:**
|
||||
|
||||
- 标注过程中,如果value是多个字符,那么linking中可以新增一个key-value对,如`[[0, 1], [0, 2]]`
|
||||
|
||||
|
||||
数据量方面,一般来说,对于比较固定的场景,**50张**左右的训练图片即可达到可以接受的效果,可以使用PPOCRLabel完成KIE的标注过程。
|
||||
|
||||
模型方面,推荐使用PP-Structurev2中提出的VI-LayoutXLM模型,它基于LayoutXLM模型进行改进,去除其中的视觉特征提取模块,在精度基本无损的情况下,进一步提升了模型推理速度。更多教程请参考:[VI-LayoutXLM算法介绍](../../doc/doc_ch/algorithm_kie_vi_layoutxlm.md)与[KIE关键信息抽取使用教程](../../doc/doc_ch/kie.md)。
|
||||
|
||||
|
||||
## 3. 参考文献
|
||||
|
||||
|
||||
[1] Katti A R, Reisswig C, Guder C, et al. Chargrid: Towards understanding 2d documents[J]. arXiv preprint arXiv:1809.08799, 2018.
|
||||
|
||||
[2] Xu Y, Li M, Cui L, et al. Layoutlm: Pre-training of text and layout for document image understanding[C]//Proceedings of the 26th ACM SIGKDD International Conference on Knowledge Discovery & Data Mining. 2020: 1192-1200.
|
||||
|
||||
[3] Xu Y, Xu Y, Lv T, et al. LayoutLMv2: Multi-modal pre-training for visually-rich document understanding[J]. arXiv preprint arXiv:2012.14740, 2020.
|
||||
|
||||
[4]: Xu Y, Lv T, Cui L, et al. Layoutxlm: Multimodal pre-training for multilingual visually-rich document understanding[J]. arXiv preprint arXiv:2104.08836, 2021.
|
||||
|
||||
[5] Li Y, Qian Y, Yu Y, et al. StrucTexT: Structured Text Understanding with Multi-Modal Transformers[C]//Proceedings of the 29th ACM International Conference on Multimedia. 2021: 1912-1920.
|
||||
|
||||
[6] Liu X, Gao F, Zhang Q, et al. Graph convolution for multimodal information extraction from visually rich documents[J]. arXiv preprint arXiv:1903.11279, 2019.
|
||||
|
||||
[7] Sun H, Kuang Z, Yue X, et al. Spatial Dual-Modality Graph Reasoning for Key Information Extraction[J]. arXiv preprint arXiv:2103.14470, 2021.
|
||||
|
||||
[8] Zhang P, Xu Y, Cheng Z, et al. Trie: End-to-end text reading and information extraction for document understanding[C]//Proceedings of the 28th ACM International Conference on Multimedia. 2020: 1413-1422.
|
|
@ -40,14 +40,20 @@ logger = get_logger()
|
|||
|
||||
class SerPredictor(object):
|
||||
def __init__(self, args):
|
||||
self.ocr_engine = PaddleOCR(use_angle_cls=False, show_log=False)
|
||||
self.ocr_engine = PaddleOCR(
|
||||
use_angle_cls=args.use_angle_cls,
|
||||
det_model_dir=args.det_model_dir,
|
||||
rec_model_dir=args.rec_model_dir,
|
||||
show_log=False,
|
||||
use_gpu=args.use_gpu)
|
||||
|
||||
pre_process_list = [{
|
||||
'VQATokenLabelEncode': {
|
||||
'algorithm': args.vqa_algorithm,
|
||||
'class_path': args.ser_dict_path,
|
||||
'contains_re': False,
|
||||
'ocr_engine': self.ocr_engine
|
||||
'ocr_engine': self.ocr_engine,
|
||||
'order_method': args.ocr_order_method,
|
||||
}
|
||||
}, {
|
||||
'VQATokenPad': {
|
||||
|
|
|
@ -0,0 +1,59 @@
|
|||
===========================train_params===========================
|
||||
model_name:vi_layoutxlm_ser
|
||||
python:python3.7
|
||||
gpu_list:0|0,1
|
||||
Global.use_gpu:True|True
|
||||
Global.auto_cast:fp32
|
||||
Global.epoch_num:lite_train_lite_infer=1|whole_train_whole_infer=17
|
||||
Global.save_model_dir:./output/
|
||||
Train.loader.batch_size_per_card:lite_train_lite_infer=4|whole_train_whole_infer=8
|
||||
Architecture.Backbone.checkpoints:null
|
||||
train_model_name:latest
|
||||
train_infer_img_dir:ppstructure/docs/vqa/input/zh_val_42.jpg
|
||||
null:null
|
||||
##
|
||||
trainer:norm_train
|
||||
norm_train:tools/train.py -c ./configs/kie/vi_layoutxlm/ser_vi_layoutxlm_xfund_zh.yml -o Global.print_batch_step=1 Global.eval_batch_step=[1000,1000] Train.loader.shuffle=false
|
||||
pact_train:null
|
||||
fpgm_train:null
|
||||
distill_train:null
|
||||
null:null
|
||||
null:null
|
||||
##
|
||||
===========================eval_params===========================
|
||||
eval:null
|
||||
null:null
|
||||
##
|
||||
===========================infer_params===========================
|
||||
Global.save_inference_dir:./output/
|
||||
Architecture.Backbone.checkpoints:
|
||||
norm_export:tools/export_model.py -c ./configs/kie/vi_layoutxlm/ser_vi_layoutxlm_xfund_zh.yml -o
|
||||
quant_export:
|
||||
fpgm_export:
|
||||
distill_export:null
|
||||
export1:null
|
||||
export2:null
|
||||
##
|
||||
infer_model:null
|
||||
infer_export:null
|
||||
infer_quant:False
|
||||
inference:ppstructure/vqa/predict_vqa_token_ser.py --vqa_algorithm=LayoutXLM --ser_dict_path=train_data/XFUND/class_list_xfun.txt --output=output --ocr_order_method=tb-yx
|
||||
--use_gpu:True|False
|
||||
--enable_mkldnn:False
|
||||
--cpu_threads:6
|
||||
--rec_batch_num:1
|
||||
--use_tensorrt:False
|
||||
--precision:fp32
|
||||
--ser_model_dir:
|
||||
--image_dir:./ppstructure/docs/vqa/input/zh_val_42.jpg
|
||||
null:null
|
||||
--benchmark:False
|
||||
null:null
|
||||
===========================infer_benchmark_params==========================
|
||||
random_infer_input:[{float32,[3,224,224]}]
|
||||
===========================train_benchmark_params==========================
|
||||
batch_size:4
|
||||
fp_items:fp32|fp16
|
||||
epoch:3
|
||||
--profiler_options:batch_range=[10,20];state=GPU;tracer_option=Default;profile_path=model.profile
|
||||
flags:FLAGS_eager_delete_tensor_gb=0.0;FLAGS_fraction_of_gpu_memory_to_use=0.98
|
|
@ -106,7 +106,7 @@ if [ ${MODE} = "benchmark_train" ];then
|
|||
ln -s ./icdar2015_benckmark ./icdar2015
|
||||
cd ../
|
||||
fi
|
||||
if [ ${model_name} == "layoutxlm_ser" ]; then
|
||||
if [ ${model_name} == "layoutxlm_ser" ] || [ ${model_name} == "vi_layoutxlm_ser" ]; then
|
||||
pip install -r ppstructure/vqa/requirements.txt
|
||||
pip install paddlenlp\>=2.3.5 --force-reinstall -i https://mirrors.aliyun.com/pypi/simple/
|
||||
wget -nc -P ./train_data/ https://paddleocr.bj.bcebos.com/ppstructure/dataset/XFUND.tar --no-check-certificate
|
||||
|
@ -220,7 +220,7 @@ if [ ${MODE} = "lite_train_lite_infer" ];then
|
|||
wget -nc -P ./pretrain_models/ https://paddleocr.bj.bcebos.com/rec_r32_gaspin_bilstm_att_train.tar --no-check-certificate
|
||||
cd ./pretrain_models/ && tar xf rec_r32_gaspin_bilstm_att_train.tar && cd ../
|
||||
fi
|
||||
if [ ${model_name} == "layoutxlm_ser" ]; then
|
||||
if [ ${model_name} == "layoutxlm_ser" ] || [ ${model_name} == "vi_layoutxlm_ser" ]; then
|
||||
pip install -r ppstructure/vqa/requirements.txt
|
||||
pip install paddlenlp\>=2.3.5 --force-reinstall -i https://mirrors.aliyun.com/pypi/simple/
|
||||
wget -nc -P ./train_data/ https://paddleocr.bj.bcebos.com/ppstructure/dataset/XFUND.tar --no-check-certificate
|
||||
|
|
|
@ -122,7 +122,7 @@ def export_single_model(model,
|
|||
paddle.static.InputSpec(
|
||||
shape=[None, 3, 224, 224], dtype="int64"), # image
|
||||
]
|
||||
if arch_config["algorithm"] == "LayoutLM":
|
||||
if model.backbone.use_visual_backbone is False:
|
||||
input_spec.pop(4)
|
||||
model = to_static(model, input_spec=[input_spec])
|
||||
else:
|
||||
|
|
|
@ -342,7 +342,7 @@ class TextRecognizer(object):
|
|||
for beg_img_no in range(0, img_num, batch_num):
|
||||
end_img_no = min(img_num, beg_img_no + batch_num)
|
||||
norm_img_batch = []
|
||||
imgC, imgH, imgW = self.rec_image_shape
|
||||
imgC, imgH, imgW = self.rec_image_shape[:3]
|
||||
max_wh_ratio = imgW / imgH
|
||||
# max_wh_ratio = 0
|
||||
for ino in range(beg_img_no, end_img_no):
|
||||
|
|
|
@ -104,8 +104,6 @@ class SerPredictor(object):
|
|||
batch = transform(data, self.ops)
|
||||
batch = to_tensor(batch)
|
||||
preds = self.model(batch)
|
||||
if self.algorithm in ['LayoutLMv2', 'LayoutXLM']:
|
||||
preds = preds[0]
|
||||
|
||||
post_result = self.post_process_class(
|
||||
preds, segment_offset_ids=batch[6], ocr_infos=batch[7])
|
||||
|
|