yangjun 5709eb762c 提交PaddleOCR dygraph分支 6cbd7d1ecef832d428e21ef98c44382c5384d8f7 | 1 年間 前 | |
---|---|---|
.. | ||
tools | 1 年間 前 | |
README.md | 1 年間 前 | |
README_ch.md | 1 年間 前 | |
how_to_do_kie.md | 1 年間 前 | |
how_to_do_kie_en.md | 1 年間 前 | |
predict_kie_token_ser.py | 1 年間 前 | |
predict_kie_token_ser_re.py | 1 年間 前 | |
requirements.txt | 1 年間 前 |
English | 简体中文
Key information extraction (KIE) refers to extracting key information from text or images. As downstream task of OCR, the key information extraction task of document image has many practical application scenarios, such as form recognition, ticket information extraction, ID card information extraction, etc.
PP-Structure conducts research based on the LayoutXLM multi-modal, and proposes the VI-LayoutXLM, which gets rid of visual features when finetuning the downstream tasks. An textline sorting method is also utilized to fit in reading order. What's more, UDML knowledge distillation is used for higher accuracy. Finally, the accuracy and inference speed of VI-LayoutXLM surpass those of LayoutXLM.
The main features of the key information extraction module in PP-Structure are as follows.
We evaluate the methods on the Chinese dataset of XFUND, and the performance is as follows
Model | Backbone | Task | Config file | Hmean | Inference time (ms) | Download link |
---|---|---|---|---|---|---|
VI-LayoutXLM | VI-LayoutXLM-base | SER | ser_vi_layoutxlm_xfund_zh_udml.yml | 93.19% | 15.49 | trained model |
LayoutXLM | LayoutXLM-base | SER | ser_layoutxlm_xfund_zh.yml | 90.38% | 19.49 | trained model |
VI-LayoutXLM | VI-LayoutXLM-base | RE | re_vi_layoutxlm_xfund_zh_udml.yml | 83.92% | 15.49 | trained model |
LayoutXLM | LayoutXLM-base | RE | re_layoutxlm_xfund_zh.yml | 74.83% | 19.49 | trained model |
For more KIE models in PaddleOCR, please refer to KIE model zoo.
There are two main solutions to the key information extraction task based on VI-LayoutXLM series model.
(1) Text detection + text recognition + semantic entity recognition (SER)
(2) Text detection + text recognition + semantic entity recognition (SER) + relationship extraction (RE)
The following images are demo results of the SER and RE models. For more detailed introduction to the above solutions, please refer to KIE Guide.
Demo results for SER task are as follows.
Note: test pictures are from xfund dataset, invoice dataset and a composite ID card dataset.
Boxes of different colors in the image represent different categories.
The invoice and application form images have three categories: request
, answer
and header
. The question
and 'answer' can be used to extract the relationship.
For the ID card image, the model can directly identify the key information such as name
, gender
, nationality
, so that the subsequent relationship extraction process is not required, and the key information extraction task can be completed using only on model.
Demo results for RE task are as follows.
Red boxes are questions, blue boxes are answers. The green lines means the two conected objects are a pair.
Use the following command to install KIE dependencies.
git clone https://github.com/PaddlePaddle/PaddleOCR.git
cd PaddleOCR
pip install -r requirements.txt
pip install -r ppstructure/kie/requirements.txt
# 安装PaddleOCR引擎用于预测
pip install paddleocr -U
The visualized results of SER are saved in the ./output
folder by default. Examples of results are as follows.
Here we use XFUND dataset to quickly experience the SER model and RE model.
mkdir train_data
cd train_data
# download and uncompress the dataset
wget https://paddleocr.bj.bcebos.com/ppstructure/dataset/XFUND.tar && tar -xf XFUND.tar
cd ..
Use the following command to download the models.
mkdir pretrained_model
cd pretrained_model
# download and uncompress the SER trained model
wget https://paddleocr.bj.bcebos.com/ppstructure/models/vi_layoutxlm/ser_vi_layoutxlm_xfund_pretrained.tar && tar -xf ser_vi_layoutxlm_xfund_pretrained.tar
# download and uncompress the RE trained model
wget https://paddleocr.bj.bcebos.com/ppstructure/models/vi_layoutxlm/re_vi_layoutxlm_xfund_pretrained.tar && tar -xf re_vi_layoutxlm_xfund_pretrained.tar
If you want to use OCR engine to obtain end-to-end prediction results, you can use the following command to predict.
# just predict using SER trained model
python3 tools/infer_kie_token_ser.py \
-c configs/kie/vi_layoutxlm/ser_vi_layoutxlm_xfund_zh.yml \
-o Architecture.Backbone.checkpoints=./pretrained_model/ser_vi_layoutxlm_xfund_pretrained/best_accuracy \
Global.infer_img=./ppstructure/docs/kie/input/zh_val_42.jpg
# predict using SER and RE trained model at the same time
python3 ./tools/infer_kie_token_ser_re.py \
-c configs/kie/vi_layoutxlm/re_vi_layoutxlm_xfund_zh.yml \
-o Architecture.Backbone.checkpoints=./pretrained_model/re_vi_layoutxlm_xfund_pretrained/best_accuracy \
Global.infer_img=./train_data/XFUND/zh_val/image/zh_val_42.jpg \
-c_ser configs/kie/vi_layoutxlm/ser_vi_layoutxlm_xfund_zh.yml \
-o_ser Architecture.Backbone.checkpoints=./pretrained_model/ser_vi_layoutxlm_xfund_pretrained/best_accuracy
The visual result images and the predicted text file will be saved in the Global.save_res_path
directory.
If you want to use a custom ocr model, you can set it through the following fields
Global.kie_det_model_dir
: the detection inference model pathGlobal.kie_rec_model_dir
: the recognition inference model pathIf you want to load the text detection and recognition results collected before, you can use the following command to predict.
# just predict using SER trained model
python3 tools/infer_kie_token_ser.py \
-c configs/kie/vi_layoutxlm/ser_vi_layoutxlm_xfund_zh.yml \
-o Architecture.Backbone.checkpoints=./pretrained_model/ser_vi_layoutxlm_xfund_pretrained/best_accuracy \
Global.infer_img=./train_data/XFUND/zh_val/val.json \
Global.infer_mode=False
# predict using SER and RE trained model at the same time
python3 ./tools/infer_kie_token_ser_re.py \
-c configs/kie/vi_layoutxlm/re_vi_layoutxlm_xfund_zh.yml \
-o Architecture.Backbone.checkpoints=./pretrained_model/re_vi_layoutxlm_xfund_pretrained/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=./pretrained_model/ser_vi_layoutxlm_xfund_pretrained/best_accuracy
Firstly, download the inference SER inference model.
mkdir inference
cd inference
wget https://paddleocr.bj.bcebos.com/ppstructure/models/vi_layoutxlm/ser_vi_layoutxlm_xfund_infer.tar && tar -xf ser_vi_layoutxlm_xfund_infer.tar
wget https://paddleocr.bj.bcebos.com/ppstructure/models/vi_layoutxlm/re_vi_layoutxlm_xfund_infer.tar && tar -xf re_vi_layoutxlm_xfund_infer.tar
cd ..
Use the following command for inference.
cd ppstructure
python3 kie/predict_kie_token_ser.py \
--kie_algorithm=LayoutXLM \
--ser_model_dir=../inference/ser_vi_layoutxlm_xfund_infer \
--image_dir=./docs/kie/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"
The visual results and text file will be saved in directory output
.
Use the following command for inference.
cd ppstructure
python3 kie/predict_kie_token_ser_re.py \
--kie_algorithm=LayoutXLM \
--re_model_dir=../inference/re_vi_layoutxlm_xfund_infer \
--ser_model_dir=../inference/ser_vi_layoutxlm_xfund_infer \
--use_visual_backbone=False \
--image_dir=./docs/kie/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"
The visual results and text file will be saved in directory output
.
If you want to use a custom ocr model, you can set it through the following fields
--det_model_dir
: the detection inference model path--rec_model_dir
: the recognition inference model pathFor training, evaluation and inference tutorial for KIE models, please refer to KIE doc.
For training, evaluation and inference tutorial for text detection models, please refer to text detection doc.
For training, evaluation and inference tutorial for text recognition models, please refer to text recognition doc.
To complete the key information extraction task in your own scenario from data preparation to model selection, please refer to: Guide to End-to-end KIE。
The content of this project itself is licensed under the Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)