Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
c9b41eef
M
models
项目概览
PaddlePaddle
/
models
大约 1 年 前同步成功
通知
222
Star
6828
Fork
2962
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
602
列表
看板
标记
里程碑
合并请求
255
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
models
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
602
Issue
602
列表
看板
标记
里程碑
合并请求
255
合并请求
255
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
c9b41eef
编写于
2月 08, 2018
作者:
Y
Yibing Liu
提交者:
GitHub
2月 08, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #653 from kuke/add_infer
Add the demo script for inference
上级
e8266790
b8fae8a6
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
121 addition
and
10 deletion
+121
-10
fluid/DeepASR/infer.py
fluid/DeepASR/infer.py
+112
-0
fluid/DeepASR/train.py
fluid/DeepASR/train.py
+9
-10
未找到文件。
fluid/DeepASR/infer.py
0 → 100644
浏览文件 @
c9b41eef
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
)
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
)
fluid/DeepASR/train.py
浏览文件 @
c9b41eef
...
@@ -72,33 +72,34 @@ def parse_args():
...
@@ -72,33 +72,34 @@ def parse_args():
'--mean_var'
,
'--mean_var'
,
type
=
str
,
type
=
str
,
default
=
'data/global_mean_var_search26kHr'
,
default
=
'data/global_mean_var_search26kHr'
,
help
=
'mean var path'
)
help
=
"The path for feature's global mean and variance. "
"(default: %(default)s)"
)
parser
.
add_argument
(
parser
.
add_argument
(
'--train_feature_lst'
,
'--train_feature_lst'
,
type
=
str
,
type
=
str
,
default
=
'data/feature.lst'
,
default
=
'data/feature.lst'
,
help
=
'
feature list path for training.
'
)
help
=
'
The feature list path for training. (default: %(default)s)
'
)
parser
.
add_argument
(
parser
.
add_argument
(
'--train_label_lst'
,
'--train_label_lst'
,
type
=
str
,
type
=
str
,
default
=
'data/label.lst'
,
default
=
'data/label.lst'
,
help
=
'
label list path for training.
'
)
help
=
'
The label list path for training. (default: %(default)s)
'
)
parser
.
add_argument
(
parser
.
add_argument
(
'--val_feature_lst'
,
'--val_feature_lst'
,
type
=
str
,
type
=
str
,
default
=
'data/val_feature.lst'
,
default
=
'data/val_feature.lst'
,
help
=
'
feature list path for validation.
'
)
help
=
'
The feature list path for validation. (default: %(default)s)
'
)
parser
.
add_argument
(
parser
.
add_argument
(
'--val_label_lst'
,
'--val_label_lst'
,
type
=
str
,
type
=
str
,
default
=
'data/val_label.lst'
,
default
=
'data/val_label.lst'
,
help
=
'
label list path for validation.
'
)
help
=
'
The label list path for validation. (default: %(default)s)
'
)
parser
.
add_argument
(
parser
.
add_argument
(
'--model_save_dir'
,
'--model_save_dir'
,
type
=
str
,
type
=
str
,
default
=
'./checkpoints'
,
default
=
'./checkpoints'
,
help
=
'directory to save model. Do not save model if set to '
help
=
"The directory for saving model. Do not save model if set to "
'.'
)
"''. (default: %(default)s)"
)
args
=
parser
.
parse_args
()
args
=
parser
.
parse_args
()
return
args
return
args
...
@@ -114,8 +115,6 @@ def train(args):
...
@@ -114,8 +115,6 @@ def train(args):
"""train in loop.
"""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
(
prediction
,
avg_cost
,
accuracy
=
stacked_lstmp_model
(
hidden_dim
=
args
.
hidden_dim
,
hidden_dim
=
args
.
hidden_dim
,
proj_dim
=
args
.
proj_dim
,
proj_dim
=
args
.
proj_dim
,
...
@@ -206,7 +205,7 @@ def train(args):
...
@@ -206,7 +205,7 @@ def train(args):
sys
.
stdout
.
flush
()
sys
.
stdout
.
flush
()
# run test
# run test
val_cost
,
val_acc
=
test
(
exe
)
val_cost
,
val_acc
=
test
(
exe
)
# save model
# save model
if
args
.
model_save_dir
!=
''
:
if
args
.
model_save_dir
!=
''
:
model_path
=
os
.
path
.
join
(
model_path
=
os
.
path
.
join
(
args
.
model_save_dir
,
"deep_asr.pass_"
+
str
(
pass_id
)
+
".model"
)
args
.
model_save_dir
,
"deep_asr.pass_"
+
str
(
pass_id
)
+
".model"
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录