Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleClas
提交
18d372ff
P
PaddleClas
项目概览
PaddlePaddle
/
PaddleClas
大约 2 年 前同步成功
通知
118
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看板
提交
18d372ff
编写于
6月 11, 2020
作者:
W
wqz960
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix format for ghostnet
上级
e8c3d72b
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
230 addition
and
216 deletion
+230
-216
ppcls/modeling/architectures/ghostnet.py
ppcls/modeling/architectures/ghostnet.py
+230
-216
未找到文件。
ppcls/modeling/architectures/ghostnet.py
浏览文件 @
18d372ff
...
...
@@ -4,14 +4,12 @@ from __future__ import print_function
import
math
import
paddle
import
paddle.fluid
as
fluid
from
paddle.fluid.param_attr
import
ParamAttr
from
paddle.fluid.initializer
import
MSRA
from
paddle.fluid.contrib.model_stat
import
summary
__all__
=
[
"GhostNet"
,
"GhostNet_0_5"
,
"GhostNet_1_0"
,
"GhostNet_1_3"
]
class
GhostNet
():
def
__init__
(
self
,
width_mult
):
cfgs
=
[
...
...
@@ -60,35 +58,38 @@ class GhostNet():
act
=
None
,
name
=
None
,
data_format
=
"NCHW"
):
x
=
fluid
.
layers
.
conv2d
(
input
=
input
,
x
=
fluid
.
layers
.
conv2d
(
input
=
input
,
num_filters
=
num_filters
,
filter_size
=
filter_size
,
stride
=
stride
,
padding
=
(
filter_size
-
1
)
//
2
,
padding
=
(
filter_size
-
1
)
//
2
,
groups
=
groups
,
act
=
None
,
param_attr
=
ParamAttr
(
initializer
=
fluid
.
initializer
.
MSRA
(),
name
=
name
+
"_weights"
),
initializer
=
fluid
.
initializer
.
MSRA
(),
name
=
name
+
"_weights"
),
bias_attr
=
False
,
name
=
name
+
"_conv_op"
,
name
=
name
+
"_conv_op"
,
data_format
=
data_format
)
x
=
fluid
.
layers
.
batch_norm
(
input
=
x
,
x
=
fluid
.
layers
.
batch_norm
(
input
=
x
,
act
=
act
,
name
=
name
+
"_bn"
,
param_attr
=
ParamAttr
(
name
=
name
+
"_bn_scale"
,
regularizer
=
fluid
.
regularizer
.
L2DecayRegularizer
(
regularization_coeff
=
0.0
)),
bias_attr
=
ParamAttr
(
name
=
name
+
"_bn_offset"
,
regularizer
=
fluid
.
regularizer
.
L2DecayRegularizer
(
regularization_coeff
=
0.0
)),
moving_mean_name
=
name
+
"_bn_mean"
,
moving_variance_name
=
name
+
"_bn_variance"
,
name
=
name
+
"_bn"
,
param_attr
=
ParamAttr
(
name
=
name
+
"_bn_scale"
,
regularizer
=
fluid
.
regularizer
.
L2DecayRegularizer
(
regularization_coeff
=
0.0
)),
bias_attr
=
ParamAttr
(
name
=
name
+
"_bn_offset"
,
regularizer
=
fluid
.
regularizer
.
L2DecayRegularizer
(
regularization_coeff
=
0.0
)),
moving_mean_name
=
name
+
"_bn_mean"
,
moving_variance_name
=
name
+
"_bn_variance"
,
data_layout
=
data_format
)
return
x
def
SElayer
(
self
,
input
,
num_channels
,
reduction_ratio
=
4
,
name
=
None
):
def
SElayer
(
self
,
input
,
num_channels
,
reduction_ratio
=
4
,
name
=
None
):
pool
=
fluid
.
layers
.
pool2d
(
input
=
input
,
pool_size
=
0
,
pool_type
=
'avg'
,
global_pooling
=
True
)
stdv
=
1.0
/
math
.
sqrt
(
pool
.
shape
[
1
]
*
1.0
)
...
...
@@ -109,10 +110,8 @@ class GhostNet():
initializer
=
fluid
.
initializer
.
Uniform
(
-
stdv
,
stdv
),
name
=
name
+
'_exc_weights'
),
bias_attr
=
ParamAttr
(
name
=
name
+
'_exc_offset'
))
excitation
=
fluid
.
layers
.
clip
(
x
=
excitation
,
min
=
0
,
max
=
1
,
name
=
name
+
'_clip'
)
excitation
=
fluid
.
layers
.
clip
(
x
=
excitation
,
min
=
0
,
max
=
1
,
name
=
name
+
'_clip'
)
scale
=
fluid
.
layers
.
elementwise_mul
(
x
=
input
,
y
=
excitation
,
axis
=
0
)
return
scale
...
...
@@ -124,13 +123,14 @@ class GhostNet():
relu
=
False
,
name
=
None
,
data_format
=
"NCHW"
):
return
self
.
conv_bn_layer
(
input
=
inp
,
return
self
.
conv_bn_layer
(
input
=
inp
,
num_filters
=
oup
,
filter_size
=
kernel_size
,
stride
=
stride
,
groups
=
inp
.
shape
[
1
]
if
data_format
==
"NCHW"
else
inp
.
shape
[
-
1
],
groups
=
inp
.
shape
[
1
]
if
data_format
==
"NCHW"
else
inp
.
shape
[
-
1
],
act
=
"relu"
if
relu
else
None
,
name
=
name
+
"_dw"
,
name
=
name
+
"_dw"
,
data_format
=
data_format
)
def
GhostModule
(
self
,
...
...
@@ -143,26 +143,29 @@ class GhostNet():
relu
=
True
,
name
=
None
,
data_format
=
"NCHW"
):
self
.
oup
=
oup
init_channels
=
int
(
math
.
ceil
(
oup
/
ratio
))
new_channels
=
int
(
init_channels
*
(
ratio
-
1
))
primary_conv
=
self
.
conv_bn_layer
(
input
=
inp
,
self
.
oup
=
oup
init_channels
=
int
(
math
.
ceil
(
oup
/
ratio
))
new_channels
=
int
(
init_channels
*
(
ratio
-
1
))
primary_conv
=
self
.
conv_bn_layer
(
input
=
inp
,
num_filters
=
init_channels
,
filter_size
=
kernel_size
,
stride
=
stride
,
groups
=
1
,
act
=
"relu"
if
relu
else
None
,
name
=
name
+
"_primary_conv"
,
name
=
name
+
"_primary_conv"
,
data_format
=
"NCHW"
)
cheap_operation
=
self
.
conv_bn_layer
(
input
=
primary_conv
,
cheap_operation
=
self
.
conv_bn_layer
(
input
=
primary_conv
,
num_filters
=
new_channels
,
filter_size
=
dw_size
,
stride
=
1
,
groups
=
init_channels
,
act
=
"relu"
if
relu
else
None
,
name
=
name
+
"_cheap_operation"
,
name
=
name
+
"_cheap_operation"
,
data_format
=
data_format
)
out
=
fluid
.
layers
.
concat
([
primary_conv
,
cheap_operation
],
axis
=
1
,
name
=
name
+
"_concat"
)
out
=
fluid
.
layers
.
concat
(
[
primary_conv
,
cheap_operation
],
axis
=
1
,
name
=
name
+
"_concat"
)
return
out
def
GhostBottleneck
(
self
,
...
...
@@ -175,61 +178,61 @@ class GhostNet():
name
=
None
,
data_format
=
"NCHW"
):
inp_channels
=
inp
.
shape
[
1
]
x
=
self
.
GhostModule
(
inp
=
inp
,
x
=
self
.
GhostModule
(
inp
=
inp
,
oup
=
hidden_dim
,
kernel_size
=
1
,
stride
=
1
,
relu
=
True
,
name
=
name
+
"GhostBottle_1"
,
name
=
name
+
"GhostBottle_1"
,
data_format
=
"NCHW"
)
if
stride
==
2
:
x
=
self
.
depthwise_conv
(
inp
=
x
,
if
stride
==
2
:
x
=
self
.
depthwise_conv
(
inp
=
x
,
oup
=
hidden_dim
,
kernel_size
=
kernel_size
,
stride
=
stride
,
relu
=
False
,
name
=
name
+
"_dw2"
,
name
=
name
+
"_dw2"
,
data_format
=
"NCHW"
)
if
use_se
:
x
=
self
.
SElayer
(
input
=
x
,
num_channels
=
hidden_dim
,
name
=
name
+
"SElayer"
)
x
=
self
.
GhostModule
(
inp
=
x
,
x
=
self
.
SElayer
(
input
=
x
,
num_channels
=
hidden_dim
,
name
=
name
+
"SElayer"
)
x
=
self
.
GhostModule
(
inp
=
x
,
oup
=
oup
,
kernel_size
=
1
,
relu
=
False
,
name
=
name
+
"GhostModule_2"
)
if
stride
==
1
and
inp_channels
==
oup
:
name
=
name
+
"GhostModule_2"
)
if
stride
==
1
and
inp_channels
==
oup
:
shortcut
=
inp
else
:
shortcut
=
self
.
depthwise_conv
(
inp
=
inp
,
shortcut
=
self
.
depthwise_conv
(
inp
=
inp
,
oup
=
inp_channels
,
kernel_size
=
kernel_size
,
stride
=
stride
,
relu
=
False
,
name
=
name
+
"shortcut_depthwise_conv"
,
name
=
name
+
"shortcut_depthwise_conv"
,
data_format
=
"NCHW"
)
shortcut
=
self
.
conv_bn_layer
(
input
=
shortcut
,
shortcut
=
self
.
conv_bn_layer
(
input
=
shortcut
,
num_filters
=
oup
,
filter_size
=
1
,
stride
=
1
,
groups
=
1
,
act
=
None
,
name
=
name
+
"shortcut_conv_bn"
,
name
=
name
+
"shortcut_conv_bn"
,
data_format
=
"NCHW"
)
return
fluid
.
layers
.
elementwise_add
(
x
=
x
,
y
=
shortcut
,
axis
=-
1
,
act
=
None
,
name
=
name
+
"elementwise_add"
)
return
fluid
.
layers
.
elementwise_add
(
x
=
x
,
y
=
shortcut
,
axis
=-
1
,
act
=
None
,
name
=
name
+
"elementwise_add"
)
def
net
(
self
,
input
,
class_dim
=
1000
):
#build first layer:
output_channel
=
int
(
self
.
_make_divisible
(
16
*
self
.
width_mult
,
4
))
#print(output_channel)
x
=
self
.
conv_bn_layer
(
input
=
input
,
def
net
(
self
,
input
,
class_dim
=
1000
):
# build first layer:
output_channel
=
int
(
self
.
_make_divisible
(
16
*
self
.
width_mult
,
4
))
# print(output_channel)
x
=
self
.
conv_bn_layer
(
input
=
input
,
num_filters
=
output_channel
,
filter_size
=
3
,
stride
=
2
,
...
...
@@ -237,27 +240,27 @@ class GhostNet():
act
=
"relu"
,
name
=
"firstlayer"
,
data_format
=
"NCHW"
)
input_channel
=
output_channel
#build inverted residual blocks
# build inverted residual blocks
idx
=
0
fm
=
{}
for
k
,
exp_size
,
c
,
use_se
,
s
in
self
.
cfgs
:
output_channel
=
int
(
self
.
_make_divisible
(
c
*
self
.
width_mult
,
4
))
hidden_channel
=
int
(
self
.
_make_divisible
(
exp_size
*
self
.
width_mult
,
4
))
x
=
self
.
GhostBottleneck
(
inp
=
x
,
output_channel
=
int
(
self
.
_make_divisible
(
c
*
self
.
width_mult
,
4
))
hidden_channel
=
int
(
self
.
_make_divisible
(
exp_size
*
self
.
width_mult
,
4
))
x
=
self
.
GhostBottleneck
(
inp
=
x
,
hidden_dim
=
hidden_channel
,
oup
=
output_channel
,
kernel_size
=
k
,
stride
=
s
,
use_se
=
use_se
,
name
=
"GhostBottle_"
+
str
(
idx
),
name
=
"GhostBottle_"
+
str
(
idx
),
data_format
=
"NCHW"
)
i
nput_channel
=
output_channel
fm
[
str
(
idx
)]
=
x
idx
+=
1
#build last several layers
output_channel
=
int
(
self
.
_make_divisible
(
exp_size
*
self
.
width_mult
,
4
))
x
=
self
.
conv_bn_layer
(
input
=
x
,
i
dx
+=
1
# build last several layers
output_channel
=
int
(
self
.
_make_divisible
(
exp_size
*
self
.
width_mult
,
4
))
x
=
self
.
conv_bn_layer
(
input
=
x
,
num_filters
=
output_channel
,
filter_size
=
1
,
stride
=
1
,
...
...
@@ -265,48 +268,59 @@ class GhostNet():
act
=
"relu"
,
name
=
"lastlayer"
,
data_format
=
"NCHW"
)
x
=
fluid
.
layers
.
pool2d
(
input
=
x
,
pool_type
=
'avg'
,
global_pooling
=
True
,
data_format
=
"NCHW"
)
input_channel
=
output_channel
x
=
fluid
.
layers
.
pool2d
(
input
=
x
,
pool_type
=
'avg'
,
global_pooling
=
True
,
data_format
=
"NCHW"
)
output_channel
=
1280
stdv
=
1.0
/
math
.
sqrt
(
x
.
shape
[
1
]
*
1.0
)
out
=
fluid
.
layers
.
conv2d
(
input
=
x
,
stdv
=
1.0
/
math
.
sqrt
(
x
.
shape
[
1
]
*
1.0
)
out
=
fluid
.
layers
.
conv2d
(
input
=
x
,
num_filters
=
output_channel
,
filter_size
=
1
,
groups
=
1
,
param_attr
=
ParamAttr
(
name
=
"fc_0_w"
,
initializer
=
fluid
.
initializer
.
Uniform
(
-
stdv
,
stdv
)),
param_attr
=
ParamAttr
(
name
=
"fc_0_w"
,
initializer
=
fluid
.
initializer
.
Uniform
(
-
stdv
,
stdv
)),
bias_attr
=
False
,
name
=
"fc_0"
)
out
=
fluid
.
layers
.
batch_norm
(
input
=
out
,
out
=
fluid
.
layers
.
batch_norm
(
input
=
out
,
act
=
"relu"
,
name
=
"fc_0_bn"
,
param_attr
=
ParamAttr
(
name
=
"fc_0_bn_scale"
,
regularizer
=
fluid
.
regularizer
.
L2DecayRegularizer
(
regularization_coeff
=
0.0
)),
bias_attr
=
ParamAttr
(
name
=
"fc_0_bn_offset"
,
regularizer
=
fluid
.
regularizer
.
L2DecayRegularizer
(
regularization_coeff
=
0.0
)),
param_attr
=
ParamAttr
(
name
=
"fc_0_bn_scale"
,
regularizer
=
fluid
.
regularizer
.
L2DecayRegularizer
(
regularization_coeff
=
0.0
)),
bias_attr
=
ParamAttr
(
name
=
"fc_0_bn_offset"
,
regularizer
=
fluid
.
regularizer
.
L2DecayRegularizer
(
regularization_coeff
=
0.0
)),
moving_mean_name
=
"fc_0_bn_mean"
,
moving_variance_name
=
"fc_0_bn_variance"
,
data_layout
=
"NCHW"
)
out
=
fluid
.
layers
.
dropout
(
x
=
out
,
dropout_prob
=
0.2
)
stdv
=
1.0
/
math
.
sqrt
(
out
.
shape
[
1
]
*
1.0
)
out
=
fluid
.
layers
.
fc
(
input
=
out
,
stdv
=
1.0
/
math
.
sqrt
(
out
.
shape
[
1
]
*
1.0
)
out
=
fluid
.
layers
.
fc
(
input
=
out
,
size
=
class_dim
,
param_attr
=
ParamAttr
(
name
=
"fc_1_w"
,
initializer
=
fluid
.
initializer
.
Uniform
(
-
stdv
,
stdv
)),
param_attr
=
ParamAttr
(
name
=
"fc_1_w"
,
initializer
=
fluid
.
initializer
.
Uniform
(
-
stdv
,
stdv
)),
bias_attr
=
ParamAttr
(
name
=
"fc_1_bias"
))
return
out
,
fm
return
out
def
GhostNet_0_5
():
model
=
GhostNet
(
width_mult
=
0.5
)
return
model
def
GhostNet_1_0
():
model
=
GhostNet
(
width_mult
=
1.0
)
return
model
def
GhostNet_1_3
():
model
=
GhostNet
(
width_mult
=
1.3
)
return
model
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录