Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSeg
提交
30e44c5e
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看板
提交
30e44c5e
编写于
9月 22, 2020
作者:
C
chenguowei01
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update unet.py to 2.0beta
上级
a3bfb074
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
44 addition
and
74 deletion
+44
-74
dygraph/paddleseg/models/unet.py
dygraph/paddleseg/models/unet.py
+43
-73
dygraph/train.py
dygraph/train.py
+1
-1
未找到文件。
dygraph/paddleseg/models/unet.py
浏览文件 @
30e44c5e
...
...
@@ -14,15 +14,19 @@
import
os
import
paddle.fluid
as
fluid
from
paddle.fluid.dygraph
import
Conv2D
,
Pool2D
import
paddle
import
paddle.nn
as
nn
import
paddle.nn.functional
as
F
from
paddle.nn
import
Conv2d
from
paddle.nn
import
SyncBatchNorm
as
BatchNorm
from
paddleseg.cvlibs
import
manager
from
paddleseg
import
utils
from
paddleseg.models.common
import
layer_libs
class
UNet
(
fluid
.
dygraph
.
Layer
):
@
manager
.
MODELS
.
add_component
class
UNet
(
nn
.
Layer
):
"""
U-Net: Convolutional Networks for Biomedical Image Segmentation.
https://arxiv.org/abs/1505.04597
...
...
@@ -35,62 +39,35 @@ class UNet(fluid.dygraph.Layer):
def
__init__
(
self
,
num_classes
,
model_pretrained
=
None
,
ignore_index
=
255
):
super
(
UNet
,
self
).
__init__
()
self
.
model_pretrained
=
model_pretrained
self
.
ignore_index
=
ignore_index
self
.
encode
=
UnetEncoder
()
self
.
decode
=
UnetDecode
()
self
.
get_logit
=
GetLogit
(
64
,
num_classes
)
self
.
ignore_index
=
ignore_index
self
.
EPS
=
1e-5
self
.
init_weight
(
model_pretrained
)
self
.
init_weight
()
def
forward
(
self
,
x
,
label
=
None
):
encode_data
,
short_cuts
=
self
.
encode
(
x
)
decode_data
=
self
.
decode
(
encode_data
,
short_cuts
)
logit
=
self
.
get_logit
(
decode_data
)
if
self
.
training
:
return
self
.
_get_loss
(
logit
,
label
)
else
:
score_map
=
fluid
.
layers
.
softmax
(
logit
,
axis
=
1
)
score_map
=
fluid
.
layers
.
transpose
(
score_map
,
[
0
,
2
,
3
,
1
])
pred
=
fluid
.
layers
.
argmax
(
score_map
,
axis
=
3
)
pred
=
fluid
.
layers
.
unsqueeze
(
pred
,
axes
=
[
3
])
return
pred
,
score_map
def
init_weight
(
self
,
pretrained_model
=
None
):
return
[
logit
]
def
init_weight
(
self
):
"""
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
)
if
self
.
model_pretrained
is
not
None
:
if
os
.
path
.
exists
(
self
.
model_pretrained
):
utils
.
load_pretrained_model
(
self
,
self
.
model_pretrained
)
else
:
raise
Exception
(
'Pretrained model is not found: {}'
.
format
(
pretrained_model
))
def
_get_loss
(
self
,
logit
,
label
):
logit
=
fluid
.
layers
.
transpose
(
logit
,
[
0
,
2
,
3
,
1
])
label
=
fluid
.
layers
.
transpose
(
label
,
[
0
,
2
,
3
,
1
])
mask
=
label
!=
self
.
ignore_index
mask
=
fluid
.
layers
.
cast
(
mask
,
'float32'
)
loss
,
probs
=
fluid
.
layers
.
softmax_with_cross_entropy
(
logit
,
label
,
ignore_index
=
self
.
ignore_index
,
return_softmax
=
True
,
axis
=-
1
)
loss
=
loss
*
mask
avg_loss
=
fluid
.
layers
.
mean
(
loss
)
/
(
fluid
.
layers
.
mean
(
mask
)
+
self
.
EPS
)
label
.
stop_gradient
=
True
mask
.
stop_gradient
=
True
return
avg_loss
class
UnetEncoder
(
fluid
.
dygraph
.
Layer
):
self
.
model_pretrained
))
class
UnetEncoder
(
nn
.
Layer
):
def
__init__
(
self
):
super
(
UnetEncoder
,
self
).
__init__
()
self
.
double_conv
=
DoubleConv
(
3
,
64
)
...
...
@@ -113,7 +90,7 @@ class UnetEncoder(fluid.dygraph.Layer):
return
x
,
short_cuts
class
UnetDecode
(
fluid
.
dygraph
.
Layer
):
class
UnetDecode
(
nn
.
Layer
):
def
__init__
(
self
):
super
(
UnetDecode
,
self
).
__init__
()
self
.
up1
=
Up
(
512
,
256
)
...
...
@@ -129,20 +106,20 @@ class UnetDecode(fluid.dygraph.Layer):
return
x
class
DoubleConv
(
fluid
.
dygraph
.
Layer
):
class
DoubleConv
(
nn
.
Layer
):
def
__init__
(
self
,
num_channels
,
num_filters
):
super
(
DoubleConv
,
self
).
__init__
()
self
.
conv0
=
Conv2
D
(
num
_channels
=
num_channels
,
num_filter
s
=
num_filters
,
filter
_size
=
3
,
self
.
conv0
=
Conv2
d
(
in
_channels
=
num_channels
,
out_channel
s
=
num_filters
,
kernel
_size
=
3
,
stride
=
1
,
padding
=
1
)
self
.
bn0
=
BatchNorm
(
num_filters
)
self
.
conv1
=
Conv2
D
(
num
_channels
=
num_filters
,
num_filter
s
=
num_filters
,
filter
_size
=
3
,
self
.
conv1
=
Conv2
d
(
in
_channels
=
num_filters
,
out_channel
s
=
num_filters
,
kernel
_size
=
3
,
stride
=
1
,
padding
=
1
)
self
.
bn1
=
BatchNorm
(
num_filters
)
...
...
@@ -150,18 +127,17 @@ class DoubleConv(fluid.dygraph.Layer):
def
forward
(
self
,
x
):
x
=
self
.
conv0
(
x
)
x
=
self
.
bn0
(
x
)
x
=
fluid
.
layers
.
relu
(
x
)
x
=
F
.
relu
(
x
)
x
=
self
.
conv1
(
x
)
x
=
self
.
bn1
(
x
)
x
=
fluid
.
layers
.
relu
(
x
)
x
=
F
.
relu
(
x
)
return
x
class
Down
(
fluid
.
dygraph
.
Layer
):
class
Down
(
nn
.
Layer
):
def
__init__
(
self
,
num_channels
,
num_filters
):
super
(
Down
,
self
).
__init__
()
self
.
max_pool
=
Pool2D
(
pool_size
=
2
,
pool_type
=
'max'
,
pool_stride
=
2
,
pool_padding
=
0
)
self
.
max_pool
=
nn
.
MaxPool2d
(
kernel_size
=
2
,
stride
=
2
)
self
.
double_conv
=
DoubleConv
(
num_channels
,
num_filters
)
def
forward
(
self
,
x
):
...
...
@@ -170,34 +146,28 @@ class Down(fluid.dygraph.Layer):
return
x
class
Up
(
fluid
.
dygraph
.
Layer
):
class
Up
(
nn
.
Layer
):
def
__init__
(
self
,
num_channels
,
num_filters
):
super
(
Up
,
self
).
__init__
()
self
.
double_conv
=
DoubleConv
(
2
*
num_channels
,
num_filters
)
def
forward
(
self
,
x
,
short_cut
):
short_cut_shape
=
fluid
.
layers
.
shape
(
short_cut
)
x
=
fluid
.
layers
.
resize_bilinear
(
x
,
short_cut_shape
[
2
:])
x
=
fluid
.
layers
.
concat
([
x
,
short_cut
],
axis
=
1
)
x
=
F
.
resize_bilinear
(
x
,
short_cut
.
shape
[
2
:])
x
=
paddle
.
concat
([
x
,
short_cut
],
axis
=
1
)
x
=
self
.
double_conv
(
x
)
return
x
class
GetLogit
(
fluid
.
dygraph
.
Layer
):
class
GetLogit
(
nn
.
Layer
):
def
__init__
(
self
,
num_channels
,
num_classes
):
super
(
GetLogit
,
self
).
__init__
()
self
.
conv
=
Conv2
D
(
num
_channels
=
num_channels
,
num_filter
s
=
num_classes
,
filter
_size
=
3
,
self
.
conv
=
Conv2
d
(
in
_channels
=
num_channels
,
out_channel
s
=
num_classes
,
kernel
_size
=
3
,
stride
=
1
,
padding
=
1
)
def
forward
(
self
,
x
):
x
=
self
.
conv
(
x
)
return
x
@
manager
.
MODELS
.
add_component
def
unet
(
*
args
,
**
kwargs
):
return
UNet
(
*
args
,
**
kwargs
)
dygraph/train.py
浏览文件 @
30e44c5e
...
...
@@ -87,7 +87,7 @@ def parse_args():
def
main
(
args
):
env_info
=
get_environ_info
()
info
=
[
'{}: {}'
.
format
(
k
,
v
)
for
k
,
v
in
env_info
.
items
()]
info
=
'
\n
'
.
join
([
'
\n
'
,
format
(
'Environment Information'
,
'-^48s'
)]
+
info
+
info
=
'
\n
'
.
join
([
''
,
format
(
'Environment Information'
,
'-^48s'
)]
+
info
+
[
'-'
*
48
])
logger
.
info
(
info
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录