Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSeg
提交
b5fb876f
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看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
b5fb876f
编写于
6月 02, 2020
作者:
C
chenguowei01
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update unet.py
上级
0017c6d5
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
42 addition
and
128 deletion
+42
-128
dygraph/models/unet.py
dygraph/models/unet.py
+42
-128
未找到文件。
dygraph/models/unet.py
浏览文件 @
b5fb876f
...
...
@@ -16,32 +16,18 @@ from __future__ import absolute_import
from
__future__
import
division
from
__future__
import
print_function
from
collections
import
OrderedDict
import
paddle.fluid
as
fluid
from
paddle.fluid.dygraph
import
Conv2D
,
BatchNorm
,
Pool2D
import
contextlib
regularizer
=
fluid
.
regularizer
.
L2DecayRegularizer
(
regularization_coeff
=
0.0
)
name_scope
=
""
@
contextlib
.
contextmanager
def
scope
(
name
):
global
name_scope
bk
=
name_scope
name_scope
=
name_scope
+
name
+
'/'
yield
name_scope
=
bk
class
UNet
(
fluid
.
dygraph
.
Layer
):
def
__init__
(
self
,
num_classes
,
upsample_mode
=
'bilinear'
,
ignore_index
=
255
):
def
__init__
(
self
,
num_classes
,
ignore_index
=
255
):
super
().
__init__
()
self
.
encode
=
Encoder
()
self
.
decode
=
Decode
(
upsample_mode
=
upsample_mode
)
self
.
decode
=
Decode
()
self
.
get_logit
=
GetLogit
(
64
,
num_classes
)
self
.
ignore_index
=
ignore_index
self
.
EPS
=
1e-5
def
forward
(
self
,
x
,
label
=
None
,
mode
=
'train'
):
encode_data
,
short_cuts
=
self
.
encode
(
x
)
...
...
@@ -67,7 +53,8 @@ class UNet(fluid.dygraph.Layer):
axis
=
1
)
loss
=
loss
*
mask
avg_loss
=
fluid
.
layers
.
mean
(
loss
)
/
(
fluid
.
layers
.
mean
(
mask
)
+
0.00001
)
avg_loss
=
fluid
.
layers
.
mean
(
loss
)
/
(
fluid
.
layers
.
mean
(
mask
)
+
self
.
EPS
)
label
.
stop_gradient
=
True
mask
.
stop_gradient
=
True
...
...
@@ -77,16 +64,10 @@ class UNet(fluid.dygraph.Layer):
class
Encoder
(
fluid
.
dygraph
.
Layer
):
def
__init__
(
self
):
super
().
__init__
()
with
scope
(
'encode'
):
with
scope
(
'block1'
):
self
.
double_conv
=
DoubleConv
(
3
,
64
)
with
scope
(
'block1'
):
self
.
down1
=
Down
(
64
,
128
)
with
scope
(
'block2'
):
self
.
down2
=
Down
(
128
,
256
)
with
scope
(
'block3'
):
self
.
down3
=
Down
(
256
,
512
)
with
scope
(
'block4'
):
self
.
down4
=
Down
(
512
,
512
)
def
forward
(
self
,
x
):
...
...
@@ -104,17 +85,12 @@ class Encoder(fluid.dygraph.Layer):
class
Decode
(
fluid
.
dygraph
.
Layer
):
def
__init__
(
self
,
upsample_mode
=
'bilinear'
):
def
__init__
(
self
):
super
().
__init__
()
with
scope
(
'decode'
):
with
scope
(
'decode1'
):
self
.
up1
=
Up
(
512
,
256
,
upsample_mode
)
with
scope
(
'decode2'
):
self
.
up2
=
Up
(
256
,
128
,
upsample_mode
)
with
scope
(
'decode3'
):
self
.
up3
=
Up
(
128
,
64
,
upsample_mode
)
with
scope
(
'decode4'
):
self
.
up4
=
Up
(
64
,
64
,
upsample_mode
)
self
.
up1
=
Up
(
512
,
256
)
self
.
up2
=
Up
(
256
,
128
)
self
.
up3
=
Up
(
128
,
64
)
self
.
up4
=
Up
(
64
,
64
)
def
forward
(
self
,
x
,
short_cuts
):
x
=
self
.
up1
(
x
,
short_cuts
[
3
])
...
...
@@ -124,56 +100,23 @@ class Decode(fluid.dygraph.Layer):
return
x
class
GetLogit
(
fluid
.
dygraph
.
Layer
):
def
__init__
(
self
):
super
().
__init__
()
class
DoubleConv
(
fluid
.
dygraph
.
Layer
):
def
__init__
(
self
,
num_channels
,
num_filters
):
super
().
__init__
()
with
scope
(
'conv0'
):
param_attr
=
fluid
.
ParamAttr
(
name
=
name_scope
+
'weights'
,
regularizer
=
regularizer
,
initializer
=
fluid
.
initializer
.
TruncatedNormal
(
loc
=
0.0
,
scale
=
0.33
))
self
.
conv0
=
Conv2D
(
num_channels
=
num_channels
,
num_filters
=
num_filters
,
filter_size
=
3
,
stride
=
1
,
padding
=
1
,
param_attr
=
param_attr
)
self
.
bn0
=
BatchNorm
(
num_channels
=
num_filters
,
param_attr
=
fluid
.
ParamAttr
(
name
=
name_scope
+
'gamma'
,
regularizer
=
regularizer
),
bias_attr
=
fluid
.
ParamAttr
(
name
=
name_scope
+
'beta'
,
regularizer
=
regularizer
),
moving_mean_name
=
name_scope
+
'moving_mean'
,
moving_variance_name
=
name_scope
+
'moving_variance'
)
with
scope
(
'conv1'
):
param_attr
=
fluid
.
ParamAttr
(
name
=
name_scope
+
'weights'
,
regularizer
=
regularizer
,
initializer
=
fluid
.
initializer
.
TruncatedNormal
(
loc
=
0.0
,
scale
=
0.33
))
padding
=
1
)
self
.
bn0
=
BatchNorm
(
num_channels
=
num_filters
)
self
.
conv1
=
Conv2D
(
num_channels
=
num_filters
,
num_filters
=
num_filters
,
filter_size
=
3
,
stride
=
1
,
padding
=
1
,
param_attr
=
param_attr
)
self
.
bn1
=
BatchNorm
(
num_channels
=
num_filters
,
param_attr
=
fluid
.
ParamAttr
(
name
=
name_scope
+
'gamma'
,
regularizer
=
regularizer
),
bias_attr
=
fluid
.
ParamAttr
(
name
=
name_scope
+
'beta'
,
regularizer
=
regularizer
),
moving_mean_name
=
name_scope
+
'moving_mean'
,
moving_variance_name
=
name_scope
+
'moving_variance'
)
padding
=
1
)
self
.
bn1
=
BatchNorm
(
num_channels
=
num_filters
)
def
forward
(
self
,
x
):
x
=
self
.
conv0
(
x
)
...
...
@@ -188,7 +131,6 @@ class DoubleConv(fluid.dygraph.Layer):
class
Down
(
fluid
.
dygraph
.
Layer
):
def
__init__
(
self
,
num_channels
,
num_filters
):
super
().
__init__
()
with
scope
(
"down"
):
self
.
max_pool
=
Pool2D
(
pool_size
=
2
,
pool_type
=
'max'
,
pool_stride
=
2
,
pool_padding
=
0
)
self
.
double_conv
=
DoubleConv
(
num_channels
,
num_filters
)
...
...
@@ -200,34 +142,13 @@ class Down(fluid.dygraph.Layer):
class
Up
(
fluid
.
dygraph
.
Layer
):
def
__init__
(
self
,
num_channels
,
num_filters
,
upsample_mode
):
def
__init__
(
self
,
num_channels
,
num_filters
):
super
().
__init__
()
self
.
upsample_mode
=
upsample_mode
with
scope
(
'up'
):
if
upsample_mode
==
'bilinear'
:
self
.
double_conv
=
DoubleConv
(
2
*
num_channels
,
num_filters
)
if
not
upsample_mode
==
'bilinear'
:
param_attr
=
fluid
.
ParamAttr
(
name
=
name_scope
+
'weights'
,
regularizer
=
regularizer
,
initializer
=
fluid
.
initializer
.
XavierInitializer
(),
)
self
.
deconv
=
fluid
.
dygraph
.
Conv2DTranspose
(
num_channels
=
num_channels
,
num_filters
=
num_filters
//
2
,
filter_size
=
2
,
stride
=
2
,
padding
=
0
,
param_attr
=
param_attr
)
self
.
double_conv
=
DoubleConv
(
num_channels
+
num_filters
//
2
,
num_filters
)
def
forward
(
self
,
x
,
short_cut
):
if
self
.
upsample_mode
==
'bilinear'
:
short_cut_shape
=
fluid
.
layers
.
shape
(
short_cut
)
x
=
fluid
.
layers
.
resize_bilinear
(
x
,
short_cut_shape
[
2
:])
else
:
x
=
self
.
deconv
(
x
)
x
=
fluid
.
layers
.
concat
([
x
,
short_cut
],
axis
=
1
)
x
=
self
.
double_conv
(
x
)
return
x
...
...
@@ -236,19 +157,12 @@ class Up(fluid.dygraph.Layer):
class
GetLogit
(
fluid
.
dygraph
.
Layer
):
def
__init__
(
self
,
num_channels
,
num_classes
):
super
().
__init__
()
with
scope
(
'logit'
):
param_attr
=
fluid
.
ParamAttr
(
name
=
name_scope
+
'weights'
,
regularizer
=
regularizer
,
initializer
=
fluid
.
initializer
.
TruncatedNormal
(
loc
=
0.0
,
scale
=
0.01
))
self
.
conv
=
Conv2D
(
num_channels
=
num_channels
,
num_filters
=
num_classes
,
filter_size
=
3
,
stride
=
1
,
padding
=
1
,
param_attr
=
param_attr
)
padding
=
1
)
def
forward
(
self
,
x
):
x
=
self
.
conv
(
x
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录