Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleClas
提交
07bb46ef
P
PaddleClas
项目概览
PaddlePaddle
/
PaddleClas
1 年多 前同步成功
通知
116
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看板
提交
07bb46ef
编写于
10月 15, 2020
作者:
W
weishengyu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
modify format
上级
44889ba4
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
86 addition
and
100 deletion
+86
-100
ppcls/modeling/architectures/ghostnet.py
ppcls/modeling/architectures/ghostnet.py
+51
-70
ppcls/modeling/architectures/shufflenet_v2.py
ppcls/modeling/architectures/shufflenet_v2.py
+35
-30
未找到文件。
ppcls/modeling/architectures/ghostnet.py
浏览文件 @
07bb46ef
...
...
@@ -25,21 +25,21 @@ from paddle.nn.initializer import Uniform
class
ConvBNLayer
(
nn
.
Layer
):
def
__init__
(
self
,
num
_channels
,
num_filter
s
,
filter
_size
,
in
_channels
,
out_channel
s
,
kernel
_size
,
stride
=
1
,
groups
=
1
,
act
=
None
,
act
=
"relu"
,
name
=
None
):
super
(
ConvBNLayer
,
self
).
__init__
()
self
.
_conv
=
Conv2d
(
in_channels
=
num
_channels
,
out_channels
=
num_filter
s
,
kernel_size
=
filter
_size
,
in_channels
=
in
_channels
,
out_channels
=
out_channel
s
,
kernel_size
=
kernel
_size
,
stride
=
stride
,
padding
=
(
filter
_size
-
1
)
//
2
,
padding
=
(
kernel
_size
-
1
)
//
2
,
groups
=
groups
,
weight_attr
=
ParamAttr
(
initializer
=
nn
.
initializer
.
MSRA
(),
name
=
name
+
"_weights"
),
bias_attr
=
False
...
...
@@ -48,16 +48,10 @@ class ConvBNLayer(nn.Layer):
# In the old version, moving_variance_name was name + "_variance"
self
.
_batch_norm
=
BatchNorm
(
num_
filter
s
,
num_
channels
=
out_channel
s
,
act
=
act
,
param_attr
=
ParamAttr
(
name
=
bn_name
+
"_scale"
,
regularizer
=
L2DecayRegularizer
(
regularization_coeff
=
0.0
)
),
bias_attr
=
ParamAttr
(
name
=
bn_name
+
"_offset"
,
regularizer
=
L2DecayRegularizer
(
regularization_coeff
=
0.0
)
),
param_attr
=
ParamAttr
(
name
=
bn_name
+
"_scale"
,
regularizer
=
L2DecayRegularizer
(
regularization_coeff
=
0.0
)),
bias_attr
=
ParamAttr
(
name
=
bn_name
+
"_offset"
,
regularizer
=
L2DecayRegularizer
(
regularization_coeff
=
0.0
)),
moving_mean_name
=
bn_name
+
"_mean"
,
moving_variance_name
=
bn_name
+
"_variance"
)
...
...
@@ -83,20 +77,14 @@ class SEBlock(nn.Layer):
self
.
squeeze
=
Linear
(
num_channels
,
med_ch
,
weight_attr
=
ParamAttr
(
initializer
=
Uniform
(
-
stdv
,
stdv
),
name
=
name
+
"_1_weights"
),
weight_attr
=
ParamAttr
(
initializer
=
Uniform
(
-
stdv
,
stdv
),
name
=
name
+
"_1_weights"
),
bias_attr
=
ParamAttr
(
name
=
name
+
"_1_offset"
)
)
stdv
=
1.0
/
math
.
sqrt
(
med_ch
*
1.0
)
self
.
excitation
=
Linear
(
med_ch
,
num_channels
,
weight_attr
=
ParamAttr
(
initializer
=
Uniform
(
-
stdv
,
stdv
),
name
=
name
+
"_2_weights"
),
weight_attr
=
ParamAttr
(
initializer
=
Uniform
(
-
stdv
,
stdv
),
name
=
name
+
"_2_weights"
),
bias_attr
=
ParamAttr
(
name
=
name
+
"_2_offset"
)
)
...
...
@@ -107,10 +95,7 @@ class SEBlock(nn.Layer):
squeeze
=
F
.
relu
(
squeeze
)
excitation
=
self
.
excitation
(
squeeze
)
excitation
=
paddle
.
fluid
.
layers
.
clip
(
x
=
excitation
,
min
=
0
,
max
=
1
)
excitation
=
paddle
.
reshape
(
excitation
,
shape
=
[
-
1
,
self
.
_num_channels
,
1
,
1
]
)
excitation
=
paddle
.
reshape
(
excitation
,
shape
=
[
-
1
,
self
.
_num_channels
,
1
,
1
])
out
=
inputs
*
excitation
return
out
...
...
@@ -118,7 +103,7 @@ class SEBlock(nn.Layer):
class
GhostModule
(
nn
.
Layer
):
def
__init__
(
self
,
num
_channels
,
in
_channels
,
output_channels
,
kernel_size
=
1
,
ratio
=
2
,
...
...
@@ -131,18 +116,18 @@ class GhostModule(nn.Layer):
init_channels
=
int
(
math
.
ceil
(
output_channels
/
ratio
))
new_channels
=
int
(
init_channels
*
(
ratio
-
1
))
self
.
primary_conv
=
ConvBNLayer
(
num_channels
=
num
_channels
,
num_filter
s
=
init_channels
,
filter
_size
=
kernel_size
,
in_channels
=
in
_channels
,
out_channel
s
=
init_channels
,
kernel
_size
=
kernel_size
,
stride
=
stride
,
groups
=
1
,
act
=
"relu"
if
relu
else
None
,
name
=
name
+
"_primary_conv"
)
self
.
cheap_operation
=
ConvBNLayer
(
num
_channels
=
init_channels
,
num_filter
s
=
new_channels
,
filter
_size
=
dw_size
,
in
_channels
=
init_channels
,
out_channel
s
=
new_channels
,
kernel
_size
=
dw_size
,
stride
=
1
,
groups
=
init_channels
,
act
=
"relu"
if
relu
else
None
,
...
...
@@ -159,7 +144,7 @@ class GhostModule(nn.Layer):
class
GhostBottleneck
(
nn
.
Layer
):
def
__init__
(
self
,
num
_channels
,
in
_channels
,
hidden_dim
,
output_channels
,
kernel_size
,
...
...
@@ -170,10 +155,10 @@ class GhostBottleneck(nn.Layer):
super
(
GhostBottleneck
,
self
).
__init__
()
self
.
_stride
=
stride
self
.
_use_se
=
use_se
self
.
_num_channels
=
num
_channels
self
.
_num_channels
=
in
_channels
self
.
_output_channels
=
output_channels
self
.
ghost_module_1
=
GhostModule
(
num_channels
=
num
_channels
,
in_channels
=
in
_channels
,
output_channels
=
hidden_dim
,
kernel_size
=
1
,
stride
=
1
,
...
...
@@ -182,9 +167,9 @@ class GhostBottleneck(nn.Layer):
)
if
stride
==
2
:
self
.
depthwise_conv
=
ConvBNLayer
(
num
_channels
=
hidden_dim
,
num_filter
s
=
hidden_dim
,
filter
_size
=
kernel_size
,
in
_channels
=
hidden_dim
,
out_channel
s
=
hidden_dim
,
kernel
_size
=
kernel_size
,
stride
=
stride
,
groups
=
hidden_dim
,
act
=
None
,
...
...
@@ -196,26 +181,26 @@ class GhostBottleneck(nn.Layer):
name
=
name
+
"_se"
)
self
.
ghost_module_2
=
GhostModule
(
num
_channels
=
hidden_dim
,
in
_channels
=
hidden_dim
,
output_channels
=
output_channels
,
kernel_size
=
1
,
relu
=
False
,
name
=
name
+
"_ghost_module_2"
)
if
stride
!=
1
or
num
_channels
!=
output_channels
:
if
stride
!=
1
or
in
_channels
!=
output_channels
:
self
.
shortcut_depthwise
=
ConvBNLayer
(
num_channels
=
num
_channels
,
num_filters
=
num
_channels
,
filter
_size
=
kernel_size
,
in_channels
=
in
_channels
,
out_channels
=
in
_channels
,
kernel
_size
=
kernel_size
,
stride
=
stride
,
groups
=
num
_channels
,
groups
=
in
_channels
,
act
=
None
,
name
=
name
+
"_shortcut_depthwise"
# In the old version, name was name + "_shortcut_depthwise_depthwise"
)
self
.
shortcut_conv
=
ConvBNLayer
(
num_channels
=
num
_channels
,
num_filter
s
=
output_channels
,
filter
_size
=
1
,
in_channels
=
in
_channels
,
out_channel
s
=
output_channels
,
kernel
_size
=
1
,
stride
=
1
,
groups
=
1
,
act
=
None
,
...
...
@@ -262,9 +247,9 @@ class GhostNet(nn.Layer):
self
.
scale
=
scale
output_channels
=
int
(
self
.
_make_divisible
(
16
*
self
.
scale
,
4
))
self
.
conv1
=
ConvBNLayer
(
num
_channels
=
3
,
num_filter
s
=
output_channels
,
filter
_size
=
3
,
in
_channels
=
3
,
out_channel
s
=
output_channels
,
kernel
_size
=
3
,
stride
=
2
,
groups
=
1
,
act
=
"relu"
,
...
...
@@ -274,13 +259,13 @@ class GhostNet(nn.Layer):
idx
=
0
self
.
ghost_bottleneck_list
=
[]
for
k
,
exp_size
,
c
,
use_se
,
s
in
self
.
cfgs
:
num
_channels
=
output_channels
in
_channels
=
output_channels
output_channels
=
int
(
self
.
_make_divisible
(
c
*
self
.
scale
,
4
))
hidden_dim
=
int
(
self
.
_make_divisible
(
exp_size
,
self
.
scale
,
4
))
ghost_bottleneck
=
self
.
add_sublayer
(
name
=
"_ghostbottleneck_"
+
str
(
idx
),
sublayer
=
GhostBottleneck
(
num_channels
=
num
_channels
,
in_channels
=
in
_channels
,
hidden_dim
=
hidden_dim
,
output_channels
=
output_channels
,
kernel_size
=
k
,
...
...
@@ -292,25 +277,24 @@ class GhostNet(nn.Layer):
self
.
ghost_bottleneck_list
.
append
(
ghost_bottleneck
)
idx
+=
1
# build last several layers
num
_channels
=
output_channels
in
_channels
=
output_channels
output_channels
=
int
(
self
.
_make_divisible
(
exp_size
*
self
.
scale
,
4
))
self
.
conv_last
=
ConvBNLayer
(
num_channels
=
num
_channels
,
num_filter
s
=
output_channels
,
filter
_size
=
1
,
in_channels
=
in
_channels
,
out_channel
s
=
output_channels
,
kernel
_size
=
1
,
stride
=
1
,
groups
=
1
,
act
=
"relu"
,
name
=
"conv_last"
)
self
.
pool2d_gap
=
AdaptiveAvgPool2d
(
1
)
num_channels
=
output_channels
self
.
_num_channels
=
num_channels
in_channels
=
output_channels
self
.
_fc0_output_channels
=
1280
self
.
fc_0
=
ConvBNLayer
(
num_channels
=
num
_channels
,
num_filter
s
=
self
.
_fc0_output_channels
,
filter
_size
=
1
,
in_channels
=
in
_channels
,
out_channel
s
=
self
.
_fc0_output_channels
,
kernel
_size
=
1
,
stride
=
1
,
act
=
"relu"
,
name
=
"fc_0"
...
...
@@ -320,10 +304,7 @@ class GhostNet(nn.Layer):
self
.
fc_1
=
Linear
(
self
.
_fc0_output_channels
,
class_dim
,
weight_attr
=
ParamAttr
(
name
=
"fc_1_weights"
,
initializer
=
Uniform
(
-
stdv
,
stdv
)
),
weight_attr
=
ParamAttr
(
name
=
"fc_1_weights"
,
initializer
=
Uniform
(
-
stdv
,
stdv
)),
bias_attr
=
ParamAttr
(
name
=
"fc_1_offset"
)
)
...
...
@@ -367,4 +348,4 @@ def GhostNet_x1_0(**args):
def
GhostNet_x1_3
(
**
args
):
model
=
GhostNet
(
scale
=
1.3
)
return
model
\ No newline at end of file
return
model
ppcls/modeling/architectures/shufflenet_v2.py
浏览文件 @
07bb46ef
...
...
@@ -19,7 +19,7 @@ from __future__ import print_function
from
paddle
import
ParamAttr
,
reshape
,
transpose
,
concat
,
split
from
paddle.nn
import
Layer
,
Conv2d
,
MaxPool2d
,
AdaptiveAvgPool2d
,
BatchNorm
,
Linear
from
paddle.nn.initializer
import
MSRA
from
paddle.nn.functional
import
relu
,
swish
from
paddle.nn.functional
import
swish
__all__
=
[
...
...
@@ -53,11 +53,10 @@ class ConvBNLayer(Layer):
stride
,
padding
,
groups
=
1
,
act
=
relu
,
act
=
None
,
name
=
None
,
):
super
(
ConvBNLayer
,
self
).
__init__
()
self
.
_act
=
act
self
.
_conv
=
Conv2d
(
in_channels
=
in_channels
,
out_channels
=
out_channels
,
...
...
@@ -66,12 +65,14 @@ class ConvBNLayer(Layer):
padding
=
padding
,
groups
=
groups
,
weight_attr
=
ParamAttr
(
initializer
=
MSRA
(),
name
=
name
+
"_weights"
),
bias_attr
=
False
)
bias_attr
=
False
)
self
.
_batch_norm
=
BatchNorm
(
out_channels
,
param_attr
=
ParamAttr
(
name
=
name
+
"_bn_scale"
),
bias_attr
=
ParamAttr
(
name
=
name
+
"_bn_offset"
),
act
=
act
,
moving_mean_name
=
name
+
"_bn_mean"
,
moving_variance_name
=
name
+
"_bn_variance"
)
...
...
@@ -79,18 +80,18 @@ class ConvBNLayer(Layer):
def
forward
(
self
,
inputs
):
y
=
self
.
_conv
(
inputs
)
y
=
self
.
_batch_norm
(
y
)
if
self
.
_act
:
y
=
self
.
_act
(
y
)
return
y
class
InvertedResidual
(
Layer
):
def
__init__
(
self
,
in_channels
,
out_channels
,
stride
,
act
=
relu
,
name
=
None
):
def
__init__
(
self
,
in_channels
,
out_channels
,
stride
,
act
=
"relu"
,
name
=
None
):
super
(
InvertedResidual
,
self
).
__init__
()
self
.
_conv_pw
=
ConvBNLayer
(
in_channels
=
in_channels
//
2
,
...
...
@@ -124,25 +125,23 @@ class InvertedResidual(Layer):
)
def
forward
(
self
,
inputs
):
x1
,
x2
=
split
(
inputs
,
num_or_sections
=
[
inputs
.
shape
[
1
]
//
2
,
inputs
.
shape
[
1
]
//
2
],
axis
=
1
)
x1
,
x2
=
split
(
inputs
,
num_or_sections
=
[
inputs
.
shape
[
1
]
//
2
,
inputs
.
shape
[
1
]
//
2
],
axis
=
1
)
x2
=
self
.
_conv_pw
(
x2
)
x2
=
self
.
_conv_dw
(
x2
)
x2
=
self
.
_conv_linear
(
x2
)
out
=
concat
([
x1
,
x2
],
axis
=
1
)
return
channel_shuffle
(
out
,
2
)
class
InvertedResidualDS
(
Layer
):
def
__init__
(
self
,
in_channels
,
out_channels
,
stride
,
act
=
relu
,
name
=
None
):
def
__init__
(
self
,
in_channels
,
out_channels
,
stride
,
act
=
"relu"
,
name
=
None
):
super
(
InvertedResidualDS
,
self
).
__init__
()
# branch1
...
...
@@ -201,7 +200,6 @@ class InvertedResidualDS(Layer):
def
forward
(
self
,
inputs
):
x1
=
self
.
_conv_dw_1
(
inputs
)
x1
=
self
.
_conv_linear_1
(
x1
)
x2
=
self
.
_conv_pw_2
(
inputs
)
x2
=
self
.
_conv_dw_2
(
x2
)
x2
=
self
.
_conv_linear_2
(
x2
)
...
...
@@ -211,7 +209,7 @@ class InvertedResidualDS(Layer):
class
ShuffleNet
(
Layer
):
def
__init__
(
self
,
class_dim
=
1000
,
scale
=
1.0
,
act
=
relu
):
def
__init__
(
self
,
class_dim
=
1000
,
scale
=
1.0
,
act
=
"relu"
):
super
(
ShuffleNet
,
self
).
__init__
()
self
.
scale
=
scale
self
.
class_dim
=
class_dim
...
...
@@ -240,7 +238,8 @@ class ShuffleNet(Layer):
stride
=
2
,
padding
=
1
,
act
=
act
,
name
=
'stage1_conv'
)
name
=
'stage1_conv'
)
self
.
_max_pool
=
MaxPool2d
(
kernel_size
=
3
,
stride
=
2
,
...
...
@@ -259,7 +258,9 @@ class ShuffleNet(Layer):
out_channels
=
stage_out_channels
[
stage_id
+
2
],
stride
=
2
,
act
=
act
,
name
=
str
(
stage_id
+
2
)
+
'_'
+
str
(
i
+
1
)))
name
=
str
(
stage_id
+
2
)
+
'_'
+
str
(
i
+
1
)
)
)
else
:
block
=
self
.
add_sublayer
(
name
=
str
(
stage_id
+
2
)
+
'_'
+
str
(
i
+
1
),
...
...
@@ -268,7 +269,9 @@ class ShuffleNet(Layer):
out_channels
=
stage_out_channels
[
stage_id
+
2
],
stride
=
1
,
act
=
act
,
name
=
str
(
stage_id
+
2
)
+
'_'
+
str
(
i
+
1
)))
name
=
str
(
stage_id
+
2
)
+
'_'
+
str
(
i
+
1
)
)
)
self
.
_block_list
.
append
(
block
)
# 3. last_conv
self
.
_last_conv
=
ConvBNLayer
(
...
...
@@ -278,7 +281,8 @@ class ShuffleNet(Layer):
stride
=
1
,
padding
=
0
,
act
=
act
,
name
=
'conv5'
)
name
=
'conv5'
)
# 4. pool
self
.
_pool2d_avg
=
AdaptiveAvgPool2d
(
1
)
self
.
_out_c
=
stage_out_channels
[
-
1
]
...
...
@@ -287,7 +291,8 @@ class ShuffleNet(Layer):
stage_out_channels
[
-
1
],
class_dim
,
weight_attr
=
ParamAttr
(
name
=
'fc6_weights'
),
bias_attr
=
ParamAttr
(
name
=
'fc6_offset'
))
bias_attr
=
ParamAttr
(
name
=
'fc6_offset'
)
)
def
forward
(
self
,
inputs
):
y
=
self
.
_conv1
(
inputs
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录