Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleHub
提交
1491d0cb
P
PaddleHub
项目概览
PaddlePaddle
/
PaddleHub
大约 1 年 前同步成功
通知
282
Star
12117
Fork
2091
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
200
列表
看板
标记
里程碑
合并请求
4
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleHub
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
200
Issue
200
列表
看板
标记
里程碑
合并请求
4
合并请求
4
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
1491d0cb
编写于
1月 10, 2020
作者:
K
kinghuin
提交者:
wuzewu
1月 10, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Optimize hook (#317)
* optimize hook
上级
f378a281
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
38 addition
and
32 deletion
+38
-32
paddlehub/finetune/task/base_task.py
paddlehub/finetune/task/base_task.py
+38
-32
未找到文件。
paddlehub/finetune/task/base_task.py
浏览文件 @
1491d0cb
...
@@ -24,6 +24,7 @@ import copy
...
@@ -24,6 +24,7 @@ import copy
import
logging
import
logging
import
inspect
import
inspect
from
functools
import
partial
from
functools
import
partial
from
collections
import
OrderedDict
import
six
import
six
if
six
.
PY2
:
if
six
.
PY2
:
from
inspect
import
getargspec
as
get_args
from
inspect
import
getargspec
as
get_args
...
@@ -88,44 +89,44 @@ class RunEnv(object):
...
@@ -88,44 +89,44 @@ class RunEnv(object):
class
TaskHooks
():
class
TaskHooks
():
def
__init__
(
self
):
def
__init__
(
self
):
self
.
_registered_hooks
=
{
self
.
_registered_hooks
=
{
"build_env_start
"
:
{}
,
"build_env_start
_event"
:
OrderedDict
()
,
"build_env_end
"
:
{}
,
"build_env_end
_event"
:
OrderedDict
()
,
"finetune_start
"
:
{}
,
"finetune_start
_event"
:
OrderedDict
()
,
"finetune_end
"
:
{}
,
"finetune_end
_event"
:
OrderedDict
()
,
"predict_start
"
:
{}
,
"predict_start
_event"
:
OrderedDict
()
,
"predict_end
"
:
{}
,
"predict_end
_event"
:
OrderedDict
()
,
"eval_start
"
:
{}
,
"eval_start
_event"
:
OrderedDict
()
,
"eval_end
"
:
{}
,
"eval_end
_event"
:
OrderedDict
()
,
"log_interval
"
:
{}
,
"log_interval
_event"
:
OrderedDict
()
,
"save_ckpt_interval
"
:
{}
,
"save_ckpt_interval
_event"
:
OrderedDict
()
,
"eval_interval
"
:
{}
,
"eval_interval
_event"
:
OrderedDict
()
,
"run_step
"
:
{}
,
"run_step
_event"
:
OrderedDict
()
,
}
}
self
.
_hook_params_num
=
{
self
.
_hook_params_num
=
{
"build_env_start"
:
1
,
"build_env_start
_event
"
:
1
,
"build_env_end"
:
1
,
"build_env_end
_event
"
:
1
,
"finetune_start"
:
1
,
"finetune_start
_event
"
:
1
,
"finetune_end"
:
2
,
"finetune_end
_event
"
:
2
,
"predict_start"
:
1
,
"predict_start
_event
"
:
1
,
"predict_end"
:
2
,
"predict_end
_event
"
:
2
,
"eval_start"
:
1
,
"eval_start
_event
"
:
1
,
"eval_end"
:
2
,
"eval_end
_event
"
:
2
,
"log_interval"
:
2
,
"log_interval
_event
"
:
2
,
"save_ckpt_interval"
:
1
,
"save_ckpt_interval
_event
"
:
1
,
"eval_interval"
:
1
,
"eval_interval
_event
"
:
1
,
"run_step"
:
2
,
"run_step
_event
"
:
2
,
}
}
def
add
(
self
,
hook_type
,
name
=
None
,
func
=
None
):
def
add
(
self
,
hook_type
,
name
=
None
,
func
=
None
):
if
not
func
or
not
callable
(
func
):
if
not
func
or
not
callable
(
func
):
raise
TypeError
(
raise
TypeError
(
"The hook function is empty or it is not a function"
)
"The hook function is empty or it is not a function"
)
if
name
and
not
isinstance
(
name
,
str
):
if
name
==
None
:
raise
TypeError
(
"The hook name must be a string"
)
if
not
name
:
name
=
"hook_%s"
%
id
(
func
)
name
=
"hook_%s"
%
id
(
func
)
# check validity
# check validity
if
not
isinstance
(
name
,
str
)
or
name
.
strip
()
==
""
:
raise
TypeError
(
"The hook name must be a non-empty string"
)
if
hook_type
not
in
self
.
_registered_hooks
:
if
hook_type
not
in
self
.
_registered_hooks
:
raise
ValueError
(
"hook_type: %s does not exist"
%
(
hook_type
))
raise
ValueError
(
"hook_type: %s does not exist"
%
(
hook_type
))
if
name
in
self
.
_registered_hooks
[
hook_type
]:
if
name
in
self
.
_registered_hooks
[
hook_type
]:
...
@@ -167,13 +168,13 @@ class TaskHooks():
...
@@ -167,13 +168,13 @@ class TaskHooks():
else
:
else
:
return
True
return
True
def
info
(
self
,
only_customized
=
Tru
e
):
def
info
(
self
,
show_default
=
Fals
e
):
# formatted output the source code
# formatted output the source code
ret
=
""
ret
=
""
for
hook_type
,
hooks
in
self
.
_registered_hooks
.
items
():
for
hook_type
,
hooks
in
self
.
_registered_hooks
.
items
():
already_print_type
=
False
already_print_type
=
False
for
name
,
func
in
hooks
.
items
():
for
name
,
func
in
hooks
.
items
():
if
name
==
"default"
and
only_customized
:
if
name
==
"default"
and
not
show_default
:
continue
continue
if
not
already_print_type
:
if
not
already_print_type
:
ret
+=
"hook_type: %s{
\n
"
%
hook_type
ret
+=
"hook_type: %s{
\n
"
%
hook_type
...
@@ -186,7 +187,7 @@ class TaskHooks():
...
@@ -186,7 +187,7 @@ class TaskHooks():
if
already_print_type
:
if
already_print_type
:
ret
+=
"}
\n
"
ret
+=
"}
\n
"
if
not
ret
:
if
not
ret
:
ret
=
"Not any
hooks when only_customized=%s"
%
only_customized
ret
=
"Not any
customized hooks have been defined, you can set show_default=True to see the default hooks information"
return
ret
return
ret
def
__getitem__
(
self
,
hook_type
):
def
__getitem__
(
self
,
hook_type
):
...
@@ -263,8 +264,8 @@ class BaseTask(object):
...
@@ -263,8 +264,8 @@ class BaseTask(object):
self
.
_hooks
=
TaskHooks
()
self
.
_hooks
=
TaskHooks
()
for
hook_type
,
event_hooks
in
self
.
_hooks
.
_registered_hooks
.
items
():
for
hook_type
,
event_hooks
in
self
.
_hooks
.
_registered_hooks
.
items
():
self
.
_hooks
.
add
(
hook_type
,
"default"
,
self
.
_hooks
.
add
(
hook_type
,
"default"
,
eval
(
"self._default_%s
_event
"
%
hook_type
))
eval
(
"self._default_%s"
%
hook_type
))
setattr
(
BaseTask
,
"_%s
_event
"
%
hook_type
,
setattr
(
BaseTask
,
"_%s"
%
hook_type
,
self
.
create_event_function
(
hook_type
))
self
.
create_event_function
(
hook_type
))
# accelerate predict
# accelerate predict
...
@@ -585,13 +586,18 @@ class BaseTask(object):
...
@@ -585,13 +586,18 @@ class BaseTask(object):
return
self
.
_hooks
.
info
(
only_customized
)
return
self
.
_hooks
.
info
(
only_customized
)
def
add_hook
(
self
,
hook_type
,
name
=
None
,
func
=
None
):
def
add_hook
(
self
,
hook_type
,
name
=
None
,
func
=
None
):
if
name
==
None
:
name
=
"hook_%s"
%
id
(
func
)
self
.
_hooks
.
add
(
hook_type
,
name
=
name
,
func
=
func
)
self
.
_hooks
.
add
(
hook_type
,
name
=
name
,
func
=
func
)
logger
.
info
(
"Add hook %s:%s successfully"
%
(
hook_type
,
name
))
def
delete_hook
(
self
,
hook_type
,
name
):
def
delete_hook
(
self
,
hook_type
,
name
):
self
.
_hooks
.
delete
(
hook_type
,
name
)
self
.
_hooks
.
delete
(
hook_type
,
name
)
logger
.
info
(
"Delete hook %s:%s successfully"
%
(
hook_type
,
name
))
def
modify_hook
(
self
,
hook_type
,
name
,
func
):
def
modify_hook
(
self
,
hook_type
,
name
,
func
):
self
.
_hooks
.
modify
(
hook_type
,
name
,
func
)
self
.
_hooks
.
modify
(
hook_type
,
name
,
func
)
logger
.
info
(
"Modify hook %s:%s successfully"
%
(
hook_type
,
name
))
def
_default_build_env_start_event
(
self
):
def
_default_build_env_start_event
(
self
):
pass
pass
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录