detection.md 11.1 KB
Newer Older
1
# 文字检测
2

3
本节以icdar2015数据集为例,介绍PaddleOCR中检测模型训练、评估、测试的使用方式。
4

WenmuZhou's avatar
WenmuZhou committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- [文字检测](#文字检测)
- [1. 准备数据和模型](#1-准备数据和模型)
  - [1.1 准备数据集](#11-准备数据集)
  - [1.2 下载预训练模型](#12-下载预训练模型)
- [2. 开始训练](#2-开始训练)
  - [2.1 启动训练](#21-启动训练)
  - [2.2 断点训练](#22-断点训练)
  - [2.3 更换Backbone 训练](#23-更换backbone-训练)
  - [2.4 混合精度训练](#24-混合精度训练)
  - [2.5 分布式训练](#25-分布式训练)
  - [2.6 知识蒸馏训练](#26-知识蒸馏训练)
  - [2.7 其他训练环境](#27-其他训练环境)
- [3. 模型评估与预测](#3-模型评估与预测)
  - [3.1 指标评估](#31-指标评估)
  - [3.2 测试检测效果](#32-测试检测效果)
- [4. 模型导出与预测](#4-模型导出与预测)
21
22
23
24
25
- [5. FAQ](#5-faq)

<a name="1--------"></a>
# 1. 准备数据和模型

WenmuZhou's avatar
WenmuZhou committed
26
## 1.1 准备数据集
27

WenmuZhou's avatar
WenmuZhou committed
28
准备数据集可参考 [ocr_datasets](./dataset/ocr_datasets.md)
LDOUBLEV's avatar
LDOUBLEV committed
29

30

LDOUBLEV's avatar
LDOUBLEV committed
31
<a name="12--------"></a>
LDOUBLEV's avatar
LDOUBLEV committed
32
## 1.2 下载预训练模型
33

WenmuZhou's avatar
WenmuZhou committed
34
首先下载模型backbone的pretrain model,PaddleOCR的检测模型目前支持两种backbone,分别是MobileNetV3、ResNet_vd系列,
35
36
37
您可以根据需求使用[PaddleClas](https://github.com/PaddlePaddle/PaddleClas/tree/release/2.0/ppcls/modeling/architectures)中的模型更换backbone,
对应的backbone预训练模型可以从[PaddleClas repo 主页中找到下载链接](https://github.com/PaddlePaddle/PaddleClas/blob/release%2F2.0/README_cn.md#resnet%E5%8F%8A%E5%85%B6vd%E7%B3%BB%E5%88%97)

licx's avatar
licx committed
38
```shell
LDOUBLEV's avatar
LDOUBLEV committed
39
cd PaddleOCR/
40
# 根据backbone的不同选择下载对应的预训练模型
41
# 下载MobileNetV3的预训练模型
tink2123's avatar
tink2123 committed
42
wget -P ./pretrain_models/ https://paddleocr.bj.bcebos.com/pretrained/MobileNetV3_large_x0_5_pretrained.pdparams
WenmuZhou's avatar
WenmuZhou committed
43
# 或,下载ResNet18_vd的预训练模型
tink2123's avatar
tink2123 committed
44
wget -P ./pretrain_models/ https://paddleocr.bj.bcebos.com/pretrained/ResNet18_vd_pretrained.pdparams
WenmuZhou's avatar
WenmuZhou committed
45
# 或,下载ResNet50_vd的预训练模型
tink2123's avatar
tink2123 committed
46
wget -P ./pretrain_models/ https://paddleocr.bj.bcebos.com/pretrained/ResNet50_vd_ssld_pretrained.pdparams
47
48
```

49
50
51
52
53
<a name="2-----"></a>
# 2. 开始训练

<a name="21-----"></a>
## 2.1 启动训练
tink2123's avatar
tink2123 committed
54
55
56

*如果您安装的是cpu版本,请将配置文件中的 `use_gpu` 字段修改为false*

licx's avatar
licx committed
57
```shell
LDOUBLEV's avatar
update  
LDOUBLEV committed
58
# 单机单卡训练 mv3_db 模型
LDOUBLEV's avatar
LDOUBLEV committed
59
python3 tools/train.py -c configs/det/det_mv3_db.yml \
Leif's avatar
Leif committed
60
     -o Global.pretrained_model=./pretrain_models/MobileNetV3_large_x0_5_pretrained
61

62
# 单机多卡训练,通过 --gpus 参数设置使用的GPU ID
LDOUBLEV's avatar
LDOUBLEV committed
63
python3 -m paddle.distributed.launch --gpus '0,1,2,3' tools/train.py -c configs/det/det_mv3_db.yml \
Leif's avatar
Leif committed
64
     -o Global.pretrained_model=./pretrain_models/MobileNetV3_large_x0_5_pretrained
stephon's avatar
stephon committed
65

66
67
```

LDOUBLEV's avatar
LDOUBLEV committed
68
上述指令中,通过-c 选择训练使用configs/det/det_db_mv3.yml配置文件。
LDOUBLEV's avatar
LDOUBLEV committed
69
有关配置文件的详细解释,请参考[链接](./config.md)
stephon's avatar
stephon committed
70

71
您也可以通过-o参数在不需要修改yml文件的情况下,改变训练的参数,比如,调整训练的学习率为0.0001
licx's avatar
licx committed
72
```shell
LDOUBLEV's avatar
LDOUBLEV committed
73
python3 tools/train.py -c configs/det/det_mv3_db.yml -o Optimizer.base_lr=0.0001
74
```
stephon's avatar
stephon committed
75

76
77
<a name="22-----"></a>
## 2.2 断点训练
LDOUBLEV's avatar
LDOUBLEV committed
78
79

如果训练程序中断,如果希望加载训练中断的模型从而恢复训练,可以通过指定Global.checkpoints指定要加载的模型路径:
licx's avatar
licx committed
80
```shell
LDOUBLEV's avatar
LDOUBLEV committed
81
python3 tools/train.py -c configs/det/det_mv3_db.yml -o Global.checkpoints=./your/trained/model
82
```
LDOUBLEV's avatar
update  
LDOUBLEV committed
83

Leif's avatar
Leif committed
84
**注意**`Global.checkpoints`的优先级高于`Global.pretrained_model`的优先级,即同时指定两个参数时,优先加载`Global.checkpoints`指定的模型,如果`Global.checkpoints`指定的模型路径有误,会加载`Global.pretrained_model`指定的模型。
85

86
87
<a name="23---backbone---"></a>
## 2.3 更换Backbone 训练
88
89
90

PaddleOCR将网络划分为四部分,分别在[ppocr/modeling](../../ppocr/modeling)下。 进入网络的数据将按照顺序(transforms->backbones->
necks->heads)依次通过这四个部分。
LDOUBLEV's avatar
update  
LDOUBLEV committed
91

92
93
94
95
96
97
```bash
├── architectures # 网络的组网代码
├── transforms    # 网络的图像变换模块
├── backbones     # 网络的特征提取模块
├── necks         # 网络的特征增强模块
└── heads         # 网络的输出模块
LDOUBLEV's avatar
LDOUBLEV committed
98
```
99
如果要更换的Backbone 在PaddleOCR中有对应实现,直接修改配置yml文件中`Backbone`部分的参数即可。
LDOUBLEV's avatar
LDOUBLEV committed
100

101
如果要使用新的Backbone,更换backbones的例子如下:
LDOUBLEV's avatar
LDOUBLEV committed
102

103
104
1.[ppocr/modeling/backbones](../../ppocr/modeling/backbones) 文件夹下新建文件,如my_backbone.py。
2. 在 my_backbone.py 文件内添加相关代码,示例代码如下:
105

106
107
108
109
```python
import paddle
import paddle.nn as nn
import paddle.nn.functional as F
110
111


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
class MyBackbone(nn.Layer):
    def __init__(self, *args, **kwargs):
        super(MyBackbone, self).__init__()
        # your init code
        self.conv = nn.xxxx

    def forward(self, inputs):
        # your network forward
        y = self.conv(inputs)
        return y
```

3.[ppocr/modeling/backbones/\__init\__.py](../../ppocr/modeling/backbones/__init__.py)文件内导入添加的`MyBackbone`模块,然后修改配置文件中Backbone进行配置即可使用,格式如下:

```yaml
Backbone:
name: MyBackbone
args1: args1
```

**注意**:如果要更换网络的其他模块,可以参考[文档](./add_new_algorithm.md)

MissPenguin's avatar
update  
MissPenguin committed
134
135
136
137
<a name="24---amp---"></a>
## 2.4 混合精度训练

如果您想进一步加快训练速度,可以使用[自动混合精度训练](https://www.paddlepaddle.org.cn/documentation/docs/zh/guides/01_paddle2.0_introduction/basic_concept/amp_cn.html), 以单机单卡为例,命令如下:
WenmuZhou's avatar
WenmuZhou committed
138

MissPenguin's avatar
update  
MissPenguin committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
```shell
python3 tools/train.py -c configs/det/det_mv3_db.yml \
     -o Global.pretrained_model=./pretrain_models/MobileNetV3_large_x0_5_pretrained \
     Global.use_amp=True Global.scale_loss=1024.0 Global.use_dynamic_loss_scaling=True
 ```

<a name="26---fleet---"></a>
## 2.5 分布式训练

多机多卡训练时,通过 `--ips` 参数设置使用的机器IP地址,通过 `--gpus` 参数设置使用的GPU ID:

```bash
python3 -m paddle.distributed.launch --ips="xx.xx.xx.xx,xx.xx.xx.xx" --gpus '0,1,2,3' tools/train.py -c configs/det/det_mv3_db.yml \
     -o Global.pretrained_model=./pretrain_models/MobileNetV3_large_x0_5_pretrained
```
154

MissPenguin's avatar
update  
MissPenguin committed
155
156
**注意:** 采用多机多卡训练时,需要替换上面命令中的ips值为您机器的地址,机器之间需要能够相互ping通。另外,训练时需要在多个机器上分别启动命令。查看机器ip地址的命令为`ifconfig`。

WenmuZhou's avatar
WenmuZhou committed
157

MissPenguin's avatar
update  
MissPenguin committed
158
<a name="26---distill---"></a>
159

MissPenguin's avatar
update  
MissPenguin committed
160
## 2.6 知识蒸馏训练
161
162
163

PaddleOCR支持了基于知识蒸馏的检测模型训练过程,更多内容可以参考[知识蒸馏说明文档](./knowledge_distillation.md)。

MissPenguin's avatar
update  
MissPenguin committed
164
165
166
**注意:** 知识蒸馏训练目前只支持PP-OCR使用的`DB`和`CRNN`算法。

<a name="27---other---"></a>
167

MissPenguin's avatar
update  
MissPenguin committed
168
## 2.7 其他训练环境
169

MissPenguin's avatar
update  
MissPenguin committed
170
- Windows GPU/CPU
WenmuZhou's avatar
WenmuZhou committed
171

MissPenguin's avatar
update  
MissPenguin committed
172
- macOS
WenmuZhou's avatar
WenmuZhou committed
173

MissPenguin's avatar
update  
MissPenguin committed
174
- Linux DCU
WenmuZhou's avatar
WenmuZhou committed
175
176


177
178
179
180
181
<a name="3--------"></a>
# 3. 模型评估与预测

<a name="31-----"></a>
## 3.1 指标评估
182
183
184

PaddleOCR计算三个OCR检测相关的指标,分别是:Precision、Recall、Hmean(F-Score)。

licx's avatar
licx committed
185
训练中模型参数默认保存在`Global.save_model_dir`目录下。在评估指标时,需要设置`Global.checkpoints`指向保存的参数文件。
186

licx's avatar
licx committed
187
```shell
188
python3 tools/eval.py -c configs/det/det_mv3_db.yml  -o Global.checkpoints="{path/to/weights}/best_accuracy"
LDOUBLEV's avatar
LDOUBLEV committed
189
190
```

191

192
193
<a name="32-------"></a>
## 3.2 测试检测效果
LDOUBLEV's avatar
LDOUBLEV committed
194

MissPenguin's avatar
update  
MissPenguin committed
195
测试单张图像的检测效果:
licx's avatar
licx committed
196
```shell
197
python3 tools/infer_det.py -c configs/det/det_mv3_db.yml -o Global.infer_img="./doc/imgs_en/img_10.jpg" Global.pretrained_model="./output/det_db/best_accuracy"
LDOUBLEV's avatar
LDOUBLEV committed
198
199
```

MissPenguin's avatar
update  
MissPenguin committed
200
测试DB模型时,调整后处理阈值:
licx's avatar
licx committed
201
```shell
202
python3 tools/infer_det.py -c configs/det/det_mv3_db.yml -o Global.infer_img="./doc/imgs_en/img_10.jpg" Global.pretrained_model="./output/det_db/best_accuracy"  PostProcess.box_thresh=0.6 PostProcess.unclip_ratio=2.0
203
```
MissPenguin's avatar
update  
MissPenguin committed
204
* 注:`box_thresh`、`unclip_ratio`是DB后处理参数,其他检测模型不支持。
205

MissPenguin's avatar
update  
MissPenguin committed
206
测试文件夹下所有图像的检测效果:
licx's avatar
licx committed
207
```shell
208
python3 tools/infer_det.py -c configs/det/det_mv3_db.yml -o Global.infer_img="./doc/imgs_en/" Global.pretrained_model="./output/det_db/best_accuracy"
LDOUBLEV's avatar
LDOUBLEV committed
209
```
210

211
212
<a name="4--------"></a>
# 4. 模型导出与预测
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233

inference 模型(`paddle.jit.save`保存的模型)
一般是模型训练,把模型结构和模型参数保存在文件中的固化模型,多用于预测部署场景。
训练过程中保存的模型是checkpoints模型,保存的只有模型的参数,多用于恢复训练等。
与checkpoints模型相比,inference 模型会额外保存模型的结构信息,在预测部署、加速推理上性能优越,灵活方便,适合于实际系统集成。

检测模型转inference 模型方式:
```shell
# 加载配置文件`det_mv3_db.yml`,从`output/det_db`目录下加载`best_accuracy`模型,inference模型保存在`./output/det_db_inference`目录下
python3 tools/export_model.py -c configs/det/det_mv3_db.yml -o Global.pretrained_model="./output/det_db/best_accuracy" Global.save_inference_dir="./output/det_db_inference/"
```

DB检测模型inference 模型预测:
```shell
python3 tools/infer/predict_det.py --det_algorithm="DB" --det_model_dir="./output/det_db_inference/" --image_dir="./doc/imgs/" --use_gpu=True
```
如果是其他检测,比如EAST模型,det_algorithm参数需要修改为EAST,默认为DB算法:
```shell
python3 tools/infer/predict_det.py --det_algorithm="EAST" --det_model_dir="./output/det_db_inference/" --image_dir="./doc/imgs/" --use_gpu=True
```

234
235
<a name="5-faq"></a>
# 5. FAQ
236
237

Q1: 训练模型转inference 模型之后预测效果不一致?
238

239
240
241
**A**:此类问题出现较多,问题多是trained model预测时候的预处理、后处理参数和inference model预测的时候的预处理、后处理参数不一致导致的。以det_mv3_db.yml配置文件训练的模型为例,训练模型、inference模型预测结果不一致问题解决方式如下:
- 检查[trained model预处理](https://github.com/PaddlePaddle/PaddleOCR/blob/c1ed243fb68d5d466258243092e56cbae32e2c14/configs/det/det_mv3_db.yml#L116),和[inference model的预测预处理](https://github.com/PaddlePaddle/PaddleOCR/blob/c1ed243fb68d5d466258243092e56cbae32e2c14/tools/infer/predict_det.py#L42)函数是否一致。算法在评估的时候,输入图像大小会影响精度,为了和论文保持一致,训练icdar15配置文件中将图像resize到[736, 1280],但是在inference model预测的时候只有一套默认参数,会考虑到预测速度问题,默认限制图像最长边为960做resize的。训练模型预处理和inference模型的预处理函数位于[ppocr/data/imaug/operators.py](https://github.com/PaddlePaddle/PaddleOCR/blob/c1ed243fb68d5d466258243092e56cbae32e2c14/ppocr/data/imaug/operators.py#L147)
- 检查[trained model后处理](https://github.com/PaddlePaddle/PaddleOCR/blob/c1ed243fb68d5d466258243092e56cbae32e2c14/configs/det/det_mv3_db.yml#L51),和[inference 后处理参数](https://github.com/PaddlePaddle/PaddleOCR/blob/c1ed243fb68d5d466258243092e56cbae32e2c14/tools/infer/utility.py#L50)是否一致。
WenmuZhou's avatar
WenmuZhou committed
242
243
244
245

Q1: 训练EAST模型提示找不到lanms库?

**A**:执行pip3 install lanms-nova 即可。