aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/distribute/python/metrics_v1_test.py
blob: ae4189eb1cb217f8a209b57f91a0ddb82e63dcd9 (plain)
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# Copyright 2018 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.
# ==============================================================================
"""Tests for V1 metrics."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from absl.testing import parameterized

from tensorflow.contrib.distribute.python import combinations
from tensorflow.contrib.distribute.python import tpu_strategy
from tensorflow.python.data.ops import dataset_ops
from tensorflow.python.eager import test
from tensorflow.python.framework import ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import metrics
from tensorflow.python.ops import variables


def _labeled_dataset_fn():
  # First four batches of x: labels, predictions -> (labels == predictions)
  #  0: 0, 0 -> True;   1: 1, 1 -> True;   2: 2, 2 -> True;   3: 3, 0 -> False
  #  4: 4, 1 -> False;  5: 0, 2 -> False;  6: 1, 0 -> False;  7: 2, 1 -> False
  #  8: 3, 2 -> False;  9: 4, 0 -> False; 10: 0, 1 -> False; 11: 1, 2 -> False
  # 12: 2, 0 -> False; 13: 3, 1 -> False; 14: 4, 2 -> False; 15: 0, 0 -> True
  return dataset_ops.Dataset.range(1000).map(
      lambda x: {"labels": x % 5, "predictions": x % 3}).batch(
          4, drop_remainder=True)


def _boolean_dataset_fn():
  # First four batches of labels, predictions: {TP, FP, TN, FN}
  # with a threshold of 0.5:
  #   T, T -> TP;  F, T -> FP;   T, F -> FN
  #   F, F -> TN;  T, T -> TP;   F, T -> FP
  #   T, F -> FN;  F, F -> TN;   T, T -> TP
  #   F, T -> FP;  T, F -> FN;   F, F -> TN
  return dataset_ops.Dataset.from_tensor_slices({
      "labels": [True, False, True, False],
      "predictions": [True, True, False, False]}).repeat().batch(
          3, drop_remainder=True)


def _threshold_dataset_fn():
  # First four batches of labels, predictions: {TP, FP, TN, FN}
  # with a threshold of 0.5:
  #   True, 1.0 -> TP;  False, .75 -> FP;   True, .25 -> FN
  #  False, 0.0 -> TN;   True, 1.0 -> TP;  False, .75 -> FP
  #   True, .25 -> FN;  False, 0.0 -> TN;   True, 1.0 -> TP
  #  False, .75 -> FP;   True, .25 -> FN;  False, 0.0 -> TN
  return dataset_ops.Dataset.from_tensor_slices({
      "labels": [True, False, True, False],
      "predictions": [1.0, 0.75, 0.25, 0.]}).repeat().batch(
          3, drop_remainder=True)


def _regression_dataset_fn():
  return dataset_ops.Dataset.from_tensor_slices({
      "labels": [1., .5, 1., 0.],
      "predictions": [1., .75, .25, 0.]}).repeat()


# TODO(priyag): Add TPU Strategy to this once metrics aggregate correctly using
# TowerLocalVariables on TPUs. Submit http://cl/208914352.
def all_combinations():
  return combinations.combine(
      distribution=[combinations.default_strategy,
                    combinations.one_device_strategy,
                    combinations.mirrored_strategy_with_gpu_and_cpu,
                    combinations.mirrored_strategy_with_two_gpus],
      mode=["graph"])


def tpu_combinations():
  return combinations.combine(distribution=[combinations.tpu_strategy_one_step,
                                            combinations.tpu_strategy],
                              mode=["graph"])


# TODO(josh11b): Test metrics.recall_at_top_k, metrics.average_precision_at_k,
# metrics.precision_at_k
class MetricsV1Test(test.TestCase, parameterized.TestCase):

  def _test_metric(self, distribution, dataset_fn, metric_fn, expected_fn):
    with ops.Graph().as_default(), distribution.scope():
      iterator = distribution.distribute_dataset(
          dataset_fn).make_one_shot_iterator()
      if isinstance(distribution, tpu_strategy.TPUStrategy):
        def step_fn(ctx, inputs):
          value, update = distribution.call_for_each_tower(
              metric_fn, inputs)
          ctx.set_non_tensor_output(name="value", output=value)
          return distribution.group(update)

        ctx = distribution.run_steps_on_dataset(
            step_fn, iterator, iterations=distribution.steps_per_run)
        update = ctx.run_op
        value = ctx.non_tensor_outputs["value"]
        # In each run, we run multiple steps, and each steps consumes as many
        # batches as number of towers.
        batches_per_update = (
            distribution.num_towers * distribution.steps_per_run)
      else:
        value, update = distribution.call_for_each_tower(
            metric_fn, iterator.get_next())
        update = distribution.group(update)
        # TODO(josh11b): Once we switch to using a global batch size for input,
        # replace "distribution.num_towers" with "1".
        batches_per_update = distribution.num_towers

      self.evaluate(distribution.initialize())
      self.evaluate(variables.local_variables_initializer())

      batches_consumed = 0
      for i in range(4):
        self.evaluate(update)
        batches_consumed += batches_per_update
        self.assertAllClose(expected_fn(batches_consumed),
                            self.evaluate(value),
                            0.001,
                            msg="After update #" + str(i+1))
        if batches_consumed >= 4:  # Consume 4 input batches in total.
          break

      self.evaluate(distribution.finalize())

  @combinations.generate(all_combinations() + tpu_combinations())
  def testMean(self, distribution):
    def _dataset_fn():
      return dataset_ops.Dataset.range(1000).map(math_ops.to_float).batch(
          4, drop_remainder=True)

    def _expected_fn(num_batches):
      # Mean(0..3) = 1.5, Mean(0..7) = 3.5, Mean(0..11) = 5.5, etc.
      return num_batches * 2 - 0.5

    self._test_metric(distribution, _dataset_fn, metrics.mean, _expected_fn)

  @combinations.generate(all_combinations() + tpu_combinations())
  def testAccuracy(self, distribution):
    def _metric_fn(x):
      labels = x["labels"]
      predictions = x["predictions"]
      return metrics.accuracy(labels, predictions)

    def _expected_fn(num_batches):
      return [3./4, 3./8, 3./12, 4./16][num_batches - 1]

    self._test_metric(
        distribution, _labeled_dataset_fn, _metric_fn, _expected_fn)

  # TODO(priyag, jhseu): Enable TPU for this test once scatter_add is added
  # for TPUMirroredVariable.
  @combinations.generate(all_combinations())
  def testMeanPerClassAccuracy(self, distribution):
    def _metric_fn(x):
      labels = x["labels"]
      predictions = x["predictions"]
      return metrics.mean_per_class_accuracy(
          labels, predictions, num_classes=5)

    def _expected_fn(num_batches):
      mean = lambda x: sum(x) / len(x)
      return [mean([1., 1., 1., 0., 0.]),
              mean([0.5, 0.5, 0.5, 0., 0.]),
              mean([1./3, 1./3, 0.5, 0., 0.]),
              mean([0.5, 1./3, 1./3, 0., 0.])][num_batches - 1]

    self._test_metric(
        distribution, _labeled_dataset_fn, _metric_fn, _expected_fn)

  # NOTE(priyag): This metric doesn't work on TPUs yet.
  @combinations.generate(all_combinations())
  def testMeanIOU(self, distribution):
    def _metric_fn(x):
      labels = x["labels"]
      predictions = x["predictions"]
      return metrics.mean_iou(
          labels, predictions, num_classes=5)

    def _expected_fn(num_batches):
      mean = lambda x: sum(x) / len(x)
      return [mean([1./2, 1./1, 1./1, 0.]),  # no class 4 in first batch
              mean([1./4, 1./4, 1./3, 0., 0.]),
              mean([1./6, 1./6, 1./5, 0., 0.]),
              mean([2./8, 1./7, 1./7, 0., 0.])][num_batches - 1]

    self._test_metric(
        distribution, _labeled_dataset_fn, _metric_fn, _expected_fn)

  @combinations.generate(all_combinations() + tpu_combinations())
  def testMeanTensor(self, distribution):
    def _dataset_fn():
      dataset = dataset_ops.Dataset.range(1000).map(math_ops.to_float)
      # Want to produce a fixed, known shape, so drop remainder when batching.
      dataset = dataset.batch(4, drop_remainder=True)
      return dataset

    def _expected_fn(num_batches):
      # Mean(0, 4, ..., 4 * num_batches - 4) == 2 * num_batches - 2
      # Mean(1, 5, ..., 4 * num_batches - 3) == 2 * num_batches - 1
      # Mean(2, 6, ..., 4 * num_batches - 2) == 2 * num_batches
      # Mean(3, 7, ..., 4 * num_batches - 1) == 2 * num_batches + 1
      first = 2. * num_batches - 2.
      return [first, first + 1., first + 2., first + 3.]

    self._test_metric(
        distribution, _dataset_fn, metrics.mean_tensor, _expected_fn)

  @combinations.generate(all_combinations() + tpu_combinations())
  def testAUCROC(self, distribution):
    def _metric_fn(x):
      labels = x["labels"]
      predictions = x["predictions"]
      return metrics.auc(labels, predictions, num_thresholds=8, curve="ROC",
                         summation_method="careful_interpolation")

    def _expected_fn(num_batches):
      return [0.5, 7./9, 0.8, 0.75][num_batches - 1]

    self._test_metric(
        distribution, _threshold_dataset_fn, _metric_fn, _expected_fn)

  @combinations.generate(all_combinations() + tpu_combinations())
  def testAUCPR(self, distribution):
    def _metric_fn(x):
      labels = x["labels"]
      predictions = x["predictions"]
      return metrics.auc(labels, predictions, num_thresholds=8, curve="PR",
                         summation_method="careful_interpolation")

    def _expected_fn(num_batches):
      return [0.797267, 0.851238, 0.865411, 0.797267][num_batches - 1]

    self._test_metric(
        distribution, _threshold_dataset_fn, _metric_fn, _expected_fn)

  @combinations.generate(all_combinations() + tpu_combinations())
  def testFalseNegatives(self, distribution):
    def _metric_fn(x):
      labels = x["labels"]
      predictions = x["predictions"]
      return metrics.false_negatives(labels, predictions)

    def _expected_fn(num_batches):
      return [1., 1., 2., 3.][num_batches - 1]

    self._test_metric(
        distribution, _boolean_dataset_fn, _metric_fn, _expected_fn)

  @combinations.generate(all_combinations() + tpu_combinations())
  def testFalseNegativesAtThresholds(self, distribution):
    def _metric_fn(x):
      labels = x["labels"]
      predictions = x["predictions"]
      return metrics.false_negatives_at_thresholds(labels, predictions, [.5])

    def _expected_fn(num_batches):
      return [[1.], [1.], [2.], [3.]][num_batches - 1]

    self._test_metric(
        distribution, _threshold_dataset_fn, _metric_fn, _expected_fn)

  @combinations.generate(all_combinations() + tpu_combinations())
  def testTrueNegatives(self, distribution):
    def _metric_fn(x):
      labels = x["labels"]
      predictions = x["predictions"]
      return metrics.true_negatives(labels, predictions)

    def _expected_fn(num_batches):
      return [0., 1., 2., 3.][num_batches - 1]

    self._test_metric(
        distribution, _boolean_dataset_fn, _metric_fn, _expected_fn)

  @combinations.generate(all_combinations() + tpu_combinations())
  def testTrueNegativesAtThresholds(self, distribution):
    def _metric_fn(x):
      labels = x["labels"]
      predictions = x["predictions"]
      return metrics.true_negatives_at_thresholds(labels, predictions, [.5])

    def _expected_fn(num_batches):
      return [[0.], [1.], [2.], [3.]][num_batches - 1]

    self._test_metric(
        distribution, _threshold_dataset_fn, _metric_fn, _expected_fn)

  @combinations.generate(all_combinations() + tpu_combinations())
  def testFalsePositives(self, distribution):
    def _metric_fn(x):
      labels = x["labels"]
      predictions = x["predictions"]
      return metrics.false_positives(labels, predictions)

    def _expected_fn(num_batches):
      return [1., 2., 2., 3.][num_batches - 1]

    self._test_metric(
        distribution, _boolean_dataset_fn, _metric_fn, _expected_fn)

  @combinations.generate(all_combinations() + tpu_combinations())
  def testFalsePositivesAtThresholds(self, distribution):
    def _metric_fn(x):
      labels = x["labels"]
      predictions = x["predictions"]
      return metrics.false_positives_at_thresholds(labels, predictions, [.5])

    def _expected_fn(num_batches):
      return [[1.], [2.], [2.], [3.]][num_batches - 1]

    self._test_metric(
        distribution, _threshold_dataset_fn, _metric_fn, _expected_fn)

  @combinations.generate(all_combinations() + tpu_combinations())
  def testTruePositives(self, distribution):
    def _metric_fn(x):
      labels = x["labels"]
      predictions = x["predictions"]
      return metrics.true_positives(labels, predictions)

    def _expected_fn(num_batches):
      return [1., 2., 3., 3.][num_batches - 1]

    self._test_metric(
        distribution, _boolean_dataset_fn, _metric_fn, _expected_fn)

  @combinations.generate(all_combinations() + tpu_combinations())
  def testTruePositivesAtThresholds(self, distribution):
    def _metric_fn(x):
      labels = x["labels"]
      predictions = x["predictions"]
      return metrics.true_positives_at_thresholds(labels, predictions, [.5])

    def _expected_fn(num_batches):
      return [[1.], [2.], [3.], [3.]][num_batches - 1]

    self._test_metric(
        distribution, _threshold_dataset_fn, _metric_fn, _expected_fn)

  @combinations.generate(all_combinations() + tpu_combinations())
  def testPrecision(self, distribution):
    def _metric_fn(x):
      labels = x["labels"]
      predictions = x["predictions"]
      return metrics.precision(labels, predictions)

    def _expected_fn(num_batches):
      return [0.5, 0.5, 0.6, 0.5][num_batches - 1]

    self._test_metric(
        distribution, _boolean_dataset_fn, _metric_fn, _expected_fn)

  @combinations.generate(all_combinations() + tpu_combinations())
  def testPrecisionAtThreshold(self, distribution):
    def _metric_fn(x):
      labels = x["labels"]
      predictions = x["predictions"]
      return metrics.precision_at_thresholds(labels, predictions, [0.5])

    def _expected_fn(num_batches):
      return [[0.5], [0.5], [0.6], [0.5]][num_batches - 1]

    self._test_metric(
        distribution, _threshold_dataset_fn, _metric_fn, _expected_fn)

  @combinations.generate(all_combinations() + tpu_combinations())
  def testRecall(self, distribution):
    def _metric_fn(x):
      labels = x["labels"]
      predictions = x["predictions"]
      return metrics.recall(labels, predictions)

    def _expected_fn(num_batches):
      return [0.5, 2./3, 0.6, 0.5][num_batches - 1]

    self._test_metric(
        distribution, _boolean_dataset_fn, _metric_fn, _expected_fn)

  @combinations.generate(all_combinations() + tpu_combinations())
  def testRecallAtThreshold(self, distribution):
    def _metric_fn(x):
      labels = x["labels"]
      predictions = x["predictions"]
      return metrics.recall_at_thresholds(labels, predictions, [0.5])

    def _expected_fn(num_batches):
      return [[0.5], [2./3], [0.6], [0.5]][num_batches - 1]

    self._test_metric(
        distribution, _threshold_dataset_fn, _metric_fn, _expected_fn)

  @combinations.generate(all_combinations() + tpu_combinations())
  def testMeanSquaredError(self, distribution):
    def _metric_fn(x):
      labels = x["labels"]
      predictions = x["predictions"]
      return metrics.mean_squared_error(labels, predictions)

    def _expected_fn(num_batches):
      return [0., 1./32, 0.208333, 0.15625][num_batches - 1]

    self._test_metric(
        distribution, _regression_dataset_fn, _metric_fn, _expected_fn)

  @combinations.generate(all_combinations() + tpu_combinations())
  def testRootMeanSquaredError(self, distribution):
    def _metric_fn(x):
      labels = x["labels"]
      predictions = x["predictions"]
      return metrics.root_mean_squared_error(labels, predictions)

    def _expected_fn(num_batches):
      return [0., 0.176777, 0.456435, 0.395285][num_batches - 1]

    self._test_metric(
        distribution, _regression_dataset_fn, _metric_fn, _expected_fn)

  @combinations.generate(all_combinations())
  def testSensitivityAtSpecificity(self, distribution):
    def _metric_fn(x):
      labels = x["labels"]
      predictions = x["predictions"]
      return metrics.sensitivity_at_specificity(labels, predictions, 0.8)

    def _expected_fn(num_batches):
      return [0.5, 2./3, 0.6, 0.5][num_batches - 1]

    self._test_metric(
        distribution, _threshold_dataset_fn, _metric_fn, _expected_fn)

  @combinations.generate(all_combinations())
  def testSpecificityAtSensitivity(self, distribution):
    def _metric_fn(x):
      labels = x["labels"]
      predictions = x["predictions"]
      return metrics.specificity_at_sensitivity(labels, predictions, 0.95)

    def _expected_fn(num_batches):
      return [0., 1./3, 0.5, 0.5][num_batches - 1]

    self._test_metric(
        distribution, _threshold_dataset_fn, _metric_fn, _expected_fn)


if __name__ == "__main__":
  test.main()