Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleClas
提交
8d0b0d4b
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看板
提交
8d0b0d4b
编写于
12月 21, 2021
作者:
G
gaotingquan
提交者:
Tingquan Gao
12月 28, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor: extract _parse_pattern_str() func
上级
18dec074
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
72 addition
and
40 deletion
+72
-40
ppcls/arch/backbone/base/theseus_layer.py
ppcls/arch/backbone/base/theseus_layer.py
+72
-40
未找到文件。
ppcls/arch/backbone/base/theseus_layer.py
浏览文件 @
8d0b0d4b
...
...
@@ -66,53 +66,22 @@ class TheseusLayer(nn.Layer):
handle_res_dict
=
{}
for
pattern
in
layer_name_pattern
:
pattern_list
=
pattern
.
split
(
"."
)
#
pattern_list = pattern.split(".")
# find parent layer of sub-layer specified by pattern
sub_layer_parent
=
self
while
len
(
pattern_list
)
>
1
:
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_parent
=
getattr
(
sub_layer_parent
,
sub_layer_name
)[
sub_layer_index
]
else
:
sub_layer_parent
=
getattr
(
sub_layer_parent
,
pattern_list
[
0
],
None
)
if
sub_layer_parent
is
None
:
break
if
isinstance
(
sub_layer_parent
,
WrapLayer
):
sub_layer_parent
=
sub_layer_parent
.
sub_layer
pattern_list
=
pattern_list
[
1
:]
if
sub_layer_parent
is
None
:
msg
=
f
"Not found parent layer of sub-layer by name(
{
pattern_list
[
0
]
}
) specifed in pattern(
{
pattern
}
)."
logger
.
warning
(
msg
)
sub_layer_parent
,
_
,
_
=
parse_pattern_str
(
pattern
=
pattern
,
idx
=
(
0
,
-
1
),
sub_layer_parent
=
self
)
if
not
sub_layer_parent
:
continue
# find sub-layer specified by pattern
if
'['
in
pattern_list
[
0
]:
sub_layer_name
=
pattern_list
[
0
].
split
(
'['
)[
0
]
sub_layer_index
=
pattern_list
[
0
].
split
(
'['
)[
1
].
split
(
']'
)[
0
]
else
:
sub_layer_name
=
pattern_list
[
0
]
sub_layer_index
=
None
sub_layer
=
getattr
(
sub_layer_parent
,
sub_layer_name
,
None
)
sub_layer
,
sub_layer_name
,
sub_layer_index
=
parse_pattern_str
(
pattern
=
pattern
,
idx
=-
1
,
sub_layer_parent
=
sub_layer_parent
)
if
not
sub_layer
:
msg
=
f
"Not found sub-layer by name(
{
pattern_list
[
0
]
}
) specifed in pattern(
{
pattern
}
)."
logger
.
warning
(
msg
)
continue
if
sub_layer_index
is
not
None
:
if
int
(
sub_layer_index
)
<
0
or
int
(
sub_layer_index
)
>=
len
(
sub_layer
):
msg
=
f
"Not found sub-layer by index(
{
sub_layer_index
}
) specifed in pattern(
{
pattern
}
)."
logger
.
warning
(
msg
)
continue
sub_layer
=
sub_layer
[
sub_layer_index
]
new_sub_layer
=
handle_func
(
sub_layer
,
pattern
)
if
sub_layer_index
:
...
...
@@ -156,6 +125,7 @@ class TheseusLayer(nn.Layer):
pattern_list
=
stop_layer_name
.
split
(
"."
)
to_identity_list
=
[]
# TODO(gaotingquan): replace code by self._parse_pattern_str()
layer
=
self
while
len
(
pattern_list
)
>
0
:
layer_parent
=
layer
...
...
@@ -219,5 +189,67 @@ class WrapLayer(TheseusLayer):
def
wrap_theseus
(
sub_layer
):
wrapped_layer
=
WrapLayer
(
sub_layer
)
return
wrapped_layer
return
WrapLayer
(
sub_layer
)
def
unwrap_theseus
(
sub_layer
):
if
isinstance
(
sub_layer
,
WrapLayer
):
sub_layer
=
sub_layer
.
sub_layer
return
sub_layer
def
slice_pattern
(
pattern
,
idx
):
pattern_list
=
pattern
.
split
(
"."
)
if
idx
:
if
isinstance
(
idx
,
tuple
):
if
len
(
idx
)
==
1
:
return
pattern_list
[
idx
[
0
]]
elif
len
(
idx
)
==
2
:
return
pattern_list
[
idx
[
0
]:
idx
[
1
]]
else
:
msg
=
f
"Only support length of 'idx' is 1 or 2 when 'idx' is a tuple."
logger
.
warning
(
msg
)
return
None
elif
isinstance
(
idx
,
int
):
return
[
pattern_list
[
idx
]]
else
:
msg
=
f
"Only support type of 'idx' is int or tuple."
logger
.
warning
(
msg
)
return
None
return
pattern_list
def
parse_pattern_str
(
pattern
,
sub_layer_parent
,
idx
=
None
):
pattern_list
=
slice_pattern
(
pattern
,
idx
)
if
not
pattern_list
:
return
None
,
None
,
None
while
len
(
pattern_list
)
>
0
:
if
'['
in
pattern_list
[
0
]:
sub_layer_name
=
pattern_list
[
0
].
split
(
'['
)[
0
]
sub_layer_index
=
pattern_list
[
0
].
split
(
'['
)[
1
].
split
(
']'
)[
0
]
else
:
sub_layer_name
=
pattern_list
[
0
]
sub_layer_index
=
None
sub_layer_parent
=
getattr
(
sub_layer_parent
,
sub_layer_name
,
None
)
sub_layer_parent
=
unwrap_theseus
(
sub_layer_parent
)
if
sub_layer_parent
is
None
:
msg
=
f
"Not found layer named(
{
sub_layer_name
}
) specifed in pattern(
{
pattern
}
)."
logger
.
warning
(
msg
)
return
None
,
sub_layer_name
,
sub_layer_index
if
sub_layer_index
and
sub_layer_parent
:
if
int
(
sub_layer_index
)
<
0
or
int
(
sub_layer_index
)
>=
len
(
sub_layer_parent
):
msg
=
f
"Not found layer by index(
{
sub_layer_index
}
) specifed in pattern(
{
pattern
}
). The lenght of sub_layer's parent layer is < '
{
len
(
sub_layer_parent
)
}
' and > '0'."
logger
.
warning
(
msg
)
return
None
,
sub_layer_name
,
sub_layer_index
sub_layer_parent
=
sub_layer_parent
[
sub_layer_index
]
sub_layer_parent
=
unwrap_theseus
(
sub_layer_parent
)
pattern_list
=
pattern_list
[
1
:]
return
sub_layer_parent
,
sub_layer_name
,
sub_layer_index
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录