Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
horcham committed Apr 22, 2024
2 parents 7275209 + c698646 commit 7971ee0
Show file tree
Hide file tree
Showing 65 changed files with 8,981 additions and 756 deletions.
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ You can do MindSpore Lite inference in MindOCR using **MindOCR models** or **Thi
<details open markdown>
<summary>Key Information Extraction</summary>

- [x] [LayoutXLM SER](configs/kie/vi_layoutxlm/README_CN.md) (arXiv'2016)
- [x] [LayoutXLM](configs/kie/vi_layoutxlm/README_CN.md) (arXiv'2021)
- [x] [LayoutLMv3](configs/kie/layoutlmv3/README.md) (arXiv'2022)

</details>

Expand All @@ -212,6 +213,13 @@ You can do MindSpore Lite inference in MindOCR using **MindOCR models** or **Thi

</details>

<details open markdown>
<summary>OCR large model</summary>

- [x] [Vary](configs/llm/vary/README.md) (arXiv'2023)

</details>

For the detailed performance of the trained models, please refer to [configs](./configs).

For details of MindSpore Lite and ACL inference models support, please refer to [MindOCR Models Support List](docs/en/inference/inference_quickstart.md) and [Third-party Models Support List](docs/en/inference/inference_thirdparty_quickstart.md) (PaddleOCR, MMOCR, etc.).
Expand Down Expand Up @@ -280,6 +288,14 @@ Frequently asked questions about configuring environment and mindocr, please ref
<details close markdown>
<summary>News</summary>

- 2023/04/01
1. Add new trained models
- [LayoutLMv3](configs/kie/layoutlmv3/) for key information extraction

- 2024/03/20
1. Add new trained models
- [Vary-toy](configs/llm/vary/vary_toy.yaml) for OCR large model, providing Qwen-1.8B LLM-based object detection and OCR abilities

- 2023/12/25
1. Add new trained models
- [TableMaster](configs/table/table_master.yaml) for table recognition
Expand All @@ -288,8 +304,8 @@ Frequently asked questions about configuring environment and mindocr, please ref

- 2023/12/14
1. Add new trained models
- [LayoutXLM SER](configs/kie/vi_layoutxlm) for key information extraction
- [VI-LayoutXLM SER](configs/kie/layoutlm_series) for key information extraction
- [LayoutXLM](configs/kie/layoutxlm) for key information extraction
- [VI-LayoutXLM](configs/kie/vi_layoutxlm) for key information extraction
- [PP-OCRv3 DBNet](configs/det/dbnet/db_mobilenetv3_ppocrv3.yaml) for text detection and [PP-OCRv3 SVTR](configs/rec/svtr/svtr_ppocrv3_ch.yaml) for recognition, supporting online inferece and finetuning
2. Add more benchmark datasets and their results
- [XFUND](configs/kie/vi_layoutxlm/README_CN.md)
Expand Down
22 changes: 19 additions & 3 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ python tools/infer/text/predict_system.py --image_dir {path_to_img or dir_to_img
<details open markdown>
<summary>关键信息抽取</summary>

- [x] [LayoutXLM SER](configs/kie/vi_layoutxlm/README_CN.md) (arXiv'2016)
- [x] [LayoutXLM](configs/kie/vi_layoutxlm/README_CN.md) (arXiv'2021)
- [x] [LayoutLMv3](configs/kie/layoutlmv3/README_CN.md) (arXiv'2022)

</details>

Expand All @@ -205,6 +206,13 @@ python tools/infer/text/predict_system.py --image_dir {path_to_img or dir_to_img

</details>

<details open markdown>
<summary>OCR大模型</summary>

- [x] [Vary](configs/llm/vary/README_CN.md) (arXiv'2023)

</details>


关于以上模型的具体训练方法和结果,请参见[configs](./configs)下各模型子目录的readme文档。

Expand Down Expand Up @@ -275,6 +283,14 @@ MindOCR提供了[数据格式转换工具](tools/dataset_converters) ,以支
<details close markdown>
<summary>详细</summary>

- 2023/04/01
1. 增加新模型
- 关键信息抽取[LayoutLMv3](configs/kie/layoutlmv3/)

- 2024/03/20
1. 增加新模型
- OCR大模型[Vary-toy](configs/llm/vary/vary_toy.yaml),支持基于通义千问1.8B LLM的检测和OCR功能

- 2023/12/25
1. 增加新模型
- 表格识别[TableMaster](configs/table/table_master.yaml)
Expand All @@ -283,8 +299,8 @@ MindOCR提供了[数据格式转换工具](tools/dataset_converters) ,以支

- 2023/12/14
1. 增加新模型
- 关键信息抽取[LayoutXLM SER](configs/kie/vi_layoutxlm)
- 关键信息抽取[VI-LayoutXLM SER](configs/kie/layoutlm_series)
- 关键信息抽取[LayoutXLM](configs/kie/layoutxlm)
- 关键信息抽取[VI-LayoutXLM](configs/kie/vi_layoutxlm)
- 文本检测[PP-OCRv3 DBNet](configs/det/dbnet/db_mobilenetv3_ppocrv3.yaml)和文本识别[PP-OCRv3 SVTR](configs/rec/svtr/svtr_ppocrv3_ch.yaml),支持在线推理和微调训练
2. 添加更多基准数据集及其结果
- [XFUND](configs/kie/vi_layoutxlm/README_CN.md)
Expand Down
250 changes: 250 additions & 0 deletions configs/kie/layoutlmv3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
English | [中文](README_CN.md)

# LayoutLMv3
<!--- Guideline: use url linked to abstract in ArXiv instead of PDF for fast loading. -->

> [LayoutLMv3: Pre-training for Document AI with Unified Text and Image Masking](https://arxiv.org/abs/2204.08387)

## 1. Introduction
Unlike previous LayoutLM series models, LayoutLMv3 does not rely on complex CNN or Faster R-CNN networks to represent images in its model architecture. Instead, it directly utilizes image blocks of document images, thereby greatly reducing parameters and avoiding complex document preprocessing such as manual annotation of target region boxes and document object detection. Its simple unified architecture and training objectives make LayoutLMv3 a versatile pretraining model suitable for both text-centric and image-centric document AI tasks.

The experimental results demonstrate that LayoutLMv3 achieves better performance with fewer parameters on the following datasets:

- Text-centric datasets: Form Understanding FUNSD dataset, Receipt Understanding CORD dataset, and Document Visual Question Answering DocVQA dataset.
- Image-centric datasets: Document Image Classification RVL-CDIP dataset and Document Layout Analysis PubLayNet dataset.

LayoutLMv3 also employs a text-image multimodal Transformer architecture to learn cross-modal representations. Text vectors are obtained by adding word vectors, one-dimensional positional vectors, and two-dimensional positional vectors of words. Text from document images and their corresponding two-dimensional positional information (layout information) are extracted using optical character recognition (OCR) tools. As adjacent words in text often convey similar semantics, LayoutLMv3 shares the two-dimensional positional vectors of adjacent words, while each word in LayoutLM and LayoutLMv2 has different two-dimensional positional vectors.

The representation of image vectors typically relies on CNN-extracted feature grid features or Faster R-CNN-extracted region features, which increase computational costs or depend on region annotations. Therefore, the authors obtain image features by linearly mapping image blocks, a representation method initially proposed in ViT, which incurs minimal computational cost and does not rely on region annotations, effectively addressing the aforementioned issues. Specifically, the image is first resized to a uniform size (e.g., 224x224), then divided into fixed-size blocks (e.g., 16x16), and image features are obtained through linear mapping to form an image feature sequence, followed by addition of a learnable one-dimensional positional vector to obtain the image vector.[[1](#references)]

<p align="center">
<img src=layoutlmv3_arch.jpg width=1000 />
</p>
<p align="center">
<em> Figure 1. LayoutLMv3 architecture [<a href="#references">1</a>] </em>
</p>

## 2. Results
<!--- Guideline:
Table Format:
- Model: model name in lower case with _ seperator.
- Context: Training context denoted as {device}x{pieces}-{MS mode}, where mindspore mode can be G - graph mode or F - pynative mode with ms function. For example, D910x8-G is for training on 8 pieces of Ascend 910 NPU using graph mode.
- Top-1 and Top-5: Keep 2 digits after the decimal point.
- Params (M): # of model parameters in millions (10^6). Keep 2 digits after the decimal point
- Recipe: Training recipe/configuration linked to a yaml config file. Use absolute url path.
- Download: url of the pretrained model weights. Use absolute url path.
-->

### Accuracy


According to our experiments, the performance and accuracy evaluation([Model Evaluation](#33-Model-Evaluation)) results of training ([Model Training](#32-Model-Training)) on the XFUND Chinese dataset are as follows:

<div align="center">

| **Model** | **Task** | **Context** | **Dateset** | **Model Params** | **Batch size** | **Graph train 1P (s/epoch)** | **Graph train 1P (ms/step)** | **Graph train 1P (FPS)** | **hmean** | **Config** | **Download** |
| :----------: | :------: | :-------------: | :--------: | :--------: | :----------: | :--------------------------: | :--------------------------: | :----------------------: | :-------: | :----------------------------------------------------: | :------------------------------------------------------------------------------------------------: |
| LayoutLMv3 | SER | D910x1-MS2.1-G | XFUND_zh | 265.8 M | 8 | 19.53 | 1094.86 | 7.37 | 91.88% | [yaml](../layoutlmv3/ser_layoutlmv3_xfund_zh.yaml) | ckpt(TODO) |

</div>



## 3. Quick Start
### 3.1 Preparation

#### 3.1.1 Installation
Please refer to the [installation instruction](https://github.com/mindspore-lab/mindocr#installation) in MindOCR.

#### 3.1.2 Dataset Download

[The XFUND dataset](https://github.com/doc-analysis/XFUND) is used as the experimental dataset. The XFUND dataset is a multilingual dataset proposed by Microsoft for the Knowledge-Intensive Extraction (KIE) task. It consists of seven datasets, each containing 149 training samples and 50 validation samples.

Respectively: ZH (Chinese), JA (Japanese), ES (Spanish), FR (French), IT (Italian), DE (German), PT (Portuguese)

a preprocessed [Chinese dataset](https://download.mindspore.cn/toolkits/mindocr/vi-layoutxlm/XFUND.tar) that can be directly used is provided for everyone to download.

```bash
mkdir train_data
cd train_data
wget https://download.mindspore.cn/toolkits/mindocr/vi-layoutxlm/XFUND.tar && tar -xf XFUND.tar
cd ..
```

#### 3.1.3 Dataset Usage

After decompression, the data folder structure is as follows:

```bash
└─ zh_train/ Training set
├── image/ Folder for storing images
├── train.json Annotation information
└─ zh_val/ Validation set
├── image/ Folder for storing images
├── val.json Annotation information

```

The annotation format of this dataset is:

```bash
{
"height": 3508, # Image height
"width": 2480, # Image width
"ocr_info": [
{
"text": "邮政地址:", # Single text content
"label": "question", # Category of the text
"bbox": [261, 802, 483, 859], # Single text box
"id": 54, # Text index
"linking": [[54, 60]], # Relationships between the current text and other texts [question, answer]
"words": []
},
{
"text": "湖南省怀化市市辖区",
"label": "answer",
"bbox": [487, 810, 862, 859],
"id": 60,
"linking": [[54, 60]],
"words": []
}
]
}
```

**The data configuration for model training.**

If you want to reproduce the training of the model, it is recommended to modify the dataset-related fields in the configuration YAML file as follows:

```yaml
...
train:
...
dataset:
type: KieDataset
dataset_root: path/to/dataset/ # Root directory of the training dataset
data_dir: XFUND/zh_train/image/ # Directory of the training dataset, concatenated with `dataset_root` to form the complete directory of the training dataset
label_file: XFUND/zh_train/train.json # Path to the label file of the training dataset, concatenated with `dataset_root` to form the complete path of the label file of the training dataset
...
eval:
dataset:
type: KieDataset
dataset_root: path/to/dataset/ # Root directory of the validation dataset
data_dir: XFUND/zh_val/image/ # Directory of the validation dataset, concatenated with `dataset_root` to form the complete directory of the validation dataset
label_file: XFUND/zh_val/val.json # Path to the label file of the validation dataset, concatenated with `dataset_root` to form the complete path of the label file of the validation dataset
...

```

#### 3.1.4 Check YAML Config Files
Apart from the dataset setting, please also check the following important args: `system.distribute`, `system.val_while_train`, `common.batch_size`, `train.ckpt_save_dir`, `train.dataset.dataset_path`, `eval.ckpt_load_path`, `eval.dataset.dataset_path`, `eval.loader.batch_size`. Explanations of these important args:

```yaml
system:
mode:
distribute: False # `True` for distributed training, `False` for standalone training
amp_level: 'O0'
seed: 42
val_while_train: True # Validate while training
drop_overflow_update: False
model:
type: kie
transform: null
backbone:
name: layoutlmv3
head:
name: TokenClassificationHead
num_classes: 7
use_visual_backbone: True
use_float16: True
pretrained:
...
train:
ckpt_save_dir: './tmp_kie_ser' # The training result (including checkpoints, per-epoch performance and curves) saving directory
dataset_sink_mode: False
dataset:
type: KieDataset
dataset_root: path/to/dataset/ # Path of training dataset
data_dir: XFUND/zh_train/image/ # Path of training dataset data dir
label_file: XFUND/zh_train/train.json # Path of training dataset label file
...
eval:
ckpt_load_path: './tmp_kie_ser/best.ckpt' # checkpoint file path
dataset_sink_mode: False
dataset:
type: KieDataset
dataset_root: path/to/dataset/ # Path of evaluation dataset
data_dir: XFUND/zh_val/image/ # Path of evaluation dataset data dir
label_file: XFUND/zh_val/val.json # Path of evaluation dataset label file
...
...
...
```

**Notes:**
- As the global batch size (batch_size x num_devices) is important for reproducing the result, please adjust `batch_size` accordingly to keep the global batch size unchanged for a different number of GPUs/NPUs, or adjust the learning rate linearly to a new global batch size.


### 3.2 Model Training
<!--- Guideline: Avoid using shell script in the command line. Python script preferred. -->
* Distributed Training

It is easy to reproduce the reported results with the pre-defined training recipe. For distributed training on multiple Ascend 910 devices, please modify the configuration parameter `distribute` as True and run:

```shell
# distributed training on multiple GPU/Ascend devices
mpirun --allow-run-as-root -n 8 python tools/train.py --config configs/kie/layoutlmv3/ser_layoutlmv3_xfund_zh.yaml
```


* Standalone Training

If you want to train or finetune the model on a smaller dataset without distributed training, please modify the configuration parameter`distribute` as False and run:

```shell
# standalone training on a CPU/GPU/Ascend device
python tools/train.py --config configs/kie/layoutlmv3/ser_layoutlmv3_xfund_zh.yaml
```

The training result (including checkpoints, per-epoch performance and curves) will be saved in the directory parsed by the arg `ckpt_save_dir`. The default directory is `./tmp_kie_ser`.

### 3.3 Model Evaluation

To evaluate the accuracy of the trained model, you can use `eval.py`. Please set the checkpoint path to the arg `ckpt_load_path` in the `eval` section of yaml config file, set `distribute` to be False, and then run:

```
python tools/eval.py --config configs/kie/layoutlmv3/ser_layoutlmv3_xfund_zh.yaml
```

### 3.4 Model Inference

To perform inference using a pre-trained model, you can utilize `tools/infer/text/predict_ser.py` for inference and visualize the results.

```
python tools/infer/text/predict_ser.py --rec_algorithm CRNN_CH --image_dir {dir of images or path of image}
```

As an example of entity recognition in Chinese forms, use the script to recognize entities in the form of `configs/kie/vi_layoutxlm/example.jpg`. The results will be stored in the `./inference_results` folder by default, and you can also customize the result storage path through the `--draw_img_save_dir` command-line parameter.

<p align="center">
<img src="../vi_layoutxlm/example.jpg" width=1000 />
</p>
<p align="center">
<em> example.jpg </em>
</p>
Recognition results are as shown in the image, and the image is saved as`inference_results/example_ser.jpg`

<p align="center">
<img src="../vi_layoutxlm/example_ser.jpg" width=1000 />
</p>
<p align="center">
<em> example_ser.jpg </em>
</p>



## References
<!--- Guideline: Citation format GB/T 7714 is suggested. -->

[1] Yupan Huang, Tengchao Lv, Lei Cui, Yutong Lu, Furu Wei. LayoutLMv3: Pre-training for Document AI with Unified Text and Image Masking. arXiv preprint arXiv:2204.08387, 2022.
Loading

0 comments on commit 7971ee0

Please sign in to comment.