Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSeg
提交
cc68cb45
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看板
提交
cc68cb45
编写于
9月 10, 2020
作者:
C
chenguowei01
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
only init weight in model
上级
d22e3e5e
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
67 addition
and
71 deletion
+67
-71
dygraph/configs/_base_/cityscapes.yml
dygraph/configs/_base_/cityscapes.yml
+1
-0
dygraph/configs/fcn_hrnet/fcn_hrnetw18_cityscapes_1024x512_100k.yml
...nfigs/fcn_hrnet/fcn_hrnetw18_cityscapes_1024x512_100k.yml
+1
-1
dygraph/configs/fcn_hrnet/fcn_hrnetw18_optic_disc_512x512_10k.yml
...configs/fcn_hrnet/fcn_hrnetw18_optic_disc_512x512_10k.yml
+1
-1
dygraph/configs/fcn_hrnet/fcn_hrnetw48_cityscapes_1024x512_100k.yml
...nfigs/fcn_hrnet/fcn_hrnetw48_cityscapes_1024x512_100k.yml
+9
-0
dygraph/core/train.py
dygraph/core/train.py
+2
-2
dygraph/models/architectures/hrnet.py
dygraph/models/architectures/hrnet.py
+0
-28
dygraph/models/fcn.py
dygraph/models/fcn.py
+22
-31
dygraph/models/losses/cross_entroy_loss.py
dygraph/models/losses/cross_entroy_loss.py
+27
-5
dygraph/train.py
dygraph/train.py
+3
-2
dygraph/utils/utils.py
dygraph/utils/utils.py
+1
-1
未找到文件。
dygraph/configs/_base_/cityscapes.yml
浏览文件 @
cc68cb45
...
...
@@ -36,4 +36,5 @@ learning_rate:
loss
:
types
:
-
type
:
CrossEntropyLoss
ignore_index
:
255
coef
:
[
1
]
dygraph/configs/fcn_hrnet/fcn_hrnetw18_cityscapes_1024x512_100k.yml
浏览文件 @
cc68cb45
...
...
@@ -4,6 +4,6 @@ model:
type
:
FCN
backbone
:
type
:
HRNet_W18
backbone_pretrained
:
pretrained_model/hrnet_w18_imagenet
num_classes
:
19
backbone_channels
:
[
270
]
backbone_pretrained
:
pretrained_model/hrnet_w18_imagenet
dygraph/configs/fcn_hrnet/fcn_hrnetw18_optic_disc_512x512_10k.yml
浏览文件 @
cc68cb45
...
...
@@ -4,6 +4,6 @@ model:
type
:
FCN
backbone
:
type
:
HRNet_W18
backbone_pretrained
:
pretrained_model/hrnet_w18_imagenet
num_classes
:
2
backbone_channels
:
[
270
]
backbone_pretrained
:
pretrained_model/hrnet_w18_imagenet
dygraph/configs/fcn_hrnet/fcn_hrnetw48_cityscapes_1024x512_100k.yml
0 → 100644
浏览文件 @
cc68cb45
_base_
:
'
../_base_/cityscapes.yml'
model
:
type
:
FCN
backbone
:
type
:
HRNet_W48
num_classes
:
19
backbone_channels
:
[
720
]
backbone_pretrained
:
pretrained_model/hrnet_w48_imagenet
dygraph/core/train.py
浏览文件 @
cc68cb45
...
...
@@ -64,8 +64,8 @@ def train(model,
num_classes
=
None
,
num_workers
=
8
,
use_vdl
=
False
,
losses
=
None
):
ignore_index
=
model
.
ignore_index
losses
=
None
,
ignore_index
=
255
):
nranks
=
ParallelEnv
().
nranks
start_iter
=
0
...
...
dygraph/models/architectures/hrnet.py
浏览文件 @
cc68cb45
...
...
@@ -56,7 +56,6 @@ class HRNet(fluid.dygraph.Layer):
"""
def
__init__
(
self
,
backbone_pretrained
=
None
,
stage1_num_modules
=
1
,
stage1_num_blocks
=
[
4
],
stage1_num_channels
=
[
64
],
...
...
@@ -146,9 +145,6 @@ class HRNet(fluid.dygraph.Layer):
has_se
=
self
.
has_se
,
name
=
"st4"
)
if
self
.
training
:
self
.
init_weight
(
backbone_pretrained
)
def
forward
(
self
,
x
,
label
=
None
,
mode
=
'train'
):
input_shape
=
x
.
shape
[
2
:]
conv1
=
self
.
conv_layer1_1
(
x
)
...
...
@@ -173,30 +169,6 @@ class HRNet(fluid.dygraph.Layer):
return
[
x
]
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.
"""
params
=
self
.
parameters
()
for
param
in
params
:
param_name
=
param
.
name
if
'batch_norm'
in
param_name
:
if
'w_0'
in
param_name
:
param_init
.
constant_init
(
param
,
1.0
)
elif
'b_0'
in
param_name
:
param_init
.
constant_init
(
param
,
0.0
)
if
'conv'
in
param_name
and
'w_0'
in
param_name
:
param_init
.
normal_init
(
param
,
scale
=
0.001
)
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
))
class
ConvBNLayer
(
fluid
.
dygraph
.
Layer
):
def
__init__
(
self
,
...
...
dygraph/models/fcn.py
浏览文件 @
cc68cb45
...
...
@@ -26,6 +26,7 @@ from paddle.nn import SyncBatchNorm as BatchNorm
from
dygraph.cvlibs
import
manager
from
dygraph
import
utils
from
dygraph.cvlibs
import
param_init
from
dygraph.utils
import
logger
__all__
=
[
"fcn_hrnet_w18_small_v1"
,
"fcn_hrnet_w18_small_v2"
,
"fcn_hrnet_w18"
,
...
...
@@ -52,25 +53,22 @@ class FCN(fluid.dygraph.Layer):
backbone_channels (tuple): the same length with "backbone_indices". It indicates the channels of corresponding index.
channels (int): channels after conv layer before the last one.
ignore_index (int): the value of ground-truth mask would be ignored while computing loss or doing evaluation. Default 255.
"""
def
__init__
(
self
,
num_classes
,
backbone
,
backbone_pretrained
=
None
,
model_pretrained
=
None
,
backbone_indices
=
(
-
1
,
),
backbone_channels
=
(
270
,
),
channels
=
None
,
ignore_index
=
255
,
**
kwargs
):
channels
=
None
):
super
(
FCN
,
self
).
__init__
()
self
.
num_classes
=
num_classes
self
.
backbone_pretrained
=
backbone_pretrained
self
.
model_pretrained
=
model_pretrained
self
.
backbone_indices
=
backbone_indices
self
.
ignore_index
=
ignore_index
self
.
EPS
=
1e-5
if
channels
is
None
:
channels
=
backbone_channels
[
backbone_indices
[
0
]]
...
...
@@ -87,7 +85,7 @@ class FCN(fluid.dygraph.Layer):
stride
=
1
,
padding
=
0
)
if
self
.
training
:
self
.
init_weight
(
model_pretrained
)
self
.
init_weight
()
def
forward
(
self
,
x
):
input_shape
=
x
.
shape
[
2
:]
...
...
@@ -98,40 +96,33 @@ class FCN(fluid.dygraph.Layer):
logit
=
fluid
.
layers
.
resize_bilinear
(
logit
,
input_shape
)
return
[
logit
]
# if self.training:
# if label is None:
# raise Exception('Label is need during 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
):
"""
Initialize the parameters of model parts.
Args:
pretrained_model ([str], optional): the path of pretrained model. Defaults to None.
"""
def
init_weight
(
self
):
params
=
self
.
parameters
()
for
param
in
params
:
param_name
=
param
.
name
if
'batch_norm'
in
param_name
:
if
'w_0'
in
param_name
:
param_init
.
constant_init
(
param
,
1.0
)
param_init
.
constant_init
(
param
,
value
=
1.0
)
elif
'b_0'
in
param_name
:
param_init
.
constant_init
(
param
,
0.0
)
param_init
.
constant_init
(
param
,
value
=
0.0
)
if
'conv'
in
param_name
and
'w_0'
in
param_name
:
param_init
.
normal_init
(
param
,
scale
=
0.001
)
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
(
self
.
model_pretrained
))
elif
self
.
backbone_pretrained
is
not
None
:
if
os
.
path
.
exists
(
self
.
backbone_pretrained
):
utils
.
load_pretrained_model
(
self
.
backbone
,
self
.
backbone_pretrained
)
else
:
raise
Exception
(
'Pretrained model is not found: {}'
.
format
(
pretrained_model
))
self
.
backbone_pretrained
))
else
:
logger
.
warning
(
'No pretrained model to load, train from scratch'
)
class
ConvBNLayer
(
fluid
.
dygraph
.
Layer
):
...
...
dygraph/models/losses/cross_entroy_loss.py
浏览文件 @
cc68cb45
...
...
@@ -15,6 +15,7 @@
import
paddle
from
paddle
import
nn
import
paddle.nn.functional
as
F
import
paddle.fluid
as
fluid
from
dygraph.cvlibs
import
manager
'''
...
...
@@ -106,13 +107,34 @@ class CrossEntropyLoss(nn.Layer):
if
len
(
label
.
shape
)
!=
len
(
logit
.
shape
):
label
=
paddle
.
unsqueeze
(
label
,
1
)
loss
=
F
.
softmax_with_cross_entropy
(
logit
,
label
,
ignore_index
=
self
.
ignore_index
,
axis
=
1
)
loss
=
paddle
.
reduce_mean
(
loss
)
# logit = paddle.transpose(logit, [0, 2, 3, 1])
# label = paddle.transpose(label, [0, 2, 3, 1])
# loss = F.softmax_with_cross_entropy(
# logit, label, ignore_index=self.ignore_index, axis=-1)
# loss = paddle.reduce_mean(loss)
# mask = label != self.ignore_index
# mask = paddle.cast(mask, 'float32')
# avg_loss = loss / (paddle.mean(mask) + self.EPS)
# label.stop_gradient = True
# mask.stop_gradient = True
# return avg_loss
logit
=
fluid
.
layers
.
transpose
(
logit
,
[
0
,
2
,
3
,
1
])
label
=
fluid
.
layers
.
transpose
(
label
,
[
0
,
2
,
3
,
1
])
mask
=
label
!=
self
.
ignore_index
mask
=
paddle
.
cast
(
mask
,
'float32'
)
avg_loss
=
loss
/
(
paddle
.
mean
(
mask
)
+
self
.
EPS
)
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
...
...
dygraph/train.py
浏览文件 @
cc68cb45
...
...
@@ -53,7 +53,7 @@ def parse_args():
dest
=
'save_interval_iters'
,
help
=
'The interval iters for save a model snapshot'
,
type
=
int
,
default
=
5
)
default
=
1000
)
parser
.
add_argument
(
'--save_dir'
,
dest
=
'save_dir'
,
...
...
@@ -126,7 +126,8 @@ def main(args):
num_classes
=
train_dataset
.
num_classes
,
num_workers
=
args
.
num_workers
,
use_vdl
=
args
.
use_vdl
,
losses
=
losses
)
losses
=
losses
,
ignore_index
=
losses
[
'types'
][
0
].
ignore_index
)
if
__name__
==
'__main__'
:
...
...
dygraph/utils/utils.py
浏览文件 @
cc68cb45
...
...
@@ -63,7 +63,7 @@ def load_pretrained_model(model, pretrained_model):
'The pretrained model directory is not Found: {}'
.
format
(
pretrained_model
))
else
:
logger
.
info
(
'No pretrained model to load, train from scratch'
)
logger
.
warning
(
'No pretrained model to load, train from scratch'
)
def
resume
(
model
,
optimizer
,
resume_model
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录