Skip to content

Commit cb3c400

Browse files
add config and refine doc
1 parent 5f902e0 commit cb3c400

File tree

10 files changed

+460
-342
lines changed

10 files changed

+460
-342
lines changed

scene_text_recognition/README.md

Lines changed: 61 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
在现实生活中,包括路牌、菜单、大厦标语在内的很多场景均会有文字出现,这些场景的照片中的文字为图片场景的理解提供了更多信息,\[[1](#参考文献)\]使用深度学习模型自动识别路牌中的文字,帮助街景应用获取更加准确的地址信息。
66

7-
本例将演示如何用 PaddlePaddle 完成 **场景文字识别 (STR, Scene Text Recognition)** 任务。以下图为例,给定一个场景图片,STR需要从图片中识别出对应的文字"keep":
7+
本例将演示如何用 PaddlePaddle 完成 **场景文字识别 (STR, Scene Text Recognition)** 任务。以下图为例,给定一个场景图片,STR需要从图片中识别出对应的文字"keep"
88

99
<p align="center">
1010
<img src="./images/503.jpg"/><br/>
@@ -14,87 +14,93 @@
1414

1515
## 使用 PaddlePaddle 训练与预测
1616

17+
### 安装依赖包
18+
```bash
19+
pip install -r requirements.txt
20+
```
21+
22+
### 指定训练配置参数
23+
24+
通过 `config.py` 脚本修改训练和模型配置参数,脚本中有对可配置参数的详细解释,示例如下:
25+
```python
26+
class TrainerConfig(object):
27+
28+
# Whether to use GPU in training or not.
29+
use_gpu = True
30+
# The number of computing threads.
31+
trainer_count = 1
32+
33+
# The training batch size.
34+
batch_size = 10
35+
36+
...
37+
38+
39+
class ModelConfig(object):
40+
41+
# Number of the filters for convolution group.
42+
filter_num = 8
43+
44+
...
45+
```
46+
修改 `config.py` 对参数进行调整。例如,通过修改 `use_gpu` 参数来指定是否使用 GPU 进行训练。
47+
1748
### 模型训练
1849
训练脚本 [./train.py](./train.py) 中设置了如下命令行参数:
1950

2051
```
21-
usage: train.py [-h] --image_shape IMAGE_SHAPE --train_file_list
22-
TRAIN_FILE_LIST --test_file_list TEST_FILE_LIST
23-
[--batch_size BATCH_SIZE]
24-
[--model_output_prefix MODEL_OUTPUT_PREFIX]
25-
[--trainer_count TRAINER_COUNT]
26-
[--save_period_by_batch SAVE_PERIOD_BY_BATCH]
27-
[--num_passes NUM_PASSES]
28-
29-
PaddlePaddle CTC example
30-
31-
optional arguments:
32-
-h, --help show this help message and exit
33-
--image_shape IMAGE_SHAPE
34-
image's shape, format is like '173,46'
35-
--train_file_list TRAIN_FILE_LIST
36-
path of the file which contains path list of train
37-
image files
38-
--test_file_list TEST_FILE_LIST
39-
path of the file which contains path list of test
40-
image files
41-
--batch_size BATCH_SIZE
42-
size of a mini-batch
43-
--model_output_prefix MODEL_OUTPUT_PREFIX
44-
prefix of path for model to store (default:
45-
./model.ctc)
46-
--trainer_count TRAINER_COUNT
47-
number of training threads
48-
--save_period_by_batch SAVE_PERIOD_BY_BATCH
49-
save model to disk every N batches
50-
--num_passes NUM_PASSES
51-
number of passes to train (default: 1)
52-
```
52+
Options:
53+
--train_file_list_path TEXT The path of the file which contains path list
54+
of train image files. [required]
55+
--test_file_list_path TEXT The path of the file which contains path list
56+
of test image files. [required]
57+
--model_save_dir TEXT The path to save the trained models (default:
58+
'models').
59+
--help Show this message and exit.
5360
54-
重要的几个参数包括:
61+
```
5562

56-
- `image_shape` 图片的尺寸
5763
- `train_file_list` 训练数据的列表文件,每行一个路径加对应的text,具体格式为:
5864
```
5965
word_1.png, "PROPER"
6066
word_2.png, "FOOD"
6167
```
62-
- `test_file_list` 测试数据的列表文件,格式同上
63-
64-
### 预测
65-
预测部分由infer.py完成,使用的是最优路径解码算法,即:在每个时间步选择一个概率最大的字符。在使用过程中,需要在infer.py中指定具体的模型目录、图片固定尺寸、batch_size和图片文件的列表文件。例如:
66-
```python
67-
model_path = "model.ctc-pass-9-batch-150-test.tar.gz"
68-
image_shape = "173,46"
69-
batch_size = 50
70-
infer_file_list = 'data/test_data/Challenge2_Test_Task3_GT.txt'
71-
```
72-
然后运行```python infer.py```
73-
68+
- `test_file_list` 测试数据的列表文件,格式同上。
69+
- `model_save_dir` 模型参数会的保存目录目录, 默认为当前目录下的`models`目录。
7470

7571
### 具体执行的过程:
7672

7773
1.从官方网站下载数据\[[2](#参考文献)\](Task 2.3: Word Recognition (2013 edition)),会有三个文件: Challenge2_Training_Task3_Images_GT.zip、Challenge2_Test_Task3_Images.zip和 Challenge2_Test_Task3_GT.txt。
7874
分别对应训练集的图片和图片对应的单词,测试集的图片,测试数据对应的单词,然后执行以下命令,对数据解压并移动至目标文件夹:
7975

80-
```
76+
```bash
8177
mkdir -p data/train_data
8278
mkdir -p data/test_data
8379
unzip Challenge2_Training_Task3_Images_GT.zip -d data/train_data
8480
unzip Challenge2_Test_Task3_Images.zip -d data/test_data
8581
mv Challenge2_Test_Task3_GT.txt data/test_data
8682
```
8783

88-
2.获取训练数据文件夹中 `gt.txt` 的路径 (data/train_data)和测试数据文件夹中`Challenge2_Test_Task3_GT.txt`的路径(data/test_data)
84+
2.获取训练数据文件夹中 `gt.txt` 的路径 (data/train_data)和测试数据文件夹中`Challenge2_Test_Task3_GT.txt`的路径(data/test_data)
8985

90-
3.执行命令
86+
3.执行如下命令进行训练:
87+
```bash
88+
python train.py \
89+
--train_file_list_path 'data/train_data/gt.txt' \
90+
--test_file_list_path 'data/test_data/Challenge2_Test_Task3_GT.txt'
9191
```
92-
python train.py --train_file_list data/train_data/gt.txt --test_file_list data/test_data/Challenge2_Test_Task3_GT.txt --image_shape '173,46'
93-
```
94-
4.训练过程中,模型参数会自动备份到指定目录,默认为 ./model.ctc
92+
4.训练过程中,模型参数会自动备份到指定目录,默认会保存在 `./models` 目录下。
9593

96-
5.设置infer.py中的相关参数(模型所在路径),运行```python infer.py``` 进行预测
9794

95+
### 预测
96+
预测部分由 `infer.py` 完成,使用的是最优路径解码算法,即:在每个时间步选择一个概率最大的字符。在使用过程中,需要在 `infer.py` 中指定具体的模型目录、图片固定尺寸、batch_size(默认设置为10)和图片文件的列表文件。执行如下代码:
97+
```bash
98+
python infer.py \
99+
--model_path 'models/params_pass_00000.tar.gz' \
100+
--image_shape '173,46' \
101+
--infer_file_list_path 'data/test_data/Challenge2_Test_Task3_GT.txt'
102+
```
103+
即可进行预测。
98104

99105
### 其他数据集
100106

@@ -104,7 +110,7 @@ python train.py --train_file_list data/train_data/gt.txt --test_file_list data/t
104110
### 注意事项
105111

106112
- 由于模型依赖的 `warp CTC` 只有CUDA的实现,本模型只支持 GPU 运行
107-
- 本模型参数较多,占用显存比较大,实际执行时可以调节batch_size 控制显存占用
113+
- 本模型参数较多,占用显存比较大,实际执行时可以调节`batch_size`控制显存占用
108114
- 本模型使用的数据集较小,可以选用其他更大的数据集\[[3](#参考文献)\]来训练需要的模型
109115

110116
## 参考文献

scene_text_recognition/config.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
__all__ = ["TrainerConfig", "ModelConfig"]
2+
3+
4+
class TrainerConfig(object):
5+
6+
# Whether to use GPU in training or not.
7+
use_gpu = True
8+
# The number of computing threads.
9+
trainer_count = 1
10+
11+
# The training batch size.
12+
batch_size = 10
13+
14+
# The epoch number.
15+
num_passes = 10
16+
17+
# Parameter updates momentum.
18+
momentum = 0
19+
20+
# The shape of images
21+
image_shape = (173, 46)
22+
23+
# The buffer size of the data reader.
24+
# The number of buffer size samples will be shuffled in training.
25+
buf_size = 1000
26+
27+
# The parameter is used to control logging period.
28+
# Training log will be printed every log_period.
29+
log_period = 50
30+
31+
32+
class ModelConfig(object):
33+
34+
# Number of the filters for convolution group.
35+
filter_num = 8
36+
37+
# Use batch normalization or not in image convolution group.
38+
with_bn = True
39+
40+
# The number of channels for block expand layer.
41+
num_channels = 128
42+
43+
# The parameter stride_x in block expand layer.
44+
stride_x = 1
45+
46+
# The parameter stride_y in block expand layer.
47+
stride_y = 1
48+
49+
# The parameter block_x in block expand layer.
50+
block_x = 1
51+
52+
# The parameter block_y in block expand layer.
53+
block_y = 11
54+
55+
# The hidden size for gru.
56+
hidden_size = num_channels
57+
58+
# Use norm_by_times or not in warp ctc layer.
59+
norm_by_times = True
60+
61+
# The number of filter list in image convolution group layer.
62+
filter_num_list = [16, 32, 64, 128]
63+
64+
# The parameter conv_padding in image convolution group layer.
65+
conv_padding = 1
66+
67+
# The parameter conv_filter_size in image convolution group layer.
68+
conv_filter_size = 3
69+
70+
# The parameter pool_size in image convolution group layer.
71+
pool_size = 2
72+
73+
# The parameter pool_stride in image convolution group layer.
74+
pool_stride = 2

scene_text_recognition/data_provider.py

Lines changed: 0 additions & 100 deletions
This file was deleted.

0 commit comments

Comments
 (0)