Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
6625f543
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看板
未验证
提交
6625f543
编写于
2月 11, 2018
作者:
Y
Yibing Liu
提交者:
GitHub
2月 11, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #663 from kuke/model_init
Enable checkpoints saving and training resuming
上级
08f169cb
6a5a0e74
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
60 addition
and
22 deletion
+60
-22
fluid/DeepASR/infer.py
fluid/DeepASR/infer.py
+7
-6
fluid/DeepASR/tools/profile.py
fluid/DeepASR/tools/profile.py
+3
-2
fluid/DeepASR/train.py
fluid/DeepASR/train.py
+50
-14
未找到文件。
fluid/DeepASR/infer.py
浏览文件 @
6625f543
...
...
@@ -42,10 +42,11 @@ def parse_args():
default
=
'data/infer_label.lst'
,
help
=
'The label list path for inference. (default: %(default)s)'
)
parser
.
add_argument
(
'--
model_save
_path'
,
'--
infer_model
_path'
,
type
=
str
,
default
=
'./checkpoints/deep_asr.pass_0.model/'
,
help
=
'The directory for saving model. (default: %(default)s)'
)
default
=
'./infer_models/deep_asr.pass_0.infer.model/'
,
help
=
'The directory for loading inference model. '
'(default: %(default)s)'
)
args
=
parser
.
parse_args
()
return
args
...
...
@@ -68,15 +69,15 @@ 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!"
)
if
not
os
.
path
.
exists
(
args
.
infer_model
_path
):
raise
IOError
(
"Invalid
inference
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
)
fetch_targets
]
=
fluid
.
io
.
load_inference_model
(
args
.
infer_model
_path
,
exe
)
ltrans
=
[
trans_add_delta
.
TransAddDelta
(
2
,
2
),
...
...
fluid/DeepASR/tools/profile.py
浏览文件 @
6625f543
...
...
@@ -125,8 +125,9 @@ def profile(args):
class_num
=
1749
,
parallel
=
args
.
parallel
)
adam_optimizer
=
fluid
.
optimizer
.
Adam
(
learning_rate
=
args
.
learning_rate
)
adam_optimizer
.
minimize
(
avg_cost
)
optimizer
=
fluid
.
optimizer
.
Momentum
(
learning_rate
=
args
.
learning_rate
,
momentum
=
0.9
)
optimizer
.
minimize
(
avg_cost
)
place
=
fluid
.
CPUPlace
()
if
args
.
device
==
'CPU'
else
fluid
.
CUDAPlace
(
0
)
exe
=
fluid
.
Executor
(
place
)
...
...
fluid/DeepASR/train.py
浏览文件 @
6625f543
...
...
@@ -34,17 +34,17 @@ def parse_args():
'--stacked_num'
,
type
=
int
,
default
=
5
,
help
=
'Number of lstm layers to stack. (default: %(default)d)'
)
help
=
'Number of lstm
p
layers to stack. (default: %(default)d)'
)
parser
.
add_argument
(
'--proj_dim'
,
type
=
int
,
default
=
512
,
help
=
'Project size of lstm unit. (default: %(default)d)'
)
help
=
'Project size of lstm
p
unit. (default: %(default)d)'
)
parser
.
add_argument
(
'--hidden_dim'
,
type
=
int
,
default
=
1024
,
help
=
'Hidden size of lstm unit. (default: %(default)d)'
)
help
=
'Hidden size of lstm
p
unit. (default: %(default)d)'
)
parser
.
add_argument
(
'--pass_num'
,
type
=
int
,
...
...
@@ -95,11 +95,23 @@ def parse_args():
default
=
'data/val_label.lst'
,
help
=
'The label list path for validation. (default: %(default)s)'
)
parser
.
add_argument
(
'--model_save_dir'
,
'--init_model_path'
,
type
=
str
,
default
=
None
,
help
=
"The model (checkpoint) path which the training resumes from. "
"If None, train the model from scratch. (default: %(default)s)"
)
parser
.
add_argument
(
'--checkpoints'
,
type
=
str
,
default
=
'./checkpoints'
,
help
=
"The directory for saving model. Do not save model if set to "
"''. (default: %(default)s)"
)
help
=
"The directory for saving checkpoints. Do not save checkpoints "
"if set to ''. (default: %(default)s)"
)
parser
.
add_argument
(
'--infer_models'
,
type
=
str
,
default
=
'./infer_models'
,
help
=
"The directory for saving inference models. Do not save inference "
"models if set to ''. (default: %(default)s)"
)
args
=
parser
.
parse_args
()
return
args
...
...
@@ -115,6 +127,15 @@ def train(args):
"""train in loop.
"""
# paths check
if
args
.
init_model_path
is
not
None
and
\
not
os
.
path
.
exists
(
args
.
init_model_path
):
raise
IOError
(
"Invalid initial model path!"
)
if
args
.
checkpoints
!=
''
and
not
os
.
path
.
exists
(
args
.
checkpoints
):
os
.
mkdir
(
args
.
checkpoints
)
if
args
.
infer_models
!=
''
and
not
os
.
path
.
exists
(
args
.
infer_models
):
os
.
mkdir
(
args
.
infer_models
)
prediction
,
avg_cost
,
accuracy
=
stacked_lstmp_model
(
hidden_dim
=
args
.
hidden_dim
,
proj_dim
=
args
.
proj_dim
,
...
...
@@ -122,8 +143,9 @@ def train(args):
class_num
=
1749
,
parallel
=
args
.
parallel
)
adam_optimizer
=
fluid
.
optimizer
.
Adam
(
learning_rate
=
args
.
learning_rate
)
adam_optimizer
.
minimize
(
avg_cost
)
optimizer
=
fluid
.
optimizer
.
Momentum
(
learning_rate
=
args
.
learning_rate
,
momentum
=
0.9
)
optimizer
.
minimize
(
avg_cost
)
# program for test
test_program
=
fluid
.
default_main_program
().
clone
()
...
...
@@ -134,6 +156,10 @@ def train(args):
exe
=
fluid
.
Executor
(
place
)
exe
.
run
(
fluid
.
default_startup_program
())
# resume training if initial model provided.
if
args
.
init_model_path
is
not
None
:
fluid
.
io
.
load_persistables
(
exe
,
args
.
init_model_path
)
ltrans
=
[
trans_add_delta
.
TransAddDelta
(
2
,
2
),
trans_mean_variance_norm
.
TransMeanVarianceNorm
(
args
.
mean_var
),
...
...
@@ -200,15 +226,28 @@ def train(args):
print
(
"
\n
Batch %d, train cost: %f, train acc: %f"
%
(
batch_id
,
lodtensor_to_ndarray
(
cost
)[
0
],
lodtensor_to_ndarray
(
acc
)[
0
]))
# save the latest checkpoint
if
args
.
checkpoints
!=
''
:
model_path
=
os
.
path
.
join
(
args
.
checkpoints
,
"deep_asr.latest.checkpoint"
)
fluid
.
io
.
save_persistables
(
exe
,
model_path
)
else
:
sys
.
stdout
.
write
(
'.'
)
sys
.
stdout
.
flush
()
# run test
val_cost
,
val_acc
=
test
(
exe
)
# save model
if
args
.
model_save_dir
!=
''
:
# save checkpoint per pass
if
args
.
checkpoints
!=
''
:
model_path
=
os
.
path
.
join
(
args
.
model_save_dir
,
"deep_asr.pass_"
+
str
(
pass_id
)
+
".model"
)
args
.
checkpoints
,
"deep_asr.pass_"
+
str
(
pass_id
)
+
".checkpoint"
)
fluid
.
io
.
save_persistables
(
exe
,
model_path
)
# save inference model
if
args
.
infer_models
!=
''
:
model_path
=
os
.
path
.
join
(
args
.
infer_models
,
"deep_asr.pass_"
+
str
(
pass_id
)
+
".infer.model"
)
fluid
.
io
.
save_inference_model
(
model_path
,
[
"feature"
],
[
prediction
],
exe
)
# cal pass time
...
...
@@ -223,7 +262,4 @@ if __name__ == '__main__':
args
=
parse_args
()
print_arguments
(
args
)
if
args
.
model_save_dir
!=
''
and
not
os
.
path
.
exists
(
args
.
model_save_dir
):
os
.
mkdir
(
args
.
model_save_dir
)
train
(
args
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录