-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Reduce the config parsing codes (-360 Lines) for DS2 and make it looks cleaner. #227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
274899d
Reduce the config parsing codes for DS2 and make it looks cleaner.
xinghai-sun 0e7e07e
Rename ctc_best_path_decoder to ctc_greedy_decoder in unitest.
xinghai-sun 3e02e86
Sort the config lines to make it look better.
xinghai-sun b890f57
Re-style the config codes for tools in DS2.
xinghai-sun 8cc0cd8
Add back utils.py.
xinghai-sun 2ad7128
Update argument naming following Yibing's reviews.
xinghai-sun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,118 +9,67 @@ | |
| import struct | ||
| import wave | ||
| import paddle.v2 as paddle | ||
| from utils import print_arguments | ||
| from data_utils.data import DataGenerator | ||
| from model import DeepSpeech2Model | ||
| from data_utils.utils import read_manifest | ||
|
|
||
|
|
||
| def add_arg(argname, type, default, help, **kwargs): | ||
| type = distutils.util.strtobool if type == bool else type | ||
| parser.add_argument( | ||
| "--" + argname, | ||
| default=default, | ||
| type=type, | ||
| help=help + ' Default: %(default)s.', | ||
| **kwargs) | ||
|
|
||
|
|
||
| # yapf: disable | ||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| parser.add_argument( | ||
| "--host_ip", | ||
| default="localhost", | ||
| type=str, | ||
| help="Server IP address. (default: %(default)s)") | ||
| parser.add_argument( | ||
| "--host_port", | ||
| default=8086, | ||
| type=int, | ||
| help="Server Port. (default: %(default)s)") | ||
| parser.add_argument( | ||
| "--speech_save_dir", | ||
| default="demo_cache", | ||
| type=str, | ||
| help="Directory for saving demo speech. (default: %(default)s)") | ||
| parser.add_argument( | ||
| "--vocab_filepath", | ||
| default='datasets/vocab/eng_vocab.txt', | ||
| type=str, | ||
| help="Vocabulary filepath. (default: %(default)s)") | ||
| parser.add_argument( | ||
| "--mean_std_filepath", | ||
| default='mean_std.npz', | ||
| type=str, | ||
| help="Manifest path for normalizer. (default: %(default)s)") | ||
| parser.add_argument( | ||
| "--warmup_manifest_path", | ||
| default='datasets/manifest.test', | ||
| type=str, | ||
| help="Manifest path for warmup test. (default: %(default)s)") | ||
| parser.add_argument( | ||
| "--specgram_type", | ||
| default='linear', | ||
| type=str, | ||
| help="Feature type of audio data: 'linear' (power spectrum)" | ||
| " or 'mfcc'. (default: %(default)s)") | ||
| parser.add_argument( | ||
| "--num_conv_layers", | ||
| default=2, | ||
| type=int, | ||
| help="Convolution layer number. (default: %(default)s)") | ||
| parser.add_argument( | ||
| "--num_rnn_layers", | ||
| default=3, | ||
| type=int, | ||
| help="RNN layer number. (default: %(default)s)") | ||
| parser.add_argument( | ||
| "--rnn_layer_size", | ||
| default=2048, | ||
| type=int, | ||
| help="RNN layer cell number. (default: %(default)s)") | ||
| parser.add_argument( | ||
| "--share_rnn_weights", | ||
| default=True, | ||
| type=distutils.util.strtobool, | ||
| help="Whether to share input-hidden weights between forword and backward " | ||
| "directional simple RNNs. Only available when use_gru=False. " | ||
| "(default: %(default)s)") | ||
| parser.add_argument( | ||
| "--use_gru", | ||
| default=False, | ||
| type=distutils.util.strtobool, | ||
| help="Use GRU or simple RNN. (default: %(default)s)") | ||
| parser.add_argument( | ||
| "--use_gpu", | ||
| default=True, | ||
| type=distutils.util.strtobool, | ||
| help="Use gpu or not. (default: %(default)s)") | ||
| parser.add_argument( | ||
| "--model_filepath", | ||
| default='checkpoints/params.latest.tar.gz', | ||
| type=str, | ||
| help="Model filepath. (default: %(default)s)") | ||
| parser.add_argument( | ||
| "--decode_method", | ||
| default='beam_search', | ||
| type=str, | ||
| help="Method for ctc decoding: best_path or beam_search. " | ||
| "(default: %(default)s)") | ||
| parser.add_argument( | ||
| "--beam_size", | ||
| default=100, | ||
| type=int, | ||
| help="Width for beam search decoding. (default: %(default)d)") | ||
| parser.add_argument( | ||
| "--language_model_path", | ||
| default="lm/data/common_crawl_00.prune01111.trie.klm", | ||
| type=str, | ||
| help="Path for language model. (default: %(default)s)") | ||
| parser.add_argument( | ||
| "--alpha", | ||
| default=0.36, | ||
| type=float, | ||
| help="Parameter associated with language model. (default: %(default)f)") | ||
| parser.add_argument( | ||
| "--beta", | ||
| default=0.25, | ||
| type=float, | ||
| help="Parameter associated with word count. (default: %(default)f)") | ||
| parser.add_argument( | ||
| "--cutoff_prob", | ||
| default=0.99, | ||
| type=float, | ||
| help="The cutoff probability of pruning" | ||
| "in beam search. (default: %(default)f)") | ||
| add_arg('host_port', int, 8086, "Server's IP port.") | ||
| add_arg('beam_size', int, 500, "Beam search width.") | ||
| add_arg('num_conv_layers', int, 2, "# of convolution layers.") | ||
| add_arg('num_rnn_layers', int, 3, "# of recurrent layers.") | ||
| add_arg('rnn_layer_size', int, 2048, "# of recurrent cells per layer.") | ||
| add_arg('alpha', float, 0.36, "Coef of LM for beam search.") | ||
| add_arg('beta', float, 0.25, "Coef of WC for beam search.") | ||
| add_arg('cutoff_prob', float, 0.99, "Cutoff probability for pruning.") | ||
| add_arg('use_gru', bool, False, "Use GRUs instead of Simple RNNs.") | ||
|
||
| add_arg('use_gpu', bool, True, "Use GPU or not.") | ||
| add_arg('share_rnn_weights',bool, True, "Share input-hidden weights across " | ||
| "bi-directional RNNs. Not for GRU.") | ||
| add_arg('host_ip', str, | ||
| 'localhost', | ||
| "Server's IP address.") | ||
| add_arg('speech_save_dir', str, | ||
| 'demo_cache', | ||
| "Directory to save demo audios.") | ||
| add_arg('warmup_manifest', str, | ||
| 'datasets/manifest.test', | ||
| "Filepath of manifest to warm up.") | ||
| add_arg('mean_std_path', str, | ||
| 'mean_std.npz', | ||
| "Filepath of normalizer's mean & std.") | ||
| add_arg('vocab_path', str, | ||
| 'datasets/vocab/eng_vocab.txt', | ||
| "Filepath of vocabulary.") | ||
| add_arg('model_path', str, | ||
| './checkpoints/params.latest.tar.gz', | ||
| "If None, the training starts from scratch, " | ||
| "otherwise, it resumes from the pre-trained model.") | ||
| add_arg('lang_model_path', str, | ||
| 'lm/data/common_crawl_00.prune01111.trie.klm', | ||
| "Filepath for language model.") | ||
| add_arg('decoder_method', str, | ||
| 'ctc_beam_search', | ||
| "Decoder method. Options: ctc_beam_search, ctc_greedy", | ||
| choices = ['ctc_beam_search', 'ctc_greedy']) | ||
| add_arg('specgram_type', str, | ||
| 'linear', | ||
| "Audio feature type. Options: linear, mfcc.", | ||
| choices=['linear', 'mfcc']) | ||
| args = parser.parse_args() | ||
| # yapf: disable | ||
|
|
||
|
|
||
| class AsrTCPServer(SocketServer.TCPServer): | ||
|
|
@@ -200,8 +149,8 @@ def start_server(): | |
| """Start the ASR server""" | ||
| # prepare data generator | ||
| data_generator = DataGenerator( | ||
| vocab_filepath=args.vocab_filepath, | ||
| mean_std_filepath=args.mean_std_filepath, | ||
| vocab_filepath=args.vocab_path, | ||
| mean_std_filepath=args.mean_std_path, | ||
| augmentation_config='{}', | ||
| specgram_type=args.specgram_type, | ||
| num_threads=1) | ||
|
|
@@ -212,21 +161,21 @@ def start_server(): | |
| num_rnn_layers=args.num_rnn_layers, | ||
| rnn_layer_size=args.rnn_layer_size, | ||
| use_gru=args.use_gru, | ||
| pretrained_model_path=args.model_filepath, | ||
| pretrained_model_path=args.model_path, | ||
| share_rnn_weights=args.share_rnn_weights) | ||
|
|
||
| # prepare ASR inference handler | ||
| def file_to_transcript(filename): | ||
| feature = data_generator.process_utterance(filename, "") | ||
| result_transcript = ds2_model.infer_batch( | ||
| infer_data=[feature], | ||
| decode_method=args.decode_method, | ||
| decoder_method=args.decoder_method, | ||
| beam_alpha=args.alpha, | ||
| beam_beta=args.beta, | ||
| beam_size=args.beam_size, | ||
| cutoff_prob=args.cutoff_prob, | ||
| vocab_list=data_generator.vocab_list, | ||
| language_model_path=args.language_model_path, | ||
| language_model_path=args.lang_model_path, | ||
| num_processes=1) | ||
| return result_transcript[0] | ||
|
|
||
|
|
@@ -235,7 +184,7 @@ def file_to_transcript(filename): | |
| print('Warming up ...') | ||
| warm_up_test( | ||
| audio_process_handler=file_to_transcript, | ||
| manifest_path=args.warmup_manifest_path, | ||
| manifest_path=args.warmup_manifest, | ||
| num_test_cases=3) | ||
| print('-----------------------------------------------------------') | ||
|
|
||
|
|
@@ -249,6 +198,13 @@ def file_to_transcript(filename): | |
| server.serve_forever() | ||
|
|
||
|
|
||
| def print_arguments(args): | ||
| print("----------- Configuration Arguments -----------") | ||
| for arg, value in sorted(vars(args).iteritems()): | ||
| print("%s: %s" % (arg, value)) | ||
| print("------------------------------------------------") | ||
|
|
||
|
|
||
| def main(): | ||
| print_arguments(args) | ||
| paddle.init(use_gpu=args.use_gpu, trainer_count=1) | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move down or out this function?