Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
0ba71d92
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看板
提交
0ba71d92
编写于
3月 13, 2018
作者:
Y
Yibing Liu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add script for inference by using checkpoint
上级
c16058ea
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
161 addition
and
5 deletion
+161
-5
fluid/DeepASR/infer_from_ckpt.py
fluid/DeepASR/infer_from_ckpt.py
+158
-0
fluid/DeepASR/train.py
fluid/DeepASR/train.py
+3
-5
未找到文件。
fluid/DeepASR/infer_from_ckpt.py
0 → 100644
浏览文件 @
0ba71d92
from
__future__
import
absolute_import
from
__future__
import
division
from
__future__
import
print_function
import
sys
import
os
import
numpy
as
np
import
argparse
import
time
import
paddle.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
from
model_utils.model
import
stacked_lstmp_model
def
parse_args
():
parser
=
argparse
.
ArgumentParser
(
"Run inference by using checkpoint."
)
parser
.
add_argument
(
'--batch_size'
,
type
=
int
,
default
=
32
,
help
=
'The sequence number of a batch data. (default: %(default)d)'
)
parser
.
add_argument
(
'--minimum_batch_size'
,
type
=
int
,
default
=
1
,
help
=
'The minimum sequence number of a batch data. '
'(default: %(default)d)'
)
parser
.
add_argument
(
'--stacked_num'
,
type
=
int
,
default
=
5
,
help
=
'Number of lstmp layers to stack. (default: %(default)d)'
)
parser
.
add_argument
(
'--proj_dim'
,
type
=
int
,
default
=
512
,
help
=
'Project size of lstmp unit. (default: %(default)d)'
)
parser
.
add_argument
(
'--hidden_dim'
,
type
=
int
,
default
=
1024
,
help
=
'Hidden size of lstmp unit. (default: %(default)d)'
)
parser
.
add_argument
(
'--learning_rate'
,
type
=
float
,
default
=
0.00016
,
help
=
'Learning rate used to train. (default: %(default)f)'
)
parser
.
add_argument
(
'--device'
,
type
=
str
,
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
,
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
(
'--checkpoint'
,
type
=
str
,
default
=
'./checkpoint'
,
help
=
"Checkpoints. Do not save checkpoint if set to ''. (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
infer_from_ckpt
(
args
):
"""inference by using checkpoint.
"""
if
not
os
.
path
.
exists
(
args
.
checkpoint
):
raise
IOError
(
"Invalid checkpoint!"
)
prediction
,
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
)
infer_program
=
fluid
.
default_main_program
().
clone
()
optimizer
=
fluid
.
optimizer
.
Adam
(
learning_rate
=
args
.
learning_rate
)
optimizer
.
minimize
(
avg_cost
)
place
=
fluid
.
CPUPlace
()
if
args
.
device
==
'CPU'
else
fluid
.
CUDAPlace
(
0
)
exe
=
fluid
.
Executor
(
place
)
exe
.
run
(
fluid
.
default_startup_program
())
# load checkpoint.
fluid
.
io
.
load_persistables
(
exe
,
args
.
checkpoint
)
ltrans
=
[
trans_add_delta
.
TransAddDelta
(
2
,
2
),
trans_mean_variance_norm
.
TransMeanVarianceNorm
(
args
.
mean_var
),
trans_splice
.
TransSplice
()
]
feature_t
=
fluid
.
LoDTensor
()
label_t
=
fluid
.
LoDTensor
()
# infer data reader
infer_data_reader
=
reader
.
DataReader
(
args
.
infer_feature_lst
,
args
.
infer_label_lst
)
infer_data_reader
.
set_transformers
(
ltrans
)
infer_costs
,
infer_accs
=
[],
[]
for
batch_id
,
batch_data
in
enumerate
(
infer_data_reader
.
batch_iterator
(
args
.
batch_size
,
args
.
minimum_batch_size
)):
# load_data
(
features
,
labels
,
lod
)
=
batch_data
feature_t
.
set
(
features
,
place
)
feature_t
.
set_lod
([
lod
])
label_t
.
set
(
labels
,
place
)
label_t
.
set_lod
([
lod
])
cost
,
acc
=
exe
.
run
(
infer_program
,
feed
=
{
"feature"
:
feature_t
,
"label"
:
label_t
},
fetch_list
=
[
avg_cost
,
accuracy
],
return_numpy
=
False
)
infer_costs
.
append
(
lodtensor_to_ndarray
(
cost
)[
0
])
infer_accs
.
append
(
lodtensor_to_ndarray
(
acc
)[
0
])
print
(
np
.
mean
(
infer_costs
),
np
.
mean
(
infer_accs
))
if
__name__
==
'__main__'
:
args
=
parse_args
()
print_arguments
(
args
)
infer_from_ckpt
(
args
)
fluid/DeepASR/train.py
浏览文件 @
0ba71d92
...
...
@@ -143,13 +143,11 @@ def train(args):
class_num
=
1749
,
parallel
=
args
.
parallel
)
optimizer
=
fluid
.
optimizer
.
Adam
(
learning_rate
=
args
.
learning_rate
)
optimizer
.
minimize
(
avg_cost
)
# program for test
test_program
=
fluid
.
default_main_program
().
clone
()
with
fluid
.
program_guard
(
test_program
):
test_program
=
fluid
.
io
.
get_inference_program
([
avg_cost
,
accuracy
])
optimizer
=
fluid
.
optimizer
.
Adam
(
learning_rate
=
args
.
learning_rate
)
optimizer
.
minimize
(
avg_cost
)
place
=
fluid
.
CPUPlace
()
if
args
.
device
==
'CPU'
else
fluid
.
CUDAPlace
(
0
)
exe
=
fluid
.
Executor
(
place
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录