-
Notifications
You must be signed in to change notification settings - Fork 273
/
tfma_unit.py
392 lines (337 loc) · 16 KB
/
tfma_unit.py
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# Copyright 2018 Google LLC
#
# 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
#
# https://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.
"""Unit test library for testing your TFMA models / metrics.
This is publicly accessible as the tfma.test module. Example usage:
class MyModelTFMATest(tfma.test.TestCase):
def testWithoutBeam(self):
path = train_and_export_my_model(...)
examples = [self.makeExample(age=5, label=1.0),
self.makeExample(age=10, label=0.0)]
expected_metrics={
'average_loss': tfma.test.BoundedValue(upper_bound=1.0),
'auc': tfma.test.BoundedValue(lower_bound=0.5),
'example_count': 3.0,
}
self.assertMetricsComputedWithoutBeamAre(
eval_saved_model_path=path,
serialized_examples=examples,
expected_metrics=expected_metrics)
def testWithBeam(self):
path = train_and_export_my_model(...)
examples = [self.makeExample(age=5, label=1.0),
self.makeExample(age=10, label=0.0)]
expected_metrics={
'average_loss': tfma.test.BoundedValue(upper_bound=1.0),
'auc': tfma.test.BoundedValue(lower_bound=0.5),
'example_count': 3.0,
}
self.assertMetricsComputedWithBeamAre(
eval_saved_model_path=path,
serialized_examples=examples,
expected_metrics=expected_metrics)
We recommend that you actually train and export your model with the test, as
opposed to training and exporting the model once and saving it alongside the
test. This is so that the model is always exported using the latest code and is
of the latest format.
Note that if you are retraining a new model for each test, your model may have
different weights each time and have different metric values. As such, we
recommend that you use BoundedValue with loose bounds to avoid flaky tests.
"""
from typing import Any, Dict, List, Optional
import apache_beam as beam
from apache_beam.testing import util as beam_util
from tensorflow_model_analysis import constants
from tensorflow_model_analysis.api import model_eval_lib
from tensorflow_model_analysis.api import types
from tensorflow_model_analysis.eval_saved_model import load
from tensorflow_model_analysis.eval_saved_model import testutil
from tensorflow_model_analysis.evaluators import legacy_metrics_and_plots_evaluator
from tensorflow_model_analysis.extractors import extractor
from tensorflow_model_analysis.proto import config_pb2
from tensorflow_model_analysis.slicer import slicer_lib as slicer
from tfx_bsl.tfxio import raw_tf_record
@beam.ptransform_fn
@beam.typehints.with_input_types(types.Extracts)
@beam.typehints.with_output_types(types.Extracts)
def Extract( # pylint: disable=invalid-name
extracts: beam.pvalue.PCollection, extractors: List[extractor.Extractor]):
for x in extractors:
extracts = (extracts | x.stage_name >> x.ptransform)
return extracts
class BoundedValue:
"""Represents a bounded value for a metric for the TFMA unit test."""
def __init__(self,
lower_bound: float = float('-inf'),
upper_bound: float = float('inf')):
self.lower_bound = lower_bound
self.upper_bound = upper_bound
class TestCase(testutil.TensorflowModelAnalysisTest):
"""Test class with extra methods for unit-testing TFMA models / metrics."""
def makeExample(self, **kwargs):
"""Returns a serialized TF.Example with the fields set accordingly.
Example usage:
makeExample(name="Tom", age=20, weight=132.5, colors=["red", "green"],
label=1.0)
Note that field types will be inferred from the type of the arguments, so
1.0 goes into a float_list, 1 goes into an int64_list, and "one" goes into
a bytes_list. As the example illustrates, both singleton values and lists
of values are accepted.
Args:
**kwargs: key=value pairs specifying the field values for the example.
Returns:
Serialized TF.Example with the fields set accordingly.
"""
return self._makeExample(**kwargs).SerializeToString()
def assertDictElementsWithinBounds(self, got_values_dict: Dict[str, Any],
expected_values_dict: Dict[str, Any]):
"""Checks the elements for two dictionaries.
It asserts all values in `expected_values_dict` are close to values with the
same key in `got_values_dict`.
Args:
got_values_dict: The actual dictionary.
expected_values_dict: The expected dictionary. The values in can be either
`BoundedValue` or any type accepted by
`tf.test.TestCase.assertAllClose()`. When the type is `BoundedValue`, it
expects the corresponding value from `got_values_dict` falls into the
boundaries provided in the `BoundedValue`.
"""
for key, value in expected_values_dict.items():
self.assertIn(key, got_values_dict)
got_value = got_values_dict[key]
if isinstance(value, BoundedValue):
if got_value < value.lower_bound or got_value > value.upper_bound:
self.fail('expecting key %s to have value between %f and %f '
'(both ends inclusive), but value was %f instead' %
(key, value.lower_bound, value.upper_bound, got_value))
else:
self.assertAllClose(got_value, value, msg='key = %s' % key)
def assertMetricsComputedWithoutBeamAre(self, eval_saved_model_path: str,
serialized_examples: List[bytes],
expected_metrics: Dict[str, Any]):
"""Checks metrics in-memory using the low-level APIs without Beam.
Example usage:
self.assertMetricsComputedWithoutBeamAre(
eval_saved_model_path=path,
serialized_examples=[self.makeExample(age=5, label=1.0),
self.makeExample(age=10, label=0.0)],
expected_metrics={'average_loss': 0.1})
Args:
eval_saved_model_path: Path to the directory containing the
EvalSavedModel.
serialized_examples: List of serialized example bytes.
expected_metrics: Dictionary of expected metric values.
"""
self.assertDictElementsWithinBounds(
got_values_dict=self._computeMetricsWithoutBeam(eval_saved_model_path,
serialized_examples),
expected_values_dict=expected_metrics)
def assertMetricsComputedWithoutBeamNoBatchingAre(
self, eval_saved_model_path: str, serialized_examples: List[bytes],
expected_metrics: Dict[str, Any]):
"""Checks metrics in-memory using the low-level APIs without Beam.
This is the non-batched version of assertMetricsComputedWithoutBeamAre.
This can be useful for debugging batching issues with TFMA or with your
model (e.g. your model or metrics only works with a fixed-batch size - TFMA
requires that your model can accept batches of any size).
Args:
eval_saved_model_path: Path to the directory containing the
EvalSavedModel.
serialized_examples: List of serialized example bytes.
expected_metrics: Dictionary of expected metric values.
"""
self.assertDictElementsWithinBounds(
got_values_dict=self._computeMetricsWithoutBeamNoBatching(
eval_saved_model_path, serialized_examples),
expected_values_dict=expected_metrics)
def _computeMetricsWithoutBeam(
self, eval_saved_model_path: str,
serialized_examples: List[bytes]) -> Dict[str, Any]:
"""Computes metrics in-memory using the low-level APIs without Beam.
Args:
eval_saved_model_path: Path to the directory containing the
EvalSavedModel.
serialized_examples: List of serialized example bytes.
Returns:
Metrics computed by TFMA using your model on the given examples.
"""
eval_saved_model = load.EvalSavedModel(eval_saved_model_path)
eval_saved_model.metrics_reset_update_get_list(serialized_examples)
return eval_saved_model.get_metric_values()
def _computeMetricsWithoutBeamNoBatching(
self, eval_saved_model_path: str,
serialized_examples: List[bytes]) -> Dict[str, Any]:
"""Computes metrics in-memory using the low-level APIs without Beam.
This is the non-batched version of computeMetricsWithoutBeam. This can be
useful for debugging batching issues with TFMA or with your model
(e.g. your model or metrics only works with a fixed-batch size - TFMA
requires that your model can accept batches of any size)
Args:
eval_saved_model_path: Path to the directory containing the
EvalSavedModel.
serialized_examples: List of serialized example bytes.
Returns:
Metrics computed by TFMA using your model on the given examples.
"""
eval_saved_model = load.EvalSavedModel(eval_saved_model_path)
for example in serialized_examples:
eval_saved_model.metrics_reset_update_get_list([example])
return eval_saved_model.get_metric_values()
def assertMetricsComputedWithBeamAre(
self,
eval_saved_model_path: str,
serialized_examples: List[bytes],
expected_metrics: Dict[str, Any],
add_metrics_callbacks: Optional[List[
types.AddMetricsCallbackType]] = None):
"""Checks metrics computed using Beam.
Metrics will be computed over all examples, without any slicing. If you
want to provide your own PCollection (e.g. read a large number of examples
from a file), if you want to check metrics over certain slices, or if you
want to add additional post-export metrics, use the more general
assertGeneralMetricsComputedWithBeamAre.
Example usage:
self.assertMetricsComputedWithBeamAre(
eval_saved_model_path=path,
serialized_examples=[self.makeExample(age=5, label=1.0),
self.makeExample(age=10, label=0.0)],
expected_metrics={'average_loss': 0.1})
Args:
eval_saved_model_path: Path to the directory containing the
EvalSavedModel.
serialized_examples: List of serialized example bytes.
expected_metrics: Dictionary of expected metric values.
add_metrics_callbacks: Optional. Callbacks for adding additional metrics.
"""
def check_metrics(got):
"""Check metrics callback."""
try:
self.assertEqual(
1, len(got), 'expecting metrics for exactly one slice, but got %d '
'slices instead. metrics were: %s' % (len(got), got))
(slice_key, value) = got[0]
self.assertEqual((), slice_key)
self.assertDictElementsWithinBounds(
got_values_dict=value, expected_values_dict=expected_metrics)
except AssertionError as err:
raise beam_util.BeamAssertException(err)
eval_config = config_pb2.EvalConfig()
eval_shared_model = model_eval_lib.default_eval_shared_model(
eval_saved_model_path=eval_saved_model_path,
add_metrics_callbacks=add_metrics_callbacks)
extractors = model_eval_lib.default_extractors(
eval_config=eval_config, eval_shared_model=eval_shared_model)
tfx_io = raw_tf_record.RawBeamRecordTFXIO(
physical_format='inmemory',
raw_record_column_name=constants.ARROW_INPUT_COLUMN,
telemetry_descriptors=['TFMATest'])
with beam.Pipeline() as pipeline:
# pylint: disable=no-value-for-parameter
(metrics, _), _ = (
pipeline
| 'CreateExamples' >> beam.Create(serialized_examples)
| 'BatchExamples' >> tfx_io.BeamSource()
| 'InputsToExtracts' >> model_eval_lib.BatchedInputsToExtracts()
| 'Extract' >> Extract(extractors=extractors)
| 'ComputeMetricsAndPlots' >>
legacy_metrics_and_plots_evaluator._ComputeMetricsAndPlots( # pylint: disable=protected-access
eval_shared_model=eval_shared_model))
# pylint: enable=no-value-for-parameter
beam_util.assert_that(metrics, check_metrics)
def assertGeneralMetricsComputedWithBeamAre(
self, eval_saved_model_path: str,
examples_pcollection: beam.pvalue.PCollection,
slice_spec: List[slicer.SingleSliceSpec],
add_metrics_callbacks: List[types.AddMetricsCallbackType],
expected_slice_metrics: Dict[Any, Dict[str, Any]]):
"""Checks metrics computed using Beam.
A more general version of assertMetricsComputedWithBeamAre. Note that the
caller is responsible for setting up and running the Beam pipeline.
Example usage:
def add_metrics(features, predictions, labels):
metric_ops = {
'mse': tf.metrics.mean_squared_error(labels, predictions['logits']),
'mae': tf.metrics.mean_absolute_error(labels, predictions['logits']),
}
return metric_ops
with beam.Pipeline() as pipeline:
expected_slice_metrics = {
(): {
'mae': 0.1,
'mse': 0.2,
tfma.post_export_metrics.metric_keys.AUC:
tfma.test.BoundedValue(lower_bound=0.5)
},
(('age', 10),): {
'mae': 0.2,
'mse': 0.3,
tfma.post_export_metrics.metric_keys.AUC:
tfma.test.BoundedValue(lower_bound=0.5)
},
}
examples = pipeline | 'ReadExamples' >> beam.io.ReadFromTFRecord(path)
self.assertGeneralMetricsComputedWithBeamAre(
eval_saved_model_path=path,
examples_pcollection=examples,
slice_spec=[tfma.slicer.SingleSliceSpec(),
tfma.slicer.SingleSliceSpec(columns=['age'])],
add_metrics_callbacks=[
add_metrics, tfma.post_export_metrics.auc()],
expected_slice_metrics=expected_slice_metrics)
Args:
eval_saved_model_path: Path to the directory containing the
EvalSavedModel.
examples_pcollection: A PCollection of serialized example bytes.
slice_spec: List of slice specifications.
add_metrics_callbacks: Callbacks for adding additional metrics.
expected_slice_metrics: Dictionary of dictionaries describing the expected
metrics for each slice. The outer dictionary map slice keys to the
expected metrics for that slice.
"""
def check_metrics(got):
"""Check metrics callback."""
try:
slices = {}
for slice_key, value in got:
slices[slice_key] = value
self.assertCountEqual(list(slices), list(expected_slice_metrics))
for slice_key, expected_metrics in expected_slice_metrics.items():
self.assertDictElementsWithinBounds(
got_values_dict=slices[slice_key],
expected_values_dict=expected_metrics)
except AssertionError as err:
raise beam_util.BeamAssertException(err)
slicing_specs = None
if slice_spec:
slicing_specs = [s.to_proto() for s in slice_spec]
eval_config = config_pb2.EvalConfig(slicing_specs=slicing_specs)
eval_shared_model = self.createTestEvalSharedModel(
eval_saved_model_path=eval_saved_model_path,
add_metrics_callbacks=add_metrics_callbacks)
extractors = model_eval_lib.default_extractors(
eval_config=eval_config, eval_shared_model=eval_shared_model)
tfx_io = raw_tf_record.RawBeamRecordTFXIO(
physical_format='inmemory',
raw_record_column_name=constants.ARROW_INPUT_COLUMN,
telemetry_descriptors=['TFMATest'])
# pylint: disable=no-value-for-parameter
(metrics, _), _ = (
examples_pcollection
| 'BatchExamples' >> tfx_io.BeamSource()
| 'InputsToExtracts' >> model_eval_lib.BatchedInputsToExtracts()
| 'Extract' >> Extract(extractors=extractors)
| 'ComputeMetricsAndPlots' >>
legacy_metrics_and_plots_evaluator._ComputeMetricsAndPlots( # pylint: disable=protected-access
eval_shared_model=eval_shared_model))
# pylint: enable=no-value-for-parameter
beam_util.assert_that(metrics, check_metrics)