Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
9f53f3d0
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
9f53f3d0
编写于
11月 27, 2020
作者:
L
LielinJiang
提交者:
GitHub
11月 27, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Enhance logger callback for benchmark (#29106)
* enhance logger callback for benchmark
上级
e668cb07
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
114 addition
and
20 deletion
+114
-20
python/paddle/hapi/callbacks.py
python/paddle/hapi/callbacks.py
+100
-10
python/paddle/hapi/model.py
python/paddle/hapi/model.py
+5
-5
python/paddle/hapi/progressbar.py
python/paddle/hapi/progressbar.py
+1
-1
python/paddle/tests/test_callbacks.py
python/paddle/tests/test_callbacks.py
+8
-4
未找到文件。
python/paddle/hapi/callbacks.py
浏览文件 @
9f53f3d0
...
...
@@ -13,6 +13,7 @@
# limitations under the License.
import
os
import
time
import
numbers
import
warnings
...
...
@@ -96,8 +97,8 @@ class CallbackList(object):
func
(
*
args
)
def
_check_mode
(
self
,
mode
):
assert
mode
in
[
'train'
,
'eval'
,
'
tes
t'
],
\
'mode should be train, eval or
tes
t'
assert
mode
in
[
'train'
,
'eval'
,
'
predic
t'
],
\
'mode should be train, eval or
predic
t'
def
on_begin
(
self
,
mode
,
logs
=
None
):
self
.
_check_mode
(
mode
)
...
...
@@ -207,14 +208,14 @@ class Callback(object):
of last batch of validation dataset.
"""
def
on_
tes
t_begin
(
self
,
logs
=
None
):
def
on_
predic
t_begin
(
self
,
logs
=
None
):
"""Called at the beginning of predict.
Args:
logs (dict): The logs is a dict or None.
"""
def
on_
tes
t_end
(
self
,
logs
=
None
):
def
on_
predic
t_end
(
self
,
logs
=
None
):
"""Called at the end of predict.
Args:
...
...
@@ -278,7 +279,7 @@ class Callback(object):
of current batch.
"""
def
on_
tes
t_batch_begin
(
self
,
step
,
logs
=
None
):
def
on_
predic
t_batch_begin
(
self
,
step
,
logs
=
None
):
"""Called at the beginning of each batch in predict.
Args:
...
...
@@ -286,7 +287,7 @@ class Callback(object):
logs (dict): The logs is a dict or None.
"""
def
on_
tes
t_batch_end
(
self
,
step
,
logs
=
None
):
def
on_
predic
t_batch_end
(
self
,
step
,
logs
=
None
):
"""Called at the end of each batch in predict.
Args:
...
...
@@ -303,7 +304,9 @@ class ProgBarLogger(Callback):
log_freq (int): The frequency, in number of steps,
the logs such as loss, metrics are printed. Default: 1.
verbose (int): The verbosity mode, should be 0, 1, or 2.
0 = silent, 1 = progress bar, 2 = one line per epoch. Default: 2.
0 = silent, 1 = progress bar, 2 = one line per epoch, 3 = 2 +
time counter, such as average reader cost, samples per second.
Default: 2.
Examples:
.. code-block:: python
...
...
@@ -351,6 +354,17 @@ class ProgBarLogger(Callback):
self
.
train_metrics
=
self
.
params
[
'metrics'
]
assert
self
.
train_metrics
self
.
_train_timer
=
{
'data_time'
:
0
,
'batch_time'
:
0
,
'count'
:
0
,
'samples'
:
0
,
}
if
self
.
_is_print
():
print
(
"The loss value printed in the log is the current batch, and the metric is the average value of previous step."
)
def
on_epoch_begin
(
self
,
epoch
=
None
,
logs
=
None
):
self
.
steps
=
self
.
params
[
'steps'
]
self
.
epoch
=
epoch
...
...
@@ -359,6 +373,8 @@ class ProgBarLogger(Callback):
print
(
'Epoch %d/%d'
%
(
epoch
+
1
,
self
.
epochs
))
self
.
train_progbar
=
ProgressBar
(
num
=
self
.
steps
,
verbose
=
self
.
verbose
)
self
.
_train_timer
[
'batch_start_time'
]
=
time
.
time
()
def
_updates
(
self
,
logs
,
mode
):
values
=
[]
metrics
=
getattr
(
self
,
'%s_metrics'
%
(
mode
))
...
...
@@ -369,15 +385,39 @@ class ProgBarLogger(Callback):
if
k
in
logs
:
values
.
append
((
k
,
logs
[
k
]))
if
self
.
verbose
==
3
and
hasattr
(
self
,
'_%s_timer'
%
(
mode
)):
timer
=
getattr
(
self
,
'_%s_timer'
%
(
mode
))
cnt
=
timer
[
'count'
]
if
timer
[
'count'
]
>
0
else
1.0
samples
=
timer
[
'samples'
]
if
timer
[
'samples'
]
>
0
else
1.0
values
.
append
(
(
'avg_reader_cost'
,
"%.5f sec"
%
(
timer
[
'data_time'
]
/
cnt
)))
values
.
append
(
(
'avg_batch_cost'
,
"%.5f sec"
%
(
timer
[
'batch_time'
]
/
cnt
)))
values
.
append
(
(
'ips'
,
"%.5f samples/sec"
%
(
samples
/
(
timer
[
'batch_time'
]
+
timer
[
'batch_time'
]))))
progbar
.
update
(
steps
,
values
)
def
on_train_batch_begin
(
self
,
step
,
logs
=
None
):
self
.
_train_timer
[
'batch_data_end_time'
]
=
time
.
time
()
self
.
_train_timer
[
'data_time'
]
+=
(
self
.
_train_timer
[
'batch_data_end_time'
]
-
self
.
_train_timer
[
'batch_start_time'
])
def
on_train_batch_end
(
self
,
step
,
logs
=
None
):
logs
=
logs
or
{}
self
.
train_step
+=
1
self
.
_train_timer
[
'batch_time'
]
+=
(
time
.
time
()
-
self
.
_train_timer
[
'batch_data_end_time'
])
self
.
_train_timer
[
'count'
]
+=
1
samples
=
logs
.
get
(
'batch_size'
,
1
)
self
.
_train_timer
[
'samples'
]
+=
samples
if
self
.
_is_print
()
and
self
.
train_step
%
self
.
log_freq
==
0
:
if
self
.
steps
is
None
or
self
.
train_step
<
self
.
steps
:
self
.
_updates
(
logs
,
'train'
)
self
.
_train_timer
[
'batch_start_time'
]
=
time
.
time
()
def
on_epoch_end
(
self
,
epoch
,
logs
=
None
):
logs
=
logs
or
{}
...
...
@@ -390,10 +430,28 @@ class ProgBarLogger(Callback):
self
.
eval_step
=
0
self
.
evaled_samples
=
0
self
.
_eval_timer
=
{
'data_time'
:
0
,
'batch_time'
:
0
,
'count'
:
0
,
'samples'
:
0
,
}
self
.
eval_progbar
=
ProgressBar
(
num
=
self
.
eval_steps
,
verbose
=
self
.
verbose
)
if
self
.
_is_print
():
print
(
'Eval begin...'
)
print
(
"The loss value printed in the log is the current batch, and the metric is the average value of previous step."
)
self
.
_eval_timer
[
'batch_start_time'
]
=
time
.
time
()
def
on_eval_batch_begin
(
self
,
step
,
logs
=
None
):
self
.
_eval_timer
[
'batch_data_end_time'
]
=
time
.
time
()
self
.
_eval_timer
[
'data_time'
]
+=
(
self
.
_eval_timer
[
'batch_data_end_time'
]
-
self
.
_eval_timer
[
'batch_start_time'
])
def
on_eval_batch_end
(
self
,
step
,
logs
=
None
):
logs
=
logs
or
{}
...
...
@@ -401,37 +459,69 @@ class ProgBarLogger(Callback):
samples
=
logs
.
get
(
'batch_size'
,
1
)
self
.
evaled_samples
+=
samples
self
.
_eval_timer
[
'batch_time'
]
+=
(
time
.
time
()
-
self
.
_eval_timer
[
'batch_data_end_time'
])
self
.
_eval_timer
[
'count'
]
+=
1
samples
=
logs
.
get
(
'batch_size'
,
1
)
self
.
_eval_timer
[
'samples'
]
+=
samples
if
self
.
_is_print
()
and
self
.
eval_step
%
self
.
log_freq
==
0
:
if
self
.
eval_steps
is
None
or
self
.
eval_step
<
self
.
eval_steps
:
self
.
_updates
(
logs
,
'eval'
)
def
on_test_begin
(
self
,
logs
=
None
):
self
.
_eval_timer
[
'batch_start_time'
]
=
time
.
time
()
def
on_predict_begin
(
self
,
logs
=
None
):
self
.
test_steps
=
logs
.
get
(
'steps'
,
None
)
self
.
test_metrics
=
logs
.
get
(
'metrics'
,
[])
self
.
test_step
=
0
self
.
tested_samples
=
0
self
.
_test_timer
=
{
'data_time'
:
0
,
'batch_time'
:
0
,
'count'
:
0
,
'samples'
:
0
,
}
self
.
test_progbar
=
ProgressBar
(
num
=
self
.
test_steps
,
verbose
=
self
.
verbose
)
if
self
.
_is_print
():
print
(
'Predict begin...'
)
def
on_test_batch_end
(
self
,
step
,
logs
=
None
):
self
.
_test_timer
[
'batch_start_time'
]
=
time
.
time
()
def
on_predict_batch_begin
(
self
,
step
,
logs
=
None
):
self
.
_test_timer
[
'batch_data_end_time'
]
=
time
.
time
()
self
.
_test_timer
[
'data_time'
]
+=
(
self
.
_test_timer
[
'batch_data_end_time'
]
-
self
.
_test_timer
[
'batch_start_time'
])
def
on_predict_batch_end
(
self
,
step
,
logs
=
None
):
logs
=
logs
or
{}
self
.
test_step
+=
1
samples
=
logs
.
get
(
'batch_size'
,
1
)
self
.
tested_samples
+=
samples
self
.
_test_timer
[
'batch_time'
]
+=
(
time
.
time
()
-
self
.
_test_timer
[
'batch_data_end_time'
])
self
.
_test_timer
[
'count'
]
+=
1
samples
=
logs
.
get
(
'batch_size'
,
1
)
self
.
_test_timer
[
'samples'
]
+=
samples
if
self
.
test_step
%
self
.
log_freq
==
0
and
self
.
_is_print
():
if
self
.
test_steps
is
None
or
self
.
test_step
<
self
.
test_steps
:
self
.
_updates
(
logs
,
'test'
)
self
.
_test_timer
[
'batch_start_time'
]
=
time
.
time
()
def
on_eval_end
(
self
,
logs
=
None
):
logs
=
logs
or
{}
if
self
.
_is_print
()
and
(
self
.
eval_steps
is
not
None
):
self
.
_updates
(
logs
,
'eval'
)
print
(
'Eval samples: %d'
%
(
self
.
evaled_samples
))
def
on_
tes
t_end
(
self
,
logs
=
None
):
def
on_
predic
t_end
(
self
,
logs
=
None
):
logs
=
logs
or
{}
if
self
.
_is_print
():
if
self
.
test_step
%
self
.
log_freq
!=
0
or
self
.
verbose
==
1
:
...
...
python/paddle/hapi/model.py
浏览文件 @
9f53f3d0
...
...
@@ -1692,11 +1692,11 @@ class Model(object):
test_steps
=
self
.
_len_data_loader
(
test_loader
)
logs
=
{
'steps'
:
test_steps
}
cbks
.
on_begin
(
'
tes
t'
,
logs
)
cbks
.
on_begin
(
'
predic
t'
,
logs
)
outputs
=
[]
logs
,
outputs
=
self
.
_run_one_epoch
(
test_loader
,
cbks
,
'
tes
t'
)
logs
,
outputs
=
self
.
_run_one_epoch
(
test_loader
,
cbks
,
'
predic
t'
)
outputs
=
list
(
zip
(
*
outputs
))
...
...
@@ -1707,7 +1707,7 @@ class Model(object):
self
.
_test_dataloader
=
None
cbks
.
on_end
(
'
tes
t'
,
logs
)
cbks
.
on_end
(
'
predic
t'
,
logs
)
return
outputs
def
_save_inference_model
(
self
,
path
):
...
...
@@ -1793,7 +1793,7 @@ class Model(object):
callbacks
.
on_batch_begin
(
mode
,
step
,
logs
)
if
mode
!=
'
tes
t'
:
if
mode
!=
'
predic
t'
:
outs
=
getattr
(
self
,
mode
+
'_batch'
)(
data
[:
len
(
self
.
_inputs
)],
data
[
len
(
self
.
_inputs
):])
if
self
.
_metrics
and
self
.
_loss
:
...
...
@@ -1829,7 +1829,7 @@ class Model(object):
callbacks
.
on_batch_end
(
mode
,
step
,
logs
)
self
.
_reset_metrics
()
if
mode
==
'
tes
t'
:
if
mode
==
'
predic
t'
:
return
logs
,
outputs
return
logs
...
...
python/paddle/hapi/progressbar.py
浏览文件 @
9f53f3d0
...
...
@@ -159,7 +159,7 @@ class ProgressBar(object):
sys
.
stdout
.
write
(
info
)
sys
.
stdout
.
flush
()
self
.
_last_update
=
now
elif
self
.
_verbose
==
2
:
elif
self
.
_verbose
==
2
or
self
.
_verbose
==
3
:
if
self
.
_num
:
numdigits
=
int
(
np
.
log10
(
self
.
_num
))
+
1
count
=
(
'step %'
+
str
(
numdigits
)
+
'd/%d'
)
%
(
current_num
,
...
...
python/paddle/tests/test_callbacks.py
浏览文件 @
9f53f3d0
...
...
@@ -106,13 +106,13 @@ class TestCallbacks(unittest.TestCase):
test_logs
=
{}
params
=
{
'steps'
:
eval_steps
}
cbks
.
on_begin
(
'
tes
t'
,
params
)
cbks
.
on_begin
(
'
predic
t'
,
params
)
for
step
in
range
(
eval_steps
):
cbks
.
on_batch_begin
(
'
tes
t'
,
step
,
test_logs
)
cbks
.
on_batch_begin
(
'
predic
t'
,
step
,
test_logs
)
test_logs
[
'batch_size'
]
=
2
time
.
sleep
(
0.005
)
cbks
.
on_batch_end
(
'
tes
t'
,
step
,
test_logs
)
cbks
.
on_end
(
'
tes
t'
,
test_logs
)
cbks
.
on_batch_end
(
'
predic
t'
,
step
,
test_logs
)
cbks
.
on_end
(
'
predic
t'
,
test_logs
)
cbks
.
on_end
(
'train'
)
...
...
@@ -128,6 +128,10 @@ class TestCallbacks(unittest.TestCase):
self
.
verbose
=
2
self
.
run_callback
()
def
test_callback_verbose_3
(
self
):
self
.
verbose
=
3
self
.
run_callback
()
def
test_visualdl_callback
(
self
):
# visualdl not support python2
if
sys
.
version_info
<
(
3
,
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录