dataclass_compat.py
4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# Copyright 2020 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Utilities to migrate legacy summaries/events to generic data form.
For legacy summaries, this populates the `SummaryMetadata.data_class`
field and makes any necessary transformations to the tensor value. For
`graph_def` events, this creates a new summary event.
This should be effected after the `data_compat` transformation.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from tensorboard.compat.proto import event_pb2
from tensorboard.compat.proto import summary_pb2
from tensorboard.compat.proto import types_pb2
from tensorboard.plugins.graph import metadata as graphs_metadata
from tensorboard.plugins.histogram import metadata as histograms_metadata
from tensorboard.plugins.hparams import metadata as hparams_metadata
from tensorboard.plugins.image import metadata as images_metadata
from tensorboard.plugins.scalar import metadata as scalars_metadata
from tensorboard.plugins.text import metadata as text_metadata
from tensorboard.util import tensor_util
def migrate_event(event):
"""Migrate an event to a sequence of events.
Args:
event: An `event_pb2.Event`. The caller transfers ownership of the
event to this method; the event may be mutated, and may or may
not appear in the returned sequence.
Returns:
A sequence of `event_pb2.Event`s to use instead of `event`.
"""
if event.HasField("graph_def"):
return _migrate_graph_event(event)
if event.HasField("summary"):
return _migrate_summary_event(event)
return (event,)
def _migrate_graph_event(old_event):
result = event_pb2.Event()
result.wall_time = old_event.wall_time
result.step = old_event.step
value = result.summary.value.add(tag=graphs_metadata.RUN_GRAPH_NAME)
graph_bytes = old_event.graph_def
value.tensor.CopyFrom(tensor_util.make_tensor_proto([graph_bytes]))
value.metadata.plugin_data.plugin_name = graphs_metadata.PLUGIN_NAME
# `value.metadata.plugin_data.content` left as the empty proto
value.metadata.data_class = summary_pb2.DATA_CLASS_BLOB_SEQUENCE
# In the short term, keep both the old event and the new event to
# maintain compatibility.
return (old_event, result)
def _migrate_summary_event(event):
values = event.summary.value
new_values = [new for old in values for new in _migrate_value(old)]
# Optimization: Don't create a new event if there were no shallow
# changes (there may still have been in-place changes).
if len(values) == len(new_values) and all(
x is y for (x, y) in zip(values, new_values)
):
return (event,)
del event.summary.value[:]
event.summary.value.extend(new_values)
return (event,)
def _migrate_value(value):
"""Convert an old value to a stream of new values. May mutate."""
if value.metadata.data_class != summary_pb2.DATA_CLASS_UNKNOWN:
return (value,)
plugin_name = value.metadata.plugin_data.plugin_name
if plugin_name == histograms_metadata.PLUGIN_NAME:
return _migrate_histogram_value(value)
if plugin_name == images_metadata.PLUGIN_NAME:
return _migrate_image_value(value)
if plugin_name == scalars_metadata.PLUGIN_NAME:
return _migrate_scalar_value(value)
if plugin_name == text_metadata.PLUGIN_NAME:
return _migrate_text_value(value)
if plugin_name == hparams_metadata.PLUGIN_NAME:
return _migrate_hparams_value(value)
return (value,)
def _migrate_scalar_value(value):
value.metadata.data_class = summary_pb2.DATA_CLASS_SCALAR
return (value,)
def _migrate_histogram_value(value):
value.metadata.data_class = summary_pb2.DATA_CLASS_TENSOR
return (value,)
def _migrate_image_value(value):
value.metadata.data_class = summary_pb2.DATA_CLASS_BLOB_SEQUENCE
return (value,)
def _migrate_text_value(value):
value.metadata.data_class = summary_pb2.DATA_CLASS_TENSOR
return (value,)
def _migrate_hparams_value(value):
value.metadata.data_class = summary_pb2.DATA_CLASS_TENSOR
if not value.HasField("tensor"):
value.tensor.CopyFrom(hparams_metadata.NULL_TENSOR)
return (value,)