Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleClas
提交
4970bca0
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看板
未验证
提交
4970bca0
编写于
9月 18, 2020
作者:
C
cuicheng01
提交者:
GitHub
9月 18, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update se_resnext_vd.py
上级
b7b5a0c3
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
37 addition
and
37 deletion
+37
-37
ppcls/modeling/architectures/se_resnext_vd.py
ppcls/modeling/architectures/se_resnext_vd.py
+37
-37
未找到文件。
ppcls/modeling/architectures/se_resnext_vd.py
浏览文件 @
4970bca0
...
@@ -18,16 +18,18 @@ from __future__ import print_function
...
@@ -18,16 +18,18 @@ from __future__ import print_function
import
numpy
as
np
import
numpy
as
np
import
paddle
import
paddle
import
paddle.fluid
as
fluid
from
paddle
import
ParamAttr
from
paddle.fluid.param_attr
import
ParamAttr
import
paddle.nn
as
nn
from
paddle.fluid.dygraph.nn
import
Conv2D
,
Pool2D
,
BatchNorm
,
Linear
,
Dropout
from
paddle.nn
import
Conv2d
,
BatchNorm
,
Linear
,
Dropout
from
paddle.nn
import
AdaptiveAvgPool2d
,
MaxPool2d
,
AvgPool2d
from
paddle.nn.initializer
import
Uniform
import
math
import
math
__all__
=
[
"SE_ResNeXt50_vd_32x4d"
,
"SE_ResNeXt50_vd_32x4d"
,
"SENet154_vd"
]
__all__
=
[
"SE_ResNeXt50_vd_32x4d"
,
"SE_ResNeXt50_vd_32x4d"
,
"SENet154_vd"
]
class
ConvBNLayer
(
fluid
.
dygraph
.
Layer
):
class
ConvBNLayer
(
nn
.
Layer
):
def
__init__
(
def
__init__
(
self
,
self
,
num_channels
,
num_channels
,
...
@@ -37,21 +39,20 @@ class ConvBNLayer(fluid.dygraph.Layer):
...
@@ -37,21 +39,20 @@ class ConvBNLayer(fluid.dygraph.Layer):
groups
=
1
,
groups
=
1
,
is_vd_mode
=
False
,
is_vd_mode
=
False
,
act
=
None
,
act
=
None
,
name
=
None
,
):
name
=
None
):
super
(
ConvBNLayer
,
self
).
__init__
()
super
(
ConvBNLayer
,
self
).
__init__
()
self
.
is_vd_mode
=
is_vd_mode
self
.
is_vd_mode
=
is_vd_mode
self
.
_pool2d_avg
=
Pool2D
(
self
.
_pool2d_avg
=
AvgPool2d
(
pool_size
=
2
,
pool_stride
=
2
,
pool_padding
=
0
,
pool_type
=
'avg'
)
kernel_size
=
2
,
stride
=
2
,
padding
=
0
,
ceil_mode
=
True
)
self
.
_conv
=
Conv2
D
(
self
.
_conv
=
Conv2
d
(
num
_channels
=
num_channels
,
in
_channels
=
num_channels
,
num_filter
s
=
num_filters
,
out_channel
s
=
num_filters
,
filter
_size
=
filter_size
,
kernel
_size
=
filter_size
,
stride
=
stride
,
stride
=
stride
,
padding
=
(
filter_size
-
1
)
//
2
,
padding
=
(
filter_size
-
1
)
//
2
,
groups
=
groups
,
groups
=
groups
,
act
=
None
,
weight_attr
=
ParamAttr
(
name
=
name
+
"_weights"
),
param_attr
=
ParamAttr
(
name
=
name
+
"_weights"
),
bias_attr
=
False
)
bias_attr
=
False
)
bn_name
=
name
+
'_bn'
bn_name
=
name
+
'_bn'
self
.
_batch_norm
=
BatchNorm
(
self
.
_batch_norm
=
BatchNorm
(
...
@@ -70,7 +71,7 @@ class ConvBNLayer(fluid.dygraph.Layer):
...
@@ -70,7 +71,7 @@ class ConvBNLayer(fluid.dygraph.Layer):
return
y
return
y
class
BottleneckBlock
(
fluid
.
dygraph
.
Layer
):
class
BottleneckBlock
(
nn
.
Layer
):
def
__init__
(
self
,
def
__init__
(
self
,
num_channels
,
num_channels
,
num_filters
,
num_filters
,
...
@@ -106,7 +107,7 @@ class BottleneckBlock(fluid.dygraph.Layer):
...
@@ -106,7 +107,7 @@ class BottleneckBlock(fluid.dygraph.Layer):
num_channels
=
num_filters
*
2
if
cardinality
==
32
else
num_filters
,
num_channels
=
num_filters
*
2
if
cardinality
==
32
else
num_filters
,
num_filters
=
num_filters
*
2
if
cardinality
==
32
else
num_filters
,
num_filters
=
num_filters
*
2
if
cardinality
==
32
else
num_filters
,
reduction_ratio
=
reduction_ratio
,
reduction_ratio
=
reduction_ratio
,
name
=
'fc
_
'
+
name
)
name
=
'fc'
+
name
)
if
not
shortcut
:
if
not
shortcut
:
self
.
short
=
ConvBNLayer
(
self
.
short
=
ConvBNLayer
(
...
@@ -130,15 +131,15 @@ class BottleneckBlock(fluid.dygraph.Layer):
...
@@ -130,15 +131,15 @@ class BottleneckBlock(fluid.dygraph.Layer):
short
=
inputs
short
=
inputs
else
:
else
:
short
=
self
.
short
(
inputs
)
short
=
self
.
short
(
inputs
)
y
=
fluid
.
layers
.
elementwise_add
(
x
=
short
,
y
=
scale
,
act
=
'relu'
)
y
=
paddle
.
elementwise_add
(
x
=
short
,
y
=
scale
,
act
=
'relu'
)
return
y
return
y
class
SELayer
(
fluid
.
dygraph
.
Layer
):
class
SELayer
(
nn
.
Layer
):
def
__init__
(
self
,
num_channels
,
num_filters
,
reduction_ratio
,
name
=
None
):
def
__init__
(
self
,
num_channels
,
num_filters
,
reduction_ratio
,
name
=
None
):
super
(
SELayer
,
self
).
__init__
()
super
(
SELayer
,
self
).
__init__
()
self
.
pool2d_gap
=
Pool2D
(
pool_type
=
'avg'
,
global_pooling
=
True
)
self
.
pool2d_gap
=
AdaptiveAvgPool2d
(
1
)
self
.
_num_channels
=
num_channels
self
.
_num_channels
=
num_channels
...
@@ -147,34 +148,35 @@ class SELayer(fluid.dygraph.Layer):
...
@@ -147,34 +148,35 @@ class SELayer(fluid.dygraph.Layer):
self
.
squeeze
=
Linear
(
self
.
squeeze
=
Linear
(
num_channels
,
num_channels
,
med_ch
,
med_ch
,
act
=
"relu"
,
weight_attr
=
ParamAttr
(
param_attr
=
ParamAttr
(
initializer
=
Uniform
(
-
stdv
,
stdv
),
initializer
=
fluid
.
initializer
.
Uniform
(
-
stdv
,
stdv
),
name
=
name
+
"_sqz_weights"
),
name
=
name
+
"_sqz_weights"
),
bias_attr
=
ParamAttr
(
name
=
name
+
'_sqz_offset'
))
bias_attr
=
ParamAttr
(
name
=
name
+
'_sqz_offset'
))
self
.
relu
=
nn
.
ReLU
()
stdv
=
1.0
/
math
.
sqrt
(
med_ch
*
1.0
)
stdv
=
1.0
/
math
.
sqrt
(
med_ch
*
1.0
)
self
.
excitation
=
Linear
(
self
.
excitation
=
Linear
(
med_ch
,
med_ch
,
num_filters
,
num_filters
,
act
=
"sigmoid"
,
weight_attr
=
ParamAttr
(
param_attr
=
ParamAttr
(
initializer
=
Uniform
(
-
stdv
,
stdv
),
initializer
=
fluid
.
initializer
.
Uniform
(
-
stdv
,
stdv
),
name
=
name
+
"_exc_weights"
),
name
=
name
+
"_exc_weights"
),
bias_attr
=
ParamAttr
(
name
=
name
+
'_exc_offset'
))
bias_attr
=
ParamAttr
(
name
=
name
+
'_exc_offset'
))
self
.
sigmoid
=
nn
.
Sigmoid
()
def
forward
(
self
,
input
):
def
forward
(
self
,
input
):
pool
=
self
.
pool2d_gap
(
input
)
pool
=
self
.
pool2d_gap
(
input
)
pool
=
fluid
.
layers
.
reshape
(
pool
,
shape
=
[
-
1
,
self
.
_num_channels
])
pool
=
paddle
.
reshape
(
pool
,
shape
=
[
-
1
,
self
.
_num_channels
])
squeeze
=
self
.
squeeze
(
pool
)
squeeze
=
self
.
squeeze
(
pool
)
squeeze
=
self
.
relu
(
squeeze
)
excitation
=
self
.
excitation
(
squeeze
)
excitation
=
self
.
excitation
(
squeeze
)
excitation
=
fluid
.
layers
.
reshape
(
excitation
=
self
.
sigmoid
(
excitation
)
excitation
=
paddle
.
reshape
(
excitation
,
shape
=
[
-
1
,
self
.
_num_channels
,
1
,
1
])
excitation
,
shape
=
[
-
1
,
self
.
_num_channels
,
1
,
1
])
out
=
input
*
excitation
out
=
input
*
excitation
return
out
return
out
class
ResNeXt
(
fluid
.
dygraph
.
Layer
):
class
ResNeXt
(
nn
.
Layer
):
def
__init__
(
self
,
layers
=
50
,
class_dim
=
1000
,
cardinality
=
32
):
def
__init__
(
self
,
layers
=
50
,
class_dim
=
1000
,
cardinality
=
32
):
super
(
ResNeXt
,
self
).
__init__
()
super
(
ResNeXt
,
self
).
__init__
()
...
@@ -221,8 +223,7 @@ class ResNeXt(fluid.dygraph.Layer):
...
@@ -221,8 +223,7 @@ class ResNeXt(fluid.dygraph.Layer):
act
=
'relu'
,
act
=
'relu'
,
name
=
"conv1_3"
)
name
=
"conv1_3"
)
self
.
pool2d_max
=
Pool2D
(
self
.
pool2d_max
=
MaxPool2d
(
kernel_size
=
3
,
stride
=
2
,
padding
=
1
)
pool_size
=
3
,
pool_stride
=
2
,
pool_padding
=
1
,
pool_type
=
'max'
)
self
.
block_list
=
[]
self
.
block_list
=
[]
n
=
1
if
layers
==
50
or
layers
==
101
else
3
n
=
1
if
layers
==
50
or
layers
==
101
else
3
...
@@ -245,8 +246,7 @@ class ResNeXt(fluid.dygraph.Layer):
...
@@ -245,8 +246,7 @@ class ResNeXt(fluid.dygraph.Layer):
self
.
block_list
.
append
(
bottleneck_block
)
self
.
block_list
.
append
(
bottleneck_block
)
shortcut
=
True
shortcut
=
True
self
.
pool2d_avg
=
Pool2D
(
self
.
pool2d_avg
=
AdaptiveAvgPool2d
(
1
)
pool_size
=
7
,
pool_type
=
'avg'
,
global_pooling
=
True
)
self
.
pool2d_avg_channels
=
num_channels
[
-
1
]
*
2
self
.
pool2d_avg_channels
=
num_channels
[
-
1
]
*
2
...
@@ -255,8 +255,8 @@ class ResNeXt(fluid.dygraph.Layer):
...
@@ -255,8 +255,8 @@ class ResNeXt(fluid.dygraph.Layer):
self
.
out
=
Linear
(
self
.
out
=
Linear
(
self
.
pool2d_avg_channels
,
self
.
pool2d_avg_channels
,
class_dim
,
class_dim
,
param
_attr
=
ParamAttr
(
weight
_attr
=
ParamAttr
(
initializer
=
fluid
.
initializer
.
Uniform
(
-
stdv
,
stdv
),
initializer
=
Uniform
(
-
stdv
,
stdv
),
name
=
"fc6_weights"
),
name
=
"fc6_weights"
),
bias_attr
=
ParamAttr
(
name
=
"fc6_offset"
))
bias_attr
=
ParamAttr
(
name
=
"fc6_offset"
))
...
@@ -268,7 +268,7 @@ class ResNeXt(fluid.dygraph.Layer):
...
@@ -268,7 +268,7 @@ class ResNeXt(fluid.dygraph.Layer):
for
block
in
self
.
block_list
:
for
block
in
self
.
block_list
:
y
=
block
(
y
)
y
=
block
(
y
)
y
=
self
.
pool2d_avg
(
y
)
y
=
self
.
pool2d_avg
(
y
)
y
=
fluid
.
layers
.
reshape
(
y
,
shape
=
[
-
1
,
self
.
pool2d_avg_channels
])
y
=
paddle
.
reshape
(
y
,
shape
=
[
-
1
,
self
.
pool2d_avg_channels
])
y
=
self
.
out
(
y
)
y
=
self
.
out
(
y
)
return
y
return
y
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录