-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Add the demo script for inference #653
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 |
---|---|---|
@@ -0,0 +1,112 @@ | ||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
|
||
import os | ||
import argparse | ||
import paddle.v2.fluid as fluid | ||
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 | ||
|
||
|
||
def parse_args(): | ||
parser = argparse.ArgumentParser("Inference for stacked LSTMP model.") | ||
parser.add_argument( | ||
'--batch_size', | ||
type=int, | ||
default=32, | ||
help='The sequence number of a batch data. (default: %(default)d)') | ||
parser.add_argument( | ||
'--device', | ||
type=str, | ||
default='GPU', | ||
choices=['CPU', 'GPU'], | ||
help='The device type. (default: %(default)s)') | ||
parser.add_argument( | ||
'--mean_var', | ||
type=str, | ||
default='data/global_mean_var_search26kHr', | ||
help="The path for feature's global mean and variance. " | ||
"(default: %(default)s)") | ||
parser.add_argument( | ||
'--infer_feature_lst', | ||
type=str, | ||
default='data/infer_feature.lst', | ||
help='The feature list path for inference. (default: %(default)s)') | ||
parser.add_argument( | ||
'--infer_label_lst', | ||
type=str, | ||
default='data/infer_label.lst', | ||
help='The label list path for inference. (default: %(default)s)') | ||
parser.add_argument( | ||
'--model_save_path', | ||
type=str, | ||
default='./checkpoints/deep_asr.pass_0.model/', | ||
help='The directory for saving model. (default: %(default)s)') | ||
args = parser.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 split_infer_result(infer_seq, lod): | ||
infer_batch = [] | ||
for i in xrange(0, len(lod[0]) - 1): | ||
infer_batch.append(infer_seq[lod[0][i]:lod[0][i + 1]]) | ||
return infer_batch | ||
|
||
|
||
def infer(args): | ||
""" Gets one batch of feature data and predicts labels for each sample. | ||
""" | ||
|
||
if not os.path.exists(args.model_save_path): | ||
raise IOError("Invalid model path!") | ||
|
||
place = fluid.CUDAPlace(0) if args.device == 'GPU' else fluid.CPUPlace() | ||
exe = fluid.Executor(place) | ||
|
||
# load model | ||
[infer_program, feed_dict, | ||
fetch_targets] = fluid.io.load_inference_model(args.model_save_path, exe) | ||
|
||
ltrans = [ | ||
trans_add_delta.TransAddDelta(2, 2), | ||
trans_mean_variance_norm.TransMeanVarianceNorm(args.mean_var), | ||
trans_splice.TransSplice() | ||
] | ||
|
||
infer_data_reader = reader.DataReader(args.infer_feature_lst, | ||
args.infer_label_lst) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. It should be. |
||
infer_data_reader.set_transformers(ltrans) | ||
|
||
feature_t = fluid.LoDTensor() | ||
one_batch = infer_data_reader.batch_iterator(args.batch_size, 1).next() | ||
(features, labels, lod) = one_batch | ||
feature_t.set(features, place) | ||
feature_t.set_lod([lod]) | ||
|
||
results = exe.run(infer_program, | ||
feed={feed_dict[0]: feature_t}, | ||
fetch_list=fetch_targets, | ||
return_numpy=False) | ||
|
||
probs, lod = lodtensor_to_ndarray(results[0]) | ||
preds = probs.argmax(axis=1) | ||
infer_batch = split_infer_result(preds, lod) | ||
for index, sample in enumerate(infer_batch): | ||
print("result %d: " % index, sample, '\n') | ||
|
||
|
||
if __name__ == '__main__': | ||
args = parse_args() | ||
print_arguments(args) | ||
infer(args) |
This file contains 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
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.
Better to replace args with specified arguments. Otherwise, this function is not well self-explained.
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.
It is not necessary for this function only used in this file.