提交 53988dd3 编写于 作者: W wanghaoshuang

Fix some issues:

1. Remove illustration of arguments.
2. Make inference support for more format input.
上级 1c78d279
......@@ -85,7 +85,9 @@
**C. 待预测数据集**
待预测数据集的格式与训练集也类似,只不过list文件中的最后一列可以放任意占位字符或字符串,如下所示:
预测支持三种形式的输入:
第一种:设置`--input_images_dir``--input_images_list`, 与训练集类似, 只不过list文件中的最后一列可以放任意占位字符或字符串,如下所示:
```
185 48 00508_0215.jpg s
......@@ -94,99 +96,75 @@
...
```
在做inference时,通过inference.py的选项`--input_images_dir``--input_images_list` 来设置输入数据的路径。
#### 1.2 训练
通过以下命令调用训练脚本进行训练:
第二种:仅设置`--input_images_list`, 其中list文件中只需放图片的完整路径,如下所示:
```
python ctc_train.py [options]
data/test_images/00000.jpg
data/test_images/00001.jpg
data/test_images/00003.jpg
```
其中,options支持配置以下训练相关的参数:
**- -batch_size :** Minibatch 大小,默认为32.
**- -pass_num :** 训练多少个pass。默认为100。
**- -log_period :** 每隔多少个minibatch打印一次训练日志, 默认为1000.
**- -save_model_period :** 每隔多少个minibatch保存一次模型。默认为15000。
如果设置为-1,则永不保存模型。
**- -eval_period :** 每隔多少个minibatch用测试集测试一次模型。默认为15000。如果设置为-1,则永不进行测试。
**- -save_model_dir :** 保存模型文件的路径,默认为“./models”,如果指定路径不存在,则会自动创建路径。
**- -init_model :** 初始化模型的路径。如果模型是以单个文件存储的,这里需要指定具体文件的路径;如果模型是以多个文件存储的,这里只需指定多个文件所在文件夹的路径。该选项默认为 None,意思是不用预训练模型做初始化。
**- -learning_rate :** 全局learning rate. 默认为 0.001.
**- -l2 :** L2 regularizer. 默认为0.0004.
**- -max_clip :** Max gradient clipping threshold. 默认为10.0.
**- -momentum :** Momentum. 默认为0.9.
**- -rnn_hidden_size :** RNN 隐藏层大小。 默认为200。
**- -device DEVICE :** 设备ID。设置为-1,训练在CPU执行;设置为0,训练在GPU上执行。默认为0。
第三种:从stdin读入一张图片的path,然后进行一次inference.
**- -min_average_window :** Min average window. 默认为10000.
#### 1.2 训练
**- -max_average_window :** Max average window. 建议大小设置为一个pass内minibatch的数量。默认为15625.
Train on one GPU with defalut trainning data:
**- -average_window :** Average window. 默认为0.15.
```
env CUDA_VISIABLE_DEVICES=0 python ctc_train.py \
--device=0 \
--parallel=False \
--batch_size=32
```
**- -parallel :** 是否使用多卡进行训练。默认为True.
Train on multi-GPU with defalut trainning data:
**- -train_images :** 存放训练集图片的路径,如果设置为None,ctc_reader会自动下载使用默认数据集。如果使用自己的数据进行训练,需要修改该选项。默认为None。
```
env CUDA_VISIABLE_DEVICES=0,1,2,3 python ctc_train.py \
--device=0 \
--parallel=True \
--batch_size=128
```
**- -train_list :** 存放训练集图片信息的list文件,如果设置为None,ctc_reader会自动下载使用默认数据集。如果使用自己的数据进行训练,需要修改该选项。默认为None
执行`python ctc_train.py --help`可查看更多使用方式和参数详细说明
**- -test_images :** 存放测试集图片的路径,如果设置为None,ctc_reader会自动下载使用默认数据集。如果使用自己的数据进行测试,需要修改该选项。默认为None。
图2为使用默认参数和默认数据集训练的收敛曲线,其中横坐标轴为训练pass数,纵轴为在测试集上的sequence_error.
**- -test_list :** 存放测试集图片信息的list文件,如果设置为None,ctc_reader会自动下载使用默认数据集。如果使用自己的数据进行测试,需要修改该选项。默认为None。
<p align="center">
<img src="images/train.jpg" width="620" hspace='10'/> <br/>
<strong>图 2</strong>
</p>
**- -num_classes :** 字符集的大小。如果设置为None, 则使用ctc_reader提供的字符集大小。如果使用自己的数据进行训练,需要修改该选项。默认为None.
### 1.3 Inference
### 1.3 Evaluate
通过以下命令调用预测脚本进行预测
通过以下命令调用评估脚本用指定数据集对模型进行评估
```
python inference.py [options]
env CUDA_VISIBLE_DEVICE=0 python eval.py \
--model_path="./models/model_0" \
--input_images_dir="./eval_data/images/" \
--input_images_list="./eval_data/eval_list\" \
--device 0
```
其中,options支持配置以下预测相关的参数:
**--model_path :** 用来做预测的模型文件。如果模型是以单个文件存储的,这里需要指定具体文件的路径;如果模型是以多个文件存储的,这里只需指定多个文件所在文件夹的路径。为必设置选项。
执行`python ctc_train.py --help`可查看参数详细说明。
**--input_images_dir :** 存放待预测图片的文件夹路径。如果设置为None, 则使用ctc_reader提供的默认数据。默认为None.
**--input_images_list :** 存放待预测图片信息的list文件的路径。如果设置为None, 则使用ctc_reader提供的默认数据。默认为None.
### 1.4 Inference
**--device DEVICE :** 设备ID。设置为-1,运行在CPU上;设置为0,运行在GPU上。默认为0。
预测结果会print到标准输出。
### 1.4 Evaluate
通过以下命令调用评估脚本用指定数据集对模型进行评估:
Read image path from stdin and inference:
```
python eval.py [options]
env CUDA_VISIBLE_DEVICE=0 python inference.py \
--model_path models/model_00044_15000
```
其中,options支持配置以下评估相关的参数:
**--model_path :** 待评估模型的文件路径。如果模型是以单个文件存储的,这里需要指定具体文件的路径;如果模型是以多个文件存储的,这里只需指定多个文件所在文件夹的路径。为必设置选项。
Read image path from list file and inference:
**--input_images_dir :** 存放待评估图片的文件夹路径。如果设置为None, 则使用ctc_reader提供的默认数据。默认为None.
**--input_images_list :** 存放待评估图片信息的list文件的路径。如果设置为None, 则使用ctc_reader提供的默认数据。默认为None.
**--device DEVICE :** 设备ID。设置为-1,运行在CPU上;设置为0,运行在GPU上。默认为0。
```
env CUDA_VISIBLE_DEVICE=0 python inference.py \
--model_path=models/model_00044_15000 \
--input_images_list="data/test.list"
```
......@@ -30,10 +30,10 @@ class DataGenerator(object):
Reader interface for training.
:param img_root_dir: The root path of the image for training.
:type file_list: str
:type img_root_dir: str
:param img_label_list: The path of the <image_name, label> file for training.
:type file_list: str
:type img_label_list: str
'''
......@@ -91,10 +91,10 @@ class DataGenerator(object):
Reader interface for inference.
:param img_root_dir: The root path of the images for training.
:type file_list: str
:type img_root_dir: str
:param img_label_list: The path of the <image_name, label> file for testing.
:type file_list: list
:type img_label_list: str
'''
def reader():
......@@ -111,6 +111,42 @@ class DataGenerator(object):
return reader
def infer_reader(self, img_root_dir=None, img_label_list=None):
'''A reader interface for inference.
:param img_root_dir: The root path of the images for training.
:type img_root_dir: str
:param img_label_list: The path of the <image_name, label> file for
inference. It should be the path of <image_path> file if img_root_dir
was None. If img_label_list was set to None, it will read image path
from stdin.
:type img_root_dir: str
'''
def reader():
if img_label_list is not None:
for line in open(img_label_list):
if img_root_dir is not None:
# h, w, img_name, labels
img_name = line.split(' ')[2]
img_path = os.path.join(img_root_dir, img_name)
else:
img_path = line.strip("\t\n\r")
img = Image.open(img_path).convert('L')
img = np.array(img) - 127.5
img = img[np.newaxis, ...]
yield img, label
else:
while True:
img_path = raw_input("Please input the path of image: ")
img = Image.open(img_path).convert('L')
img = np.array(img) - 127.5
img = img[np.newaxis, ...]
yield img, [[0]]
return reader
def num_classes():
'''Get classes number of this dataset.
......@@ -145,6 +181,12 @@ def test(batch_size=1, test_images_dir=None, test_list_file=None):
generator.test_reader(test_images_dir, test_list_file), batch_size)
def inference(infer_images_dir=None, infer_list_file=None):
generator = DataGenerator()
return paddle.batch(
generator.infer_reader(infer_images_dir, infer_list_file), 1)
def download_data():
'''Download train and test data.
'''
......
......@@ -13,10 +13,10 @@ parser = argparse.ArgumentParser(description=__doc__)
add_arg = functools.partial(add_arguments, argparser=parser)
# yapf: disable
add_arg('batch_size', int, 32, "Minibatch size.")
add_arg('pass_num', int, 100, "# of training epochs.")
add_arg('pass_num', int, 100, "Number of training epochs.")
add_arg('log_period', int, 1000, "Log period.")
add_arg('save_model_period', int, 15000, "Save model period.")
add_arg('eval_period', int, 15000, "Evaluate period.")
add_arg('save_model_period', int, 15000, "Save model period. '-1' means never saving the model.")
add_arg('eval_period', int, 15000, "Evaluate period. '-1' means never evaluating the model.")
add_arg('save_model_dir', str, "./models", "The directory the model to be saved to.")
add_arg('init_model', str, None, "The init model file of directory.")
add_arg('learning_rate', float, 1.0e-3, "Learning rate.")
......@@ -28,7 +28,7 @@ add_arg('rnn_hidden_size', int, 200, "Hidden size of rnn layers.")
add_arg('device', int, 0, "Device id.'-1' means running on CPU"
"while '0' means GPU-0.")
add_arg('min_average_window',int, 10000, "Min average window.")
add_arg('max_average_window',int, 15625, "Max average window.")
add_arg('max_average_window',int, 15625, "Max average window. It is proposed to be set as the number of minibatch in a pass.")
add_arg('average_window', float, 0.15, "Average window.")
add_arg('parallel', bool, False, "Whether use parallel training.")
add_arg('train_images', str, None, "The directory of training images."
......
......@@ -25,7 +25,7 @@ def inference(args, infer=ctc_infer, data_reader=ctc_reader):
images = fluid.layers.data(name='pixel', shape=data_shape, dtype='float32')
sequence = infer(images, num_classes)
# data reader
test_reader = data_reader.test(test_images_dir=args.input_images_dir, test_list_file=args.input_images_list)
infer_reader = data_reader.inference(infer_images_dir=args.input_images_dir, infer_list_file=args.input_images_list)
# prepare environment
place = fluid.CPUPlace()
if args.device >= 0:
......@@ -44,7 +44,7 @@ def inference(args, infer=ctc_infer, data_reader=ctc_reader):
print "Init model from: %s." % args.model_path
for data in test_reader():
for data in infer_reader():
result = exe.run(fluid.default_main_program(),
feed=get_feeder_data(
data, place, need_label=False),
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册