Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleClas
提交
e9162595
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看板
提交
e9162595
编写于
9月 15, 2021
作者:
W
weishengyu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update return_res method
上级
5131956d
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
42 addition
and
15 deletion
+42
-15
ppcls/arch/__init__.py
ppcls/arch/__init__.py
+7
-0
ppcls/arch/backbone/base/theseus_layer.py
ppcls/arch/backbone/base/theseus_layer.py
+6
-0
ppcls/arch/backbone/legendary_models/hrnet.py
ppcls/arch/backbone/legendary_models/hrnet.py
+5
-2
ppcls/arch/backbone/legendary_models/inception_v3.py
ppcls/arch/backbone/legendary_models/inception_v3.py
+4
-1
ppcls/arch/backbone/legendary_models/mobilenet_v1.py
ppcls/arch/backbone/legendary_models/mobilenet_v1.py
+5
-5
ppcls/arch/backbone/legendary_models/mobilenet_v3.py
ppcls/arch/backbone/legendary_models/mobilenet_v3.py
+5
-1
ppcls/arch/backbone/legendary_models/resnet.py
ppcls/arch/backbone/legendary_models/resnet.py
+5
-1
ppcls/arch/backbone/legendary_models/vgg.py
ppcls/arch/backbone/legendary_models/vgg.py
+5
-5
未找到文件。
ppcls/arch/__init__.py
浏览文件 @
e9162595
...
@@ -23,6 +23,7 @@ from . import backbone, gears
...
@@ -23,6 +23,7 @@ from . import backbone, gears
from
.backbone
import
*
from
.backbone
import
*
from
.gears
import
build_gear
from
.gears
import
build_gear
from
.utils
import
*
from
.utils
import
*
from
ppcls.arch.backbone.base.theseus_layer
import
TheseusLayer
from
ppcls.utils
import
logger
from
ppcls.utils
import
logger
from
ppcls.utils.save_load
import
load_dygraph_pretrain
from
ppcls.utils.save_load
import
load_dygraph_pretrain
...
@@ -32,8 +33,11 @@ __all__ = ["build_model", "RecModel", "DistillationModel"]
...
@@ -32,8 +33,11 @@ __all__ = ["build_model", "RecModel", "DistillationModel"]
def
build_model
(
config
):
def
build_model
(
config
):
config
=
copy
.
deepcopy
(
config
)
config
=
copy
.
deepcopy
(
config
)
model_type
=
config
.
pop
(
"name"
)
model_type
=
config
.
pop
(
"name"
)
return_patterns
=
config
.
pop
(
"return_patterns"
,
None
)
mod
=
importlib
.
import_module
(
__name__
)
mod
=
importlib
.
import_module
(
__name__
)
arch
=
getattr
(
mod
,
model_type
)(
**
config
)
arch
=
getattr
(
mod
,
model_type
)(
**
config
)
if
return_patterns
is
not
None
and
isinstance
(
arch
,
TheseusLayer
):
arch
.
update_res
(
return_patterns
=
return_patterns
,
return_dict
=
True
)
return
arch
return
arch
...
@@ -55,7 +59,10 @@ class RecModel(nn.Layer):
...
@@ -55,7 +59,10 @@ class RecModel(nn.Layer):
super
().
__init__
()
super
().
__init__
()
backbone_config
=
config
[
"Backbone"
]
backbone_config
=
config
[
"Backbone"
]
backbone_name
=
backbone_config
.
pop
(
"name"
)
backbone_name
=
backbone_config
.
pop
(
"name"
)
return_patterns
=
config
.
pop
(
"return_patterns"
,
None
)
self
.
backbone
=
eval
(
backbone_name
)(
**
backbone_config
)
self
.
backbone
=
eval
(
backbone_name
)(
**
backbone_config
)
if
return_patterns
is
not
None
and
isinstance
(
self
.
backbone
,
TheseusLayer
):
self
.
backbone
.
update_res
(
return_patterns
=
return_patterns
,
return_dict
=
True
)
if
"BackboneStopLayer"
in
config
:
if
"BackboneStopLayer"
in
config
:
backbone_stop_layer
=
config
[
"BackboneStopLayer"
][
"name"
]
backbone_stop_layer
=
config
[
"BackboneStopLayer"
][
"name"
]
self
.
backbone
.
stop_after
(
backbone_stop_layer
)
self
.
backbone
.
stop_after
(
backbone_stop_layer
)
...
...
ppcls/arch/backbone/base/theseus_layer.py
浏览文件 @
e9162595
...
@@ -57,6 +57,12 @@ class TheseusLayer(nn.Layer):
...
@@ -57,6 +57,12 @@ class TheseusLayer(nn.Layer):
def
_save_sub_res_hook
(
self
,
layer
,
input
,
output
):
def
_save_sub_res_hook
(
self
,
layer
,
input
,
output
):
self
.
res_dict
[
layer
.
full_name
()]
=
output
self
.
res_dict
[
layer
.
full_name
()]
=
output
def
_return_dict_hook
(
self
,
layer
,
input
,
output
):
res_dict
=
{
"output"
:
output
}
for
res_key
in
list
(
self
.
res_dict
):
res_dict
[
res_key
]
=
self
.
res_dict
.
pop
(
res_key
)
return
res_dict
def
replace_sub
(
self
,
layer_name_pattern
,
replace_function
,
recursive
=
True
):
def
replace_sub
(
self
,
layer_name_pattern
,
replace_function
,
recursive
=
True
):
for
layer_i
in
self
.
_sub_layers
:
for
layer_i
in
self
.
_sub_layers
:
layer_name
=
self
.
_sub_layers
[
layer_i
].
full_name
()
layer_name
=
self
.
_sub_layers
[
layer_i
].
full_name
()
...
...
ppcls/arch/backbone/legendary_models/hrnet.py
浏览文件 @
e9162595
...
@@ -367,7 +367,7 @@ class HRNet(TheseusLayer):
...
@@ -367,7 +367,7 @@ class HRNet(TheseusLayer):
model: nn.Layer. Specific HRNet model depends on args.
model: nn.Layer. Specific HRNet model depends on args.
"""
"""
def
__init__
(
self
,
width
=
18
,
has_se
=
False
,
class_num
=
1000
):
def
__init__
(
self
,
width
=
18
,
has_se
=
False
,
class_num
=
1000
,
return_patterns
=
None
):
super
().
__init__
()
super
().
__init__
()
self
.
width
=
width
self
.
width
=
width
...
@@ -456,8 +456,11 @@ class HRNet(TheseusLayer):
...
@@ -456,8 +456,11 @@ class HRNet(TheseusLayer):
2048
,
2048
,
class_num
,
class_num
,
weight_attr
=
ParamAttr
(
initializer
=
Uniform
(
-
stdv
,
stdv
)))
weight_attr
=
ParamAttr
(
initializer
=
Uniform
(
-
stdv
,
stdv
)))
if
return_patterns
is
not
None
:
self
.
update_res
(
return_patterns
)
self
.
register_forward_post_hook
(
self
.
_return_dict_hook
)
def
forward
(
self
,
x
,
res_dict
=
None
):
def
forward
(
self
,
x
):
x
=
self
.
conv_layer1_1
(
x
)
x
=
self
.
conv_layer1_1
(
x
)
x
=
self
.
conv_layer1_2
(
x
)
x
=
self
.
conv_layer1_2
(
x
)
...
...
ppcls/arch/backbone/legendary_models/inception_v3.py
浏览文件 @
e9162595
...
@@ -454,7 +454,7 @@ class Inception_V3(TheseusLayer):
...
@@ -454,7 +454,7 @@ class Inception_V3(TheseusLayer):
model: nn.Layer. Specific Inception_V3 model depends on args.
model: nn.Layer. Specific Inception_V3 model depends on args.
"""
"""
def
__init__
(
self
,
config
,
class_num
=
1000
):
def
__init__
(
self
,
config
,
class_num
=
1000
,
return_patterns
=
None
):
super
().
__init__
()
super
().
__init__
()
self
.
inception_a_list
=
config
[
"inception_a"
]
self
.
inception_a_list
=
config
[
"inception_a"
]
...
@@ -496,6 +496,9 @@ class Inception_V3(TheseusLayer):
...
@@ -496,6 +496,9 @@ class Inception_V3(TheseusLayer):
class_num
,
class_num
,
weight_attr
=
ParamAttr
(
initializer
=
Uniform
(
-
stdv
,
stdv
)),
weight_attr
=
ParamAttr
(
initializer
=
Uniform
(
-
stdv
,
stdv
)),
bias_attr
=
ParamAttr
())
bias_attr
=
ParamAttr
())
if
return_patterns
is
not
None
:
self
.
update_res
(
return_patterns
)
self
.
register_forward_post_hook
(
self
.
_return_dict_hook
)
def
forward
(
self
,
x
):
def
forward
(
self
,
x
):
x
=
self
.
inception_stem
(
x
)
x
=
self
.
inception_stem
(
x
)
...
...
ppcls/arch/backbone/legendary_models/mobilenet_v1.py
浏览文件 @
e9162595
...
@@ -102,7 +102,7 @@ class MobileNet(TheseusLayer):
...
@@ -102,7 +102,7 @@ class MobileNet(TheseusLayer):
model: nn.Layer. Specific MobileNet model depends on args.
model: nn.Layer. Specific MobileNet model depends on args.
"""
"""
def
__init__
(
self
,
scale
=
1.0
,
class_num
=
1000
):
def
__init__
(
self
,
scale
=
1.0
,
class_num
=
1000
,
return_patterns
=
None
):
super
().
__init__
()
super
().
__init__
()
self
.
scale
=
scale
self
.
scale
=
scale
...
@@ -145,16 +145,16 @@ class MobileNet(TheseusLayer):
...
@@ -145,16 +145,16 @@ class MobileNet(TheseusLayer):
int
(
1024
*
scale
),
int
(
1024
*
scale
),
class_num
,
class_num
,
weight_attr
=
ParamAttr
(
initializer
=
KaimingNormal
()))
weight_attr
=
ParamAttr
(
initializer
=
KaimingNormal
()))
if
return_patterns
is
not
None
:
self
.
update_res
(
return_patterns
)
self
.
register_forward_post_hook
(
self
.
_return_dict_hook
)
def
forward
(
self
,
x
,
res_dict
=
None
):
def
forward
(
self
,
x
):
x
=
self
.
conv
(
x
)
x
=
self
.
conv
(
x
)
x
=
self
.
blocks
(
x
)
x
=
self
.
blocks
(
x
)
x
=
self
.
avg_pool
(
x
)
x
=
self
.
avg_pool
(
x
)
x
=
self
.
flatten
(
x
)
x
=
self
.
flatten
(
x
)
x
=
self
.
fc
(
x
)
x
=
self
.
fc
(
x
)
if
self
.
res_dict
and
res_dict
is
not
None
:
for
res_key
in
list
(
self
.
res_dict
):
res_dict
[
res_key
]
=
self
.
res_dict
.
pop
(
res_key
)
return
x
return
x
...
...
ppcls/arch/backbone/legendary_models/mobilenet_v3.py
浏览文件 @
e9162595
...
@@ -142,7 +142,8 @@ class MobileNetV3(TheseusLayer):
...
@@ -142,7 +142,8 @@ class MobileNetV3(TheseusLayer):
inplanes
=
STEM_CONV_NUMBER
,
inplanes
=
STEM_CONV_NUMBER
,
class_squeeze
=
LAST_SECOND_CONV_LARGE
,
class_squeeze
=
LAST_SECOND_CONV_LARGE
,
class_expand
=
LAST_CONV
,
class_expand
=
LAST_CONV
,
dropout_prob
=
0.2
):
dropout_prob
=
0.2
,
return_patterns
=
None
):
super
().
__init__
()
super
().
__init__
()
self
.
cfg
=
config
self
.
cfg
=
config
...
@@ -199,6 +200,9 @@ class MobileNetV3(TheseusLayer):
...
@@ -199,6 +200,9 @@ class MobileNetV3(TheseusLayer):
self
.
flatten
=
nn
.
Flatten
(
start_axis
=
1
,
stop_axis
=-
1
)
self
.
flatten
=
nn
.
Flatten
(
start_axis
=
1
,
stop_axis
=-
1
)
self
.
fc
=
Linear
(
self
.
class_expand
,
class_num
)
self
.
fc
=
Linear
(
self
.
class_expand
,
class_num
)
if
return_patterns
is
not
None
:
self
.
update_res
(
return_patterns
)
self
.
register_forward_post_hook
(
self
.
_return_dict_hook
)
def
forward
(
self
,
x
):
def
forward
(
self
,
x
):
x
=
self
.
conv
(
x
)
x
=
self
.
conv
(
x
)
...
...
ppcls/arch/backbone/legendary_models/resnet.py
浏览文件 @
e9162595
...
@@ -269,7 +269,8 @@ class ResNet(TheseusLayer):
...
@@ -269,7 +269,8 @@ class ResNet(TheseusLayer):
class_num
=
1000
,
class_num
=
1000
,
lr_mult_list
=
[
1.0
,
1.0
,
1.0
,
1.0
,
1.0
],
lr_mult_list
=
[
1.0
,
1.0
,
1.0
,
1.0
,
1.0
],
data_format
=
"NCHW"
,
data_format
=
"NCHW"
,
input_image_channel
=
3
):
input_image_channel
=
3
,
return_patterns
=
None
):
super
().
__init__
()
super
().
__init__
()
self
.
cfg
=
config
self
.
cfg
=
config
...
@@ -337,6 +338,9 @@ class ResNet(TheseusLayer):
...
@@ -337,6 +338,9 @@ class ResNet(TheseusLayer):
weight_attr
=
ParamAttr
(
initializer
=
Uniform
(
-
stdv
,
stdv
)))
weight_attr
=
ParamAttr
(
initializer
=
Uniform
(
-
stdv
,
stdv
)))
self
.
data_format
=
data_format
self
.
data_format
=
data_format
if
return_patterns
is
not
None
:
self
.
update_res
(
return_patterns
)
self
.
register_forward_post_hook
(
self
.
_return_dict_hook
)
def
forward
(
self
,
x
):
def
forward
(
self
,
x
):
with
paddle
.
static
.
amp
.
fp16_guard
():
with
paddle
.
static
.
amp
.
fp16_guard
():
...
...
ppcls/arch/backbone/legendary_models/vgg.py
浏览文件 @
e9162595
...
@@ -111,7 +111,7 @@ class VGGNet(TheseusLayer):
...
@@ -111,7 +111,7 @@ class VGGNet(TheseusLayer):
model: nn.Layer. Specific VGG model depends on args.
model: nn.Layer. Specific VGG model depends on args.
"""
"""
def
__init__
(
self
,
config
,
stop_grad_layers
=
0
,
class_num
=
1000
):
def
__init__
(
self
,
config
,
stop_grad_layers
=
0
,
class_num
=
1000
,
return_patterns
=
None
):
super
().
__init__
()
super
().
__init__
()
self
.
stop_grad_layers
=
stop_grad_layers
self
.
stop_grad_layers
=
stop_grad_layers
...
@@ -137,8 +137,11 @@ class VGGNet(TheseusLayer):
...
@@ -137,8 +137,11 @@ class VGGNet(TheseusLayer):
self
.
fc1
=
Linear
(
7
*
7
*
512
,
4096
)
self
.
fc1
=
Linear
(
7
*
7
*
512
,
4096
)
self
.
fc2
=
Linear
(
4096
,
4096
)
self
.
fc2
=
Linear
(
4096
,
4096
)
self
.
fc3
=
Linear
(
4096
,
class_num
)
self
.
fc3
=
Linear
(
4096
,
class_num
)
if
return_patterns
is
not
None
:
self
.
update_res
(
return_patterns
)
self
.
register_forward_post_hook
(
self
.
_return_dict_hook
)
def
forward
(
self
,
inputs
,
res_dict
=
None
):
def
forward
(
self
,
inputs
):
x
=
self
.
conv_block_1
(
inputs
)
x
=
self
.
conv_block_1
(
inputs
)
x
=
self
.
conv_block_2
(
x
)
x
=
self
.
conv_block_2
(
x
)
x
=
self
.
conv_block_3
(
x
)
x
=
self
.
conv_block_3
(
x
)
...
@@ -152,9 +155,6 @@ class VGGNet(TheseusLayer):
...
@@ -152,9 +155,6 @@ class VGGNet(TheseusLayer):
x
=
self
.
relu
(
x
)
x
=
self
.
relu
(
x
)
x
=
self
.
drop
(
x
)
x
=
self
.
drop
(
x
)
x
=
self
.
fc3
(
x
)
x
=
self
.
fc3
(
x
)
if
self
.
res_dict
and
res_dict
is
not
None
:
for
res_key
in
list
(
self
.
res_dict
):
res_dict
[
res_key
]
=
self
.
res_dict
.
pop
(
res_key
)
return
x
return
x
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录