Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSeg
提交
455cf5d3
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看板
提交
455cf5d3
编写于
9月 16, 2020
作者:
C
chenguowei01
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update to beta version
上级
c6064306
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
73 addition
and
80 deletion
+73
-80
dygraph/paddleseg/core/train.py
dygraph/paddleseg/core/train.py
+11
-14
dygraph/paddleseg/utils/config.py
dygraph/paddleseg/utils/config.py
+9
-12
dygraph/train.py
dygraph/train.py
+35
-36
dygraph/val.py
dygraph/val.py
+18
-18
未找到文件。
dygraph/paddleseg/core/train.py
浏览文件 @
455cf5d3
...
...
@@ -15,11 +15,9 @@
import
os
import
paddle
import
paddle.fluid
as
fluid
from
paddle.fluid.dygraph.parallel
import
ParallelEnv
from
paddle.fluid.io
import
DataLoader
# from paddle.incubate.hapi.distributed import DistributedBatchSampler
from
paddle.distributed
import
ParallelEnv
from
paddle.io
import
DistributedBatchSampler
from
paddle.io
import
DataLoader
import
paddle.nn.functional
as
F
import
paddleseg.utils.logger
as
logger
...
...
@@ -79,8 +77,8 @@ def train(model,
os
.
makedirs
(
save_dir
)
if
nranks
>
1
:
strategy
=
fluid
.
dygraph
.
prepare_context
()
ddp_model
=
fluid
.
dygraph
.
DataParallel
(
model
,
strategy
)
strategy
=
paddle
.
distributed
.
prepare_context
()
ddp_model
=
paddle
.
DataParallel
(
model
,
strategy
)
batch_sampler
=
DistributedBatchSampler
(
train_dataset
,
batch_size
=
batch_size
,
shuffle
=
True
,
drop_last
=
True
)
...
...
@@ -130,7 +128,7 @@ def train(model,
optimizer
.
minimize
(
loss
)
model
.
clear_gradients
()
avg_loss
+=
loss
.
numpy
()[
0
]
lr
=
optimizer
.
current_step
_lr
()
lr
=
optimizer
.
get
_lr
()
train_batch_cost
+=
timer
.
elapsed_time
()
if
(
iter
)
%
log_iters
==
0
and
ParallelEnv
().
local_rank
==
0
:
avg_loss
/=
log_iters
...
...
@@ -160,10 +158,10 @@ def train(model,
"iter_{}"
.
format
(
iter
))
if
not
os
.
path
.
isdir
(
current_save_dir
):
os
.
makedirs
(
current_save_dir
)
fluid
.
save_dygraph
(
model
.
state_dict
(),
os
.
path
.
join
(
current_save_dir
,
'model'
))
fluid
.
save_dygraph
(
optimizer
.
state_dict
(),
os
.
path
.
join
(
current_save_dir
,
'model'
))
paddle
.
save
(
model
.
state_dict
(),
os
.
path
.
join
(
current_save_dir
,
'model'
))
paddle
.
save
(
optimizer
.
state_dict
(),
os
.
path
.
join
(
current_save_dir
,
'model'
))
if
eval_dataset
is
not
None
:
mean_iou
,
avg_acc
=
evaluate
(
...
...
@@ -177,9 +175,8 @@ def train(model,
best_mean_iou
=
mean_iou
best_model_iter
=
iter
best_model_dir
=
os
.
path
.
join
(
save_dir
,
"best_model"
)
fluid
.
save_dygraph
(
model
.
state_dict
(),
os
.
path
.
join
(
best_model_dir
,
'model'
))
paddle
.
save
(
model
.
state_dict
(),
os
.
path
.
join
(
best_model_dir
,
'model'
))
logger
.
info
(
'Current evaluated best model in eval_dataset is iter_{}, miou={:4f}'
.
format
(
best_model_iter
,
best_mean_iou
))
...
...
dygraph/paddleseg/utils/config.py
浏览文件 @
455cf5d3
...
...
@@ -15,11 +15,14 @@
import
codecs
import
os
from
typing
import
Any
,
Callable
import
pprint
import
yaml
import
paddle.fluid
as
fluid
import
paddle
import
paddle.nn.functional
as
F
import
paddleseg.cvlibs.manager
as
manager
from
paddleseg.utils
import
logger
class
Config
(
object
):
...
...
@@ -36,7 +39,7 @@ class Config(object):
if
path
.
endswith
(
'yml'
)
or
path
.
endswith
(
'yaml'
):
dic
=
self
.
_parse_from_yaml
(
path
)
print
(
dic
)
logger
.
info
(
'
\n
'
+
pprint
.
pformat
(
dic
)
)
self
.
_build
(
dic
)
else
:
raise
RuntimeError
(
'Config file should in yaml format!'
)
...
...
@@ -127,24 +130,18 @@ class Config(object):
lr
=
self
.
_learning_rate
args
=
self
.
decay_args
args
.
setdefault
(
'decay_steps'
,
self
.
iters
)
return
fluid
.
layers
.
polynomial_decay
(
lr
,
**
args
)
return
paddle
.
optimizer
.
PolynomialLR
(
lr
,
**
args
)
else
:
raise
RuntimeError
(
'Only poly decay support.'
)
@
property
def
optimizer
(
self
)
->
fluid
.
optimizer
.
Optimizer
:
def
optimizer
(
self
)
->
paddle
.
optimizer
.
Optimizer
:
if
self
.
optimizer_type
==
'sgd'
:
lr
=
self
.
learning_rate
args
=
self
.
optimizer_args
args
.
setdefault
(
'momentum'
,
0.9
)
weight_decay
=
args
.
get
(
'weight_decay'
,
None
)
if
weight_decay
is
not
None
:
args
.
pop
(
'weight_decay'
)
regularization
=
fluid
.
regularizer
.
L2DecayRegularizer
(
regularization_coeff
=
weight_decay
)
args
.
setdefault
(
'regularization'
,
None
)
return
fluid
.
optimizer
.
Momentum
(
lr
,
parameter_list
=
self
.
model
.
parameters
(),
**
args
)
return
paddle
.
optimizer
.
Momentum
(
lr
,
parameters
=
self
.
model
.
parameters
(),
**
args
)
else
:
raise
RuntimeError
(
'Only sgd optimizer support.'
)
...
...
dygraph/train.py
浏览文件 @
455cf5d3
...
...
@@ -14,8 +14,8 @@
import
argparse
import
paddle
.fluid
as
fluid
from
paddle.
fluid.dygraph.parallel
import
ParallelEnv
import
paddle
from
paddle.
distributed
import
ParallelEnv
import
paddleseg
from
paddleseg.cvlibs
import
manager
...
...
@@ -91,41 +91,40 @@ def main(args):
[
'-'
*
48
])
logger
.
info
(
info
)
places
=
fluid
.
CUDAPlace
(
ParallelEnv
().
dev_id
)
\
places
=
paddle
.
CUDAPlace
(
ParallelEnv
().
dev_id
)
\
if
env_info
[
'Paddle compiled with cuda'
]
and
env_info
[
'GPUs used'
]
\
else
fluid
.
CPUPlace
()
with
fluid
.
dygraph
.
guard
(
places
):
if
not
args
.
cfg
:
raise
RuntimeError
(
'No configuration file specified.'
)
cfg
=
Config
(
args
.
cfg
)
train_dataset
=
cfg
.
train_dataset
if
not
train_dataset
:
raise
RuntimeError
(
'The training dataset is not specified in the configuration file.'
)
val_dataset
=
cfg
.
val_dataset
if
args
.
do_eval
else
None
losses
=
cfg
.
loss
train
(
cfg
.
model
,
train_dataset
,
places
=
places
,
eval_dataset
=
val_dataset
,
optimizer
=
cfg
.
optimizer
,
save_dir
=
args
.
save_dir
,
iters
=
cfg
.
iters
,
batch_size
=
cfg
.
batch_size
,
save_interval_iters
=
args
.
save_interval_iters
,
log_iters
=
args
.
log_iters
,
num_classes
=
train_dataset
.
num_classes
,
num_workers
=
args
.
num_workers
,
use_vdl
=
args
.
use_vdl
,
losses
=
losses
,
ignore_index
=
losses
[
'types'
][
0
].
ignore_index
)
else
paddle
.
CPUPlace
()
paddle
.
disable_static
(
places
)
if
not
args
.
cfg
:
raise
RuntimeError
(
'No configuration file specified.'
)
cfg
=
Config
(
args
.
cfg
)
train_dataset
=
cfg
.
train_dataset
if
not
train_dataset
:
raise
RuntimeError
(
'The training dataset is not specified in the configuration file.'
)
val_dataset
=
cfg
.
val_dataset
if
args
.
do_eval
else
None
losses
=
cfg
.
loss
train
(
cfg
.
model
,
train_dataset
,
places
=
places
,
eval_dataset
=
val_dataset
,
optimizer
=
cfg
.
optimizer
,
save_dir
=
args
.
save_dir
,
iters
=
cfg
.
iters
,
batch_size
=
cfg
.
batch_size
,
save_interval_iters
=
args
.
save_interval_iters
,
log_iters
=
args
.
log_iters
,
num_classes
=
train_dataset
.
num_classes
,
num_workers
=
args
.
num_workers
,
use_vdl
=
args
.
use_vdl
,
losses
=
losses
,
ignore_index
=
losses
[
'types'
][
0
].
ignore_index
)
if
__name__
==
'__main__'
:
...
...
dygraph/val.py
浏览文件 @
455cf5d3
...
...
@@ -14,8 +14,8 @@
import
argparse
import
paddle
.fluid
as
fluid
from
paddle.
fluid.dygraph.parallel
import
ParallelEnv
import
paddle
from
paddle.
distributed
import
ParallelEnv
import
paddleseg
from
paddleseg.cvlibs
import
manager
...
...
@@ -41,25 +41,25 @@ def parse_args():
def
main
(
args
):
env_info
=
get_environ_info
()
places
=
fluid
.
CUDAPlace
(
ParallelEnv
().
dev_id
)
\
places
=
paddle
.
CUDAPlace
(
ParallelEnv
().
dev_id
)
\
if
env_info
[
'Paddle compiled with cuda'
]
and
env_info
[
'GPUs used'
]
\
else
fluid
.
CPUPlace
()
else
paddle
.
CPUPlace
()
with
fluid
.
dygraph
.
guard
(
places
):
if
not
args
.
cfg
:
raise
RuntimeError
(
'No configuration file specified.'
)
paddle
.
disable_static
(
places
)
if
not
args
.
cfg
:
raise
RuntimeError
(
'No configuration file specified.'
)
cfg
=
Config
(
args
.
cfg
)
val_dataset
=
cfg
.
val_dataset
if
not
val_dataset
:
raise
RuntimeError
(
'The verification dataset is not specified in the configuration file.'
)
evaluate
(
cfg
.
model
,
val_dataset
,
model_dir
=
args
.
model_dir
,
num_classes
=
val_dataset
.
num_classes
)
cfg
=
Config
(
args
.
cfg
)
val_dataset
=
cfg
.
val_dataset
if
not
val_dataset
:
raise
RuntimeError
(
'The verification dataset is not specified in the configuration file.'
)
evaluate
(
cfg
.
model
,
val_dataset
,
model_dir
=
args
.
model_dir
,
num_classes
=
val_dataset
.
num_classes
)
if
__name__
==
'__main__'
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录