PaddleOCR/ppstructure/table/README_ch.md

136 lines
6.6 KiB
Markdown
Raw Normal View History

2022-04-19 15:08:34 +08:00
[English](README.md) | 简体中文
2021-06-10 14:24:59 +08:00
2022-02-12 16:26:58 +08:00
# 表格识别
2022-04-19 15:08:34 +08:00
- [1. 表格识别 pipeline](#1)
- [2. 性能](#2)
- [3. 使用](#3)
- [3.1 快速开始](#31)
- [3.2 训练](#32)
- [3.3 评估](#33)
- [3.4 预测](#34)
<a name="1"></a>
2021-08-03 10:44:18 +08:00
## 1. 表格识别 pipeline
2021-08-03 10:44:18 +08:00
表格识别主要包含三个模型
2021-06-10 14:24:59 +08:00
1. 单行文本检测-DB
2. 单行文本识别-CRNN
3. 表格结构和cell坐标预测-RARE
具体流程图如下
2022-04-19 15:08:34 +08:00
![tableocr_pipeline](../docs/table/tableocr_pipeline.jpg)
2021-06-10 14:24:59 +08:00
2021-07-29 17:59:44 +08:00
流程说明:
1. 图片由单行文字检测模型检测到单行文字的坐标,然后送入识别模型拿到识别结果。
2021-06-11 14:17:59 +08:00
2. 图片由表格结构和cell坐标预测模型拿到表格的结构信息和单元格的坐标信息。
2021-06-10 14:24:59 +08:00
3. 由单行文字的坐标、识别结果和单元格的坐标一起组合出单元格的识别结果。
4. 单元格的识别结果和表格结构一起构造表格的html字符串。
2022-04-19 15:08:34 +08:00
<a name="2"></a>
2021-08-03 15:30:12 +08:00
## 2. 性能
2022-04-19 15:08:34 +08:00
2021-08-03 15:30:12 +08:00
我们在 PubTabNet<sup>[1]</sup> 评估数据集上对算法进行了评估,性能如下
2021-06-10 14:24:59 +08:00
2021-08-03 15:30:12 +08:00
|算法|[TEDS(Tree-Edit-Distance-based Similarity)](https://github.com/ibm-aur-nlp/PubTabNet/tree/master/src)|
| --- | --- |
| EDD<sup>[2]</sup> | 88.3 |
2022-08-10 19:01:03 +08:00
| TableRec-RARE(ours) | 93.32 |
| SLANet(ours) | 94.98 |
2021-08-03 15:30:12 +08:00
2022-04-19 15:08:34 +08:00
<a name="3"></a>
2021-08-03 15:30:12 +08:00
## 3. 使用
2022-02-12 16:26:58 +08:00
2022-04-19 15:08:34 +08:00
<a name="31"></a>
2021-08-03 15:30:12 +08:00
### 3.1 快速开始
2021-08-02 19:42:10 +08:00
```python
cd PaddleOCR/ppstructure
# 下载模型
mkdir inference && cd inference
# 下载超轻量级表格英文OCR模型的检测模型并解压
wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/table/en_ppocr_mobile_v2.0_table_det_infer.tar && tar xf en_ppocr_mobile_v2.0_table_det_infer.tar
# 下载超轻量级表格英文OCR模型的识别模型并解压
wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/table/en_ppocr_mobile_v2.0_table_rec_infer.tar && tar xf en_ppocr_mobile_v2.0_table_rec_infer.tar
2021-08-02 19:42:10 +08:00
# 下载超轻量级英文表格英寸模型并解压
wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/table/en_ppocr_mobile_v2.0_table_structure_infer.tar && tar xf en_ppocr_mobile_v2.0_table_structure_infer.tar
cd ..
# 执行预测
python3 table/predict_table.py --det_model_dir=inference/en_ppocr_mobile_v2.0_table_det_infer --rec_model_dir=inference/en_ppocr_mobile_v2.0_table_rec_infer --table_model_dir=inference/en_ppocr_mobile_v2.0_table_structure_infer --image_dir=./docs/table/table.jpg --rec_char_dict_path=../ppocr/utils/dict/table_dict.txt --table_char_dict_path=../ppocr/utils/dict/table_structure_dict.txt --det_limit_side_len=736 --det_limit_type=min --output ./output/table
2021-08-02 19:42:10 +08:00
```
2022-08-10 19:01:03 +08:00
运行完成后每张图片的excel表格会保存到output字段指定的目录下同时在该目录下回生产一个html文件用于可视化查看单元格坐标和识别的表格。
2021-08-02 19:42:10 +08:00
note: 上述模型是在 PubLayNet 数据集上训练的表格识别模型,仅支持英文扫描场景,如需识别其他场景需要自己训练模型后替换 `det_model_dir`,`rec_model_dir`,`table_model_dir`三个字段即可。
2022-02-12 16:26:58 +08:00
2022-04-19 15:08:34 +08:00
<a name="32"></a>
2021-08-03 15:30:12 +08:00
### 3.2 训练
2022-02-12 16:26:58 +08:00
2021-07-28 16:11:50 +08:00
在这一章节中,我们仅介绍表格结构模型的训练,[文字检测](../../doc/doc_ch/detection.md)和[文字识别](../../doc/doc_ch/recognition.md)的模型训练请参考对应的文档。
2022-02-12 16:26:58 +08:00
* 数据准备
2021-07-29 17:59:44 +08:00
训练数据使用公开数据集PubTabNet ([论文](https://arxiv.org/abs/1911.10683)[下载地址](https://github.com/ibm-aur-nlp/PubTabNet))。PubTabNet数据集包含约50万张表格数据的图像以及图像对应的html格式的注释。
2021-07-06 19:53:49 +08:00
2022-02-12 16:26:58 +08:00
* 启动训练
2021-07-06 19:53:49 +08:00
*如果您安装的是cpu版本请将配置文件中的 `use_gpu` 字段修改为false*
```shell
# 单机单卡训练
python3 tools/train.py -c configs/table/table_mv3.yml
# 单机多卡训练,通过 --gpus 参数设置使用的GPU ID
python3 -m paddle.distributed.launch --gpus '0,1,2,3' tools/train.py -c configs/table/table_mv3.yml
```
2021-07-28 16:11:50 +08:00
上述指令中,通过-c 选择训练使用configs/table/table_mv3.yml配置文件。有关配置文件的详细解释请参考[链接](../../doc/doc_ch/config.md)。
2021-07-06 19:53:49 +08:00
2022-02-12 16:26:58 +08:00
* 断点训练
2021-07-06 19:53:49 +08:00
如果训练程序中断如果希望加载训练中断的模型从而恢复训练可以通过指定Global.checkpoints指定要加载的模型路径
```shell
python3 tools/train.py -c configs/table/table_mv3.yml -o Global.checkpoints=./your/trained/model
```
**注意**`Global.checkpoints`的优先级高于`Global.pretrain_weights`的优先级,即同时指定两个参数时,优先加载`Global.checkpoints`指定的模型,如果`Global.checkpoints`指定的模型路径有误,会加载`Global.pretrain_weights`指定的模型。
2022-04-19 15:08:34 +08:00
<a name="33"></a>
2021-08-03 15:30:12 +08:00
### 3.3 评估
2021-06-10 14:24:59 +08:00
2021-08-03 15:30:12 +08:00
表格使用 [TEDS(Tree-Edit-Distance-based Similarity)](https://github.com/ibm-aur-nlp/PubTabNet/tree/master/src) 作为模型的评估指标。在进行模型评估之前需要将pipeline中的三个模型分别导出为inference模型(我们已经提供好)还需要准备评估的gt gt示例如下:
2022-08-10 19:01:03 +08:00
```txt
PMC5755158_010_01.png <html><body><table><thead><tr><td></td><td><b>Weaning</b></td><td><b>Week 15</b></td><td><b>Off-test</b></td></tr></thead><tbody><tr><td>Weaning</td><td></td><td></td><td></td></tr><tr><td>Week 15</td><td></td><td>0.17 ± 0.08</td><td>0.16 ± 0.03</td></tr><tr><td>Off-test</td><td></td><td>0.80 ± 0.24</td><td>0.19 ± 0.09</td></tr></tbody></table></body></html>
```
gt每一行都由文件名和表格的html字符串组成文件名和表格的html字符串之间使用`\t`分隔。
也可使用如下命令由标注文件生成评估的gt文件
```python
python3 ppstructure/table/convert_label2html.py --ori_gt_path /path/to/your_label_file --save_path /path/to/save_file
2021-06-10 14:24:59 +08:00
```
准备完成后使用如下命令进行评估评估完成后会输出teds指标。
```python
2021-07-29 17:59:44 +08:00
cd PaddleOCR/ppstructure
2022-08-10 19:01:03 +08:00
python3 table/eval_table.py --det_model_dir=path/to/det_model_dir --rec_model_dir=path/to/rec_model_dir --table_model_dir=path/to/table_model_dir --image_dir=../doc/table/1.png --rec_char_dict_path=../ppocr/utils/dict/table_dict.txt --table_char_dict_path=../ppocr/utils/dict/table_structure_dict.txt --det_limit_side_len=736 --det_limit_type=min --gt_path=path/to/gt.txt
2021-06-10 14:24:59 +08:00
```
2021-08-03 15:04:24 +08:00
如使用PubLatNet评估数据集将会输出
```bash
2022-08-10 19:01:03 +08:00
teds: 94.98
2021-08-03 15:04:24 +08:00
```
2022-04-19 15:08:34 +08:00
<a name="34"></a>
2021-08-03 15:30:12 +08:00
### 3.4 预测
2021-06-10 14:24:59 +08:00
```python
2021-07-29 17:59:44 +08:00
cd PaddleOCR/ppstructure
2022-04-08 16:00:13 +08:00
python3 table/predict_table.py --det_model_dir=path/to/det_model_dir --rec_model_dir=path/to/rec_model_dir --table_model_dir=path/to/table_model_dir --image_dir=../doc/table/1.png --rec_char_dict_path=../ppocr/utils/dict/table_dict.txt --table_char_dict_path=../ppocr/utils/dict/table_structure_dict.txt --det_limit_side_len=736 --det_limit_type=min --output ../output/table
2021-08-03 10:44:18 +08:00
```
2022-04-19 15:08:34 +08:00
# Reference
2021-08-03 15:30:12 +08:00
1. https://github.com/ibm-aur-nlp/PubTabNet
2. https://arxiv.org/pdf/1911.10683