Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleClas
提交
43a89a95
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看板
提交
43a89a95
编写于
9月 13, 2020
作者:
littletomatodonkey
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix resnet series
上级
52844f6f
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
88 addition
and
105 deletion
+88
-105
ppcls/modeling/architectures/resnet.py
ppcls/modeling/architectures/resnet.py
+4
-5
ppcls/modeling/architectures/resnet_vc.py
ppcls/modeling/architectures/resnet_vc.py
+21
-23
ppcls/modeling/architectures/resnet_vd.py
ppcls/modeling/architectures/resnet_vd.py
+23
-29
ppcls/modeling/architectures/resnext.py
ppcls/modeling/architectures/resnext.py
+19
-21
ppcls/modeling/architectures/resnext_vd.py
ppcls/modeling/architectures/resnext_vd.py
+21
-27
未找到文件。
ppcls/modeling/architectures/resnet.py
浏览文件 @
43a89a95
...
@@ -20,7 +20,8 @@ import numpy as np
...
@@ -20,7 +20,8 @@ import numpy as np
import
paddle
import
paddle
from
paddle
import
ParamAttr
from
paddle
import
ParamAttr
import
paddle.nn
as
nn
import
paddle.nn
as
nn
from
paddle.nn
import
Conv2d
,
Pool2D
,
BatchNorm
,
Linear
,
Dropout
from
paddle.nn
import
Conv2d
,
BatchNorm
,
Linear
,
Dropout
from
paddle.nn
import
AdaptiveAvgPool2d
,
MaxPool2d
,
AvgPool2d
from
paddle.nn.initializer
import
Uniform
from
paddle.nn.initializer
import
Uniform
import
math
import
math
...
@@ -195,8 +196,7 @@ class ResNet(nn.Layer):
...
@@ -195,8 +196,7 @@ class ResNet(nn.Layer):
stride
=
2
,
stride
=
2
,
act
=
"relu"
,
act
=
"relu"
,
name
=
"conv1"
)
name
=
"conv1"
)
self
.
pool2d_max
=
Pool2D
(
self
.
pool2d_max
=
MaxPool2d
(
kernel_size
=
3
,
stride
=
2
,
padding
=
1
)
pool_size
=
3
,
pool_stride
=
2
,
pool_padding
=
1
,
pool_type
=
"max"
)
self
.
block_list
=
[]
self
.
block_list
=
[]
if
layers
>=
50
:
if
layers
>=
50
:
...
@@ -238,8 +238,7 @@ class ResNet(nn.Layer):
...
@@ -238,8 +238,7 @@ class ResNet(nn.Layer):
self
.
block_list
.
append
(
basic_block
)
self
.
block_list
.
append
(
basic_block
)
shortcut
=
True
shortcut
=
True
self
.
pool2d_avg
=
Pool2D
(
self
.
pool2d_avg
=
AdaptiveAvgPool2d
(
1
)
pool_size
=
7
,
pool_type
=
'avg'
,
global_pooling
=
True
)
self
.
pool2d_avg_channels
=
num_channels
[
-
1
]
*
2
self
.
pool2d_avg_channels
=
num_channels
[
-
1
]
*
2
...
...
ppcls/modeling/architectures/resnet_vc.py
浏览文件 @
43a89a95
...
@@ -18,9 +18,11 @@ from __future__ import print_function
...
@@ -18,9 +18,11 @@ from __future__ import print_function
import
numpy
as
np
import
numpy
as
np
import
paddle
import
paddle
import
paddle.fluid
as
fluid
from
paddle
import
ParamAttr
from
paddle.fluid.param_attr
import
ParamAttr
import
paddle.nn
as
nn
from
paddle.fluid.dygraph.nn
import
Conv2D
,
Pool2D
,
BatchNorm
,
Linear
,
Dropout
from
paddle.nn
import
Conv2d
,
BatchNorm
,
Linear
,
Dropout
from
paddle.nn
import
AdaptiveAvgPool2d
,
MaxPool2d
,
AvgPool2d
from
paddle.nn.initializer
import
Uniform
import
math
import
math
...
@@ -29,7 +31,7 @@ __all__ = [
...
@@ -29,7 +31,7 @@ __all__ = [
]
]
class
ConvBNLayer
(
fluid
.
dygraph
.
Layer
):
class
ConvBNLayer
(
nn
.
Layer
):
def
__init__
(
self
,
def
__init__
(
self
,
num_channels
,
num_channels
,
num_filters
,
num_filters
,
...
@@ -40,15 +42,14 @@ class ConvBNLayer(fluid.dygraph.Layer):
...
@@ -40,15 +42,14 @@ class ConvBNLayer(fluid.dygraph.Layer):
name
=
None
):
name
=
None
):
super
(
ConvBNLayer
,
self
).
__init__
()
super
(
ConvBNLayer
,
self
).
__init__
()
self
.
_conv
=
Conv2
D
(
self
.
_conv
=
Conv2
d
(
num
_channels
=
num_channels
,
in
_channels
=
num_channels
,
num_filter
s
=
num_filters
,
out_channel
s
=
num_filters
,
filter
_size
=
filter_size
,
kernel
_size
=
filter_size
,
stride
=
stride
,
stride
=
stride
,
padding
=
(
filter_size
-
1
)
//
2
,
padding
=
(
filter_size
-
1
)
//
2
,
groups
=
groups
,
groups
=
groups
,
act
=
None
,
weight_attr
=
ParamAttr
(
name
=
name
+
"_weights"
),
param_attr
=
ParamAttr
(
name
=
name
+
"_weights"
),
bias_attr
=
False
)
bias_attr
=
False
)
if
name
==
"conv1"
:
if
name
==
"conv1"
:
bn_name
=
"bn_"
+
name
bn_name
=
"bn_"
+
name
...
@@ -68,7 +69,7 @@ class ConvBNLayer(fluid.dygraph.Layer):
...
@@ -68,7 +69,7 @@ class ConvBNLayer(fluid.dygraph.Layer):
return
y
return
y
class
BottleneckBlock
(
fluid
.
dygraph
.
Layer
):
class
BottleneckBlock
(
nn
.
Layer
):
def
__init__
(
self
,
def
__init__
(
self
,
num_channels
,
num_channels
,
num_filters
,
num_filters
,
...
@@ -119,11 +120,11 @@ class BottleneckBlock(fluid.dygraph.Layer):
...
@@ -119,11 +120,11 @@ class BottleneckBlock(fluid.dygraph.Layer):
else
:
else
:
short
=
self
.
short
(
inputs
)
short
=
self
.
short
(
inputs
)
y
=
fluid
.
layers
.
elementwise_add
(
x
=
short
,
y
=
conv2
,
act
=
'relu'
)
y
=
paddle
.
elementwise_add
(
x
=
short
,
y
=
conv2
,
act
=
'relu'
)
return
y
return
y
class
BasicBlock
(
fluid
.
dygraph
.
Layer
):
class
BasicBlock
(
nn
.
Layer
):
def
__init__
(
self
,
def
__init__
(
self
,
num_channels
,
num_channels
,
num_filters
,
num_filters
,
...
@@ -164,11 +165,11 @@ class BasicBlock(fluid.dygraph.Layer):
...
@@ -164,11 +165,11 @@ class BasicBlock(fluid.dygraph.Layer):
short
=
inputs
short
=
inputs
else
:
else
:
short
=
self
.
short
(
inputs
)
short
=
self
.
short
(
inputs
)
y
=
fluid
.
layers
.
elementwise_add
(
x
=
short
,
y
=
conv1
,
act
=
'relu'
)
y
=
paddle
.
elementwise_add
(
x
=
short
,
y
=
conv1
,
act
=
'relu'
)
return
y
return
y
class
ResNet_vc
(
fluid
.
dygraph
.
Layer
):
class
ResNet_vc
(
nn
.
Layer
):
def
__init__
(
self
,
layers
=
50
,
class_dim
=
1000
):
def
__init__
(
self
,
layers
=
50
,
class_dim
=
1000
):
super
(
ResNet_vc
,
self
).
__init__
()
super
(
ResNet_vc
,
self
).
__init__
()
...
@@ -212,8 +213,7 @@ class ResNet_vc(fluid.dygraph.Layer):
...
@@ -212,8 +213,7 @@ class ResNet_vc(fluid.dygraph.Layer):
act
=
'relu'
,
act
=
'relu'
,
name
=
"conv1_3"
)
name
=
"conv1_3"
)
self
.
pool2d_max
=
Pool2D
(
self
.
pool2d_max
=
MaxPool2d
(
kernel_size
=
3
,
stride
=
2
,
padding
=
1
)
pool_size
=
3
,
pool_stride
=
2
,
pool_padding
=
1
,
pool_type
=
'max'
)
self
.
block_list
=
[]
self
.
block_list
=
[]
if
layers
>=
50
:
if
layers
>=
50
:
...
@@ -255,8 +255,7 @@ class ResNet_vc(fluid.dygraph.Layer):
...
@@ -255,8 +255,7 @@ class ResNet_vc(fluid.dygraph.Layer):
self
.
block_list
.
append
(
basic_block
)
self
.
block_list
.
append
(
basic_block
)
shortcut
=
True
shortcut
=
True
self
.
pool2d_avg
=
Pool2D
(
self
.
pool2d_avg
=
AdaptiveAvgPool2d
(
1
)
pool_size
=
7
,
pool_type
=
'avg'
,
global_pooling
=
True
)
self
.
pool2d_avg_channels
=
num_channels
[
-
1
]
*
2
self
.
pool2d_avg_channels
=
num_channels
[
-
1
]
*
2
...
@@ -265,9 +264,8 @@ class ResNet_vc(fluid.dygraph.Layer):
...
@@ -265,9 +264,8 @@ class ResNet_vc(fluid.dygraph.Layer):
self
.
out
=
Linear
(
self
.
out
=
Linear
(
self
.
pool2d_avg_channels
,
self
.
pool2d_avg_channels
,
class_dim
,
class_dim
,
param_attr
=
ParamAttr
(
weight_attr
=
ParamAttr
(
initializer
=
fluid
.
initializer
.
Uniform
(
-
stdv
,
stdv
),
initializer
=
Uniform
(
-
stdv
,
stdv
),
name
=
"fc_0.w_0"
),
name
=
"fc_0.w_0"
),
bias_attr
=
ParamAttr
(
name
=
"fc_0.b_0"
))
bias_attr
=
ParamAttr
(
name
=
"fc_0.b_0"
))
def
forward
(
self
,
inputs
):
def
forward
(
self
,
inputs
):
...
@@ -278,7 +276,7 @@ class ResNet_vc(fluid.dygraph.Layer):
...
@@ -278,7 +276,7 @@ class ResNet_vc(fluid.dygraph.Layer):
for
block
in
self
.
block_list
:
for
block
in
self
.
block_list
:
y
=
block
(
y
)
y
=
block
(
y
)
y
=
self
.
pool2d_avg
(
y
)
y
=
self
.
pool2d_avg
(
y
)
y
=
fluid
.
layers
.
reshape
(
y
,
shape
=
[
-
1
,
self
.
pool2d_avg_channels
])
y
=
paddle
.
reshape
(
y
,
shape
=
[
-
1
,
self
.
pool2d_avg_channels
])
y
=
self
.
out
(
y
)
y
=
self
.
out
(
y
)
return
y
return
y
...
...
ppcls/modeling/architectures/resnet_vd.py
浏览文件 @
43a89a95
...
@@ -18,9 +18,11 @@ from __future__ import print_function
...
@@ -18,9 +18,11 @@ from __future__ import print_function
import
numpy
as
np
import
numpy
as
np
import
paddle
import
paddle
import
paddle.fluid
as
fluid
from
paddle
import
ParamAttr
from
paddle.fluid.param_attr
import
ParamAttr
import
paddle.nn
as
nn
from
paddle.fluid.dygraph.nn
import
Conv2D
,
Pool2D
,
BatchNorm
,
Linear
,
Dropout
from
paddle.nn
import
Conv2d
,
BatchNorm
,
Linear
,
Dropout
from
paddle.nn
import
AdaptiveAvgPool2d
,
MaxPool2d
,
AvgPool2d
from
paddle.nn.initializer
import
Uniform
import
math
import
math
...
@@ -29,7 +31,7 @@ __all__ = [
...
@@ -29,7 +31,7 @@ __all__ = [
]
]
class
ConvBNLayer
(
fluid
.
dygraph
.
Layer
):
class
ConvBNLayer
(
nn
.
Layer
):
def
__init__
(
def
__init__
(
self
,
self
,
num_channels
,
num_channels
,
...
@@ -43,21 +45,16 @@ class ConvBNLayer(fluid.dygraph.Layer):
...
@@ -43,21 +45,16 @@ class ConvBNLayer(fluid.dygraph.Layer):
super
(
ConvBNLayer
,
self
).
__init__
()
super
(
ConvBNLayer
,
self
).
__init__
()
self
.
is_vd_mode
=
is_vd_mode
self
.
is_vd_mode
=
is_vd_mode
self
.
_pool2d_avg
=
Pool2D
(
self
.
_pool2d_avg
=
AvgPool2d
(
pool_size
=
2
,
kernel_size
=
2
,
stride
=
2
,
padding
=
0
,
ceil_mode
=
True
)
pool_stride
=
2
,
self
.
_conv
=
Conv2d
(
pool_padding
=
0
,
in_channels
=
num_channels
,
pool_type
=
'avg'
,
out_channels
=
num_filters
,
ceil_mode
=
True
)
kernel_size
=
filter_size
,
self
.
_conv
=
Conv2D
(
num_channels
=
num_channels
,
num_filters
=
num_filters
,
filter_size
=
filter_size
,
stride
=
stride
,
stride
=
stride
,
padding
=
(
filter_size
-
1
)
//
2
,
padding
=
(
filter_size
-
1
)
//
2
,
groups
=
groups
,
groups
=
groups
,
act
=
None
,
weight_attr
=
ParamAttr
(
name
=
name
+
"_weights"
),
param_attr
=
ParamAttr
(
name
=
name
+
"_weights"
),
bias_attr
=
False
)
bias_attr
=
False
)
if
name
==
"conv1"
:
if
name
==
"conv1"
:
bn_name
=
"bn_"
+
name
bn_name
=
"bn_"
+
name
...
@@ -79,7 +76,7 @@ class ConvBNLayer(fluid.dygraph.Layer):
...
@@ -79,7 +76,7 @@ class ConvBNLayer(fluid.dygraph.Layer):
return
y
return
y
class
BottleneckBlock
(
fluid
.
dygraph
.
Layer
):
class
BottleneckBlock
(
nn
.
Layer
):
def
__init__
(
self
,
def
__init__
(
self
,
num_channels
,
num_channels
,
num_filters
,
num_filters
,
...
@@ -129,11 +126,11 @@ class BottleneckBlock(fluid.dygraph.Layer):
...
@@ -129,11 +126,11 @@ class BottleneckBlock(fluid.dygraph.Layer):
short
=
inputs
short
=
inputs
else
:
else
:
short
=
self
.
short
(
inputs
)
short
=
self
.
short
(
inputs
)
y
=
fluid
.
layers
.
elementwise_add
(
x
=
short
,
y
=
conv2
,
act
=
'relu'
)
y
=
paddle
.
elementwise_add
(
x
=
short
,
y
=
conv2
,
act
=
'relu'
)
return
y
return
y
class
BasicBlock
(
fluid
.
dygraph
.
Layer
):
class
BasicBlock
(
nn
.
Layer
):
def
__init__
(
self
,
def
__init__
(
self
,
num_channels
,
num_channels
,
num_filters
,
num_filters
,
...
@@ -176,11 +173,11 @@ class BasicBlock(fluid.dygraph.Layer):
...
@@ -176,11 +173,11 @@ class BasicBlock(fluid.dygraph.Layer):
short
=
inputs
short
=
inputs
else
:
else
:
short
=
self
.
short
(
inputs
)
short
=
self
.
short
(
inputs
)
y
=
fluid
.
layers
.
elementwise_add
(
x
=
short
,
y
=
conv1
,
act
=
'relu'
)
y
=
paddle
.
elementwise_add
(
x
=
short
,
y
=
conv1
,
act
=
'relu'
)
return
y
return
y
class
ResNet_vd
(
fluid
.
dygraph
.
Layer
):
class
ResNet_vd
(
nn
.
Layer
):
def
__init__
(
self
,
layers
=
50
,
class_dim
=
1000
):
def
__init__
(
self
,
layers
=
50
,
class_dim
=
1000
):
super
(
ResNet_vd
,
self
).
__init__
()
super
(
ResNet_vd
,
self
).
__init__
()
...
@@ -225,8 +222,7 @@ class ResNet_vd(fluid.dygraph.Layer):
...
@@ -225,8 +222,7 @@ class ResNet_vd(fluid.dygraph.Layer):
stride
=
1
,
stride
=
1
,
act
=
'relu'
,
act
=
'relu'
,
name
=
"conv1_3"
)
name
=
"conv1_3"
)
self
.
pool2d_max
=
Pool2D
(
self
.
pool2d_max
=
MaxPool2d
(
kernel_size
=
3
,
stride
=
2
,
padding
=
1
)
pool_size
=
3
,
pool_stride
=
2
,
pool_padding
=
1
,
pool_type
=
'max'
)
self
.
block_list
=
[]
self
.
block_list
=
[]
if
layers
>=
50
:
if
layers
>=
50
:
...
@@ -270,8 +266,7 @@ class ResNet_vd(fluid.dygraph.Layer):
...
@@ -270,8 +266,7 @@ class ResNet_vd(fluid.dygraph.Layer):
self
.
block_list
.
append
(
basic_block
)
self
.
block_list
.
append
(
basic_block
)
shortcut
=
True
shortcut
=
True
self
.
pool2d_avg
=
Pool2D
(
self
.
pool2d_avg
=
AdaptiveAvgPool2d
(
1
)
pool_size
=
7
,
pool_type
=
'avg'
,
global_pooling
=
True
)
self
.
pool2d_avg_channels
=
num_channels
[
-
1
]
*
2
self
.
pool2d_avg_channels
=
num_channels
[
-
1
]
*
2
...
@@ -280,9 +275,8 @@ class ResNet_vd(fluid.dygraph.Layer):
...
@@ -280,9 +275,8 @@ class ResNet_vd(fluid.dygraph.Layer):
self
.
out
=
Linear
(
self
.
out
=
Linear
(
self
.
pool2d_avg_channels
,
self
.
pool2d_avg_channels
,
class_dim
,
class_dim
,
param_attr
=
ParamAttr
(
weight_attr
=
ParamAttr
(
initializer
=
fluid
.
initializer
.
Uniform
(
-
stdv
,
stdv
),
initializer
=
Uniform
(
-
stdv
,
stdv
),
name
=
"fc_0.w_0"
),
name
=
"fc_0.w_0"
),
bias_attr
=
ParamAttr
(
name
=
"fc_0.b_0"
))
bias_attr
=
ParamAttr
(
name
=
"fc_0.b_0"
))
def
forward
(
self
,
inputs
):
def
forward
(
self
,
inputs
):
...
@@ -293,7 +287,7 @@ class ResNet_vd(fluid.dygraph.Layer):
...
@@ -293,7 +287,7 @@ class ResNet_vd(fluid.dygraph.Layer):
for
block
in
self
.
block_list
:
for
block
in
self
.
block_list
:
y
=
block
(
y
)
y
=
block
(
y
)
y
=
self
.
pool2d_avg
(
y
)
y
=
self
.
pool2d_avg
(
y
)
y
=
fluid
.
layers
.
reshape
(
y
,
shape
=
[
-
1
,
self
.
pool2d_avg_channels
])
y
=
paddle
.
reshape
(
y
,
shape
=
[
-
1
,
self
.
pool2d_avg_channels
])
y
=
self
.
out
(
y
)
y
=
self
.
out
(
y
)
return
y
return
y
...
...
ppcls/modeling/architectures/resnext.py
浏览文件 @
43a89a95
...
@@ -18,9 +18,11 @@ from __future__ import print_function
...
@@ -18,9 +18,11 @@ from __future__ import print_function
import
numpy
as
np
import
numpy
as
np
import
paddle
import
paddle
import
paddle.fluid
as
fluid
from
paddle
import
ParamAttr
from
paddle.fluid.param_attr
import
ParamAttr
import
paddle.nn
as
nn
from
paddle.fluid.dygraph.nn
import
Conv2D
,
Pool2D
,
BatchNorm
,
Linear
,
Dropout
from
paddle.nn
import
Conv2d
,
BatchNorm
,
Linear
,
Dropout
from
paddle.nn
import
AdaptiveAvgPool2d
,
MaxPool2d
,
AvgPool2d
from
paddle.nn.initializer
import
Uniform
import
math
import
math
...
@@ -30,7 +32,7 @@ __all__ = [
...
@@ -30,7 +32,7 @@ __all__ = [
]
]
class
ConvBNLayer
(
fluid
.
dygraph
.
Layer
):
class
ConvBNLayer
(
nn
.
Layer
):
def
__init__
(
self
,
def
__init__
(
self
,
num_channels
,
num_channels
,
num_filters
,
num_filters
,
...
@@ -41,15 +43,14 @@ class ConvBNLayer(fluid.dygraph.Layer):
...
@@ -41,15 +43,14 @@ class ConvBNLayer(fluid.dygraph.Layer):
name
=
None
):
name
=
None
):
super
(
ConvBNLayer
,
self
).
__init__
()
super
(
ConvBNLayer
,
self
).
__init__
()
self
.
_conv
=
Conv2
D
(
self
.
_conv
=
Conv2
d
(
num
_channels
=
num_channels
,
in
_channels
=
num_channels
,
num_filter
s
=
num_filters
,
out_channel
s
=
num_filters
,
filter
_size
=
filter_size
,
kernel
_size
=
filter_size
,
stride
=
stride
,
stride
=
stride
,
padding
=
(
filter_size
-
1
)
//
2
,
padding
=
(
filter_size
-
1
)
//
2
,
groups
=
groups
,
groups
=
groups
,
act
=
None
,
weight_attr
=
ParamAttr
(
name
=
name
+
"_weights"
),
param_attr
=
ParamAttr
(
name
=
name
+
"_weights"
),
bias_attr
=
False
)
bias_attr
=
False
)
if
name
==
"conv1"
:
if
name
==
"conv1"
:
bn_name
=
"bn_"
+
name
bn_name
=
"bn_"
+
name
...
@@ -69,7 +70,7 @@ class ConvBNLayer(fluid.dygraph.Layer):
...
@@ -69,7 +70,7 @@ class ConvBNLayer(fluid.dygraph.Layer):
return
y
return
y
class
BottleneckBlock
(
fluid
.
dygraph
.
Layer
):
class
BottleneckBlock
(
nn
.
Layer
):
def
__init__
(
self
,
def
__init__
(
self
,
num_channels
,
num_channels
,
num_filters
,
num_filters
,
...
@@ -121,11 +122,11 @@ class BottleneckBlock(fluid.dygraph.Layer):
...
@@ -121,11 +122,11 @@ class BottleneckBlock(fluid.dygraph.Layer):
else
:
else
:
short
=
self
.
short
(
inputs
)
short
=
self
.
short
(
inputs
)
y
=
fluid
.
layers
.
elementwise_add
(
x
=
short
,
y
=
conv2
,
act
=
'relu'
)
y
=
paddle
.
elementwise_add
(
x
=
short
,
y
=
conv2
,
act
=
'relu'
)
return
y
return
y
class
ResNeXt
(
fluid
.
dygraph
.
Layer
):
class
ResNeXt
(
nn
.
Layer
):
def
__init__
(
self
,
layers
=
50
,
class_dim
=
1000
,
cardinality
=
32
):
def
__init__
(
self
,
layers
=
50
,
class_dim
=
1000
,
cardinality
=
32
):
super
(
ResNeXt
,
self
).
__init__
()
super
(
ResNeXt
,
self
).
__init__
()
...
@@ -156,8 +157,7 @@ class ResNeXt(fluid.dygraph.Layer):
...
@@ -156,8 +157,7 @@ class ResNeXt(fluid.dygraph.Layer):
stride
=
2
,
stride
=
2
,
act
=
'relu'
,
act
=
'relu'
,
name
=
"res_conv1"
)
name
=
"res_conv1"
)
self
.
pool2d_max
=
Pool2D
(
self
.
pool2d_max
=
MaxPool2d
(
kernel_size
=
3
,
stride
=
2
,
padding
=
1
)
pool_size
=
3
,
pool_stride
=
2
,
pool_padding
=
1
,
pool_type
=
'max'
)
self
.
block_list
=
[]
self
.
block_list
=
[]
for
block
in
range
(
len
(
depth
)):
for
block
in
range
(
len
(
depth
)):
...
@@ -183,8 +183,7 @@ class ResNeXt(fluid.dygraph.Layer):
...
@@ -183,8 +183,7 @@ class ResNeXt(fluid.dygraph.Layer):
self
.
block_list
.
append
(
bottleneck_block
)
self
.
block_list
.
append
(
bottleneck_block
)
shortcut
=
True
shortcut
=
True
self
.
pool2d_avg
=
Pool2D
(
self
.
pool2d_avg
=
AdaptiveAvgPool2d
(
1
)
pool_size
=
7
,
pool_type
=
'avg'
,
global_pooling
=
True
)
self
.
pool2d_avg_channels
=
num_channels
[
-
1
]
*
2
self
.
pool2d_avg_channels
=
num_channels
[
-
1
]
*
2
...
@@ -193,9 +192,8 @@ class ResNeXt(fluid.dygraph.Layer):
...
@@ -193,9 +192,8 @@ class ResNeXt(fluid.dygraph.Layer):
self
.
out
=
Linear
(
self
.
out
=
Linear
(
self
.
pool2d_avg_channels
,
self
.
pool2d_avg_channels
,
class_dim
,
class_dim
,
param_attr
=
ParamAttr
(
weight_attr
=
ParamAttr
(
initializer
=
fluid
.
initializer
.
Uniform
(
-
stdv
,
stdv
),
initializer
=
Uniform
(
-
stdv
,
stdv
),
name
=
"fc_weights"
),
name
=
"fc_weights"
),
bias_attr
=
ParamAttr
(
name
=
"fc_offset"
))
bias_attr
=
ParamAttr
(
name
=
"fc_offset"
))
def
forward
(
self
,
inputs
):
def
forward
(
self
,
inputs
):
...
@@ -204,7 +202,7 @@ class ResNeXt(fluid.dygraph.Layer):
...
@@ -204,7 +202,7 @@ class ResNeXt(fluid.dygraph.Layer):
for
block
in
self
.
block_list
:
for
block
in
self
.
block_list
:
y
=
block
(
y
)
y
=
block
(
y
)
y
=
self
.
pool2d_avg
(
y
)
y
=
self
.
pool2d_avg
(
y
)
y
=
fluid
.
layers
.
reshape
(
y
,
shape
=
[
-
1
,
self
.
pool2d_avg_channels
])
y
=
paddle
.
reshape
(
y
,
shape
=
[
-
1
,
self
.
pool2d_avg_channels
])
y
=
self
.
out
(
y
)
y
=
self
.
out
(
y
)
return
y
return
y
...
...
ppcls/modeling/architectures/resnext_vd.py
浏览文件 @
43a89a95
...
@@ -18,9 +18,11 @@ from __future__ import print_function
...
@@ -18,9 +18,11 @@ from __future__ import print_function
import
numpy
as
np
import
numpy
as
np
import
paddle
import
paddle
import
paddle.fluid
as
fluid
from
paddle
import
ParamAttr
from
paddle.fluid.param_attr
import
ParamAttr
import
paddle.nn
as
nn
from
paddle.fluid.dygraph.nn
import
Conv2D
,
Pool2D
,
BatchNorm
,
Linear
,
Dropout
from
paddle.nn
import
Conv2d
,
BatchNorm
,
Linear
,
Dropout
from
paddle.nn
import
AdaptiveAvgPool2d
,
MaxPool2d
,
AvgPool2d
from
paddle.nn.initializer
import
Uniform
import
math
import
math
...
@@ -30,7 +32,7 @@ __all__ = [
...
@@ -30,7 +32,7 @@ __all__ = [
]
]
class
ConvBNLayer
(
fluid
.
dygraph
.
Layer
):
class
ConvBNLayer
(
nn
.
Layer
):
def
__init__
(
def
__init__
(
self
,
self
,
num_channels
,
num_channels
,
...
@@ -44,21 +46,16 @@ class ConvBNLayer(fluid.dygraph.Layer):
...
@@ -44,21 +46,16 @@ class ConvBNLayer(fluid.dygraph.Layer):
super
(
ConvBNLayer
,
self
).
__init__
()
super
(
ConvBNLayer
,
self
).
__init__
()
self
.
is_vd_mode
=
is_vd_mode
self
.
is_vd_mode
=
is_vd_mode
self
.
_pool2d_avg
=
Pool2D
(
self
.
_pool2d_avg
=
AvgPool2d
(
pool_size
=
2
,
kernel_size
=
2
,
stride
=
2
,
padding
=
0
,
ceil_mode
=
True
)
pool_stride
=
2
,
self
.
_conv
=
Conv2d
(
pool_padding
=
0
,
in_channels
=
num_channels
,
pool_type
=
'avg'
,
out_channels
=
num_filters
,
ceil_mode
=
True
)
kernel_size
=
filter_size
,
self
.
_conv
=
Conv2D
(
num_channels
=
num_channels
,
num_filters
=
num_filters
,
filter_size
=
filter_size
,
stride
=
stride
,
stride
=
stride
,
padding
=
(
filter_size
-
1
)
//
2
,
padding
=
(
filter_size
-
1
)
//
2
,
groups
=
groups
,
groups
=
groups
,
act
=
None
,
weight_attr
=
ParamAttr
(
name
=
name
+
"_weights"
),
param_attr
=
ParamAttr
(
name
=
name
+
"_weights"
),
bias_attr
=
False
)
bias_attr
=
False
)
if
name
==
"conv1"
:
if
name
==
"conv1"
:
bn_name
=
"bn_"
+
name
bn_name
=
"bn_"
+
name
...
@@ -80,7 +77,7 @@ class ConvBNLayer(fluid.dygraph.Layer):
...
@@ -80,7 +77,7 @@ class ConvBNLayer(fluid.dygraph.Layer):
return
y
return
y
class
BottleneckBlock
(
fluid
.
dygraph
.
Layer
):
class
BottleneckBlock
(
nn
.
Layer
):
def
__init__
(
self
,
def
__init__
(
self
,
num_channels
,
num_channels
,
num_filters
,
num_filters
,
...
@@ -134,11 +131,11 @@ class BottleneckBlock(fluid.dygraph.Layer):
...
@@ -134,11 +131,11 @@ class BottleneckBlock(fluid.dygraph.Layer):
else
:
else
:
short
=
self
.
short
(
inputs
)
short
=
self
.
short
(
inputs
)
y
=
fluid
.
layers
.
elementwise_add
(
x
=
short
,
y
=
conv2
,
act
=
'relu'
)
y
=
paddle
.
elementwise_add
(
x
=
short
,
y
=
conv2
,
act
=
'relu'
)
return
y
return
y
class
ResNeXt
(
fluid
.
dygraph
.
Layer
):
class
ResNeXt
(
nn
.
Layer
):
def
__init__
(
self
,
layers
=
50
,
class_dim
=
1000
,
cardinality
=
32
):
def
__init__
(
self
,
layers
=
50
,
class_dim
=
1000
,
cardinality
=
32
):
super
(
ResNeXt
,
self
).
__init__
()
super
(
ResNeXt
,
self
).
__init__
()
...
@@ -184,8 +181,7 @@ class ResNeXt(fluid.dygraph.Layer):
...
@@ -184,8 +181,7 @@ class ResNeXt(fluid.dygraph.Layer):
act
=
'relu'
,
act
=
'relu'
,
name
=
"conv1_3"
)
name
=
"conv1_3"
)
self
.
pool2d_max
=
Pool2D
(
self
.
pool2d_max
=
MaxPool2d
(
kernel_size
=
3
,
stride
=
2
,
padding
=
1
)
pool_size
=
3
,
pool_stride
=
2
,
pool_padding
=
1
,
pool_type
=
'max'
)
self
.
block_list
=
[]
self
.
block_list
=
[]
for
block
in
range
(
len
(
depth
)):
for
block
in
range
(
len
(
depth
)):
...
@@ -212,8 +208,7 @@ class ResNeXt(fluid.dygraph.Layer):
...
@@ -212,8 +208,7 @@ class ResNeXt(fluid.dygraph.Layer):
self
.
block_list
.
append
(
bottleneck_block
)
self
.
block_list
.
append
(
bottleneck_block
)
shortcut
=
True
shortcut
=
True
self
.
pool2d_avg
=
Pool2D
(
self
.
pool2d_avg
=
AdaptiveAvgPool2d
(
1
)
pool_size
=
7
,
pool_type
=
'avg'
,
global_pooling
=
True
)
self
.
pool2d_avg_channels
=
num_channels
[
-
1
]
*
2
self
.
pool2d_avg_channels
=
num_channels
[
-
1
]
*
2
...
@@ -222,9 +217,8 @@ class ResNeXt(fluid.dygraph.Layer):
...
@@ -222,9 +217,8 @@ class ResNeXt(fluid.dygraph.Layer):
self
.
out
=
Linear
(
self
.
out
=
Linear
(
self
.
pool2d_avg_channels
,
self
.
pool2d_avg_channels
,
class_dim
,
class_dim
,
param_attr
=
ParamAttr
(
weight_attr
=
ParamAttr
(
initializer
=
fluid
.
initializer
.
Uniform
(
-
stdv
,
stdv
),
initializer
=
Uniform
(
-
stdv
,
stdv
),
name
=
"fc_weights"
),
name
=
"fc_weights"
),
bias_attr
=
ParamAttr
(
name
=
"fc_offset"
))
bias_attr
=
ParamAttr
(
name
=
"fc_offset"
))
def
forward
(
self
,
inputs
):
def
forward
(
self
,
inputs
):
...
@@ -235,7 +229,7 @@ class ResNeXt(fluid.dygraph.Layer):
...
@@ -235,7 +229,7 @@ class ResNeXt(fluid.dygraph.Layer):
for
block
in
self
.
block_list
:
for
block
in
self
.
block_list
:
y
=
block
(
y
)
y
=
block
(
y
)
y
=
self
.
pool2d_avg
(
y
)
y
=
self
.
pool2d_avg
(
y
)
y
=
fluid
.
layers
.
reshape
(
y
,
shape
=
[
-
1
,
self
.
pool2d_avg_channels
])
y
=
paddle
.
reshape
(
y
,
shape
=
[
-
1
,
self
.
pool2d_avg_channels
])
y
=
self
.
out
(
y
)
y
=
self
.
out
(
y
)
return
y
return
y
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录