Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleClas
提交
f8ee6c0f
P
PaddleClas
项目概览
PaddlePaddle
/
PaddleClas
大约 1 年 前同步成功
通知
115
Star
4999
Fork
1114
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
19
列表
看板
标记
里程碑
合并请求
6
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleClas
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
19
Issue
19
列表
看板
标记
里程碑
合并请求
6
合并请求
6
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
f8ee6c0f
编写于
12月 20, 2021
作者:
G
gaotingquan
提交者:
Tingquan Gao
12月 28, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix: fix result returned by stop_after
上级
cf205e13
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
32 addition
and
15 deletion
+32
-15
ppcls/arch/backbone/base/theseus_layer.py
ppcls/arch/backbone/base/theseus_layer.py
+32
-15
未找到文件。
ppcls/arch/backbone/base/theseus_layer.py
浏览文件 @
f8ee6c0f
from
typing
import
List
,
Union
,
Callable
,
Any
from
typing
import
List
,
Dict
,
Union
,
Callable
,
Any
from
paddle
import
nn
from
ppcls.utils
import
logger
...
...
@@ -19,7 +19,6 @@ class TheseusLayer(nn.Layer):
self
.
pruner
=
None
self
.
quanter
=
None
# TODO(gaotingquan): weishengyu
def
_return_dict_hook
(
self
,
layer
,
input
,
output
):
res_dict
=
{
"output"
:
output
}
for
res_key
in
list
(
self
.
res_dict
):
...
...
@@ -54,7 +53,7 @@ class TheseusLayer(nn.Layer):
sub_layer_parent
=
sub_layer_parent
.
sub_layer
pattern_list
=
pattern_list
[
1
:]
if
sub_layer_parent
is
None
:
msg
=
f
"Not found layer by name(
{
pattern_list
[
0
]
}
) specifed in pattern(
{
pattern
}
)."
msg
=
f
"Not found
parent layer of sub-
layer by name(
{
pattern_list
[
0
]
}
) specifed in pattern(
{
pattern
}
)."
logger
.
warning
(
msg
)
continue
...
...
@@ -62,17 +61,33 @@ class TheseusLayer(nn.Layer):
if
'['
in
pattern_list
[
0
]:
sub_layer_name
=
pattern_list
[
0
].
split
(
'['
)[
0
]
sub_layer_index
=
pattern_list
[
0
].
split
(
'['
)[
1
].
split
(
']'
)[
0
]
sub_layer
=
getattr
(
sub_layer_parent
,
sub_layer_name
)[
sub_layer_index
]
else
:
sub_layer_name
=
pattern_list
[
0
]
sub_layer_index
=
None
sub_layer
=
getattr
(
sub_layer_parent
,
sub_layer_name
,
False
)
if
sub_layer
is
False
:
msg
=
f
"Not found sub-layer by name(
{
pattern_list
[
0
]
}
) specifed in pattern(
{
pattern
}
)."
logger
.
warning
(
msg
)
continue
try
:
sub_layer
=
sub_layer
[
sub_layer_index
]
if
sub_layer_index
is
not
None
else
sub_layer
except
KeyError
as
e
:
msg
=
f
"Not found sub-layer by index(
{
sub_layer_index
}
) specifed in pattern(
{
pattern
}
)."
logger
.
warning
(
msg
)
continue
if
not
isinstance
(
sub_layer
,
TheseusLayer
):
sub_layer
=
wrap_theseus
(
sub_layer
)
if
sub_layer_index
:
getattr
(
sub_layer_parent
,
sub_layer_name
)[
sub_layer_index
]
=
sub_layer
else
:
sub_layer
=
getattr
(
sub_layer_parent
,
pattern_list
[
0
])
if
not
isinstance
(
sub_layer
,
TheseusLayer
):
sub_layer
=
wrap_theseus
(
sub_layer
)
setattr
(
sub_layer_parent
,
pattern_list
[
0
],
sub_layer
)
setattr
(
sub_layer_parent
,
sub_layer_name
,
sub_layer
)
handle_res
=
handle_func
(
sub_layer
,
pattern
)
handle_res_dict
[
pattern
]
=
handle_res
...
...
@@ -136,7 +151,7 @@ class TheseusLayer(nn.Layer):
return
self
.
_find_layers_handle
(
layer_name_pattern
,
handle_func
=
replace_function
)
# stop doesn't work when stop layer has a parallel branch.
#
TODO(weishengyu):
stop doesn't work when stop layer has a parallel branch.
def
stop_after
(
self
,
stop_layer_name
:
str
)
->
bool
:
"""stop forward and backward after 'stop_layer_name'.
...
...
@@ -176,14 +191,15 @@ class TheseusLayer(nn.Layer):
return
False
return
True
def
update_res
(
self
,
return_patterns
:
Union
[
str
,
List
[
str
]])
->
bool
:
def
update_res
(
self
,
return_patterns
:
Union
[
str
,
List
[
str
]])
->
Dict
[
str
,
bool
]:
"""update the results needed returned.
Args:
return_patterns (Union[str, List[str]]):
The layer(s)' name to be retruened.
return_patterns (Union[str, List[str]]):
[description]
Returns:
bool: 'True' if successful, 'False'
otherwise.
Dict[str, bool]: The pattern(str) is be set successfully if True(bool), failed
otherwise.
"""
class
Handler
(
object
):
...
...
@@ -194,6 +210,7 @@ class TheseusLayer(nn.Layer):
layer
.
res_dict
=
self
.
res_dict
layer
.
res_name
=
pattern
layer
.
register_forward_post_hook
(
layer
.
_save_sub_res_hook
)
return
True
handle_func
=
Handler
(
self
.
res_dict
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录