From d6d819c26e4690e7189e6673158077aefc6ad123 Mon Sep 17 00:00:00 2001 From: Yibing Liu Date: Wed, 7 Feb 2018 07:04:22 +0000 Subject: [PATCH] Adjust dir struct and fix bug in data_reader --- fluid/DeepASR/data_utils/data_reader.py | 4 ++-- fluid/DeepASR/{ => model_utils}/model.py | 8 ++++++-- fluid/DeepASR/tools/_init_paths.py | 19 +++++++++++++++++++ fluid/DeepASR/{ => tools}/profile.py | 13 ++++++++----- fluid/DeepASR/train.py | 12 ++++++++---- 5 files changed, 43 insertions(+), 13 deletions(-) rename fluid/DeepASR/{ => model_utils}/model.py (92%) create mode 100644 fluid/DeepASR/tools/_init_paths.py rename fluid/DeepASR/{ => tools}/profile.py (95%) diff --git a/fluid/DeepASR/data_utils/data_reader.py b/fluid/DeepASR/data_utils/data_reader.py index b08e7f55..f7bc9c66 100644 --- a/fluid/DeepASR/data_utils/data_reader.py +++ b/fluid/DeepASR/data_utils/data_reader.py @@ -225,8 +225,8 @@ class DataReader(object): @suppress_complaints(verbose=self._verbose) def ordered_processing_task(sample_info_queue, sample_queue, out_order): if self._verbose == 0: - signal.signal(signal.SIGTERM, suppress_signal()) - signal.signal(signal.SIGINT, suppress_signal()) + signal.signal(signal.SIGTERM, suppress_signal) + signal.signal(signal.SIGINT, suppress_signal) def read_bytes(fpath, start, size): f = open(fpath, 'r') diff --git a/fluid/DeepASR/model.py b/fluid/DeepASR/model_utils/model.py similarity index 92% rename from fluid/DeepASR/model.py rename to fluid/DeepASR/model_utils/model.py index 226a06b0..4e29394a 100644 --- a/fluid/DeepASR/model.py +++ b/fluid/DeepASR/model_utils/model.py @@ -9,12 +9,16 @@ import paddle.v2.fluid as fluid def stacked_lstmp_model(hidden_dim, proj_dim, stacked_num, + class_num, parallel=False, - is_train=True, - class_num=1749): + is_train=True): """ The model for DeepASR. The main structure is composed of stacked identical LSTMP (LSTM with recurrent projection) layers. + When running in training and validation phase, the feeding dictionary + is {'feature', 'label'}, fed by the LodTensor for feature data and + label data respectively. And in inference, only `feature` is needed. + Args: hidden_dim(int): The hidden state's dimension of the LSTMP layer. proj_dim(int): The projection size of the LSTMP layer. diff --git a/fluid/DeepASR/tools/_init_paths.py b/fluid/DeepASR/tools/_init_paths.py new file mode 100644 index 00000000..228dbae6 --- /dev/null +++ b/fluid/DeepASR/tools/_init_paths.py @@ -0,0 +1,19 @@ +"""Add the parent directory to $PYTHONPATH""" +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import os.path +import sys + + +def add_path(path): + if path not in sys.path: + sys.path.insert(0, path) + + +this_dir = os.path.dirname(__file__) + +# Add project path to PYTHONPATH +proj_path = os.path.join(this_dir, '..') +add_path(proj_path) diff --git a/fluid/DeepASR/profile.py b/fluid/DeepASR/tools/profile.py similarity index 95% rename from fluid/DeepASR/profile.py rename to fluid/DeepASR/tools/profile.py index 9f9cce04..a45ce98a 100644 --- a/fluid/DeepASR/profile.py +++ b/fluid/DeepASR/tools/profile.py @@ -7,14 +7,14 @@ import numpy as np import argparse import time -import paddle.v2 as paddle import paddle.v2.fluid as fluid import paddle.v2.fluid.profiler as profiler +import _init_paths 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 model_utils.model import stacked_lstmp_model from data_utils.util import lodtensor_to_ndarray @@ -118,8 +118,12 @@ def profile(args): raise ValueError( "arg 'first_batches_to_skip' must not be smaller than 0.") - _, avg_cost, accuracy = stacked_lstmp_model(args.hidden_dim, args.proj_dim, - args.stacked_num, args.parallel) + _, avg_cost, accuracy = stacked_lstmp_model( + hidden_dim=args.hidden_dim, + proj_dim=args.proj_dim, + stacked_num=args.stacked_num, + class_num=1749, + parallel=args.parallel) adam_optimizer = fluid.optimizer.Adam(learning_rate=args.learning_rate) adam_optimizer.minimize(avg_cost) @@ -173,7 +177,6 @@ def profile(args): else: sys.stdout.write('.') sys.stdout.flush() - time_consumed = time.time() - start_time frames_per_sec = frames_seen / time_consumed print("\nTime consumed: %f s, performance: %f frames/s." % diff --git a/fluid/DeepASR/train.py b/fluid/DeepASR/train.py index b2584259..1c45f0a0 100644 --- a/fluid/DeepASR/train.py +++ b/fluid/DeepASR/train.py @@ -8,15 +8,13 @@ import numpy as np import argparse import time -import paddle.v2 as paddle import paddle.v2.fluid as fluid -import paddle.v2.fluid.profiler as profiler 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 data_utils.util import lodtensor_to_ndarray -from model import stacked_lstmp_model +from model_utils.model import stacked_lstmp_model def parse_args(): @@ -116,8 +114,14 @@ def train(args): """train in loop. """ + # prediction, avg_cost, accuracy = stacked_lstmp_model(args.hidden_dim, + # args.proj_dim, args.stacked_num, class_num=1749, args.parallel) prediction, avg_cost, accuracy = stacked_lstmp_model( - args.hidden_dim, args.proj_dim, args.stacked_num, args.parallel) + hidden_dim=args.hidden_dim, + proj_dim=args.proj_dim, + stacked_num=args.stacked_num, + class_num=1749, + parallel=args.parallel) adam_optimizer = fluid.optimizer.Adam(learning_rate=args.learning_rate) adam_optimizer.minimize(avg_cost) -- GitLab