Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSeg
提交
aa160894
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看板
提交
aa160894
编写于
6月 22, 2020
作者:
C
chenguowei01
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add resume training
上级
40950add
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
33 addition
and
6 deletion
+33
-6
dygraph/train.py
dygraph/train.py
+15
-5
dygraph/utils/utils.py
dygraph/utils/utils.py
+18
-1
未找到文件。
dygraph/train.py
浏览文件 @
aa160894
...
...
@@ -26,6 +26,7 @@ import models
import
utils.logging
as
logging
from
utils
import
get_environ_info
from
utils
import
load_pretrained_model
from
utils
import
resume
from
val
import
evaluate
...
...
@@ -117,13 +118,18 @@ def train(model,
num_epochs
=
100
,
batch_size
=
2
,
pretrained_model
=
None
,
resume_model
=
None
,
save_interval_epochs
=
1
,
num_classes
=
None
,
num_workers
=
8
):
ignore_index
=
model
.
ignore_index
nranks
=
ParallelEnv
().
nranks
load_pretrained_model
(
model
,
pretrained_model
)
start_epoch
=
0
if
resume_model
is
not
None
:
start_epoch
=
resume
(
optimizer
,
resume_model
)
elif
pretrained_model
is
not
None
:
load_pretrained_model
(
model
,
pretrained_model
)
if
not
os
.
path
.
isdir
(
save_dir
):
if
os
.
path
.
exists
(
save_dir
):
...
...
@@ -144,7 +150,7 @@ def train(model,
return_list
=
True
,
)
for
epoch
in
range
(
num_epochs
):
for
epoch
in
range
(
start_epoch
,
num_epochs
):
for
step
,
data
in
enumerate
(
loader
):
images
=
data
[
0
]
labels
=
data
[
1
].
astype
(
'int64'
)
...
...
@@ -158,9 +164,11 @@ def train(model,
loss
.
backward
()
optimizer
.
minimize
(
loss
)
model
.
clear_gradients
()
logging
.
info
(
"[TRAIN] Epoch={}/{}, Step={}/{}, loss={}"
.
format
(
epoch
+
1
,
num_epochs
,
step
+
1
,
len
(
batch_sampler
),
loss
.
numpy
()))
lr
=
optimizer
.
current_step_lr
()
logging
.
info
(
"[TRAIN] Epoch={}/{}, Step={}/{}, loss={}, lr={}"
.
format
(
epoch
+
1
,
num_epochs
,
step
+
1
,
len
(
batch_sampler
),
loss
.
numpy
(),
lr
))
if
((
epoch
+
1
)
%
save_interval_epochs
==
0
or
epoch
==
num_epochs
-
1
)
and
ParallelEnv
().
local_rank
==
0
:
...
...
@@ -170,6 +178,8 @@ def train(model,
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'
))
if
eval_dataset
is
not
None
:
evaluate
(
...
...
dygraph/utils/utils.py
浏览文件 @
aa160894
...
...
@@ -49,7 +49,7 @@ def get_environ_info():
def
load_pretrained_model
(
model
,
pretrained_model
):
if
pretrained_model
is
not
None
:
logging
.
info
(
'Load pretrained model
!'
)
logging
.
info
(
'Load pretrained model
from {}'
.
format
(
pretrained_model
)
)
if
os
.
path
.
exists
(
pretrained_model
):
ckpt_path
=
os
.
path
.
join
(
pretrained_model
,
'model'
)
para_state_dict
,
_
=
fluid
.
load_dygraph
(
ckpt_path
)
...
...
@@ -78,6 +78,23 @@ def load_pretrained_model(model, pretrained_model):
pretrained_model
))
def
resume
(
optimizer
,
resume_model
):
if
resume_model
is
not
None
:
logging
.
info
(
'Resume model from {}'
.
format
(
resume_model
))
if
os
.
path
.
exists
(
resume_model
):
ckpt_path
=
os
.
path
.
join
(
resume_model
,
'model'
)
_
,
opti_state_dict
=
fluid
.
load_dygraph
(
ckpt_path
)
optimizer
.
set_dict
(
opti_state_dict
)
epoch
=
resume_model
.
split
(
'_'
)[
-
1
]
if
epoch
.
isdigit
():
epoch
=
int
(
epoch
)
return
epoch
else
:
raise
ValueError
(
'The resume model directory is not Found: {}'
.
formnat
(
resume_model
))
def
visualize
(
image
,
result
,
save_dir
=
None
,
weight
=
0.6
):
"""
Convert segment result to color image, and save added image.
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录