train_layoutparser_model.md 7.3 KB
Newer Older
G
grasswolfs 已提交
1
# Training layout-parse
W
WenmuZhou 已提交
2

G
grasswolfs 已提交
3
[1. Installation](#Installation)
W
WenmuZhou 已提交
4

G
grasswolfs 已提交
5
[1.1 Requirements](#Requirements)
W
WenmuZhou 已提交
6

G
grasswolfs 已提交
7
[1.2 Install PaddleDetection](#Install PaddleDetection)
W
WenmuZhou 已提交
8

G
grasswolfs 已提交
9
[2.  Data preparation](#Data preparation)
W
WenmuZhou 已提交
10

G
grasswolfs 已提交
11
[3. Configuration](#Configuration)
W
WenmuZhou 已提交
12

G
grasswolfs 已提交
13
[4. Training](#Training)
W
WenmuZhou 已提交
14

G
grasswolfs 已提交
15
[5. Prediction](#Prediction)
W
WenmuZhou 已提交
16

G
grasswolfs 已提交
17
[6. Deployment](#Deployment)
W
WenmuZhou 已提交
18

G
grasswolfs 已提交
19
[6.1 Export model](#Export model)
W
WenmuZhou 已提交
20

G
grasswolfs 已提交
21
[6.2 Inference](#Inference)  
W
WenmuZhou 已提交
22

G
grasswolfs 已提交
23
<a name="Installation"></a>
W
WenmuZhou 已提交
24

G
grasswolfs 已提交
25
## 1.  Installation
W
WenmuZhou 已提交
26

G
grasswolfs 已提交
27
<a name="Requirements"></a>
W
WenmuZhou 已提交
28

G
grasswolfs 已提交
29
### 1.1 Requirements
W
WenmuZhou 已提交
30 31 32 33 34 35 36 37

- 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

G
grasswolfs 已提交
38
<a name="Install PaddleDetection"></a>
W
WenmuZhou 已提交
39

G
grasswolfs 已提交
40
### 1.2 Install PaddleDetection
W
WenmuZhou 已提交
41 42

```bash
G
grasswolfs 已提交
43
# Clone PaddleDetection repository
W
WenmuZhou 已提交
44 45 46 47
cd <path/to/clone/PaddleDetection>
git clone https://github.com/PaddlePaddle/PaddleDetection.git

cd PaddleDetection
G
grasswolfs 已提交
48
# Install other dependencies
W
WenmuZhou 已提交
49 50 51
pip install -r requirements.txt
```

G
grasswolfs 已提交
52
For more installation tutorials, please refer to: [Install doc](https://github.com/PaddlePaddle/PaddleDetection/blob/release/2.1/docs/tutorials/INSTALL_cn.md)
W
WenmuZhou 已提交
53

G
grasswolfs 已提交
54
<a name="Data preparation"></a>
W
WenmuZhou 已提交
55

G
grasswolfs 已提交
56
## 2. Data preparation
W
WenmuZhou 已提交
57

G
grasswolfs 已提交
58
Download the [PubLayNet](https://github.com/ibm-aur-nlp/PubLayNet) dataset
W
WenmuZhou 已提交
59 60 61 62

```bash
cd PaddleDetection/dataset/
mkdir publaynet
G
grasswolfs 已提交
63
# execute the command,download PubLayNet
W
WenmuZhou 已提交
64
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
G
grasswolfs 已提交
65
# unpack
W
WenmuZhou 已提交
66 67 68
tar -xvf publaynet.tar.gz
```

G
grasswolfs 已提交
69
PubLayNet directory structure after decompressing :
W
WenmuZhou 已提交
70 71 72 73 74 75

| 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  |
G
grasswolfs 已提交
76 77 78 79
| `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      |
W
WenmuZhou 已提交
80

G
grasswolfs 已提交
81
For other datasets,please refer to [the PrepareDataSet]((https://github.com/PaddlePaddle/PaddleDetection/blob/release/2.1/docs/tutorials/PrepareDataSet.md) )
W
WenmuZhou 已提交
82

G
grasswolfs 已提交
83
<a name="Configuration"></a>
W
WenmuZhou 已提交
84

G
grasswolfs 已提交
85
## 3. Configuration
W
WenmuZhou 已提交
86

G
grasswolfs 已提交
87
We use the  `configs/ppyolo/ppyolov2_r50vd_dcn_365e_coco.yml` configuration for training,the configuration file is as follows
W
WenmuZhou 已提交
88

W
WenmuZhou 已提交
89 90 91 92 93 94 95 96 97 98 99 100
```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
```
G
grasswolfs 已提交
101
The `ppyolov2_r50vd_dcn_365e_coco.yml` configuration depends on other configuration files, in this case:
W
WenmuZhou 已提交
102

G
grasswolfs 已提交
103
- coco_detection.yml:mainly explains the path of training data and verification data
W
WenmuZhou 已提交
104

G
grasswolfs 已提交
105
- runtime.yml:mainly describes the common parameters, such as whether to use the GPU and how many epoch to save model etc.
W
WenmuZhou 已提交
106

G
grasswolfs 已提交
107
- optimizer_365e.yml:mainly explains the learning rate and optimizer configuration
W
WenmuZhou 已提交
108

G
grasswolfs 已提交
109
- ppyolov2_r50vd_dcn.yml:mainly describes the model and the  network
W
WenmuZhou 已提交
110

G
grasswolfs 已提交
111
- 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.
W
WenmuZhou 已提交
112 113


G
grasswolfs 已提交
114
Modify the preceding files, such as the dataset path and batch size etc.
W
WenmuZhou 已提交
115

G
grasswolfs 已提交
116
<a name="Training"></a>
W
WenmuZhou 已提交
117

G
grasswolfs 已提交
118
## 4. Training
W
WenmuZhou 已提交
119

G
grasswolfs 已提交
120
PaddleDetection provides single-card/multi-card training mode to meet various training needs of users:
W
WenmuZhou 已提交
121

G
grasswolfs 已提交
122
* GPU single card training
W
WenmuZhou 已提交
123 124

```bash
G
grasswolfs 已提交
125
export CUDA_VISIBLE_DEVICES=0 #Don't need to run this command on Windows and Mac
W
WenmuZhou 已提交
126 127 128
python tools/train.py -c configs/ppyolo/ppyolov2_r50vd_dcn_365e_coco.yml
```

G
grasswolfs 已提交
129
* GPU multi-card training
W
WenmuZhou 已提交
130 131 132 133 134 135

```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
```

G
grasswolfs 已提交
136
--eval: training while verifying
W
WenmuZhou 已提交
137

G
grasswolfs 已提交
138
* Model recovery training
W
WenmuZhou 已提交
139

G
grasswolfs 已提交
140
During the daily training, if training is interrupted due to some reasons, you can use the -r command to resume the training:
W
WenmuZhou 已提交
141 142 143

```bash
export CUDA_VISIBLE_DEVICES=0,1,2,3
W
fix doc  
WenmuZhou 已提交
144
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
W
WenmuZhou 已提交
145 146
```

G
grasswolfs 已提交
147
Note: If you encounter "`Out of memory error`" , try reducing `batch_size` in the `ppyolov2_reader.yml`  file
W
WenmuZhou 已提交
148

G
grasswolfs 已提交
149
prediction<a name="Prediction"></a>
W
WenmuZhou 已提交
150

G
grasswolfs 已提交
151
## 5. Prediction
W
WenmuZhou 已提交
152

G
grasswolfs 已提交
153
Set parameters and use PaddleDetection to predict:
W
WenmuZhou 已提交
154 155 156

```bash
export CUDA_VISIBLE_DEVICES=0
W
fix doc  
WenmuZhou 已提交
157
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
W
WenmuZhou 已提交
158 159
```

G
grasswolfs 已提交
160
`--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。
W
WenmuZhou 已提交
161

G
grasswolfs 已提交
162
<a name="Deployment"></a>
W
WenmuZhou 已提交
163

G
grasswolfs 已提交
164
## 6. Deployment
W
WenmuZhou 已提交
165

G
grasswolfs 已提交
166
Use your trained model in Layout Parser
W
WenmuZhou 已提交
167

G
grasswolfs 已提交
168
<a name="Export model"></a>
W
WenmuZhou 已提交
169

G
grasswolfs 已提交
170
### 6.1 Export model
W
WenmuZhou 已提交
171

G
grasswolfs 已提交
172
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.
W
WenmuZhou 已提交
173

G
grasswolfs 已提交
174
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'.
W
WenmuZhou 已提交
175

G
grasswolfs 已提交
176
Execute the script to export model:
W
WenmuZhou 已提交
177 178

```bash
W
fix doc  
WenmuZhou 已提交
179
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
W
WenmuZhou 已提交
180 181
```

G
grasswolfs 已提交
182
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`
W
WenmuZhou 已提交
183

G
grasswolfs 已提交
184
More model export tutorials, please refer to:[EXPORT_MODEL](https://github.com/PaddlePaddle/PaddleDetection/blob/release/2.1/deploy/EXPORT_MODEL.md)
W
WenmuZhou 已提交
185

G
grasswolfs 已提交
186
<a name="Inference"></a>
W
WenmuZhou 已提交
187

G
grasswolfs 已提交
188
### 6.2 Inference
W
WenmuZhou 已提交
189

G
grasswolfs 已提交
190
`model_path` represent  the trained model path, and layoutparser is used to predict:
W
WenmuZhou 已提交
191 192 193

```bash
import layoutparser as lp
W
fix doc  
WenmuZhou 已提交
194
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)
W
WenmuZhou 已提交
195 196 197 198 199 200
```



***

G
grasswolfs 已提交
201
More PaddleDetection training tutorials,please reference:[PaddleDetection Training](https://github.com/PaddlePaddle/PaddleDetection/blob/release/2.1/docs/tutorials/GETTING_STARTED_cn.md)
W
WenmuZhou 已提交
202 203

***