Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PALM
提交
8a99149a
P
PALM
项目概览
PaddlePaddle
/
PALM
通知
4
Star
3
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
10
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PALM
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
10
Issue
10
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
8a99149a
编写于
1月 08, 2020
作者:
X
xixiaoyao
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add predict
上级
2efeb39b
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
48 addition
and
6 deletion
+48
-6
paddlepalm/trainer.py
paddlepalm/trainer.py
+48
-6
未找到文件。
paddlepalm/trainer.py
浏览文件 @
8a99149a
...
@@ -122,7 +122,16 @@ class Trainer(object):
...
@@ -122,7 +122,16 @@ class Trainer(object):
pred_task_inputs
=
{
'backbone'
:
pred_bb_output_vars
,
'reader'
:
cur_inputs
}
pred_task_inputs
=
{
'backbone'
:
pred_bb_output_vars
,
'reader'
:
cur_inputs
}
scope
=
self
.
name
+
'.'
scope
=
self
.
name
+
'.'
with
fluid
.
unique_name
.
guard
(
scope
):
with
fluid
.
unique_name
.
guard
(
scope
):
self
.
_build_head
(
pred_task_inputs
,
phase
=
'pred'
,
scope
=
scope
)
output_vars
=
self
.
_build_head
(
pred_task_inputs
,
phase
=
'pred'
,
scope
=
scope
)
if
output_vars
is
not
None
:
self
.
_pred_fetch_name_list
,
self
.
_pred_fetch_var_list
=
zip
(
*
output_vars
.
items
())
else
:
self
.
_pred_fetch_name_list
=
[]
self
.
_pred_fetch_var_list
=
[]
self
.
_distribute_pred_prog
=
fluid
.
CompiledProgram
(
self
.
_pred_prog
).
with_data_parallel
()
return
output_vars
def
build_forward
(
self
,
backbone
,
pred_backbone
=
None
,
train_prog
=
None
,
train_init_prog
=
None
,
pred_prog
=
None
,
pred_init_prog
=
None
):
def
build_forward
(
self
,
backbone
,
pred_backbone
=
None
,
train_prog
=
None
,
train_init_prog
=
None
,
pred_prog
=
None
,
pred_init_prog
=
None
):
...
@@ -277,11 +286,35 @@ class Trainer(object):
...
@@ -277,11 +286,35 @@ class Trainer(object):
return
distribute_feeder_fn
()
return
distribute_feeder_fn
()
def
random_init_params
(
self
):
def
random_init_params
(
self
):
assert
self
.
_train_init_prog
is
not
None
,
"train graph not foung! You should build_forward first before you random init parameters."
on_gpu
=
gpu_dev_count
>
0
on_gpu
=
gpu_dev_count
>
0
self
.
_exe
=
helper
.
build_executor
(
on_gpu
)
self
.
_exe
=
helper
.
build_executor
(
on_gpu
)
print
(
'random init params...'
)
print
(
'random init params...'
)
self
.
_exe
.
run
(
self
.
_train_init_prog
)
self
.
_exe
.
run
(
self
.
_train_init_prog
)
def
load_ckpt
(
self
,
model_path
,
phase
=
'train'
):
# load pretrain model (or ckpt)
assert
self
.
_exe
is
not
None
,
"You need to random_init_params before load pretrain models."
if
phase
==
'train'
:
assert
self
.
_train_init_prog
is
not
None
,
"train graph not found! You should build_forward first before load checkpoint."
saver
.
init_pretraining_params
(
self
.
_exe
,
model_path
,
main_program
=
self
.
_train_init_prog
)
elif
phase
==
'predict'
:
assert
self
.
_pred_init_prog
is
not
None
,
"predict graph not found! You should build_predict_head first before load checkpoint."
saver
.
init_pretraining_params
(
self
.
_exe
,
model_path
,
main_program
=
self
.
_pred_init_prog
)
else
:
raise
NotImplementedError
()
def
load_predict_model
(
self
,
model_path
):
raise
NotImplementedError
()
def
load_pretrain
(
self
,
model_path
):
def
load_pretrain
(
self
,
model_path
):
# load pretrain model (or ckpt)
# load pretrain model (or ckpt)
assert
self
.
_exe
is
not
None
,
"You need to random_init_params before load pretrain models."
assert
self
.
_exe
is
not
None
,
"You need to random_init_params before load pretrain models."
...
@@ -401,17 +434,25 @@ class Trainer(object):
...
@@ -401,17 +434,25 @@ class Trainer(object):
rt_outputs
=
{
k
:
v
for
k
,
v
in
zip
(
self
.
_fetch_names
,
rt_outputs
)}
rt_outputs
=
{
k
:
v
for
k
,
v
in
zip
(
self
.
_fetch_names
,
rt_outputs
)}
return
rt_outputs
return
rt_outputs
def
predict_one_batch
(
self
,
batch
):
if
gpu_dev_count
>
1
:
feed
,
mask
=
batch
rt_outputs
=
self
.
exe
.
run
(
self
.
_distribute_train_prog
,
feed
=
feed
,
fetch_list
=
self
.
_fetch_list
)
while
mask
.
pop
()
==
False
:
rt_outputs
.
pop
()
else
:
feed
=
self
.
_feed_batch_process_fn
(
batch
)
rt_outputs
=
self
.
_exe
.
run
(
self
.
_distribute_train_prog
,
feed
=
feed
,
fetch_list
=
self
.
_fetch_list
)
rt_outputs
=
{
k
:
v
for
k
,
v
in
zip
(
self
.
_fetch_names
,
rt_outputs
)}
def
_build_head
(
self
,
net_inputs
,
phase
,
scope
=
""
):
def
_build_head
(
self
,
net_inputs
,
phase
,
scope
=
""
):
if
phase
==
'train'
:
if
phase
==
'train'
:
output_vars
=
self
.
_task_head
.
build
(
net_inputs
,
scope_name
=
scope
)
output_vars
=
self
.
_task_head
.
build
(
net_inputs
,
scope_name
=
scope
)
if
phase
==
'pred'
:
if
phase
==
'pred'
:
output_vars
=
self
.
_pred_head
.
build
(
net_inputs
,
scope_name
=
scope
)
output_vars
=
self
.
_pred_head
.
build
(
net_inputs
,
scope_name
=
scope
)
if
output_vars
is
not
None
:
self
.
_pred_fetch_name_list
,
self
.
_pred_fetch_var_list
=
zip
(
*
output_vars
.
items
())
else
:
self
.
_pred_fetch_name_list
=
[]
self
.
_pred_fetch_var_list
=
[]
return
output_vars
return
output_vars
def
_postprocess
(
self
,
rt_outputs
,
phase
):
def
_postprocess
(
self
,
rt_outputs
,
phase
):
...
@@ -441,6 +482,7 @@ class Trainer(object):
...
@@ -441,6 +482,7 @@ class Trainer(object):
writer
.
write
(
json
.
dumps
(
conf
,
indent
=
1
))
writer
.
write
(
json
.
dumps
(
conf
,
indent
=
1
))
print
(
self
.
_name
+
': predict model saved at '
+
dirpath
)
print
(
self
.
_name
+
': predict model saved at '
+
dirpath
)
def
_load
(
self
,
infer_model_path
=
None
):
def
_load
(
self
,
infer_model_path
=
None
):
if
infer_model_path
is
None
:
if
infer_model_path
is
None
:
infer_model_path
=
self
.
_save_infermodel_path
infer_model_path
=
self
.
_save_infermodel_path
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录