Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleClas
提交
c05df5fa
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看板
提交
c05df5fa
编写于
5月 28, 2021
作者:
W
weishengyu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
modify code style
上级
dfe7f5b0
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
109 addition
and
95 deletion
+109
-95
docs/zh_CN/feature_visiualization/get_started.md
docs/zh_CN/feature_visiualization/get_started.md
+1
-1
ppcls/arch/backbone/legendary_models/hrnet.py
ppcls/arch/backbone/legendary_models/hrnet.py
+108
-94
未找到文件。
docs/zh_CN/feature_visiualization/get_started.md
浏览文件 @
c05df5fa
...
...
@@ -37,7 +37,7 @@ def forward(self, inputs):
y
=
self
.
pool2d_max
(
y
)
for
bottleneck_block
in
self
.
bottleneck_block_list
:
y
=
bottleneck_block
(
y
)
y
=
self
.
pool2d_avg
(
y
)
y
=
self
.
avg_pool
(
y
)
y
=
fluid
.
layers
.
reshape
(
y
,
shape
=
[
-
1
,
self
.
pool2d_avg_output
])
y
=
self
.
out
(
y
)
return
y
,
self
.
fm
...
...
ppcls/arch/backbone/legendary_models/hrnet.py
浏览文件 @
c05df5fa
...
...
@@ -17,7 +17,6 @@ from __future__ import division
from
__future__
import
print_function
import
math
import
numpy
as
np
import
paddle
from
paddle
import
ParamAttr
import
paddle.nn
as
nn
...
...
@@ -25,7 +24,7 @@ import paddle.nn.functional as F
from
paddle.nn
import
AdaptiveAvgPool2D
,
MaxPool2D
,
AvgPool2D
from
paddle.nn.initializer
import
Uniform
from
ppcls.arch.backbone.base.theseus_layer
import
TheseusLayer
from
ppcls.arch.backbone.base.theseus_layer
import
TheseusLayer
,
Identity
__all__
=
[
"HRNet_W18_C"
,
...
...
@@ -57,7 +56,7 @@ class ConvBNLayer(TheseusLayer):
act
=
"relu"
):
super
(
ConvBNLayer
,
self
).
__init__
()
self
.
_
conv
=
nn
.
Conv2D
(
self
.
conv
=
nn
.
Conv2D
(
in_channels
=
num_channels
,
out_channels
=
num_filters
,
kernel_size
=
filter_size
,
...
...
@@ -65,14 +64,28 @@ class ConvBNLayer(TheseusLayer):
padding
=
(
filter_size
-
1
)
//
2
,
groups
=
groups
,
bias_attr
=
False
)
self
.
_batch_norm
=
nn
.
BatchNorm
(
self
.
bn
=
nn
.
BatchNorm
(
num_filters
,
act
=
act
)
act
=
None
)
self
.
act
=
create_act
(
act
)
def
forward
(
self
,
x
,
res_dict
=
None
):
y
=
self
.
_conv
(
x
)
y
=
self
.
_batch_norm
(
y
)
return
y
def
forward
(
self
,
x
):
x
=
self
.
conv
(
x
)
x
=
self
.
bn
(
x
)
x
=
self
.
act
(
x
)
return
x
def
create_act
(
act
):
if
act
==
'hardswish'
:
return
nn
.
Hardswish
()
elif
act
==
'relu'
:
return
nn
.
ReLU
()
elif
act
is
None
:
return
Identity
()
else
:
raise
RuntimeError
(
'The activation function is not supported: {}'
.
format
(
act
))
class
BottleneckBlock
(
TheseusLayer
):
...
...
@@ -116,22 +129,20 @@ class BottleneckBlock(TheseusLayer):
num_channels
=
num_filters
*
4
,
num_filters
=
num_filters
*
4
,
reduction_ratio
=
16
)
self
.
relu
=
nn
.
ReLU
()
def
forward
(
self
,
x
,
res_dict
=
None
):
residual
=
x
conv1
=
self
.
conv1
(
x
)
conv2
=
self
.
conv2
(
conv1
)
conv3
=
self
.
conv3
(
conv2
)
x
=
self
.
conv1
(
x
)
x
=
self
.
conv2
(
x
)
x
=
self
.
conv3
(
x
)
if
self
.
downsample
:
residual
=
self
.
conv_down
(
x
)
residual
=
self
.
conv_down
(
residual
)
if
self
.
has_se
:
conv3
=
self
.
se
(
conv3
)
y
=
paddle
.
add
(
x
=
residual
,
y
=
conv3
)
y
=
F
.
relu
(
y
)
return
y
x
=
self
.
se
(
x
)
x
=
paddle
.
add
(
x
=
residual
,
y
=
x
)
x
=
self
.
relu
(
x
)
return
x
class
BasicBlock
(
nn
.
Layer
):
...
...
@@ -161,18 +172,19 @@ class BasicBlock(nn.Layer):
num_channels
=
num_filters
,
num_filters
=
num_filters
,
reduction_ratio
=
16
)
self
.
relu
=
nn
.
ReLU
()
def
forward
(
self
,
input
):
residual
=
input
conv1
=
self
.
conv1
(
input
)
conv2
=
self
.
conv2
(
conv1
)
def
forward
(
self
,
x
):
residual
=
x
x
=
self
.
conv1
(
x
)
x
=
self
.
conv2
(
x
)
if
self
.
has_se
:
conv2
=
self
.
se
(
conv2
)
x
=
self
.
se
(
x
)
y
=
paddle
.
add
(
x
=
residual
,
y
=
conv2
)
y
=
F
.
relu
(
y
)
return
y
x
=
paddle
.
add
(
x
=
residual
,
y
=
x
)
x
=
self
.
relu
(
x
)
return
x
class
SELayer
(
TheseusLayer
):
...
...
@@ -185,29 +197,31 @@ class SELayer(TheseusLayer):
med_ch
=
int
(
num_channels
/
reduction_ratio
)
stdv
=
1.0
/
math
.
sqrt
(
num_channels
*
1.0
)
self
.
squeeze
=
nn
.
Linear
(
self
.
fc_
squeeze
=
nn
.
Linear
(
num_channels
,
med_ch
,
weight_attr
=
ParamAttr
(
initializer
=
Uniform
(
-
stdv
,
stdv
)))
self
.
relu
=
nn
.
ReLU
()
stdv
=
1.0
/
math
.
sqrt
(
med_ch
*
1.0
)
self
.
excitation
=
nn
.
Linear
(
self
.
fc_
excitation
=
nn
.
Linear
(
med_ch
,
num_filters
,
weight_attr
=
ParamAttr
(
initializer
=
Uniform
(
-
stdv
,
stdv
)))
self
.
sigmoid
=
nn
.
Sigmoid
()
def
forward
(
self
,
input
,
res_dict
=
None
):
pool
=
self
.
pool2d_gap
(
input
)
pool
=
paddle
.
squeeze
(
pool
,
axis
=
[
2
,
3
])
squeeze
=
self
.
squeeze
(
pool
)
squeeze
=
F
.
relu
(
squeeze
)
excitation
=
self
.
excitation
(
squeeze
)
excitation
=
F
.
sigmoid
(
excitation
)
excitation
=
paddle
.
unsqueeze
(
excitation
,
axis
=
[
2
,
3
])
out
=
input
*
excitation
return
out
def
forward
(
self
,
x
,
res_dict
=
None
):
residual
=
x
x
=
self
.
pool2d_gap
(
x
)
x
=
paddle
.
squeeze
(
x
,
axis
=
[
2
,
3
])
x
=
self
.
fc_squeeze
(
x
)
x
=
self
.
relu
(
x
)
x
=
self
.
fc_excitation
(
x
)
x
=
self
.
sigmoid
(
x
)
x
=
paddle
.
unsqueeze
(
x
,
axis
=
[
2
,
3
])
x
=
residual
*
x
return
x
class
Stage
(
TheseusLayer
):
...
...
@@ -226,11 +240,11 @@ class Stage(TheseusLayer):
num_filters
=
num_filters
,
has_se
=
has_se
))
def
forward
(
self
,
input
,
res_dict
=
None
):
out
=
input
def
forward
(
self
,
x
,
res_dict
=
None
):
x
=
x
for
idx
in
range
(
self
.
_num_modules
):
out
=
self
.
stage_func_list
[
idx
](
out
)
return
out
x
=
self
.
stage_func_list
[
idx
](
x
)
return
x
class
HighResolutionModule
(
TheseusLayer
):
...
...
@@ -253,15 +267,14 @@ class HighResolutionModule(TheseusLayer):
in_channels
=
num_filters
,
out_channels
=
num_filters
)
def
forward
(
self
,
input
,
res_dict
=
None
):
outs
=
[]
for
idx
,
input
in
enumerate
(
input
):
conv
=
input
def
forward
(
self
,
x
,
res_dict
=
None
):
out
=
[]
for
idx
,
xi
in
enumerate
(
x
):
basic_block_list
=
self
.
basic_block_list
[
idx
]
for
basic_block_func
in
basic_block_list
:
conv
=
basic_block_func
(
conv
)
out
s
.
append
(
conv
)
out
=
self
.
fuse_func
(
out
s
)
xi
=
basic_block_func
(
xi
)
out
.
append
(
xi
)
out
=
self
.
fuse_func
(
out
)
return
out
...
...
@@ -275,6 +288,7 @@ class FuseLayers(TheseusLayer):
self
.
_in_channels
=
in_channels
self
.
residual_func_list
=
nn
.
LayerList
()
self
.
relu
=
nn
.
ReLU
()
for
i
in
range
(
len
(
in_channels
)):
for
j
in
range
(
len
(
in_channels
)):
if
j
>
i
:
...
...
@@ -307,30 +321,30 @@ class FuseLayers(TheseusLayer):
act
=
"relu"
))
pre_num_filters
=
out_channels
[
j
]
def
forward
(
self
,
input
,
res_dict
=
None
):
out
s
=
[]
def
forward
(
self
,
x
,
res_dict
=
None
):
out
=
[]
residual_func_idx
=
0
for
i
in
range
(
len
(
self
.
_in_channels
)):
residual
=
input
[
i
]
residual
=
x
[
i
]
for
j
in
range
(
len
(
self
.
_in_channels
)):
if
j
>
i
:
y
=
self
.
residual_func_list
[
residual_func_idx
](
input
[
j
])
xj
=
self
.
residual_func_list
[
residual_func_idx
](
x
[
j
])
residual_func_idx
+=
1
y
=
F
.
upsample
(
y
,
scale_factor
=
2
**
(
j
-
i
),
mode
=
"nearest"
)
residual
=
paddle
.
add
(
x
=
residual
,
y
=
y
)
xj
=
F
.
upsample
(
xj
,
scale_factor
=
2
**
(
j
-
i
),
mode
=
"nearest"
)
residual
=
paddle
.
add
(
x
=
residual
,
y
=
xj
)
elif
j
<
i
:
y
=
input
[
j
]
xj
=
x
[
j
]
for
k
in
range
(
i
-
j
):
y
=
self
.
residual_func_list
[
residual_func_idx
](
y
)
xj
=
self
.
residual_func_list
[
residual_func_idx
](
xj
)
residual_func_idx
+=
1
residual
=
paddle
.
add
(
x
=
residual
,
y
=
y
)
residual
=
paddle
.
add
(
x
=
residual
,
y
=
xj
)
residual
=
F
.
relu
(
residual
)
out
s
.
append
(
residual
)
residual
=
self
.
relu
(
residual
)
out
.
append
(
residual
)
return
out
s
return
out
class
LastClsOut
(
TheseusLayer
):
...
...
@@ -349,12 +363,12 @@ class LastClsOut(TheseusLayer):
has_se
=
has_se
,
downsample
=
True
))
def
forward
(
self
,
inputs
,
res_dict
=
None
):
out
s
=
[]
for
idx
,
input
in
enumerate
(
inputs
):
out
=
self
.
func_list
[
idx
](
input
)
out
s
.
append
(
out
)
return
out
s
def
forward
(
self
,
x
,
res_dict
=
None
):
out
=
[]
for
idx
,
xi
in
enumerate
(
x
):
xi
=
self
.
func_list
[
idx
](
xi
)
out
.
append
(
xi
)
return
out
class
HRNet
(
TheseusLayer
):
...
...
@@ -400,11 +414,11 @@ class HRNet(TheseusLayer):
for
i
in
range
(
4
)
])
self
.
tr1_1
=
ConvBNLayer
(
self
.
conv_
tr1_1
=
ConvBNLayer
(
num_channels
=
256
,
num_filters
=
width
,
filter_size
=
3
)
self
.
tr1_2
=
ConvBNLayer
(
self
.
conv_
tr1_2
=
ConvBNLayer
(
num_channels
=
256
,
num_filters
=
width
*
2
,
filter_size
=
3
,
...
...
@@ -416,7 +430,7 @@ class HRNet(TheseusLayer):
num_filters
=
channels_2
,
has_se
=
self
.
has_se
)
self
.
tr2
=
ConvBNLayer
(
self
.
conv_
tr2
=
ConvBNLayer
(
num_channels
=
width
*
2
,
num_filters
=
width
*
4
,
filter_size
=
3
,
...
...
@@ -427,7 +441,7 @@ class HRNet(TheseusLayer):
num_filters
=
channels_3
,
has_se
=
self
.
has_se
)
self
.
tr3
=
ConvBNLayer
(
self
.
conv_
tr3
=
ConvBNLayer
(
num_channels
=
width
*
4
,
num_filters
=
width
*
8
,
filter_size
=
3
,
...
...
@@ -462,44 +476,44 @@ class HRNet(TheseusLayer):
filter_size
=
1
,
stride
=
1
)
self
.
pool2d_avg
=
AdaptiveAvgPool2D
(
1
)
self
.
avg_pool
=
AdaptiveAvgPool2D
(
1
)
stdv
=
1.0
/
math
.
sqrt
(
2048
*
1.0
)
self
.
out
=
nn
.
Linear
(
self
.
fc
=
nn
.
Linear
(
2048
,
class_num
,
weight_attr
=
ParamAttr
(
initializer
=
Uniform
(
-
stdv
,
stdv
)))
def
forward
(
self
,
input
,
res_dict
=
None
):
conv1
=
self
.
conv_layer1_1
(
input
)
conv2
=
self
.
conv_layer1_2
(
conv1
)
def
forward
(
self
,
x
,
res_dict
=
None
):
x
=
self
.
conv_layer1_1
(
x
)
x
=
self
.
conv_layer1_2
(
x
)
la1
=
self
.
layer1
(
conv2
)
x
=
self
.
layer1
(
x
)
tr1_1
=
self
.
tr1_1
(
la1
)
tr1_2
=
self
.
tr1_2
(
la1
)
st2
=
self
.
st2
([
tr1_1
,
tr1_2
])
tr1_1
=
self
.
conv_tr1_1
(
x
)
tr1_2
=
self
.
conv_tr1_2
(
x
)
x
=
self
.
st2
([
tr1_1
,
tr1_2
])
tr2
=
self
.
tr2
(
st2
[
-
1
])
st2
.
append
(
tr2
)
st3
=
self
.
st3
(
st2
)
tr2
=
self
.
conv_tr2
(
x
[
-
1
])
x
.
append
(
tr2
)
x
=
self
.
st3
(
x
)
tr3
=
self
.
tr3
(
st3
[
-
1
])
st3
.
append
(
tr3
)
st4
=
self
.
st4
(
st3
)
tr3
=
self
.
conv_tr3
(
x
[
-
1
])
x
.
append
(
tr3
)
x
=
self
.
st4
(
x
)
last_cls
=
self
.
last_cls
(
st4
)
x
=
self
.
last_cls
(
x
)
y
=
last_cls
[
0
]
y
=
x
[
0
]
for
idx
in
range
(
3
):
y
=
paddle
.
add
(
last_cls
[
idx
+
1
],
self
.
cls_head_conv_list
[
idx
](
y
))
y
=
paddle
.
add
(
x
[
idx
+
1
],
self
.
cls_head_conv_list
[
idx
](
y
))
y
=
self
.
conv_last
(
y
)
y
=
self
.
pool2d_avg
(
y
)
y
=
self
.
avg_pool
(
y
)
y
=
paddle
.
reshape
(
y
,
shape
=
[
-
1
,
y
.
shape
[
1
]])
y
=
self
.
out
(
y
)
y
=
self
.
fc
(
y
)
return
y
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录