Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSeg
提交
c6064306
P
PaddleSeg
项目概览
PaddlePaddle
/
PaddleSeg
通知
285
Star
8
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
53
列表
看板
标记
里程碑
合并请求
3
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleSeg
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
53
Issue
53
列表
看板
标记
里程碑
合并请求
3
合并请求
3
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
c6064306
编写于
9月 16, 2020
作者:
C
chenguowei01
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update fluid to beta
上级
04fa1750
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
22 addition
and
80 deletion
+22
-80
dygraph/paddleseg/models/backbones/resnet_vd.py
dygraph/paddleseg/models/backbones/resnet_vd.py
+17
-71
dygraph/paddleseg/models/fcn.py
dygraph/paddleseg/models/fcn.py
+5
-9
未找到文件。
dygraph/paddleseg/models/backbones/resnet_vd.py
浏览文件 @
c6064306
...
@@ -21,13 +21,11 @@ import math
...
@@ -21,13 +21,11 @@ import math
import
numpy
as
np
import
numpy
as
np
import
paddle
import
paddle
import
paddle.nn
as
nn
import
paddle.nn.functional
as
F
from
paddle.nn
import
SyncBatchNorm
as
BatchNorm
from
paddle.nn
import
SyncBatchNorm
as
BatchNorm
from
paddle.nn
import
Conv2d
from
paddle.nn
import
Conv2d
,
Linear
,
Dropout
from
paddle.nn
import
AdaptiveAvgPool2d
,
MaxPool2d
,
AvgPool2d
import
paddle.fluid
as
fluid
from
paddle.fluid.param_attr
import
ParamAttr
from
paddle.fluid.layer_helper
import
LayerHelper
from
paddle.fluid.dygraph.nn
import
Pool2D
,
Linear
,
Dropout
from
paddleseg.utils
import
utils
from
paddleseg.utils
import
utils
from
paddleseg.models.common
import
layer_utils
from
paddleseg.models.common
import
layer_utils
...
@@ -38,7 +36,7 @@ __all__ = [
...
@@ -38,7 +36,7 @@ __all__ = [
]
]
class
ConvBNLayer
(
fluid
.
dygraph
.
Layer
):
class
ConvBNLayer
(
nn
.
Layer
):
def
__init__
(
def
__init__
(
self
,
self
,
in_channels
,
in_channels
,
...
@@ -54,12 +52,8 @@ class ConvBNLayer(fluid.dygraph.Layer):
...
@@ -54,12 +52,8 @@ 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
,
pool_padding
=
0
,
pool_type
=
'avg'
,
ceil_mode
=
True
)
self
.
_conv
=
Conv2d
(
self
.
_conv
=
Conv2d
(
in_channels
=
in_channels
,
in_channels
=
in_channels
,
out_channels
=
out_channels
,
out_channels
=
out_channels
,
...
@@ -68,16 +62,12 @@ class ConvBNLayer(fluid.dygraph.Layer):
...
@@ -68,16 +62,12 @@ class ConvBNLayer(fluid.dygraph.Layer):
padding
=
(
kernel_size
-
1
)
//
2
if
dilation
==
1
else
0
,
padding
=
(
kernel_size
-
1
)
//
2
if
dilation
==
1
else
0
,
dilation
=
dilation
,
dilation
=
dilation
,
groups
=
groups
,
groups
=
groups
,
weight_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
else
:
else
:
bn_name
=
"bn"
+
name
[
3
:]
bn_name
=
"bn"
+
name
[
3
:]
self
.
_batch_norm
=
BatchNorm
(
self
.
_batch_norm
=
BatchNorm
(
out_channels
)
out_channels
,
weight_attr
=
ParamAttr
(
name
=
bn_name
+
'_scale'
),
bias_attr
=
ParamAttr
(
bn_name
+
'_offset'
))
self
.
_act_op
=
layer_utils
.
Activation
(
act
=
act
)
self
.
_act_op
=
layer_utils
.
Activation
(
act
=
act
)
def
forward
(
self
,
inputs
):
def
forward
(
self
,
inputs
):
...
@@ -90,7 +80,7 @@ class ConvBNLayer(fluid.dygraph.Layer):
...
@@ -90,7 +80,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
,
in_channels
,
in_channels
,
out_channels
,
out_channels
,
...
@@ -143,8 +133,7 @@ class BottleneckBlock(fluid.dygraph.Layer):
...
@@ -143,8 +133,7 @@ class BottleneckBlock(fluid.dygraph.Layer):
# If given dilation rate > 1, using corresponding padding
# If given dilation rate > 1, using corresponding padding
if
self
.
dilation
>
1
:
if
self
.
dilation
>
1
:
padding
=
self
.
dilation
padding
=
self
.
dilation
y
=
fluid
.
layers
.
pad
(
y
=
F
.
pad
(
y
,
[
0
,
0
,
0
,
0
,
padding
,
padding
,
padding
,
padding
])
y
,
[
0
,
0
,
0
,
0
,
padding
,
padding
,
padding
,
padding
])
#####################################################################
#####################################################################
conv1
=
self
.
conv1
(
y
)
conv1
=
self
.
conv1
(
y
)
conv2
=
self
.
conv2
(
conv1
)
conv2
=
self
.
conv2
(
conv1
)
...
@@ -154,12 +143,11 @@ class BottleneckBlock(fluid.dygraph.Layer):
...
@@ -154,12 +143,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
)
y
=
paddle
.
elementwise_add
(
x
=
short
,
y
=
conv2
,
act
=
'relu'
)
layer_helper
=
LayerHelper
(
self
.
full_name
(),
act
=
'relu'
)
return
y
return
layer_helper
.
append_activation
(
y
)
class
BasicBlock
(
fluid
.
dygraph
.
Layer
):
class
BasicBlock
(
nn
.
Layer
):
def
__init__
(
self
,
def
__init__
(
self
,
in_channels
,
in_channels
,
out_channels
,
out_channels
,
...
@@ -202,13 +190,12 @@ class BasicBlock(fluid.dygraph.Layer):
...
@@ -202,13 +190,12 @@ 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
)
y
=
paddle
.
elementwise_add
(
x
=
short
,
y
=
conv1
,
act
=
'relu'
)
layer_helper
=
LayerHelper
(
self
.
full_name
(),
act
=
'relu'
)
return
y
return
layer_helper
.
append_activation
(
y
)
class
ResNet_vd
(
fluid
.
dygraph
.
Layer
):
class
ResNet_vd
(
nn
.
Layer
):
def
__init__
(
self
,
def
__init__
(
self
,
backbone_pretrained
=
None
,
backbone_pretrained
=
None
,
layers
=
50
,
layers
=
50
,
...
@@ -264,8 +251,7 @@ class ResNet_vd(fluid.dygraph.Layer):
...
@@ -264,8 +251,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 = []
self
.
stage_list
=
[]
self
.
stage_list
=
[]
...
@@ -330,23 +316,6 @@ class ResNet_vd(fluid.dygraph.Layer):
...
@@ -330,23 +316,6 @@ class ResNet_vd(fluid.dygraph.Layer):
shortcut
=
True
shortcut
=
True
self
.
stage_list
.
append
(
block_list
)
self
.
stage_list
.
append
(
block_list
)
self
.
pool2d_avg
=
Pool2D
(
pool_size
=
7
,
pool_type
=
'avg'
,
global_pooling
=
True
)
self
.
pool2d_avg_channels
=
num_channels
[
-
1
]
*
2
stdv
=
1.0
/
math
.
sqrt
(
self
.
pool2d_avg_channels
*
1.0
)
self
.
out
=
Linear
(
self
.
pool2d_avg_channels
,
class_dim
,
param_attr
=
ParamAttr
(
initializer
=
fluid
.
initializer
.
Uniform
(
-
stdv
,
stdv
),
name
=
"fc_0.w_0"
),
bias_attr
=
ParamAttr
(
name
=
"fc_0.b_0"
))
self
.
init_weight
(
backbone_pretrained
)
def
forward
(
self
,
inputs
):
def
forward
(
self
,
inputs
):
y
=
self
.
conv1_1
(
inputs
)
y
=
self
.
conv1_1
(
inputs
)
y
=
self
.
conv1_2
(
y
)
y
=
self
.
conv1_2
(
y
)
...
@@ -358,33 +327,10 @@ class ResNet_vd(fluid.dygraph.Layer):
...
@@ -358,33 +327,10 @@ class ResNet_vd(fluid.dygraph.Layer):
for
i
,
stage
in
enumerate
(
self
.
stage_list
):
for
i
,
stage
in
enumerate
(
self
.
stage_list
):
for
j
,
block
in
enumerate
(
stage
):
for
j
,
block
in
enumerate
(
stage
):
y
=
block
(
y
)
y
=
block
(
y
)
#print("stage {} block {}".format(i+1, j+1), y.shape)
feat_list
.
append
(
y
)
feat_list
.
append
(
y
)
y
=
self
.
pool2d_avg
(
y
)
y
=
fluid
.
layers
.
reshape
(
y
,
shape
=
[
-
1
,
self
.
pool2d_avg_channels
])
y
=
self
.
out
(
y
)
return
feat_list
return
feat_list
# def init_weight(self, pretrained_model=None):
# if pretrained_model is not None:
# if os.path.exists(pretrained_model):
# utils.load_pretrained_model(self, pretrained_model)
def
init_weight
(
self
,
pretrained_model
=
None
):
"""
Initialize the parameters of model parts.
Args:
pretrained_model ([str], optional): the path of pretrained model. Defaults to None.
"""
if
pretrained_model
is
not
None
:
if
os
.
path
.
exists
(
pretrained_model
):
utils
.
load_pretrained_model
(
self
,
pretrained_model
)
else
:
raise
Exception
(
'Pretrained model is not found: {}'
.
format
(
pretrained_model
))
@
manager
.
BACKBONES
.
add_component
@
manager
.
BACKBONES
.
add_component
def
ResNet18_vd
(
**
args
):
def
ResNet18_vd
(
**
args
):
...
...
dygraph/paddleseg/models/fcn.py
浏览文件 @
c6064306
...
@@ -16,13 +16,9 @@ import math
...
@@ -16,13 +16,9 @@ import math
import
os
import
os
import
paddle
import
paddle
import
paddle.nn
as
nn
import
paddle.nn.functional
as
F
from
paddle.nn
import
Conv2d
from
paddle.nn
import
Conv2d
import
paddle.fluid
as
fluid
from
paddle.fluid.param_attr
import
ParamAttr
from
paddle.fluid.layer_helper
import
LayerHelper
from
paddle.fluid.dygraph.nn
import
Pool2D
,
Linear
from
paddle.fluid.initializer
import
Normal
from
paddle.nn
import
SyncBatchNorm
as
BatchNorm
from
paddle.nn
import
SyncBatchNorm
as
BatchNorm
from
paddleseg.cvlibs
import
manager
from
paddleseg.cvlibs
import
manager
...
@@ -39,7 +35,7 @@ __all__ = [
...
@@ -39,7 +35,7 @@ __all__ = [
@
manager
.
MODELS
.
add_component
@
manager
.
MODELS
.
add_component
class
FCN
(
fluid
.
dygraph
.
Layer
):
class
FCN
(
nn
.
Layer
):
"""
"""
Fully Convolutional Networks for Semantic Segmentation.
Fully Convolutional Networks for Semantic Segmentation.
https://arxiv.org/abs/1411.4038
https://arxiv.org/abs/1411.4038
...
@@ -96,7 +92,7 @@ class FCN(fluid.dygraph.Layer):
...
@@ -96,7 +92,7 @@ class FCN(fluid.dygraph.Layer):
x
=
fea_list
[
self
.
backbone_indices
[
0
]]
x
=
fea_list
[
self
.
backbone_indices
[
0
]]
x
=
self
.
conv_last_2
(
x
)
x
=
self
.
conv_last_2
(
x
)
logit
=
self
.
conv_last_1
(
x
)
logit
=
self
.
conv_last_1
(
x
)
logit
=
fluid
.
layers
.
resize_bilinear
(
logit
,
input_shape
)
logit
=
F
.
resize_bilinear
(
logit
,
input_shape
)
return
[
logit
]
return
[
logit
]
def
init_weight
(
self
):
def
init_weight
(
self
):
...
@@ -128,7 +124,7 @@ class FCN(fluid.dygraph.Layer):
...
@@ -128,7 +124,7 @@ class FCN(fluid.dygraph.Layer):
logger
.
warning
(
'No pretrained model to load, train from scratch'
)
logger
.
warning
(
'No pretrained model to load, train from scratch'
)
class
ConvBNLayer
(
fluid
.
dygraph
.
Layer
):
class
ConvBNLayer
(
nn
.
Layer
):
def
__init__
(
self
,
def
__init__
(
self
,
in_channels
,
in_channels
,
out_channels
,
out_channels
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录