Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleClas
提交
70641360
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看板
提交
70641360
编写于
9月 30, 2020
作者:
W
weishengyu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
dbg ghostnet
上级
7c9e695f
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
48 addition
and
24 deletion
+48
-24
ppcls/modeling/architectures/__init__.py
ppcls/modeling/architectures/__init__.py
+1
-0
ppcls/modeling/architectures/ghostnet.py
ppcls/modeling/architectures/ghostnet.py
+47
-24
未找到文件。
ppcls/modeling/architectures/__init__.py
浏览文件 @
70641360
...
...
@@ -27,6 +27,7 @@ from .hrnet import HRNet_W18_C
from
.efficientnet
import
EfficientNetB0
from
.resnest
import
ResNeSt50_fast_1s1x64d
,
ResNeSt50
from
.googlenet
import
GoogLeNet
from
.ghostnet
import
GhostNet_x0_5
,
GhostNet_x1_0
,
GhostNet_x1_3
from
.mobilenet_v1
import
MobileNetV1_x0_25
,
MobileNetV1_x0_5
,
MobileNetV1_x0_75
,
MobileNetV1
from
.mobilenet_v2
import
MobileNetV2_x0_25
,
MobileNetV2_x0_5
,
MobileNetV2_x0_75
,
MobileNetV2
,
MobileNetV2_x1_5
,
MobileNetV2_x2_0
from
.mobilenet_v3
import
MobileNetV3_small_x0_35
,
MobileNetV3_small_x0_5
,
MobileNetV3_small_x0_75
,
MobileNetV3_small_x1_0
,
MobileNetV3_small_x1_25
,
MobileNetV3_large_x0_35
,
MobileNetV3_large_x0_5
,
MobileNetV3_large_x0_75
,
MobileNetV3_large_x1_0
,
MobileNetV3_large_x1_25
...
...
ppcls/modeling/architectures/ghostnet.py
浏览文件 @
70641360
...
...
@@ -20,7 +20,6 @@ import paddle.nn.functional as F
from
paddle.nn
import
Conv2d
,
BatchNorm
,
AdaptiveAvgPool2d
,
Linear
from
paddle.fluid.regularizer
import
L2DecayRegularizer
from
paddle.nn.initializer
import
Uniform
from
paddle
import
fluid
class
ConvBNLayer
(
nn
.
Layer
):
...
...
@@ -42,9 +41,12 @@ class ConvBNLayer(nn.Layer):
stride
=
stride
,
padding
=
(
filter_size
-
1
)
//
2
,
groups
=
groups
,
weight_attr
=
ParamAttr
(
name
=
name
+
"_weights"
),
bias_attr
=
False
)
weight_attr
=
ParamAttr
(
initializer
=
nn
.
initializer
.
MSRA
(),
name
=
name
+
"_weights"
),
bias_attr
=
False
)
bn_name
=
name
+
"_bn"
# In the old version, moving_variance_name was name + "_variance"
self
.
_batch_norm
=
BatchNorm
(
num_filters
,
act
=
act
,
...
...
@@ -104,7 +106,7 @@ class SEBlock(nn.Layer):
squeeze
=
self
.
squeeze
(
pool
)
squeeze
=
F
.
relu
(
squeeze
)
excitation
=
self
.
excitation
(
squeeze
)
excitation
=
F
.
sigmoid
(
excitation
)
excitation
=
paddle
.
fluid
.
layers
.
clip
(
x
=
excitation
,
min
=
0
,
max
=
1
)
excitation
=
paddle
.
reshape
(
excitation
,
shape
=
[
-
1
,
self
.
_num_channels
,
1
,
1
]
...
...
@@ -138,7 +140,7 @@ class GhostModule(nn.Layer):
name
=
name
+
"_primary_conv"
)
self
.
cheap_operation
=
ConvBNLayer
(
num_channels
=
num
_channels
,
num_channels
=
init
_channels
,
num_filters
=
new_channels
,
filter_size
=
dw_size
,
stride
=
1
,
...
...
@@ -186,7 +188,7 @@ class GhostBottleneck(nn.Layer):
stride
=
stride
,
groups
=
hidden_dim
,
act
=
None
,
name
=
name
+
"_depthwise"
name
=
name
+
"_depthwise"
# In the old version, name was name + "_depthwise_depthwise"
)
if
use_se
:
self
.
se_block
=
SEBlock
(
...
...
@@ -194,7 +196,7 @@ class GhostBottleneck(nn.Layer):
name
=
name
+
"_se"
)
self
.
ghost_module_2
=
GhostModule
(
num_channels
=
num_channels
,
num_channels
=
hidden_dim
,
output_channels
=
output_channels
,
kernel_size
=
1
,
relu
=
False
,
...
...
@@ -208,7 +210,7 @@ class GhostBottleneck(nn.Layer):
stride
=
stride
,
groups
=
num_channels
,
act
=
None
,
name
=
name
+
"_sho
tcut
_depthwise"
name
=
name
+
"_sho
rtcut_depthwise"
# In the old version, name was name + "_shortcut_depthwise
_depthwise"
)
self
.
shortcut_conv
=
ConvBNLayer
(
num_channels
=
num_channels
,
...
...
@@ -217,11 +219,11 @@ class GhostBottleneck(nn.Layer):
stride
=
1
,
groups
=
1
,
act
=
None
,
name
=
name
+
"_shotcut_conv"
name
=
name
+
"_sho
r
tcut_conv"
)
def
forward
(
self
,
inputs
):
x
=
self
.
ghost_module
(
inputs
)
x
=
self
.
ghost_module
_1
(
inputs
)
if
self
.
_stride
==
2
:
x
=
self
.
depthwise_conv
(
x
)
if
self
.
_use_se
:
...
...
@@ -275,14 +277,17 @@ class GhostNet(nn.Layer):
num_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
=
GhostBottleneck
(
num_channels
=
num_channels
,
hidden_dim
=
hidden_dim
,
output_channels
=
output_channels
,
kernel_size
=
k
,
stride
=
s
,
use_se
=
use_se
,
name
=
"_ghostbottleneck"
+
str
(
idx
)
ghost_bottleneck
=
self
.
add_sublayer
(
name
=
"_ghostbottleneck_"
+
str
(
idx
),
sublayer
=
GhostBottleneck
(
num_channels
=
num_channels
,
hidden_dim
=
hidden_dim
,
output_channels
=
output_channels
,
kernel_size
=
k
,
stride
=
s
,
use_se
=
use_se
,
name
=
"_ghostbottleneck_"
+
str
(
idx
)
)
)
self
.
ghost_bottleneck_list
.
append
(
ghost_bottleneck
)
idx
+=
1
...
...
@@ -300,24 +305,26 @@ class GhostNet(nn.Layer):
)
self
.
pool2d_gap
=
AdaptiveAvgPool2d
(
1
)
num_channels
=
output_channels
output_channels
=
1280
self
.
_num_channels
=
num_channels
self
.
_fc0_output_channels
=
1280
self
.
fc_0
=
ConvBNLayer
(
num_channels
=
num_channels
,
num_filters
=
output_channels
,
num_filters
=
self
.
_fc0_
output_channels
,
filter_size
=
1
,
stride
=
1
,
act
=
"relu"
,
name
=
"fc_0"
)
self
.
dropout
=
nn
.
Dropout
(
p
=
0.2
)
stdv
=
1.0
/
math
.
sqrt
(
output_channels
*
1.0
)
stdv
=
1.0
/
math
.
sqrt
(
self
.
_fc0_
output_channels
*
1.0
)
self
.
fc_1
=
Linear
(
output_channels
,
self
.
_fc0_
output_channels
,
class_dim
,
param
_attr
=
ParamAttr
(
weight
_attr
=
ParamAttr
(
name
=
"fc_1_weights"
,
initializer
=
Uniform
(
-
stdv
,
stdv
)
)
),
bias_attr
=
ParamAttr
(
name
=
"fc_1_offset"
)
)
def
forward
(
self
,
inputs
):
...
...
@@ -328,6 +335,7 @@ class GhostNet(nn.Layer):
x
=
self
.
pool2d_gap
(
x
)
x
=
self
.
fc_0
(
x
)
x
=
self
.
dropout
(
x
)
x
=
paddle
.
reshape
(
x
,
shape
=
[
-
1
,
self
.
_fc0_output_channels
])
x
=
self
.
fc_1
(
x
)
return
x
...
...
@@ -345,3 +353,18 @@ class GhostNet(nn.Layer):
if
new_v
<
0.9
*
v
:
new_v
+=
divisor
return
new_v
def
GhostNet_x0_5
():
model
=
GhostNet
(
scale
=
0.5
)
return
model
def
GhostNet_x1_0
():
model
=
GhostNet
(
scale
=
1.0
)
return
model
def
GhostNet_x1_3
():
model
=
GhostNet
(
scale
=
1.3
)
return
model
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录