Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
cea62c00
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
cea62c00
编写于
4月 12, 2023
作者:
W
WangZhen
提交者:
GitHub
4月 12, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Eval during train for ResNet (#52768)
* Eval during train for ResNet
上级
9f2e3064
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
47 addition
and
31 deletion
+47
-31
test/prim/model/test_resnet_prim_cinn.py
test/prim/model/test_resnet_prim_cinn.py
+47
-31
未找到文件。
test/prim/model/test_resnet_prim_cinn.py
浏览文件 @
cea62c00
...
@@ -131,31 +131,13 @@ def optimizer_setting(parameter_list=None):
...
@@ -131,31 +131,13 @@ def optimizer_setting(parameter_list=None):
return
optimizer
return
optimizer
def
train
(
to_static
,
enable_prim
,
enable_cinn
):
def
run
(
model
,
data_loader
,
optimizer
,
mode
):
if
core
.
is_compiled_with_cuda
():
if
mode
==
'train'
:
paddle
.
set_device
(
'gpu'
)
model
.
train
()
else
:
end_step
=
9
paddle
.
set_device
(
'cpu'
)
elif
mode
==
'eval'
:
np
.
random
.
seed
(
SEED
)
model
.
eval
()
paddle
.
seed
(
SEED
)
end_step
=
1
paddle
.
framework
.
random
.
_manual_program_seed
(
SEED
)
fluid
.
core
.
_set_prim_all_enabled
(
enable_prim
)
train_reader
=
paddle
.
batch
(
reader_decorator
(
paddle
.
dataset
.
flowers
.
train
(
use_xmap
=
False
)),
batch_size
=
batch_size
,
drop_last
=
True
,
)
data_loader
=
fluid
.
io
.
DataLoader
.
from_generator
(
capacity
=
5
,
iterable
=
True
)
data_loader
.
set_sample_list_generator
(
train_reader
)
resnet
=
resnet50
(
False
)
if
to_static
:
build_strategy
=
paddle
.
static
.
BuildStrategy
()
if
enable_cinn
:
build_strategy
.
build_cinn_pass
=
True
resnet
=
paddle
.
jit
.
to_static
(
resnet
,
build_strategy
=
build_strategy
)
optimizer
=
optimizer_setting
(
parameter_list
=
resnet
.
parameters
())
for
epoch
in
range
(
epoch_num
):
for
epoch
in
range
(
epoch_num
):
total_acc1
=
0.0
total_acc1
=
0.0
...
@@ -167,7 +149,7 @@ def train(to_static, enable_prim, enable_cinn):
...
@@ -167,7 +149,7 @@ def train(to_static, enable_prim, enable_cinn):
start_time
=
time
.
time
()
start_time
=
time
.
time
()
img
,
label
=
data
img
,
label
=
data
pred
=
resnet
(
img
)
pred
=
model
(
img
)
avg_loss
=
paddle
.
nn
.
functional
.
cross_entropy
(
avg_loss
=
paddle
.
nn
.
functional
.
cross_entropy
(
input
=
pred
,
input
=
pred
,
label
=
label
,
label
=
label
,
...
@@ -179,9 +161,10 @@ def train(to_static, enable_prim, enable_cinn):
...
@@ -179,9 +161,10 @@ def train(to_static, enable_prim, enable_cinn):
acc_top1
=
paddle
.
static
.
accuracy
(
input
=
pred
,
label
=
label
,
k
=
1
)
acc_top1
=
paddle
.
static
.
accuracy
(
input
=
pred
,
label
=
label
,
k
=
1
)
acc_top5
=
paddle
.
static
.
accuracy
(
input
=
pred
,
label
=
label
,
k
=
5
)
acc_top5
=
paddle
.
static
.
accuracy
(
input
=
pred
,
label
=
label
,
k
=
5
)
avg_loss
.
backward
()
if
mode
==
'train'
:
optimizer
.
minimize
(
avg_loss
)
avg_loss
.
backward
()
resnet
.
clear_gradients
()
optimizer
.
minimize
(
avg_loss
)
model
.
clear_gradients
()
total_acc1
+=
acc_top1
total_acc1
+=
acc_top1
total_acc5
+=
acc_top5
total_acc5
+=
acc_top5
...
@@ -190,8 +173,9 @@ def train(to_static, enable_prim, enable_cinn):
...
@@ -190,8 +173,9 @@ def train(to_static, enable_prim, enable_cinn):
end_time
=
time
.
time
()
end_time
=
time
.
time
()
print
(
print
(
"epoch %d | batch step %d, loss %0.8f, acc1 %0.3f, acc5 %0.3f, time %f"
"
[%s]
epoch %d | batch step %d, loss %0.8f, acc1 %0.3f, acc5 %0.3f, time %f"
%
(
%
(
mode
,
epoch
,
epoch
,
batch_id
,
batch_id
,
avg_loss
,
avg_loss
,
...
@@ -200,7 +184,7 @@ def train(to_static, enable_prim, enable_cinn):
...
@@ -200,7 +184,7 @@ def train(to_static, enable_prim, enable_cinn):
end_time
-
start_time
,
end_time
-
start_time
,
)
)
)
)
if
batch_id
>=
9
:
if
batch_id
>=
end_step
:
# avoid dataloader throw abort signaal
# avoid dataloader throw abort signaal
data_loader
.
_reset
()
data_loader
.
_reset
()
break
break
...
@@ -208,6 +192,38 @@ def train(to_static, enable_prim, enable_cinn):
...
@@ -208,6 +192,38 @@ def train(to_static, enable_prim, enable_cinn):
return
losses
return
losses
def
train
(
to_static
,
enable_prim
,
enable_cinn
):
if
core
.
is_compiled_with_cuda
():
paddle
.
set_device
(
'gpu'
)
else
:
paddle
.
set_device
(
'cpu'
)
np
.
random
.
seed
(
SEED
)
paddle
.
seed
(
SEED
)
paddle
.
framework
.
random
.
_manual_program_seed
(
SEED
)
fluid
.
core
.
_set_prim_all_enabled
(
enable_prim
)
train_reader
=
paddle
.
batch
(
reader_decorator
(
paddle
.
dataset
.
flowers
.
train
(
use_xmap
=
False
)),
batch_size
=
batch_size
,
drop_last
=
True
,
)
data_loader
=
fluid
.
io
.
DataLoader
.
from_generator
(
capacity
=
5
,
iterable
=
True
)
data_loader
.
set_sample_list_generator
(
train_reader
)
resnet
=
resnet50
(
False
)
if
to_static
:
build_strategy
=
paddle
.
static
.
BuildStrategy
()
if
enable_cinn
:
build_strategy
.
build_cinn_pass
=
True
resnet
=
paddle
.
jit
.
to_static
(
resnet
,
build_strategy
=
build_strategy
)
optimizer
=
optimizer_setting
(
parameter_list
=
resnet
.
parameters
())
train_losses
=
run
(
resnet
,
data_loader
,
optimizer
,
'train'
)
if
to_static
and
enable_prim
and
enable_cinn
:
eval_losses
=
run
(
resnet
,
data_loader
,
optimizer
,
'eval'
)
return
train_losses
class
TestResnet
(
unittest
.
TestCase
):
class
TestResnet
(
unittest
.
TestCase
):
@
unittest
.
skipIf
(
@
unittest
.
skipIf
(
not
(
paddle
.
is_compiled_with_cinn
()
and
paddle
.
is_compiled_with_cuda
()),
not
(
paddle
.
is_compiled_with_cinn
()
and
paddle
.
is_compiled_with_cuda
()),
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录