未验证 提交 e3eca3a9 编写于 作者: W whs 提交者: GitHub

[cherry-pick] cherry pick some fix on ocr model, gan and PaddleSlim into model-1.4 (#2108)

* Fix eval.py in ocr ctc demo. (#1970)

* Refine readme in cycle gan. (#1972)

* 1. Refine readme
2. Fix some issues

* Refine README.md.

* Fix atention model. (#2059)

* 1. Enable GC strategy in run.sh (#2063)

2. Fix loading checkpoint in filter pruning strategy by adding init_model option into config file.
上级 8d4a562e

运行本目录下的程序示例需要使用PaddlePaddle develop最新版本。如果您的PaddlePaddle安装版本低于此要求,请按照[安装文档](http://www.paddlepaddle.org/docs/develop/documentation/zh/build_and_install/pip_install_cn.html)中的说明更新PaddlePaddle安装版本。 运行本目录下的程序示例需要使用PaddlePaddle develop最新版本。如果您的PaddlePaddle安装版本低于此要求,请按照[安装文档](http://www.paddlepaddle.org/docs/develop/documentation/zh/build_and_install/pip_install_cn.html)中的说明更新PaddlePaddle安装版本。
...@@ -73,8 +72,8 @@ env CUDA_VISIBLE_DEVICES=0 python train.py ...@@ -73,8 +72,8 @@ env CUDA_VISIBLE_DEVICES=0 python train.py
执行以下命令读取多张图片进行预测: 执行以下命令读取多张图片进行预测:
``` ```
env CUDA_VISIBLE_DEVICE=0 python infer.py \ env CUDA_VISIBLE_DEVICES=0 python infer.py \
--init_model="checkpoints/1" --input="./data/inputA/*" \ --init_model="output/checkpoints/1" --input="./data/horse2zebra/trainA/*" \
--input_style A --output="./output" --input_style A --output="./output"
``` ```
...@@ -89,3 +88,5 @@ env CUDA_VISIBLE_DEVICE=0 python infer.py \ ...@@ -89,3 +88,5 @@ env CUDA_VISIBLE_DEVICE=0 python infer.py \
<img src="images/B2A.jpg" width="620" hspace='10'/> <br/> <img src="images/B2A.jpg" width="620" hspace='10'/> <br/>
<strong>图 3</strong> <strong>图 3</strong>
</p> </p>
>在本文示例中,均可通过修改`CUDA_VISIBLE_DEVICES`改变使用的显卡号。
...@@ -44,7 +44,6 @@ def infer(args): ...@@ -44,7 +44,6 @@ def infer(args):
if not os.path.exists(args.output): if not os.path.exists(args.output):
os.makedirs(args.output) os.makedirs(args.output)
for file in glob.glob(args.input): for file in glob.glob(args.input):
print "read %s" % file
image_name = os.path.basename(file) image_name = os.path.basename(file)
image = Image.open(file) image = Image.open(file)
image = image.resize((256, 256)) image = image.resize((256, 256))
......
...@@ -22,7 +22,7 @@ add_arg = functools.partial(add_arguments, argparser=parser) ...@@ -22,7 +22,7 @@ add_arg = functools.partial(add_arguments, argparser=parser)
# yapf: disable # yapf: disable
add_arg('batch_size', int, 1, "Minibatch size.") add_arg('batch_size', int, 1, "Minibatch size.")
add_arg('epoch', int, 2, "The number of epoched to be trained.") add_arg('epoch', int, 2, "The number of epoched to be trained.")
add_arg('output', str, "./output_0", "The directory the model and the test result to be saved to.") add_arg('output', str, "./output", "The directory the model and the test result to be saved to.")
add_arg('init_model', str, None, "The init model file of directory.") add_arg('init_model', str, None, "The init model file of directory.")
add_arg('save_checkpoints', bool, True, "Whether to save checkpoints.") add_arg('save_checkpoints', bool, True, "Whether to save checkpoints.")
add_arg('run_test', bool, True, "Whether to run test.") add_arg('run_test', bool, True, "Whether to run test.")
......

运行本目录下的程序示例需要使用PaddlePaddle develop最新版本。如果您的PaddlePaddle安装版本低于此要求,请按照[安装文档](http://www.paddlepaddle.org/docs/develop/documentation/zh/build_and_install/pip_install_cn.html)中的说明更新PaddlePaddle安装版本。 运行本目录下的程序示例需要使用PaddlePaddle develop最新版本。如果您的PaddlePaddle安装版本低于此要求,请按照[安装文档](http://www.paddlepaddle.org/docs/develop/documentation/zh/build_and_install/pip_install_cn.html)中的说明更新PaddlePaddle安装版本。
...@@ -156,12 +155,13 @@ env CUDA_VISIBLE_DEVICES=0,1,2,3 python train.py --parallel=True ...@@ -156,12 +155,13 @@ env CUDA_VISIBLE_DEVICES=0,1,2,3 python train.py --parallel=True
通过以下命令调用评估脚本用指定数据集对模型进行评估: 通过以下命令调用评估脚本用指定数据集对模型进行评估:
``` ```
env CUDA_VISIBLE_DEVICE=0 python eval.py \ env CUDA_VISIBLE_DEVICES=0 python eval.py \
--model_path="./models/model_0" \ --model_path="./models/model_0" \
--input_images_dir="./eval_data/images/" \ --input_images_dir="./eval_data/images/" \
--input_images_list="./eval_data/eval_list\" \ --input_images_list="./eval_data/eval_list"
``` ```
执行`python train.py --help`可查看参数详细说明。 执行`python train.py --help`可查看参数详细说明。
...@@ -170,7 +170,7 @@ env CUDA_VISIBLE_DEVICE=0 python eval.py \ ...@@ -170,7 +170,7 @@ env CUDA_VISIBLE_DEVICE=0 python eval.py \
从标准输入读取一张图片的路径,并对齐进行预测: 从标准输入读取一张图片的路径,并对齐进行预测:
``` ```
env CUDA_VISIBLE_DEVICE=0 python infer.py \ env CUDA_VISIBLE_DEVICES=0 python infer.py \
--model_path="models/model_00044_15000" --model_path="models/model_00044_15000"
``` ```
...@@ -193,7 +193,7 @@ result: [2067 2067 8187 8477 5027 7191 2431 1462] ...@@ -193,7 +193,7 @@ result: [2067 2067 8187 8477 5027 7191 2431 1462]
从文件中批量读取图片路径,并对其进行预测: 从文件中批量读取图片路径,并对其进行预测:
``` ```
env CUDA_VISIBLE_DEVICE=0 python infer.py \ env CUDA_VISIBLE_DEVICES=0 python infer.py \
--model_path="models/model_00044_15000" \ --model_path="models/model_00044_15000" \
--input_images_list="data/test.list" --input_images_list="data/test.list"
``` ```
...@@ -204,3 +204,5 @@ env CUDA_VISIBLE_DEVICE=0 python infer.py \ ...@@ -204,3 +204,5 @@ env CUDA_VISIBLE_DEVICE=0 python infer.py \
|- |:-: | |- |:-: |
|[ocr_ctc_params](https://paddle-ocr-models.bj.bcebos.com/ocr_ctc.zip) | 22.3% | |[ocr_ctc_params](https://paddle-ocr-models.bj.bcebos.com/ocr_ctc.zip) | 22.3% |
|[ocr_attention_params](https://paddle-ocr-models.bj.bcebos.com/ocr_attention.zip) | 15.8%| |[ocr_attention_params](https://paddle-ocr-models.bj.bcebos.com/ocr_attention.zip) | 15.8%|
>在本文示例中,均可通过修改`CUDA_VISIBLE_DEVICES`改变当前任务使用的显卡号。
...@@ -339,7 +339,7 @@ def attention_infer(images, num_classes, use_cudnn=True): ...@@ -339,7 +339,7 @@ def attention_infer(images, num_classes, use_cudnn=True):
return ids return ids
def attention_eval(data_shape, num_classes): def attention_eval(data_shape, num_classes, use_cudnn=True):
images = fluid.layers.data(name='pixel', shape=data_shape, dtype='float32') images = fluid.layers.data(name='pixel', shape=data_shape, dtype='float32')
label_in = fluid.layers.data( label_in = fluid.layers.data(
name='label_in', shape=[1], dtype='int32', lod_level=1) name='label_in', shape=[1], dtype='int32', lod_level=1)
...@@ -349,7 +349,7 @@ def attention_eval(data_shape, num_classes): ...@@ -349,7 +349,7 @@ def attention_eval(data_shape, num_classes):
label_in = fluid.layers.cast(x=label_in, dtype='int64') label_in = fluid.layers.cast(x=label_in, dtype='int64')
gru_backward, encoded_vector, encoded_proj = encoder_net( gru_backward, encoded_vector, encoded_proj = encoder_net(
images, is_test=True) images, is_test=True, use_cudnn=use_cudnn)
backward_first = fluid.layers.sequence_pool( backward_first = fluid.layers.sequence_pool(
input=gru_backward, pool_type='first') input=gru_backward, pool_type='first')
......
...@@ -213,12 +213,12 @@ def ctc_train_net(args, data_shape, num_classes): ...@@ -213,12 +213,12 @@ def ctc_train_net(args, data_shape, num_classes):
return sum_cost, error_evaluator, inference_program, model_average return sum_cost, error_evaluator, inference_program, model_average
def ctc_infer(images, num_classes, use_cudnn): def ctc_infer(images, num_classes, use_cudnn=True):
fc_out = encoder_net(images, num_classes, is_test=True, use_cudnn=use_cudnn) fc_out = encoder_net(images, num_classes, is_test=True, use_cudnn=use_cudnn)
return fluid.layers.ctc_greedy_decoder(input=fc_out, blank=num_classes) return fluid.layers.ctc_greedy_decoder(input=fc_out, blank=num_classes)
def ctc_eval(data_shape, num_classes, use_cudnn): def ctc_eval(data_shape, num_classes, use_cudnn=True):
images = fluid.layers.data(name='pixel', shape=data_shape, dtype='float32') images = fluid.layers.data(name='pixel', shape=data_shape, dtype='float32')
label = fluid.layers.data( label = fluid.layers.data(
name='label', shape=[1], dtype='int32', lod_level=1) name='label', shape=[1], dtype='int32', lod_level=1)
......
...@@ -10,6 +10,11 @@ from os import path ...@@ -10,6 +10,11 @@ from os import path
from paddle.dataset.image import load_image from paddle.dataset.image import load_image
import paddle import paddle
try:
input = raw_input
except NameError:
pass
SOS = 0 SOS = 0
EOS = 1 EOS = 1
NUM_CLASSES = 95 NUM_CLASSES = 95
...@@ -175,7 +180,7 @@ class DataGenerator(object): ...@@ -175,7 +180,7 @@ class DataGenerator(object):
yield img, label yield img, label
else: else:
while True: while True:
img_path = raw_input("Please input the path of image: ") img_path = input("Please input the path of image: ")
img = Image.open(img_path).convert('L') img = Image.open(img_path).convert('L')
img = np.array(img) - 127.5 img = np.array(img) - 127.5
img = img[np.newaxis, ...] img = img[np.newaxis, ...]
......
...@@ -31,7 +31,8 @@ def evaluate(args): ...@@ -31,7 +31,8 @@ def evaluate(args):
num_classes = data_reader.num_classes() num_classes = data_reader.num_classes()
data_shape = data_reader.data_shape() data_shape = data_reader.data_shape()
# define network # define network
evaluator, cost = eval(data_shape, num_classes) evaluator, cost = eval(
data_shape, num_classes, use_cudnn=True if args.use_gpu else False)
# data reader # data reader
test_reader = data_reader.test( test_reader = data_reader.test(
...@@ -62,8 +63,8 @@ def evaluate(args): ...@@ -62,8 +63,8 @@ def evaluate(args):
count += 1 count += 1
exe.run(fluid.default_main_program(), feed=get_feeder_data(data, place)) exe.run(fluid.default_main_program(), feed=get_feeder_data(data, place))
avg_distance, avg_seq_error = evaluator.eval(exe) avg_distance, avg_seq_error = evaluator.eval(exe)
print("Read %d samples; avg_distance: %s; avg_seq_error: %s" % ( print("Read %d samples; avg_distance: %s; avg_seq_error: %s" %
count, avg_distance, avg_seq_error)) (count, avg_distance, avg_seq_error))
def main(): def main():
......
...@@ -20,6 +20,7 @@ strategies: ...@@ -20,6 +20,7 @@ strategies:
metric_name: 'acc_top1' metric_name: 'acc_top1'
compressor: compressor:
epoch: 200 epoch: 200
#init_model: './checkpoints/0' # Please enable this option for loading checkpoint.
checkpoint_path: './checkpoints/' checkpoint_path: './checkpoints/'
strategies: strategies:
- sensitive_pruning_strategy - sensitive_pruning_strategy
...@@ -16,6 +16,7 @@ strategies: ...@@ -16,6 +16,7 @@ strategies:
metric_name: 'acc_top1' metric_name: 'acc_top1'
compressor: compressor:
epoch: 200 epoch: 200
#init_model: './checkpoints/0' # Please enable this option for loading checkpoint.
checkpoint_path: './checkpoints/' checkpoint_path: './checkpoints/'
strategies: strategies:
- uniform_pruning_strategy - uniform_pruning_strategy
...@@ -30,6 +30,7 @@ strategies: ...@@ -30,6 +30,7 @@ strategies:
activation_quantize_type: 'abs_max' activation_quantize_type: 'abs_max'
compressor: compressor:
epoch: 142 epoch: 142
#init_model: './checkpoints/0' # Please enable this option for loading checkpoint.
checkpoint_path: './checkpoints/' checkpoint_path: './checkpoints/'
strategies: strategies:
- uniform_pruning_strategy - uniform_pruning_strategy
......
...@@ -24,6 +24,10 @@ fi ...@@ -24,6 +24,10 @@ fi
cd - cd -
# enable GC strategy
export FLAGS_fast_eager_deletion_mode=1
export FLAGS_eager_delete_tensor_gb=0.0
# for distillation # for distillation
#----------------- #-----------------
export CUDA_VISIBLE_DEVICES=0,1,2,3 export CUDA_VISIBLE_DEVICES=0,1,2,3
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册