train_layoutparser_model.md 7.16 KB
Newer Older
WenmuZhou's avatar
WenmuZhou committed
1
2
3
4
5
6
7
8
9
10
11
12
English | [简体中文](train_layoutparser_model_ch.md)
- [Training layout-parse](#training-layout-parse)
  - [1.  Installation](#1--installation)
    - [1.1 Requirements](#11-requirements)
    - [1.2 Install PaddleDetection](#12-install-paddledetection)
  - [2. Data preparation](#2-data-preparation)
  - [3. Configuration](#3-configuration)
  - [4. Training](#4-training)
  - [5. Prediction](#5-prediction)
  - [6. Deployment](#6-deployment)
    - [6.1 Export model](#61-export-model)
    - [6.2 Inference](#62-inference)
WenmuZhou's avatar
WenmuZhou committed
13

WenmuZhou's avatar
WenmuZhou committed
14
# Training layout-parse
WenmuZhou's avatar
WenmuZhou committed
15

grasswolfs's avatar
grasswolfs committed
16
## 1.  Installation
WenmuZhou's avatar
WenmuZhou committed
17

grasswolfs's avatar
grasswolfs committed
18
### 1.1 Requirements
WenmuZhou's avatar
WenmuZhou committed
19
20
21
22
23
24
25
26

- PaddlePaddle 2.1
- OS 64 bit
- Python 3(3.5.1+/3.6/3.7/3.8/3.9),64 bit
- pip/pip3(9.0.1+), 64 bit
- CUDA >= 10.1
- cuDNN >= 7.6

grasswolfs's avatar
grasswolfs committed
27
### 1.2 Install PaddleDetection
WenmuZhou's avatar
WenmuZhou committed
28
29

```bash
grasswolfs's avatar
grasswolfs committed
30
# Clone PaddleDetection repository
WenmuZhou's avatar
WenmuZhou committed
31
32
33
34
cd <path/to/clone/PaddleDetection>
git clone https://github.com/PaddlePaddle/PaddleDetection.git

cd PaddleDetection
grasswolfs's avatar
grasswolfs committed
35
# Install other dependencies
WenmuZhou's avatar
WenmuZhou committed
36
37
38
pip install -r requirements.txt
```

grasswolfs's avatar
grasswolfs committed
39
For more installation tutorials, please refer to: [Install doc](https://github.com/PaddlePaddle/PaddleDetection/blob/release/2.1/docs/tutorials/INSTALL_cn.md)
WenmuZhou's avatar
WenmuZhou committed
40

grasswolfs's avatar
grasswolfs committed
41
## 2. Data preparation
WenmuZhou's avatar
WenmuZhou committed
42

grasswolfs's avatar
grasswolfs committed
43
Download the [PubLayNet](https://github.com/ibm-aur-nlp/PubLayNet) dataset
WenmuZhou's avatar
WenmuZhou committed
44
45
46
47

```bash
cd PaddleDetection/dataset/
mkdir publaynet
grasswolfs's avatar
grasswolfs committed
48
# execute the command,download PubLayNet
WenmuZhou's avatar
WenmuZhou committed
49
wget -O publaynet.tar.gz https://dax-cdn.cdn.appdomain.cloud/dax-publaynet/1.0.0/publaynet.tar.gz?_ga=2.104193024.1076900768.1622560733-649911202.1622560733
grasswolfs's avatar
grasswolfs committed
50
# unpack
WenmuZhou's avatar
WenmuZhou committed
51
52
53
tar -xvf publaynet.tar.gz
```

grasswolfs's avatar
grasswolfs committed
54
PubLayNet directory structure after decompressing :
WenmuZhou's avatar
WenmuZhou committed
55
56
57
58
59
60

| File or Folder | Description                                      | num     |
| :------------- | :----------------------------------------------- | ------- |
| `train/`       | Images in the training subset                    | 335,703 |
| `val/`         | Images in the validation subset                  | 11,245  |
| `test/`        | Images in the testing subset                     | 11,405  |
grasswolfs's avatar
grasswolfs committed
61
62
63
64
| `train.json`   | Annotations for training images                  |  1       |
| `val.json`     | Annotations for validation images                |  1       |
| `LICENSE.txt`  | Plaintext version of the CDLA-Permissive license |   1      |
| `README.txt`   | Text file with the file names and description    |   1      |
WenmuZhou's avatar
WenmuZhou committed
65

grasswolfs's avatar
grasswolfs committed
66
For other datasets,please refer to [the PrepareDataSet]((https://github.com/PaddlePaddle/PaddleDetection/blob/release/2.1/docs/tutorials/PrepareDataSet.md) )
WenmuZhou's avatar
WenmuZhou committed
67

grasswolfs's avatar
grasswolfs committed
68
## 3. Configuration
WenmuZhou's avatar
WenmuZhou committed
69

grasswolfs's avatar
grasswolfs committed
70
We use the  `configs/ppyolo/ppyolov2_r50vd_dcn_365e_coco.yml` configuration for training,the configuration file is as follows
WenmuZhou's avatar
WenmuZhou committed
71

WenmuZhou's avatar
WenmuZhou committed
72
73
74
75
76
77
78
79
80
81
82
83
```bash
_BASE_: [
  '../datasets/coco_detection.yml',
  '../runtime.yml',
  './_base_/ppyolov2_r50vd_dcn.yml',
  './_base_/optimizer_365e.yml',
  './_base_/ppyolov2_reader.yml',
]

snapshot_epoch: 8
weights: output/ppyolov2_r50vd_dcn_365e_coco/model_final
```
grasswolfs's avatar
grasswolfs committed
84
The `ppyolov2_r50vd_dcn_365e_coco.yml` configuration depends on other configuration files, in this case:
WenmuZhou's avatar
WenmuZhou committed
85

grasswolfs's avatar
grasswolfs committed
86
- coco_detection.yml:mainly explains the path of training data and verification data
WenmuZhou's avatar
WenmuZhou committed
87

grasswolfs's avatar
grasswolfs committed
88
- runtime.yml:mainly describes the common parameters, such as whether to use the GPU and how many epoch to save model etc.
WenmuZhou's avatar
WenmuZhou committed
89

grasswolfs's avatar
grasswolfs committed
90
- optimizer_365e.yml:mainly explains the learning rate and optimizer configuration
WenmuZhou's avatar
WenmuZhou committed
91

grasswolfs's avatar
grasswolfs committed
92
- ppyolov2_r50vd_dcn.yml:mainly describes the model and the  network
WenmuZhou's avatar
WenmuZhou committed
93

grasswolfs's avatar
grasswolfs committed
94
- ppyolov2_reader.yml:mainly describes the configuration of data readers, such as batch size and number of concurrent loading child processes, and also includes post preprocessing, such as resize and data augmention etc.
WenmuZhou's avatar
WenmuZhou committed
95
96


grasswolfs's avatar
grasswolfs committed
97
Modify the preceding files, such as the dataset path and batch size etc.
WenmuZhou's avatar
WenmuZhou committed
98

grasswolfs's avatar
grasswolfs committed
99
## 4. Training
WenmuZhou's avatar
WenmuZhou committed
100

grasswolfs's avatar
grasswolfs committed
101
PaddleDetection provides single-card/multi-card training mode to meet various training needs of users:
WenmuZhou's avatar
WenmuZhou committed
102

grasswolfs's avatar
grasswolfs committed
103
* GPU single card training
WenmuZhou's avatar
WenmuZhou committed
104
105

```bash
grasswolfs's avatar
grasswolfs committed
106
export CUDA_VISIBLE_DEVICES=0 #Don't need to run this command on Windows and Mac
WenmuZhou's avatar
WenmuZhou committed
107
108
109
python tools/train.py -c configs/ppyolo/ppyolov2_r50vd_dcn_365e_coco.yml
```

grasswolfs's avatar
grasswolfs committed
110
* GPU multi-card training
WenmuZhou's avatar
WenmuZhou committed
111
112
113
114
115
116

```bash
export CUDA_VISIBLE_DEVICES=0,1,2,3
python -m paddle.distributed.launch --gpus 0,1,2,3 tools/train.py -c configs/ppyolo/ppyolov2_r50vd_dcn_365e_coco.yml --eval
```

grasswolfs's avatar
grasswolfs committed
117
--eval: training while verifying
WenmuZhou's avatar
WenmuZhou committed
118

grasswolfs's avatar
grasswolfs committed
119
* Model recovery training
WenmuZhou's avatar
WenmuZhou committed
120

grasswolfs's avatar
grasswolfs committed
121
During the daily training, if training is interrupted due to some reasons, you can use the -r command to resume the training:
WenmuZhou's avatar
WenmuZhou committed
122
123
124

```bash
export CUDA_VISIBLE_DEVICES=0,1,2,3
WenmuZhou's avatar
fix doc  
WenmuZhou committed
125
python -m paddle.distributed.launch --gpus 0,1,2,3 tools/train.py -c configs/ppyolo/ppyolov2_r50vd_dcn_365e_coco.yml --eval -r output/ppyolov2_r50vd_dcn_365e_coco/10000
WenmuZhou's avatar
WenmuZhou committed
126
127
```

grasswolfs's avatar
grasswolfs committed
128
Note: If you encounter "`Out of memory error`" , try reducing `batch_size` in the `ppyolov2_reader.yml`  file
WenmuZhou's avatar
WenmuZhou committed
129

grasswolfs's avatar
grasswolfs committed
130
## 5. Prediction
WenmuZhou's avatar
WenmuZhou committed
131

grasswolfs's avatar
grasswolfs committed
132
Set parameters and use PaddleDetection to predict:
WenmuZhou's avatar
WenmuZhou committed
133
134
135

```bash
export CUDA_VISIBLE_DEVICES=0
WenmuZhou's avatar
fix doc  
WenmuZhou committed
136
python tools/infer.py -c configs/ppyolo/ppyolov2_r50vd_dcn_365e_coco.yml --infer_img=images/paper-image.jpg --output_dir=infer_output/ --draw_threshold=0.5 -o weights=output/ppyolov2_r50vd_dcn_365e_coco/model_final --use_vdl=Ture
WenmuZhou's avatar
WenmuZhou committed
137
138
```

grasswolfs's avatar
grasswolfs committed
139
`--draw_threshold` is an optional parameter. According to the calculation of [NMS](https://ieeexplore.ieee.org/document/1699659), different threshold will produce different results, ` keep_top_k ` represent  the maximum amount of output target, the default value is 10. You can set different value according to your own actual situation。
WenmuZhou's avatar
WenmuZhou committed
140

grasswolfs's avatar
grasswolfs committed
141
## 6. Deployment
WenmuZhou's avatar
WenmuZhou committed
142

grasswolfs's avatar
grasswolfs committed
143
Use your trained model in Layout Parser
WenmuZhou's avatar
WenmuZhou committed
144

grasswolfs's avatar
grasswolfs committed
145
### 6.1 Export model
WenmuZhou's avatar
WenmuZhou committed
146

grasswolfs's avatar
grasswolfs committed
147
n the process of model training, the model file saved contains the process of forward prediction and back propagation. In the actual industrial deployment, there is no need for back propagation. Therefore, the model should be translated into the model format required by the deployment. The `tools/export_model.py` script is provided in PaddleDetection to export the model.
WenmuZhou's avatar
WenmuZhou committed
148

grasswolfs's avatar
grasswolfs committed
149
The exported model name defaults to `model.*`, Layout Parser's code model is `inference.*`, So change [PaddleDetection/ppdet/engine/trainer. Py ](https://github.com/PaddlePaddle/PaddleDetection/blob/b87a1ea86fa18ce69e44a17ad1b49c1326f19ff9/ppdet/engine/trainer.py# L512) (click on the link to see the detailed line of code), change 'model' to 'inference'.
WenmuZhou's avatar
WenmuZhou committed
150

grasswolfs's avatar
grasswolfs committed
151
Execute the script to export model:
WenmuZhou's avatar
WenmuZhou committed
152
153

```bash
WenmuZhou's avatar
fix doc  
WenmuZhou committed
154
python tools/export_model.py -c configs/ppyolo/ppyolov2_r50vd_dcn_365e_coco.yml --output_dir=./inference -o weights=output/ppyolov2_r50vd_dcn_365e_coco/model_final.pdparams
WenmuZhou's avatar
WenmuZhou committed
155
156
```

grasswolfs's avatar
grasswolfs committed
157
The prediction model is exported to `inference/ppyolov2_r50vd_dcn_365e_coco` ,including:`infer_cfg.yml`(prediction not required), `inference.pdiparams`, `inference.pdiparams.info`,`inference.pdmodel`
WenmuZhou's avatar
WenmuZhou committed
158

grasswolfs's avatar
grasswolfs committed
159
More model export tutorials, please refer to:[EXPORT_MODEL](https://github.com/PaddlePaddle/PaddleDetection/blob/release/2.1/deploy/EXPORT_MODEL.md)
WenmuZhou's avatar
WenmuZhou committed
160

grasswolfs's avatar
grasswolfs committed
161
### 6.2 Inference
WenmuZhou's avatar
WenmuZhou committed
162

grasswolfs's avatar
grasswolfs committed
163
`model_path` represent  the trained model path, and layoutparser is used to predict:
WenmuZhou's avatar
WenmuZhou committed
164
165
166

```bash
import layoutparser as lp
WenmuZhou's avatar
fix doc  
WenmuZhou committed
167
model = lp.PaddleDetectionLayoutModel(model_path="inference/ppyolov2_r50vd_dcn_365e_coco", threshold=0.5,label_map={0: "Text", 1: "Title", 2: "List", 3:"Table", 4:"Figure"},enforce_cpu=True,enable_mkldnn=True)
WenmuZhou's avatar
WenmuZhou committed
168
169
170
171
```

***

grasswolfs's avatar
grasswolfs committed
172
More PaddleDetection training tutorials,please reference:[PaddleDetection Training](https://github.com/PaddlePaddle/PaddleDetection/blob/release/2.1/docs/tutorials/GETTING_STARTED_cn.md)
WenmuZhou's avatar
WenmuZhou committed
173
174

***