Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleClas
提交
93137d32
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看板
提交
93137d32
编写于
7月 20, 2020
作者:
S
shippingwang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add evonorm
上级
5d3fe63f
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
96 addition
and
12 deletion
+96
-12
ppcls/modeling/architectures/evonorm.py
ppcls/modeling/architectures/evonorm.py
+86
-0
ppcls/modeling/architectures/resnet.py
ppcls/modeling/architectures/resnet.py
+10
-12
未找到文件。
ppcls/modeling/architectures/evonorm.py
0 → 100644
浏览文件 @
93137d32
import
paddle
import
torch
import
numpy
as
np
import
paddle.fluid
as
fluid
def
instance_std_paddle
(
input
,
epsilon
=
1e-5
):
v
=
paddle
.
var
(
input
,
axis
=
[
2
,
3
],
keepdim
=
True
)
v
=
paddle
.
expand_as
(
v
,
input
)
return
paddle
.
sqrt
(
v
+
epsilon
)
def
instance_std
(
x
,
eps
=
1e-5
):
var
=
torch
.
var
(
x
,
dim
=
(
2
,
3
),
keepdim
=
True
).
expand_as
(
x
)
if
torch
.
isnan
(
var
).
any
():
var
=
torch
.
zeros
(
var
.
shape
)
return
torch
.
sqrt
(
var
+
eps
)
def
group_std_paddle
(
input
,
groups
=
32
,
epsilon
=
1e-5
):
#N, C, H, W = paddle.shape(input)
N
,
C
,
H
,
W
=
input
.
shape
#print(N,C,H,W)
input
=
paddle
.
reshape
(
input
,
[
N
,
groups
,
C
//
groups
,
H
,
W
])
v
=
paddle
.
var
(
input
,
axis
=
[
2
,
3
,
4
],
keepdim
=
True
)
v
=
paddle
.
expand_as
(
v
,
input
)
return
paddle
.
reshape
(
paddle
.
sqrt
(
v
+
epsilon
),(
N
,
C
,
H
,
W
))
def
group_std
(
x
,
groups
=
32
,
eps
=
1e-5
):
N
,
C
,
H
,
W
=
x
.
size
()
x
=
torch
.
reshape
(
x
,
(
N
,
groups
,
C
//
groups
,
H
,
W
))
var
=
torch
.
var
(
x
,
dim
=
(
2
,
3
,
4
),
keepdim
=
True
).
expand_as
(
x
)
return
torch
.
reshape
(
torch
.
sqrt
(
var
+
eps
),
(
N
,
C
,
H
,
W
))
class
EvoNorm
(
fluid
.
dygraph
.
Layer
):
def
__init__
(
self
,
channels
,
version
=
'B0'
,
affine
=
True
,
non_linear
=
True
,
groups
=
32
,
epsilon
=
1e-5
,
momentum
=
0.9
,
training
=
True
):
super
(
EvoNorm
,
self
).
__init__
()
self
.
channels
=
channels
self
.
affine
=
affine
self
.
version
=
version
self
.
non_linear
=
non_linear
self
.
groups
=
groups
self
.
epsilon
=
epsilon
self
.
training
=
training
self
.
momentum
=
momentum
if
self
.
affine
:
self
.
gamma
=
self
.
create_parameter
([
1
,
self
.
channels
,
1
,
1
],
default_initializer
=
fluid
.
initializer
.
Constant
(
value
=
1.0
))
self
.
beta
=
self
.
create_parameter
([
1
,
self
.
channels
,
1
,
1
],
default_initializer
=
fluid
.
initializer
.
Constant
(
value
=
0.0
))
if
self
.
non_linear
:
self
.
v
=
self
.
create_parameter
([
1
,
self
.
channels
,
1
,
1
],
default_initializer
=
fluid
.
initializer
.
Constant
(
value
=
1.0
))
else
:
self
.
register_parameter
(
'gamma'
,
None
)
self
.
register_parameter
(
'beta'
,
None
)
self
.
register_buffer
(
'v'
,
None
)
#self.running_var = self.create_parameter([1, self.channels, 1, 1],
# default_initializer=fluid.initializer.Constant(value=0.0))
#self.running_var.stop_gradient = True
#self.register_buffer('running_var', self.create_parameter([1, self.channels, 1, 1],
# default_initializer=fluid.initializer.Constant(value=1.0)))
self
.
register_buffer
(
'running_var'
,
paddle
.
fluid
.
layers
.
ones
(
shape
=
[
1
,
self
.
channels
,
1
,
1
],
dtype
=
'float32'
))
def
forward
(
self
,
input
):
if
self
.
version
==
'S0'
:
if
self
.
non_linear
:
num
=
input
*
paddle
.
fluid
.
layers
.
sigmoid
(
self
.
v
*
input
)
return
num
/
group_std_paddle
(
input
,
groups
=
self
.
groups
,
epsilon
=
self
.
epsilon
)
*
self
.
gamma
+
self
.
beta
else
:
return
input
*
self
.
gamma
+
self
.
beta
if
self
.
version
==
'B0'
:
if
self
.
training
:
var
=
paddle
.
var
(
input
,
axis
=
[
0
,
2
,
3
],
unbiased
=
False
,
keepdim
=
True
)
self
.
running_var
=
self
.
running_var
*
self
.
momentum
self
.
running_var
=
self
.
running_var
+
(
1
-
self
.
momentum
)
*
var
else
:
var
=
self
.
running_var
if
self
.
non_linear
:
den
=
paddle
.
elementwise_max
(
paddle
.
sqrt
((
var
+
self
.
epsilon
)),
self
.
v
*
input
+
instance_std_paddle
(
input
,
epsilon
=
self
.
epsilon
))
return
input
/
den
*
self
.
gamma
+
self
.
beta
else
:
return
input
*
self
.
gamma
+
self
.
beta
ppcls/modeling/architectures/resnet.py
浏览文件 @
93137d32
...
...
@@ -77,15 +77,15 @@ class BottleneckBlock(fluid.dygraph.Layer):
filter_size
=
1
,
act
=
None
)
if
not
shortcut
:
self
.
shortcut
=
shortcut
if
not
self
.
shortcut
:
self
.
short
=
ConvBNLayer
(
num_channels
=
num_channels
,
num_filters
=
num_filters
*
4
,
filter_size
=
1
,
stride
=
stride
)
self
.
shortcut
=
shortcut
self
.
_num_channels_out
=
num_filters
*
4
def
forward
(
self
,
inputs
):
...
...
@@ -108,18 +108,16 @@ class ResNet(fluid.dygraph.Layer):
def
__init__
(
self
,
layers
=
50
,
class_dim
=
1000
):
super
(
ResNet
,
self
).
__init__
()
self
.
layers
=
layers
supported_layers
=
[
50
,
101
,
152
]
assert
layers
in
supported_layers
,
\
"supported layers are {} but input layer is {}"
.
format
(
supported_layers
,
layers
)
if
layers
==
50
:
if
layers
==
18
:
depth
=
[
2
,
2
,
2
,
2
]
elif
layers
==
18
or
layers
==
50
:
depth
=
[
3
,
4
,
6
,
3
]
elif
layers
==
101
:
depth
=
[
3
,
4
,
23
,
3
]
elif
layers
==
152
:
depth
=
[
3
,
8
,
36
,
3
]
else
:
raise
ValueError
(
'Input layer is not supported'
)
num_channels
=
[
64
,
256
,
512
,
1024
]
num_filters
=
[
64
,
128
,
256
,
512
]
...
...
@@ -191,6 +189,6 @@ def ResNet101(**kwargs):
return
model
def
ResNet152
(
class_dim
=
1000
):
model
=
ResNet
(
layers
=
152
,
class_dim
=
class_dim
)
def
ResNet152
(
**
kwargs
):
model
=
ResNet
(
layers
=
152
,
**
kwargs
)
return
model
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录