diff --git a/fluid/DeepASR/model.py b/fluid/DeepASR/model.py index 470c3fcb62826e9032b5c5cddac01e1055f87bfd..6eacae33bc2edae401103d91705f6981b18ad89a 100644 --- a/fluid/DeepASR/model.py +++ b/fluid/DeepASR/model.py @@ -9,11 +9,40 @@ import paddle.v2.fluid as fluid def stacked_lstmp_model(hidden_dim, proj_dim, stacked_num, - class_num=1749, - is_train=True): + parallel=False, + is_train=True, + class_num=1749): feature = fluid.layers.data( name="feature", shape=[-1, 120 * 11], dtype="float32", lod_level=1) + label = fluid.layers.data( + name="label", shape=[-1, 1], dtype="int64", lod_level=1) + + if parallel: + places = fluid.layers.get_places() + pd = fluid.layers.ParallelDo(places) + with pd.do(): + feat_ = pd.read_input(feature) + label_ = pd.read_input(label) + prediction, avg_cost, acc = _net_conf(feat_, label_, hidden_dim, + proj_dim, stacked_num, + class_num, is_train) + for out in [avg_cost, acc]: + pd.write_output(out) + + # get mean loss and acc through every devices. + avg_cost, acc = pd() + avg_cost = fluid.layers.mean(x=avg_cost) + acc = fluid.layers.mean(x=acc) + else: + prediction, avg_cost, acc = _net_conf(feature, label, hidden_dim, + proj_dim, stacked_num, class_num, + is_train) + + return prediction, avg_cost, acc + +def _net_conf(feature, label, hidden_dim, proj_dim, stacked_num, class_num, + is_train): seq_conv1 = fluid.layers.sequence_conv( input=feature, num_filters=1024, @@ -57,9 +86,7 @@ def stacked_lstmp_model(hidden_dim, if not is_train: return feature, prediction - label = fluid.layers.data( - name="label", shape=[-1, 1], dtype="int64", lod_level=1) cost = fluid.layers.cross_entropy(input=prediction, label=label) avg_cost = fluid.layers.mean(x=cost) - - return prediction, label, avg_cost + acc = fluid.layers.accuracy(input=prediction, label=label) + return prediction, avg_cost, acc diff --git a/fluid/DeepASR/profile.py b/fluid/DeepASR/profile.py index 694a0464d7649816ddfed9f35479f458eec1bc00..9fe4d132c146330d922c5747f24ea2bf4291a042 100644 --- a/fluid/DeepASR/profile.py +++ b/fluid/DeepASR/profile.py @@ -10,16 +10,16 @@ import time import paddle.v2 as paddle import paddle.v2.fluid as fluid import paddle.v2.fluid.profiler as profiler -import data_utils.trans_mean_variance_norm as trans_mean_variance_norm -import data_utils.trans_add_delta as trans_add_delta -import data_utils.trans_splice as trans_splice +import data_utils.augmentor.trans_mean_variance_norm as trans_mean_variance_norm +import data_utils.augmentor.trans_add_delta as trans_add_delta +import data_utils.augmentor.trans_splice as trans_splice import data_utils.data_reader as reader from model import stacked_lstmp_model -from utils import print_arguments, lodtensor_to_ndarray +from data_utils.util import lodtensor_to_ndarray def parse_args(): - parser = argparse.ArgumentParser("LSTM model benchmark.") + parser = argparse.ArgumentParser("Profiling for stacked LSTMP model.") parser.add_argument( '--batch_size', type=int, @@ -51,6 +51,8 @@ def parse_args(): default='GPU', choices=['CPU', 'GPU'], help='The device type. (default: %(default)s)') + parser.add_argument( + '--parallel', action='store_true', help='If set, run in parallel.') parser.add_argument( '--mean_var', type=str, @@ -91,6 +93,13 @@ def parse_args(): return args +def print_arguments(args): + print('----------- Configuration Arguments -----------') + for arg, value in sorted(vars(args).iteritems()): + print('%s: %s' % (arg, value)) + print('------------------------------------------------') + + def profile(args): """profile the training process""" @@ -100,14 +109,12 @@ def profile(args): if not args.num_batch_to_skip >= 0: raise ValueError("arg 'num_batch_to_skip' must not be smaller than 0.") - prediction, label, avg_cost = stacked_lstmp_model( - args.hidden_dim, args.proj_dim, args.stacked_num) + prediction, avg_cost, accuracy = stacked_lstmp_model( + args.hidden_dim, args.proj_dim, args.stacked_num, args.parallel) adam_optimizer = fluid.optimizer.Adam(learning_rate=args.learning_rate) adam_optimizer.minimize(avg_cost) - accuracy = fluid.evaluator.Accuracy(input=prediction, label=label) - place = fluid.CPUPlace() if args.device == 'CPU' else fluid.CUDAPlace(0) exe = fluid.Executor(place) exe.run(fluid.default_startup_program()) @@ -127,7 +134,6 @@ def profile(args): sorted_key = None if args.sorted_key is 'None' else args.sorted_key with profiler.profiler(args.device, sorted_key) as prof: frames_seen, start_time = 0, 0.0 - accuracy.reset(exe) for batch_id in range(0, args.max_batch_num): if args.num_batch_to_skip == batch_id: profiler.reset_profiler() @@ -148,7 +154,7 @@ def profile(args): outs = exe.run(fluid.default_main_program(), feed={"feature": res_feature, "label": res_label}, - fetch_list=[avg_cost] + accuracy.metrics, + fetch_list=[avg_cost, accuracy], return_numpy=False) if args.print_train_acc: diff --git a/fluid/DeepASR/train.py b/fluid/DeepASR/train.py index 171723ac30827eef342d500262ac93e9e4562b12..f580923e15fbd4f9e386cddb34f2a333b5f97808 100644 --- a/fluid/DeepASR/train.py +++ b/fluid/DeepASR/train.py @@ -10,16 +10,16 @@ import time import paddle.v2 as paddle import paddle.v2.fluid as fluid import paddle.v2.fluid.profiler as profiler -import data_utils.trans_mean_variance_norm as trans_mean_variance_norm -import data_utils.trans_add_delta as trans_add_delta -import data_utils.trans_splice as trans_splice +import data_utils.augmentor.trans_mean_variance_norm as trans_mean_variance_norm +import data_utils.augmentor.trans_add_delta as trans_add_delta +import data_utils.augmentor.trans_splice as trans_splice import data_utils.data_reader as reader from model import stacked_lstmp_model -from utils import print_arguments, lodtensor_to_ndarray +from data_utils.util import lodtensor_to_ndarray def parse_args(): - parser = argparse.ArgumentParser("LSTM model benchmark.") + parser = argparse.ArgumentParser("Training for stacked LSTMP model.") parser.add_argument( '--batch_size', type=int, @@ -61,6 +61,8 @@ def parse_args(): default='GPU', choices=['CPU', 'GPU'], help='The device type. (default: %(default)s)') + parser.add_argument( + '--parallel', action='store_true', help='If set, run in parallel.') parser.add_argument( '--mean_var', type=str, @@ -80,24 +82,22 @@ def parse_args(): return args +def print_arguments(args): + print('----------- Configuration Arguments -----------') + for arg, value in sorted(vars(args).iteritems()): + print('%s: %s' % (arg, value)) + print('------------------------------------------------') + + def train(args): """train in loop.""" - prediction, label, avg_cost = stacked_lstmp_model( - args.hidden_dim, args.proj_dim, args.stacked_num) + prediction, avg_cost, accuracy = stacked_lstmp_model( + args.hidden_dim, args.proj_dim, args.stacked_num, args.parallel) adam_optimizer = fluid.optimizer.Adam(learning_rate=args.learning_rate) adam_optimizer.minimize(avg_cost) - accuracy = fluid.evaluator.Accuracy(input=prediction, label=label) - - # clone from default main program - inference_program = fluid.default_main_program().clone() - with fluid.program_guard(inference_program): - test_accuracy = fluid.evaluator.Accuracy(input=prediction, label=label) - test_target = [avg_cost] + test_accuracy.metrics + test_accuracy.states - inference_program = fluid.io.get_inference_program(test_target) - place = fluid.CPUPlace() if args.device == 'CPU' else fluid.CUDAPlace(0) exe = fluid.Executor(place) exe.run(fluid.default_startup_program()) @@ -115,7 +115,6 @@ def train(args): res_label = fluid.LoDTensor() for pass_id in xrange(args.pass_num): pass_start_time = time.time() - accuracy.reset(exe) batch_id = 0 while True: # load_data @@ -132,7 +131,7 @@ def train(args): _, acc = exe.run(fluid.default_main_program(), feed={"feature": res_feature, "label": res_label}, - fetch_list=[avg_cost] + accuracy.metrics, + fetch_list=[avg_cost, accuracy], return_numpy=False) if batch_id > 0 and (batch_id % args.print_per_batches == 0):