Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
曾经的那一瞬间
Models
提交
9556ebcc
M
Models
项目概览
曾经的那一瞬间
/
Models
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
Models
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
9556ebcc
编写于
7月 27, 2020
作者:
R
Ruoxin Sang
提交者:
A. Unique TensorFlower
7月 27, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Internal change
PiperOrigin-RevId: 323518930
上级
a540c3d7
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
145 addition
and
28 deletion
+145
-28
orbit/controller.py
orbit/controller.py
+2
-2
orbit/controller_test.py
orbit/controller_test.py
+76
-0
orbit/runner.py
orbit/runner.py
+4
-2
orbit/standard_runner.py
orbit/standard_runner.py
+4
-2
orbit/utils.py
orbit/utils.py
+59
-22
未找到文件。
orbit/controller.py
浏览文件 @
9556ebcc
...
...
@@ -207,7 +207,7 @@ class Controller:
else
:
logging
.
info
(
"Evaluating at train step: %s"
,
current_step
)
with
self
.
eval_summary_manager
.
summary_writer
.
as_default
():
with
self
.
eval_summary_manager
.
summary_writer
()
.
as_default
():
eval_outputs
=
self
.
evaluator
.
evaluate
(
steps
)
if
eval_outputs
:
...
...
@@ -339,7 +339,7 @@ class Controller:
current_step
+=
num_steps
num_steps
=
tf
.
convert_to_tensor
(
num_steps
,
dtype
=
tf
.
int32
)
with
self
.
summary_manager
.
summary_writer
.
as_default
():
with
self
.
summary_manager
.
summary_writer
()
.
as_default
():
# Create a lambda that returns true when summaries should be written.
should_record
=
False
# Allows static optimization in no-summary cases.
if
self
.
summary_interval
:
...
...
orbit/controller_test.py
浏览文件 @
9556ebcc
...
...
@@ -158,6 +158,57 @@ class TestEvaluator(standard_runner.StandardEvaluator):
}
class
TestEvaluatorWithNestedSummary
(
standard_runner
.
StandardEvaluator
):
"""Implements the training and evaluation APIs for the test model."""
def
__init__
(
self
):
self
.
strategy
=
tf
.
distribute
.
get_strategy
()
self
.
model
=
create_model
()
dataset
=
self
.
strategy
.
experimental_distribute_datasets_from_function
(
dataset_fn
)
dataset2
=
self
.
strategy
.
experimental_distribute_datasets_from_function
(
dataset_fn
)
self
.
loss
=
tf
.
keras
.
metrics
.
Mean
(
"loss"
,
dtype
=
tf
.
float32
)
self
.
accuracy
=
tf
.
keras
.
metrics
.
CategoricalAccuracy
(
"accuracy"
,
dtype
=
tf
.
float32
)
self
.
loss2
=
tf
.
keras
.
metrics
.
Mean
(
"loss"
,
dtype
=
tf
.
float32
)
self
.
accuracy2
=
tf
.
keras
.
metrics
.
CategoricalAccuracy
(
"accuracy"
,
dtype
=
tf
.
float32
)
standard_runner
.
StandardEvaluator
.
__init__
(
self
,
eval_dataset
=
{
"dataset"
:
dataset
,
"dataset2"
:
dataset2
})
def
eval_step
(
self
,
iterator
):
def
_replicated_step
(
loss
,
accuracy
,
inputs
):
"""Replicated evaluation step."""
inputs
,
targets
=
inputs
outputs
=
self
.
model
(
inputs
)
loss
.
update_state
(
tf
.
keras
.
losses
.
MSE
(
targets
,
outputs
))
accuracy
.
update_state
(
targets
,
outputs
)
self
.
strategy
.
run
(
lambda
inputs
:
_replicated_step
(
self
.
loss
,
self
.
accuracy
,
inputs
),
args
=
(
next
(
iterator
[
"dataset"
]),))
self
.
strategy
.
run
(
lambda
inputs
:
_replicated_step
(
self
.
loss2
,
self
.
accuracy2
,
inputs
),
args
=
(
next
(
iterator
[
"dataset2"
]),))
def
eval_end
(
self
):
return
{
"dataset"
:
{
"loss"
:
self
.
loss
.
result
(),
"accuracy"
:
self
.
accuracy
.
result
()
},
"dataset2"
:
{
"loss"
:
self
.
loss2
.
result
(),
"accuracy"
:
self
.
accuracy2
.
result
()
},
}
class
TestTrainerWithSummaries
(
standard_runner
.
StandardTrainer
):
"""A Trainer model with summaries for testing purposes."""
...
...
@@ -570,6 +621,31 @@ class ControllerTest(tf.test.TestCase, parameterized.TestCase):
self
.
assertLen
(
summaries_with_matching_keyword
(
"eval_loss"
,
self
.
model_dir
),
2
)
def
test_evaluate_with_nested_summaries
(
self
):
test_evaluator
=
TestEvaluatorWithNestedSummary
()
test_controller
=
controller
.
Controller
(
evaluator
=
test_evaluator
,
global_step
=
tf
.
Variable
(
0
,
dtype
=
tf
.
int64
),
eval_summary_dir
=
self
.
model_dir
)
test_controller
.
evaluate
(
steps
=
5
)
self
.
assertNotEmpty
(
tf
.
io
.
gfile
.
listdir
(
os
.
path
.
join
(
self
.
model_dir
,
"dataset"
)))
self
.
assertNotEmpty
(
summaries_with_matching_keyword
(
"loss"
,
os
.
path
.
join
(
self
.
model_dir
,
"dataset"
)))
self
.
assertNotEmpty
(
summaries_with_matching_keyword
(
"accuracy"
,
os
.
path
.
join
(
self
.
model_dir
,
"dataset"
)))
self
.
assertNotEmpty
(
tf
.
io
.
gfile
.
listdir
(
os
.
path
.
join
(
self
.
model_dir
,
"dataset2"
)))
self
.
assertNotEmpty
(
summaries_with_matching_keyword
(
"loss"
,
os
.
path
.
join
(
self
.
model_dir
,
"dataset2"
)))
self
.
assertNotEmpty
(
summaries_with_matching_keyword
(
"accuracy"
,
os
.
path
.
join
(
self
.
model_dir
,
"dataset2"
)))
if
__name__
==
"__main__"
:
tf
.
test
.
main
()
orbit/runner.py
浏览文件 @
9556ebcc
...
...
@@ -45,7 +45,8 @@ class AbstractTrainer(tf.Module, metaclass=abc.ABCMeta):
Returns:
The function may return a dictionary of `Tensors` or numpy arrays, which
will be written to logs and as TensorBoard summaries.
will be written to logs and as TensorBoard summaries. It can also be a
nested dictionary, yielding a hierarchy of summary directories.
"""
pass
...
...
@@ -67,6 +68,7 @@ class AbstractEvaluator(tf.Module, metaclass=abc.ABCMeta):
Returns:
The function may return a dictionary of `Tensors` or numpy arrays, which
will be written to logs and as TensorBoard summaries.
will be written to logs and as TensorBoard summaries. It can also be a
nested dictionary, yielding a hierarchy of summary directories.
"""
pass
orbit/standard_runner.py
浏览文件 @
9556ebcc
...
...
@@ -144,7 +144,8 @@ class StandardTrainer(runner.AbstractTrainer, metaclass=abc.ABCMeta):
Returns:
The function may return a dictionary of `Tensors`, which will be
written to logs and as TensorBoard summaries.
written to logs and as TensorBoard summaries. It can also be a
nested dictionary, yielding a hierarchy of summary directories.
"""
pass
...
...
@@ -261,7 +262,8 @@ class StandardEvaluator(runner.AbstractEvaluator, metaclass=abc.ABCMeta):
Returns:
The function may return a dictionary of `Tensors`, which will be
written to logs and as TensorBoard summaries.
written to logs and as TensorBoard summaries. It can also be a
nested dictionary, yielding a hierarchy of summary directories.
"""
pass
...
...
orbit/utils.py
浏览文件 @
9556ebcc
...
...
@@ -20,6 +20,7 @@ import contextlib
import
functools
import
inspect
import
os
import
numpy
as
np
import
tensorflow
as
tf
...
...
@@ -153,44 +154,80 @@ class SummaryManager:
self
.
_enabled
=
(
summary_dir
is
not
None
)
self
.
_summary_dir
=
summary_dir
self
.
_summary_fn
=
summary_fn
self
.
_summary_writer
=
None
self
.
_summary_writer
s
=
{}
if
global_step
is
None
:
self
.
_global_step
=
tf
.
summary
.
experimental
.
get_step
()
else
:
self
.
_global_step
=
global_step
@
property
def
summary_writer
(
self
):
"""Returns the underlying summary writer."""
if
self
.
_summary_writer
is
not
None
:
return
self
.
_summary_writer
def
summary_writer
(
self
,
relative_path
=
""
):
"""Returns the underlying summary writer.
Args:
relative_path: The current path in which to write summaries, relative to
the summary directory. By default it is empty, which specifies the root
directory.
"""
if
self
.
_summary_writers
and
relative_path
in
self
.
_summary_writers
:
return
self
.
_summary_writers
[
relative_path
]
if
self
.
_enabled
:
self
.
_summary_writer
=
tf
.
summary
.
create_file_writer
(
self
.
_summary_dir
)
self
.
_summary_writers
[
relative_path
]
=
tf
.
summary
.
create_file_writer
(
os
.
path
.
join
(
self
.
_summary_dir
,
relative_path
))
else
:
self
.
_summary_writer
=
tf
.
summary
.
create_noop_writer
()
return
self
.
_summary_writer
self
.
_summary_writer
s
[
relative_path
]
=
tf
.
summary
.
create_noop_writer
()
return
self
.
_summary_writer
s
[
relative_path
]
def
flush
(
self
):
"""Flush the underlying summary writer."""
"""Flush the underlying summary writer
s
."""
if
self
.
_enabled
:
tf
.
summary
.
flush
(
self
.
summary_writer
)
def
write_summaries
(
self
,
items
):
"""Write a bulk of summaries.
tf
.
nest
.
map_structure
(
tf
.
summary
.
flush
,
self
.
_summary_writers
)
def
write_summaries
(
self
,
summary_dict
):
"""Write summaries for the given values.
This recursively creates sub-directories for any nested dictionaries
provided in `summary_dict`, yielding a hierarchy of directories which will
then be reflected in the TensorBoard UI as different colored curves.
E.g. users may evaluate on muliple datasets and return `summary_dict` as a
nested
dictionary.
```
{
"dataset": {
"loss": loss,
"accuracy": accuracy
},
"dataset2": {
"loss": loss2,
"accuracy": accuracy2
},
}
```
It will create two sub directories "dataset" and "dataset2" inside summary
root directory. And each directory write both "loss" and "accuracy"
summaries inside.
Args:
items: a dictionary of `Tensors` for writing summaries.
summary_dict: A dictionary of values. If any value in `summary_dict` is
itself a dictionary, then the function will recursively create
subdirectories with names given by the keys in the dictionary. The
Tensor values are summarized using the summary writer instance specific
to the parent relative path.
"""
# TODO(rxsang): Support writing summaries with nested structure, so users
# can split the summaries into different directories for nicer visualization
# in Tensorboard, like train and eval metrics.
if
not
self
.
_enabled
:
return
with
self
.
summary_writer
.
as_default
():
for
name
,
tensor
in
items
.
items
():
self
.
_summary_fn
(
name
,
tensor
,
step
=
self
.
_global_step
)
self
.
_write_summaries
(
summary_dict
)
def
_write_summaries
(
self
,
summary_dict
,
relative_path
=
""
):
for
name
,
value
in
summary_dict
.
items
():
if
isinstance
(
value
,
dict
):
self
.
_write_summaries
(
value
,
relative_path
=
os
.
path
.
join
(
relative_path
,
name
))
else
:
with
self
.
summary_writer
(
relative_path
).
as_default
():
self
.
_summary_fn
(
name
,
value
,
step
=
self
.
_global_step
)
class
Trigger
(
metaclass
=
abc
.
ABCMeta
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录