segmentation_metrics_test.py 2.92 KB
Newer Older
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
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
# Copyright 2022 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 segmentation_metrics."""

from absl.testing import parameterized
import tensorflow as tf

from official.vision.evaluation import segmentation_metrics


class SegmentationMetricsTest(parameterized.TestCase, tf.test.TestCase):

  def _create_test_data(self):
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
26
27
28
29
30
    y_pred_cls0 = tf.constant([[1, 1, 0], [1, 1, 0], [0, 0, 0]],
                              dtype=tf.uint16)[tf.newaxis, :, :, tf.newaxis]
    y_pred_cls1 = tf.constant([[0, 0, 0], [0, 0, 1], [0, 0, 1]],
                              dtype=tf.uint16)[tf.newaxis, :, :, tf.newaxis]
    y_pred = tf.concat((y_pred_cls0, y_pred_cls1), axis=-1)
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
31
32
33

    y_true = {
        'masks':
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
34
35
36
37
            tf.constant(
                [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0],
                 [0, 0, 0, 1, 1, 1], [0, 0, 0, 1, 1, 1], [0, 0, 0, 1, 1, 1]],
                dtype=tf.uint16)[tf.newaxis, :, :, tf.newaxis],
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
38
        'valid_masks':
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
39
            tf.ones([1, 6, 6, 1], dtype=tf.bool),
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
40
        'image_info':
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
41
42
            tf.constant([[[6, 6], [3, 3], [0.5, 0.5], [0, 0]]],
                        dtype=tf.float32)
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
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
    }
    return y_pred, y_true

  @parameterized.parameters(True, False)
  def test_mean_iou_metric(self, rescale_predictions):
    tf.config.experimental_run_functions_eagerly(True)
    mean_iou_metric = segmentation_metrics.MeanIoU(
        num_classes=2, rescale_predictions=rescale_predictions)
    y_pred, y_true = self._create_test_data()
    # Disable autograph for correct coverage statistics.
    update_fn = tf.autograph.experimental.do_not_convert(
        mean_iou_metric.update_state)
    update_fn(y_true=y_true, y_pred=y_pred)
    miou = mean_iou_metric.result()
    self.assertAlmostEqual(miou.numpy(), 0.762, places=3)

  @parameterized.parameters(True, False)
  def test_per_class_mean_iou_metric(self, rescale_predictions):
    per_class_iou_metric = segmentation_metrics.PerClassIoU(
        num_classes=2, rescale_predictions=rescale_predictions)
    y_pred, y_true = self._create_test_data()
    # Disable autograph for correct coverage statistics.
    update_fn = tf.autograph.experimental.do_not_convert(
        per_class_iou_metric.update_state)
    update_fn(y_true=y_true, y_pred=y_pred)
    per_class_miou = per_class_iou_metric.result()
    self.assertAllClose(per_class_miou.numpy(), [0.857, 0.667], atol=1e-3)


if __name__ == '__main__':
  tf.test.main()