detection.md 12.8 KB
Newer Older
LDOUBLEV's avatar
LDOUBLEV committed
1

2
# 文字检测
3

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

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
- [1. 准备数据和模型](#1--------)
  * [1.1 数据准备](#11-----)
  * [1.2 下载预训练模型](#12--------)
- [2. 开始训练](#2-----)
  * [2.1 启动训练](#21-----)
  * [2.2 断点训练](#22-----)
  * [2.3 更换Backbone 训练](#23---backbone---)
- [3. 模型评估与预测](#3--------)
  * [3.1 指标评估](#31-----)
  * [3.2 测试检测效果](#32-------)
- [4. 模型导出与预测](#4--------)
- [5. FAQ](#5-faq)

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

LDOUBLEV's avatar
LDOUBLEV committed
22
<a name="11-----"></a>
LDOUBLEV's avatar
LDOUBLEV committed
23
## 1.1 数据准备
24

LDOUBLEV's avatar
LDOUBLEV committed
25
26
icdar2015 TextLocalization数据集是文本检测的数据集,包含1000张训练图像和500张测试图像。
icdar2015数据集可以从[官网](https://rrc.cvc.uab.es/?ch=4&com=downloads)下载到,首次下载需注册。
LDOUBLEV's avatar
LDOUBLEV committed
27
28
29
注册完成登陆后,下载下图中红色框标出的部分,其中, `Training Set Images`下载的内容保存为`icdar_c4_train_imgs`文件夹下,`Test Set Images` 下载的内容保存为`ch4_test_images`文件夹下

<p align="center">
LDOUBLEV's avatar
LDOUBLEV committed
30
 <img src="../datasets/ic15_location_download.png" align="middle" width = "700"/>
LDOUBLEV's avatar
LDOUBLEV committed
31
32
<p align="center">

LDOUBLEV's avatar
LDOUBLEV committed
33
将下载到的数据集解压到工作目录下,假设解压在 PaddleOCR/train_data/下。另外,PaddleOCR将零散的标注文件整理成单独的标注文件
34
,您可以通过wget的方式进行下载。
licx's avatar
licx committed
35
```shell
tink2123's avatar
fix doc  
tink2123 committed
36
37
38
39
# 在PaddleOCR路径下
cd PaddleOCR/
wget -P ./train_data/  https://paddleocr.bj.bcebos.com/dataset/train_icdar2015_label.txt
wget -P ./train_data/  https://paddleocr.bj.bcebos.com/dataset/test_icdar2015_label.txt
40
41
```

LDOUBLEV's avatar
LDOUBLEV committed
42
PaddleOCR 也提供了数据格式转换脚本,可以将官网 label 转换支持的数据格式。 数据转换工具在 `ppocr/utils/gen_label.py`, 这里以训练集为例:
WenmuZhou's avatar
WenmuZhou committed
43
44
45

```
# 将官网下载的标签文件转换为 train_icdar2015_label.txt
WenmuZhou's avatar
WenmuZhou committed
46
python gen_label.py --mode="det" --root_path="/path/to/icdar_c4_train_imgs/"  \
WenmuZhou's avatar
WenmuZhou committed
47
48
                    --input_path="/path/to/ch4_training_localization_transcription_gt" \
                    --output_label="/path/to/train_icdar2015_label.txt"
WenmuZhou's avatar
WenmuZhou committed
49
50
```

51
解压数据集和下载标注文件后,PaddleOCR/train_data/ 有两个文件夹和两个文件,按照如下方式组织icdar2015数据集:
52
```
tink2123's avatar
tink2123 committed
53
/PaddleOCR/train_data/icdar2015/text_localization/
54
55
56
57
58
59
  └─ icdar_c4_train_imgs/         icdar数据集的训练数据
  └─ ch4_test_images/             icdar数据集的测试数据
  └─ train_icdar2015_label.txt    icdar数据集的训练标注
  └─ test_icdar2015_label.txt     icdar数据集的测试标注
```

licx's avatar
licx committed
60
提供的标注文件格式如下,中间用"\t"分隔:
61
62
```
" 图像文件名                    json.dumps编码的图像标注信息"
LDOUBLEV's avatar
LDOUBLEV committed
63
ch4_test_images/img_61.jpg    [{"transcription": "MASA", "points": [[310, 104], [416, 141], [418, 216], [312, 179]]}, {...}]
64
```
tink2123's avatar
fix doc  
tink2123 committed
65
json.dumps编码前的图像标注信息是包含多个字典的list,字典中的 `points` 表示文本框的四个点的坐标(x, y),从左上角的点开始顺时针排列。
licx's avatar
licx committed
66
`transcription` 表示当前文本框的文字,**当其内容为“###”时,表示该文本框无效,在训练时会跳过。**
67

licx's avatar
licx committed
68
如果您想在其他数据集上训练,可以按照上述形式构建标注文件。
69

LDOUBLEV's avatar
LDOUBLEV committed
70
<a name="12--------"></a>
LDOUBLEV's avatar
LDOUBLEV committed
71
## 1.2 下载预训练模型
72

WenmuZhou's avatar
WenmuZhou committed
73
首先下载模型backbone的pretrain model,PaddleOCR的检测模型目前支持两种backbone,分别是MobileNetV3、ResNet_vd系列,
74
75
76
您可以根据需求使用[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
77
```shell
LDOUBLEV's avatar
LDOUBLEV committed
78
cd PaddleOCR/
79
# 根据backbone的不同选择下载对应的预训练模型
80
# 下载MobileNetV3的预训练模型
81
wget -P ./pretrain_models/ https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/MobileNetV3_large_x0_5_pretrained.pdparams
WenmuZhou's avatar
WenmuZhou committed
82
# 或,下载ResNet18_vd的预训练模型
83
wget -P ./pretrain_models/ https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNet18_vd_pretrained.pdparams
WenmuZhou's avatar
WenmuZhou committed
84
# 或,下载ResNet50_vd的预训练模型
85
wget -P ./pretrain_models/ https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/ResNet50_vd_ssld_pretrained.pdparams
86
87
```

88
89
90
91
92
<a name="2-----"></a>
# 2. 开始训练

<a name="21-----"></a>
## 2.1 启动训练
tink2123's avatar
tink2123 committed
93
94
95

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

licx's avatar
licx committed
96
```shell
LDOUBLEV's avatar
update  
LDOUBLEV committed
97
# 单机单卡训练 mv3_db 模型
LDOUBLEV's avatar
LDOUBLEV committed
98
python3 tools/train.py -c configs/det/det_mv3_db.yml \
Leif's avatar
Leif committed
99
     -o Global.pretrained_model=./pretrain_models/MobileNetV3_large_x0_5_pretrained
100

101
# 单机多卡训练,通过 --gpus 参数设置使用的GPU ID
LDOUBLEV's avatar
LDOUBLEV committed
102
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
103
     -o Global.pretrained_model=./pretrain_models/MobileNetV3_large_x0_5_pretrained
stephon's avatar
stephon committed
104

Bin Lu's avatar
Bin Lu committed
105
# 多机多卡训练,通过 --ips 参数设置使用的机器IP地址,通过 --gpus 参数设置使用的GPU ID
stephon's avatar
stephon committed
106
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 \
Bin Lu's avatar
Bin Lu committed
107
     -o Global.pretrained_model=./pretrain_models/MobileNetV3_large_x0_5_pretrained
108
109
```

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

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

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

Bin Lu's avatar
Bin Lu committed
120
121
122
123
124
125
如果您想进一步加快训练速度,可以使用[自动混合精度训练](https://www.paddlepaddle.org.cn/documentation/docs/zh/guides/01_paddle2.0_introduction/basic_concept/amp_cn.html), 以单机单卡为例,命令如下:
```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
 ```
126

127
128
<a name="22-----"></a>
## 2.2 断点训练
LDOUBLEV's avatar
LDOUBLEV committed
129
130

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

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

137
138
<a name="23---backbone---"></a>
## 2.3 更换Backbone 训练
139
140
141

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

143
144
145
146
147
148
```bash
├── architectures # 网络的组网代码
├── transforms    # 网络的图像变换模块
├── backbones     # 网络的特征提取模块
├── necks         # 网络的特征增强模块
└── heads         # 网络的输出模块
LDOUBLEV's avatar
LDOUBLEV committed
149
```
150
如果要更换的Backbone 在PaddleOCR中有对应实现,直接修改配置yml文件中`Backbone`部分的参数即可。
LDOUBLEV's avatar
LDOUBLEV committed
151

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

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

157
158
159
160
```python
import paddle
import paddle.nn as nn
import paddle.nn.functional as F
161
162


163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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)。

185
186
187
188
189
<a name="3--------"></a>
# 3. 模型评估与预测

<a name="31-----"></a>
## 3.1 指标评估
190
191
192

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

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

licx's avatar
licx committed
195
```shell
196
python3 tools/eval.py -c configs/det/det_mv3_db.yml  -o Global.checkpoints="{path/to/weights}/best_accuracy"
LDOUBLEV's avatar
LDOUBLEV committed
197
198
```

licx's avatar
licx committed
199
* 注:`box_thresh`、`unclip_ratio`是DB后处理所需要的参数,在评估EAST模型时不需要设置
200

201
202
<a name="32-------"></a>
## 3.2 测试检测效果
LDOUBLEV's avatar
LDOUBLEV committed
203
204

测试单张图像的检测效果
licx's avatar
licx committed
205
```shell
206
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
207
208
```

209
测试DB模型时,调整后处理阈值
licx's avatar
licx committed
210
```shell
211
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
212
213
```

LDOUBLEV's avatar
LDOUBLEV committed
214
测试文件夹下所有图像的检测效果
licx's avatar
licx committed
215
```shell
216
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
217
```
218

219
220
<a name="4--------"></a>
# 4. 模型导出与预测
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241

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

242
243
<a name="5-faq"></a>
# 5. FAQ
244
245

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

247
248
249
**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)是否一致。