Layout Detection Module Development Tutorial¶
I. Overview¶
The core task of structure analysis is to parse and segment the content of input document images. By identifying different elements in the image (such as text, charts, images, etc.), they are classified into predefined categories (e.g., pure text area, title area, table area, image area, list area, etc.), and the position and size of these regions in the document are determined.
II. Supported Model List¶
Model | Model Download Link | mAP(0.5) (%) | GPU Inference Time (ms) | CPU Inference Time (ms) | Model Size (M) | Description |
---|---|---|---|---|---|---|
PicoDet_layout_1x | Inference Model/Trained Model | 86.8 | 13.0 | 91.3 | 7.4 | An efficient layout area localization model trained on the PubLayNet dataset based on PicoDet-1x can locate five types of areas, including text, titles, tables, images, and lists. |
PicoDet-S_layout_3cls | Inference Model/Trained Model | 87.1 | 13.5 | 45.8 | 4.8 | An high-efficient layout area localization model trained on a self-constructed dataset based on PicoDet-S for scenarios such as Chinese and English papers, magazines, and research reports includes three categories: tables, images, and seals. |
PicoDet-S_layout_17cls | Inference Model/Trained Model | 70.3 | 13.6 | 46.2 | 4.8 | A high-efficient layout area localization model trained on a self-constructed dataset based on PicoDet-S_layout_17cls for scenarios such as Chinese and English papers, magazines, and research reports includes 17 common layout categories, namely: paragraph titles, images, text, numbers, abstracts, content, chart titles, formulas, tables, table titles, references, document titles, footnotes, headers, algorithms, footers, and seals. |
PicoDet-L_layout_3cls | Inference Model/Trained Model | 89.3 | 15.7 | 159.8 | 22.6 | An efficient layout area localization model trained on a self-constructed dataset based on PicoDet-L for scenarios such as Chinese and English papers, magazines, and research reports includes three categories: tables, images, and seals. |
PicoDet-L_layout_17cls | Inference Model/Trained Model | 79.9 | 17.2 | 160.2 | 22.6 | A efficient layout area localization model trained on a self-constructed dataset based on PicoDet-L_layout_17cls for scenarios such as Chinese and English papers, magazines, and research reports includes 17 common layout categories, namely: paragraph titles, images, text, numbers, abstracts, content, chart titles, formulas, tables, table titles, references, document titles, footnotes, headers, algorithms, footers, and seals. |
RT-DETR-H_layout_3cls | Inference Model/Trained Model | 95.9 | 114.6 | 3832.6 | 470.1 | A high-precision layout area localization model trained on a self-constructed dataset based on RT-DETR-H for scenarios such as Chinese and English papers, magazines, and research reports includes three categories: tables, images, and seals. |
RT-DETR-H_layout_17cls | Inference Model/Trained Model | 92.6 | 115.1 | 3827.2 | 470.2 | A high-precision layout area localization model trained on a self-constructed dataset based on RT-DETR-H for scenarios such as Chinese and English papers, magazines, and research reports includes 17 common layout categories, namely: paragraph titles, images, text, numbers, abstracts, content, chart titles, formulas, tables, table titles, references, document titles, footnotes, headers, algorithms, footers, and seals. |
Note: The evaluation set for the above accuracy metrics is PaddleOCR's self-built layout region analysis dataset, containing 10,000 images of common document types, including English and Chinese papers, magazines, research reports, etc. GPU inference time is based on an NVIDIA Tesla T4 machine with FP32 precision. CPU inference speed is based on an Intel(R) Xeon(R) Gold 5117 CPU @ 2.00GHz with 8 threads and FP32 precision.
III. Quick Integration ¶
❗ Before quick integration, please install the PaddleX wheel package. For detailed instructions, refer to PaddleX Local Installation Tutorial
After installing the wheel package, a few lines of code can complete the inference of the structure analysis module. You can switch models under this module freely, and you can also integrate the model inference of the structure analysis module into your project. Before running the following code, please download the demo image to your local machine.
from paddlex import create_model
model_name = "PicoDet-L_layout_3cls"
model = create_model(model_name)
output = model.predict("layout.jpg", batch_size=1)
for res in output:
res.print(json_format=False)
res.save_to_img("./output/")
res.save_to_json("./output/res.json")
For more information on using PaddleX's single-model inference API, refer to PaddleX Single Model Python Script Usage Instructions.
IV. Custom Development¶
If you seek higher accuracy from existing models, you can use PaddleX's custom development capabilities to develop better structure analysis models. Before developing a structure analysis model with PaddleX, ensure you have installed PaddleX's Detection-related model training capabilities. The installation process can be found in PaddleX Local Installation Tutorial.
4.1 Data Preparation¶
Before model training, you need to prepare the corresponding dataset for the task module. PaddleX provides a data validation function for each module, and only data that passes the validation can be used for model training. Additionally, PaddleX provides demo datasets for each module, which you can use to complete subsequent development based on the official demos. If you wish to use private datasets for subsequent model training, refer to the PaddleX Object Detection Task Module Data Annotation Tutorial.
4.1.1 Demo Data Download¶
You can use the following commands to download the demo dataset to a specified folder:
cd /path/to/paddlex
wget https://paddle-model-ecology.bj.bcebos.com/paddlex/data/det_layout_examples.tar -P ./dataset
tar -xf ./dataset/det_layout_examples.tar -C ./dataset/
4.1.2 Data Validation¶
A single command can complete data validation:
python main.py -c paddlex/configs/structure_analysis/PicoDet-L_layout_3cls.yaml \
-o Global.mode=check_dataset \
-o Global.dataset_dir=./dataset/det_layout_examples
After executing the above command, PaddleX will validate the dataset and collect its basic information. Upon successful execution, the log will print the message Check dataset passed !
. The validation result file will be saved in ./output/check_dataset_result.json
, and related outputs will be saved in the ./output/check_dataset
directory of the current directory. The output directory includes visualized example images and histograms of sample distributions.
👉 Validation Result Details (Click to Expand)
The specific content of the validation result file is:
{
"done_flag": true,
"check_pass": true,
"attributes": {
"num_classes": 11,
"train_samples": 90,
"train_sample_paths": [
"check_dataset/demo_img/JPEGImages/train_0077.jpg",
"check_dataset/demo_img/JPEGImages/train_0028.jpg",
"check_dataset/demo_img/JPEGImages/train_0012.jpg"
],
"val_samples": 20,
"val_sample_paths": [
"check_dataset/demo_img/JPEGImages/val_0007.jpg",
"check_dataset/demo_img/JPEGImages/val_0019.jpg",
"check_dataset/demo_img/JPEGImages/val_0010.jpg"
]
},
"analysis": {
"histogram": "check_dataset/histogram.png"
},
"dataset_path": "./dataset/example_data/det_layout_examples",
"show_type": "image",
"dataset_type": "COCODetDataset"
}
The verification results mentioned above indicate that check_pass
being True
means the dataset format meets the requirements. Details of other indicators are as follows:
attributes.num_classes
: The number of classes in this dataset is 11;attributes.train_samples
: The number of training samples in this dataset is 90;attributes.val_samples
: The number of validation samples in this dataset is 20;attributes.train_sample_paths
: The list of relative paths to the visualization images of training samples in this dataset;attributes.val_sample_paths
: The list of relative paths to the visualization images of validation samples in this dataset;
The dataset verification also analyzes the distribution of sample numbers across all classes and generates a histogram (histogram.png):
4.1.3 Dataset Format Conversion/Dataset Splitting (Optional)¶
After completing dataset verification, you can convert the dataset format or re-split the training/validation ratio by modifying the configuration file or appending hyperparameters.
👉 Details on Format Conversion/Dataset Splitting (Click to Expand)
(1) Dataset Format Conversion
Layout detection does not support data format conversion.
(2) Dataset Splitting
Parameters for dataset splitting can be set by modifying the CheckDataset
section in the configuration file. Examples of some parameters in the configuration file are as follows:
CheckDataset
:split
:enable
: Whether to re-split the dataset. Set toTrue
to enable dataset splitting, default isFalse
;train_percent
: If re-splitting the dataset, set the percentage of the training set. The type is any integer between 0-100, ensuring the sum withval_percent
is 100;
For example, if you want to re-split the dataset with a 90% training set and a 10% validation set, modify the configuration file as follows:
......
CheckDataset:
......
split:
enable: True
train_percent: 90
val_percent: 10
......
Then execute the command:
python main.py -c paddlex/configs/structure_analysis/PicoDet-L_layout_3cls.yaml \
-o Global.mode=check_dataset \
-o Global.dataset_dir=./dataset/det_layout_examples
After dataset splitting, the original annotation files will be renamed to xxx.bak
in the original path.
The above parameters can also be set by appending command-line arguments:
python main.py -c paddlex/configs/structure_analysis/PicoDet-L_layout_3cls.yaml \
-o Global.mode=check_dataset \
-o Global.dataset_dir=./dataset/det_layout_examples \
-o CheckDataset.split.enable=True \
-o CheckDataset.split.train_percent=90 \
-o CheckDataset.split.val_percent=10
4.2 Model Training¶
A single command is sufficient to complete model training, taking the training of PicoDet-L_layout_3cls as an example:
python main.py -c paddlex/configs/structure_analysis/PicoDet-L_layout_3cls.yaml \
-o Global.mode=train \
-o Global.dataset_dir=./dataset/det_layout_examples
- Specify the path to the
.yaml
configuration file of the model (here it isPicoDet-L_layout_3cls.yaml
,When training other models, you need to specify the corresponding configuration files. The relationship between the model and configuration files can be found in the PaddleX Model List (CPU/GPU)) - Specify the mode as model training:
-o Global.mode=train
- Specify the path to the training dataset:
-o Global.dataset_dir
Other related parameters can be set by modifying the Global
and Train
fields in the .yaml
configuration file, or adjusted by appending parameters in the command line. For example, to specify training on the first two GPUs: -o Global.device=gpu:0,1
; to set the number of training epochs to 10: -o Train.epochs_iters=10
. For more modifiable parameters and their detailed explanations, refer to the PaddleX Common Configuration Parameters for Model Tasks.
👉 More Details (Click to Expand)
- During model training, PaddleX automatically saves model weight files, defaulting to
output
. To specify a save path, use the-o Global.output
field in the configuration file. - PaddleX shields you from the concepts of dynamic graph weights and static graph weights. During model training, both dynamic and static graph weights are produced, and static graph weights are selected by default for model inference.
-
After completing the model training, all outputs are saved in the specified output directory (default is
./output/
), typically including: -
train_result.json
: Training result record file, recording whether the training task was completed normally, as well as the output weight metrics, related file paths, etc.; train.log
: Training log file, recording changes in model metrics and loss during training;config.yaml
: Training configuration file, recording the hyperparameter configuration for this training session;.pdparams
,.pdema
,.pdopt.pdstate
,.pdiparams
,.pdmodel
: Model weight-related files, including network parameters, optimizer, EMA, static graph network parameters, static graph network structure, etc.;
4.3 Model Evaluation¶
After completing model training, you can evaluate the specified model weight file on the validation set to verify the model's accuracy. Using PaddleX for model evaluation, you can complete the evaluation with a single command:
python main.py -c paddlex/configs/structure_analysis/PicoDet-L_layout_3cls.yaml \
-o Global.mode=evaluate \
-o Global.dataset_dir=./dataset/det_layout_examples
- Specify the path to the
.yaml
configuration file for the model(here it'sPicoDet-L_layout_3cls.yaml
) - Set the mode to model evaluation:
-o Global.mode=evaluate
- Specify the path to the validation dataset:
-o Global.dataset_dir
Other related parameters can be configured by modifying the fields underGlobal
andEvaluate
in the.yaml
configuration file. For detailed information, please refer to PaddleX Common Configuration Parameters for Models。
👉 More Details (Click to Expand)
When evaluating the model, you need to specify the model weights file path. Each configuration file has a default weight save path built-in. If you need to change it, simply set it by appending a command line parameter, such as -o Evaluate.weight_path=./output/best_model/best_model/model.pdparams
.
After completing the model evaluation, an evaluate_result.json
file will be generated, which records the evaluation results, specifically whether the evaluation task was completed successfully, and the model's evaluation metrics, including AP.
4.4 Model Inference¶
After completing model training and evaluation, you can use the trained model weights for inference predictions. In PaddleX, model inference predictions can be achieved through two methods: command line and wheel package.
4.4.1 Model Inference¶
-
To perform inference predictions through the command line, simply use the following command. Before running the following code, please download the demo image to your local machine.
Similar to model training and evaluation, the following steps are required: -
Specify the
.yaml
configuration file path of the model (here it isPicoDet-L_layout_3cls.yaml
) - Set the mode to model inference prediction:
-o Global.mode=predict
- Specify the model weights path:
-o Predict.model_dir="./output/best_model/inference"
-
Specify the input data path:
-o Predict.input="..."
Other related parameters can be set by modifying the fields underGlobal
andPredict
in the.yaml
configuration file. For details, please refer to PaddleX Common Model Configuration File Parameter Description. -
Alternatively, you can use the PaddleX wheel package for inference, easily integrating the model into your own project. To integrate, simply add the
model_dir="/output/best_model/inference"
parameter to thecreate_model(model_name=model_name, kernel_option=kernel_option)
function in the quick integration method from Step 3.
4.4.2 Model Integration¶
The model can be directly integrated into PaddleX pipelines or into your own projects.
-
Pipeline Integration The structure analysis module can be integrated into PaddleX pipelines such as the General Table Recognition Pipeline and the Document Scene Information Extraction Pipeline v3 (PP-ChatOCRv3). Simply replace the model path to update the layout area localization module. In pipeline integration, you can use high-performance inference and service-oriented deployment to deploy your model.
-
Module Integration The weights you produce can be directly integrated into the layout area localization module. You can refer to the Python example code in the Quick Integration section, simply replacing the model with the path to your trained model.