Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleClas
提交
8ff9d3fb
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看板
提交
8ff9d3fb
编写于
6月 22, 2020
作者:
W
wqz960
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix weight name
上级
ac50d65d
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
145 addition
and
170 deletion
+145
-170
ppcls/modeling/architectures/__init__.py
ppcls/modeling/architectures/__init__.py
+1
-1
ppcls/modeling/architectures/ghostnet.py
ppcls/modeling/architectures/ghostnet.py
+144
-169
未找到文件。
ppcls/modeling/architectures/__init__.py
浏览文件 @
8ff9d3fb
ppcls/modeling/architectures/ghostnet.py
浏览文件 @
8ff9d3fb
...
@@ -37,8 +37,7 @@ class GhostNet():
...
@@ -37,8 +37,7 @@ class GhostNet():
def
net
(
self
,
input
,
class_dim
=
1000
):
def
net
(
self
,
input
,
class_dim
=
1000
):
# build first layer:
# build first layer:
output_channel
=
int
(
self
.
_make_divisible
(
16
*
self
.
scale
,
4
))
output_channel
=
int
(
self
.
_make_divisible
(
16
*
self
.
scale
,
4
))
x
=
self
.
conv_bn_layer
(
x
=
self
.
conv_bn_layer
(
input
=
input
,
input
=
input
,
num_filters
=
output_channel
,
num_filters
=
output_channel
,
filter_size
=
3
,
filter_size
=
3
,
stride
=
2
,
stride
=
2
,
...
@@ -49,48 +48,39 @@ class GhostNet():
...
@@ -49,48 +48,39 @@ class GhostNet():
idx
=
0
idx
=
0
for
k
,
exp_size
,
c
,
use_se
,
s
in
self
.
cfgs
:
for
k
,
exp_size
,
c
,
use_se
,
s
in
self
.
cfgs
:
output_channel
=
int
(
self
.
_make_divisible
(
c
*
self
.
scale
,
4
))
output_channel
=
int
(
self
.
_make_divisible
(
c
*
self
.
scale
,
4
))
hidden_channel
=
int
(
hidden_channel
=
int
(
self
.
_make_divisible
(
exp_size
*
self
.
scale
,
4
))
self
.
_make_divisible
(
exp_size
*
self
.
scale
,
4
))
x
=
self
.
ghost_bottleneck
(
input
=
x
,
x
=
self
.
ghost_bottleneck
(
inp
=
x
,
hidden_dim
=
hidden_channel
,
hidden_dim
=
hidden_channel
,
oup
=
output_channel
,
output
=
output_channel
,
kernel_size
=
k
,
kernel_size
=
k
,
stride
=
s
,
stride
=
s
,
use_se
=
use_se
,
use_se
=
use_se
,
name
=
"ghost_bottle
_"
+
str
(
idx
))
name
=
"_ghostbottleneck
_"
+
str
(
idx
))
idx
+=
1
idx
+=
1
# build last several layers
# build last several layers
output_channel
=
int
(
output_channel
=
int
(
self
.
_make_divisible
(
exp_size
*
self
.
scale
,
4
))
self
.
_make_divisible
(
exp_size
*
self
.
scale
,
4
))
x
=
self
.
conv_bn_layer
(
input
=
x
,
x
=
self
.
conv_bn_layer
(
input
=
x
,
num_filters
=
output_channel
,
num_filters
=
output_channel
,
filter_size
=
1
,
filter_size
=
1
,
stride
=
1
,
stride
=
1
,
groups
=
1
,
groups
=
1
,
act
=
"relu"
,
act
=
"relu"
,
name
=
"conv2"
)
name
=
"conv_last"
)
x
=
fluid
.
layers
.
pool2d
(
x
=
fluid
.
layers
.
pool2d
(
input
=
x
,
pool_type
=
'avg'
,
global_pooling
=
True
)
input
=
x
,
pool_type
=
'avg'
,
global_pooling
=
True
)
output_channel
=
1280
output_channel
=
1280
stdv
=
1.0
/
math
.
sqrt
(
x
.
shape
[
1
]
*
1.0
)
stdv
=
1.0
/
math
.
sqrt
(
x
.
shape
[
1
]
*
1.0
)
out
=
self
.
conv_bn_layer
(
out
=
self
.
conv_bn_layer
(
input
=
x
,
input
=
x
,
num_filters
=
output_channel
,
num_filters
=
output_channel
,
filter_size
=
1
,
filter_size
=
1
,
stride
=
1
,
stride
=
1
,
groups
=
1
,
act
=
"relu"
,
act
=
"relu"
,
name
=
"fc_0"
)
name
=
"fc_0"
)
out
=
fluid
.
layers
.
dropout
(
x
=
out
,
dropout_prob
=
0.2
)
out
=
fluid
.
layers
.
dropout
(
x
=
out
,
dropout_prob
=
0.2
)
stdv
=
1.0
/
math
.
sqrt
(
out
.
shape
[
1
]
*
1.0
)
stdv
=
1.0
/
math
.
sqrt
(
out
.
shape
[
1
]
*
1.0
)
out
=
fluid
.
layers
.
fc
(
out
=
fluid
.
layers
.
fc
(
input
=
out
,
input
=
out
,
size
=
class_dim
,
size
=
class_dim
,
param_attr
=
ParamAttr
(
param_attr
=
ParamAttr
(
name
=
"fc_1_weights"
,
name
=
"fc_1_weight"
,
initializer
=
fluid
.
initializer
.
Uniform
(
-
stdv
,
stdv
)),
initializer
=
fluid
.
initializer
.
Uniform
(
-
stdv
,
stdv
)),
bias_attr
=
ParamAttr
(
name
=
"fc_1_offset"
))
bias_attr
=
ParamAttr
(
name
=
"fc_1_offset"
))
...
@@ -119,8 +109,7 @@ class GhostNet():
...
@@ -119,8 +109,7 @@ class GhostNet():
groups
=
1
,
groups
=
1
,
act
=
None
,
act
=
None
,
name
=
None
):
name
=
None
):
x
=
fluid
.
layers
.
conv2d
(
x
=
fluid
.
layers
.
conv2d
(
input
=
input
,
input
=
input
,
num_filters
=
num_filters
,
num_filters
=
num_filters
,
filter_size
=
filter_size
,
filter_size
=
filter_size
,
stride
=
stride
,
stride
=
stride
,
...
@@ -130,149 +119,135 @@ class GhostNet():
...
@@ -130,149 +119,135 @@ class GhostNet():
param_attr
=
ParamAttr
(
param_attr
=
ParamAttr
(
initializer
=
fluid
.
initializer
.
MSRA
(),
name
=
name
+
"_weights"
),
initializer
=
fluid
.
initializer
.
MSRA
(),
name
=
name
+
"_weights"
),
bias_attr
=
False
)
bias_attr
=
False
)
bn_name
=
name
+
"_bn"
x
=
fluid
.
layers
.
batch_norm
(
x
=
fluid
.
layers
.
batch_norm
(
input
=
x
,
input
=
x
,
act
=
act
,
act
=
act
,
param_attr
=
ParamAttr
(
param_attr
=
ParamAttr
(
name
=
name
+
"_bn
_scale"
,
name
=
bn_name
+
"
_scale"
,
regularizer
=
fluid
.
regularizer
.
L2DecayRegularizer
(
regularizer
=
fluid
.
regularizer
.
L2DecayRegularizer
(
regularization_coeff
=
0.0
)),
regularization_coeff
=
0.0
)),
bias_attr
=
ParamAttr
(
bias_attr
=
ParamAttr
(
name
=
name
+
"_bn
_offset"
,
name
=
bn_name
+
"
_offset"
,
regularizer
=
fluid
.
regularizer
.
L2DecayRegularizer
(
regularizer
=
fluid
.
regularizer
.
L2DecayRegularizer
(
regularization_coeff
=
0.0
)),
regularization_coeff
=
0.0
)),
moving_mean_name
=
name
+
"_bn
_mean"
,
moving_mean_name
=
bn_name
+
"
_mean"
,
moving_variance_name
=
name
+
"_bn
_variance"
)
moving_variance_name
=
name
+
"
_variance"
)
return
x
return
x
def
se_layer
(
self
,
input
,
num_channels
,
reduction_ratio
=
4
,
name
=
None
):
def
se_block
(
self
,
input
,
num_channels
,
reduction_ratio
=
4
,
name
=
None
):
pool
=
fluid
.
layers
.
pool2d
(
pool
=
fluid
.
layers
.
pool2d
(
input
=
input
,
pool_type
=
'avg'
,
global_pooling
=
True
,
use_cudnn
=
False
)
input
=
input
,
pool_size
=
0
,
pool_type
=
'avg'
,
global_pooling
=
True
)
stdv
=
1.0
/
math
.
sqrt
(
pool
.
shape
[
1
]
*
1.0
)
stdv
=
1.0
/
math
.
sqrt
(
pool
.
shape
[
1
]
*
1.0
)
squeeze
=
fluid
.
layers
.
fc
(
squeeze
=
fluid
.
layers
.
fc
(
input
=
pool
,
input
=
pool
,
size
=
num_channels
//
reduction_ratio
,
size
=
num_channels
//
reduction_ratio
,
act
=
'relu'
,
act
=
'relu'
,
param_attr
=
fluid
.
param_attr
.
ParamAttr
(
param_attr
=
fluid
.
param_attr
.
ParamAttr
(
initializer
=
fluid
.
initializer
.
Uniform
(
-
stdv
,
stdv
),
initializer
=
fluid
.
initializer
.
Uniform
(
-
stdv
,
stdv
),
name
=
name
+
'_sqz
_weights'
),
name
=
name
+
'_1
_weights'
),
bias_attr
=
ParamAttr
(
name
=
name
+
'_sqz
_offset'
))
bias_attr
=
ParamAttr
(
name
=
name
+
'_1
_offset'
))
stdv
=
1.0
/
math
.
sqrt
(
squeeze
.
shape
[
1
]
*
1.0
)
stdv
=
1.0
/
math
.
sqrt
(
squeeze
.
shape
[
1
]
*
1.0
)
excitation
=
fluid
.
layers
.
fc
(
excitation
=
fluid
.
layers
.
fc
(
input
=
squeeze
,
input
=
squeeze
,
size
=
num_channels
,
size
=
num_channels
,
act
=
None
,
act
=
None
,
param_attr
=
fluid
.
param_attr
.
ParamAttr
(
param_attr
=
fluid
.
param_attr
.
ParamAttr
(
initializer
=
fluid
.
initializer
.
Uniform
(
-
stdv
,
stdv
),
initializer
=
fluid
.
initializer
.
Uniform
(
-
stdv
,
stdv
),
name
=
name
+
'_exc_weights'
),
name
=
name
+
'_2_weights'
),
bias_attr
=
ParamAttr
(
name
=
name
+
'_exc_offset'
))
bias_attr
=
ParamAttr
(
name
=
name
+
'_2_offset'
))
excitation
=
fluid
.
layers
.
clip
(
excitation
=
fluid
.
layers
.
clip
(
x
=
excitation
,
min
=
0
,
max
=
1
)
x
=
excitation
,
min
=
0
,
max
=
1
)
se_scale
=
fluid
.
layers
.
elementwise_mul
(
x
=
input
,
y
=
excitation
,
axis
=
0
)
se_scale
=
fluid
.
layers
.
elementwise_mul
(
x
=
input
,
y
=
excitation
,
axis
=
0
)
return
se_scale
return
se_scale
def
depthwise_conv
(
self
,
def
depthwise_conv
(
self
,
inp
,
inp
ut
,
ou
p
,
ou
tput
,
kernel_size
,
kernel_size
,
stride
=
1
,
stride
=
1
,
relu
=
False
,
relu
=
False
,
name
=
None
):
name
=
None
):
return
self
.
conv_bn_layer
(
return
self
.
conv_bn_layer
(
input
=
input
,
input
=
inp
,
num_filters
=
output
,
num_filters
=
oup
,
filter_size
=
kernel_size
,
filter_size
=
kernel_size
,
stride
=
stride
,
stride
=
stride
,
groups
=
inp
.
shape
[
1
],
groups
=
input
.
shape
[
1
],
act
=
"relu"
if
relu
else
None
,
act
=
"relu"
if
relu
else
None
,
name
=
name
+
"_dw
"
)
name
=
name
+
"_depthwise
"
)
def
ghost_module
(
self
,
def
ghost_module
(
self
,
inp
,
inp
ut
,
ou
p
,
ou
tput
,
kernel_size
=
1
,
kernel_size
=
1
,
ratio
=
2
,
ratio
=
2
,
dw_size
=
3
,
dw_size
=
3
,
stride
=
1
,
stride
=
1
,
relu
=
True
,
relu
=
True
,
name
=
None
):
name
=
None
):
self
.
ou
p
=
oup
self
.
ou
tput
=
output
init_channels
=
int
(
math
.
ceil
(
ou
p
/
ratio
))
init_channels
=
int
(
math
.
ceil
(
ou
tput
/
ratio
))
new_channels
=
int
(
init_channels
*
(
ratio
-
1
))
new_channels
=
int
(
init_channels
*
(
ratio
-
1
))
primary_conv
=
self
.
conv_bn_layer
(
primary_conv
=
self
.
conv_bn_layer
(
input
=
input
,
input
=
inp
,
num_filters
=
init_channels
,
num_filters
=
init_channels
,
filter_size
=
kernel_size
,
filter_size
=
kernel_size
,
stride
=
stride
,
stride
=
stride
,
groups
=
1
,
groups
=
1
,
act
=
"relu"
if
relu
else
None
,
act
=
"relu"
if
relu
else
None
,
name
=
name
+
"_primary_conv"
)
name
=
name
+
"_primary_conv"
)
cheap_operation
=
self
.
conv_bn_layer
(
cheap_operation
=
self
.
conv_bn_layer
(
input
=
primary_conv
,
input
=
primary_conv
,
num_filters
=
new_channels
,
num_filters
=
new_channels
,
filter_size
=
dw_size
,
filter_size
=
dw_size
,
stride
=
1
,
stride
=
1
,
groups
=
init_channels
,
groups
=
init_channels
,
act
=
"relu"
if
relu
else
None
,
act
=
"relu"
if
relu
else
None
,
name
=
name
+
"_cheap_operation"
)
name
=
name
+
"_cheap_operation"
)
out
=
fluid
.
layers
.
concat
(
out
=
fluid
.
layers
.
concat
([
primary_conv
,
cheap_operation
],
axis
=
1
)
[
primary_conv
,
cheap_operation
],
axis
=
1
)
return
out
return
out
def
ghost_bottleneck
(
self
,
def
ghost_bottleneck
(
self
,
inp
,
inp
ut
,
hidden_dim
,
hidden_dim
,
ou
p
,
ou
tput
,
kernel_size
,
kernel_size
,
stride
,
stride
,
use_se
,
use_se
,
name
=
None
):
name
=
None
):
inp_channels
=
inp
.
shape
[
1
]
inp_channels
=
input
.
shape
[
1
]
x
=
self
.
ghost_module
(
x
=
self
.
ghost_module
(
input
=
input
,
inp
=
inp
,
output
=
hidden_dim
,
oup
=
hidden_dim
,
kernel_size
=
1
,
kernel_size
=
1
,
stride
=
1
,
stride
=
1
,
relu
=
True
,
relu
=
True
,
name
=
name
+
"
ghost_module_1"
)
name
=
name
+
"_
ghost_module_1"
)
if
stride
==
2
:
if
stride
==
2
:
x
=
self
.
depthwise_conv
(
x
=
self
.
depthwise_conv
(
input
=
x
,
inp
=
x
,
output
=
hidden_dim
,
oup
=
hidden_dim
,
kernel_size
=
kernel_size
,
kernel_size
=
kernel_size
,
stride
=
stride
,
stride
=
stride
,
relu
=
False
,
relu
=
False
,
name
=
name
+
"_dw2
"
)
name
=
name
+
"_depthwise
"
)
if
use_se
:
if
use_se
:
x
=
self
.
se_layer
(
x
=
self
.
se_block
(
input
=
x
,
num_channels
=
hidden_dim
,
name
=
name
+
"_se"
)
input
=
x
,
num_channels
=
hidden_dim
,
name
=
name
+
"se_layer"
)
x
=
self
.
ghost_module
(
input
=
x
,
x
=
self
.
ghost_module
(
output
=
output
,
inp
=
x
,
oup
=
oup
,
kernel_size
=
1
,
kernel_size
=
1
,
relu
=
False
,
relu
=
False
,
name
=
name
+
"
ghost_module_2"
)
name
=
name
+
"_
ghost_module_2"
)
if
stride
==
1
and
inp_channels
==
ou
p
:
if
stride
==
1
and
inp_channels
==
ou
tput
:
shortcut
=
inp
shortcut
=
inp
ut
else
:
else
:
shortcut
=
self
.
depthwise_conv
(
shortcut
=
self
.
depthwise_conv
(
input
=
input
,
inp
=
inp
,
output
=
inp_channels
,
oup
=
inp_channels
,
kernel_size
=
kernel_size
,
kernel_size
=
kernel_size
,
stride
=
stride
,
stride
=
stride
,
relu
=
False
,
relu
=
False
,
name
=
name
+
"shortcut_depthwise_conv"
)
name
=
name
+
"_shortcut_depthwise"
)
shortcut
=
self
.
conv_bn_layer
(
shortcut
=
self
.
conv_bn_layer
(
input
=
shortcut
,
input
=
shortcut
,
num_filters
=
output
,
num_filters
=
oup
,
filter_size
=
1
,
filter_size
=
1
,
stride
=
1
,
stride
=
1
,
groups
=
1
,
groups
=
1
,
act
=
None
,
act
=
None
,
name
=
name
+
"shortcut_conv_bn"
)
name
=
name
+
"_shortcut_conv"
)
return
fluid
.
layers
.
elementwise_add
(
return
fluid
.
layers
.
elementwise_add
(
x
=
x
,
x
=
x
,
y
=
shortcut
,
axis
=-
1
,
act
=
None
)
y
=
shortcut
,
axis
=-
1
)
def
GhostNet_x0_5
():
def
GhostNet_x0_5
():
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录