File size: 5,390 Bytes
5672777
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright 2023 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 iou metric."""

import tensorflow as tf, tf_keras

from official.vision.evaluation import iou


class IoUTest(tf.test.TestCase):

  def test_config(self):
    m_obj = iou.PerClassIoU(num_classes=2, name='per_class_iou')
    self.assertEqual(m_obj.name, 'per_class_iou')
    self.assertEqual(m_obj.num_classes, 2)

    m_obj2 = iou.PerClassIoU.from_config(m_obj.get_config())
    self.assertEqual(m_obj2.name, 'per_class_iou')
    self.assertEqual(m_obj2.num_classes, 2)

  def test_unweighted(self):
    y_pred = [0, 1, 0, 1]
    y_true = [0, 0, 1, 1]

    m_obj = iou.PerClassIoU(num_classes=2)

    result = m_obj(y_true, y_pred)

    # cm = [[1, 1],
    #       [1, 1]]
    # sum_row = [2, 2], sum_col = [2, 2], true_positives = [1, 1]
    # iou = true_positives / (sum_row + sum_col - true_positives))
    expected_result = [1 / (2 + 2 - 1), 1 / (2 + 2 - 1)]
    self.assertAllClose(expected_result, result, atol=1e-3)

  def test_weighted(self):
    y_pred = tf.constant([0, 1, 0, 1], dtype=tf.float32)
    y_true = tf.constant([0, 0, 1, 1])
    sample_weight = tf.constant([0.2, 0.3, 0.4, 0.1])

    m_obj = iou.PerClassIoU(num_classes=2)

    result = m_obj(y_true, y_pred, sample_weight=sample_weight)

    # cm = [[0.2, 0.3],
    #       [0.4, 0.1]]
    # sum_row = [0.6, 0.4], sum_col = [0.5, 0.5], true_positives = [0.2, 0.1]
    # iou = true_positives / (sum_row + sum_col - true_positives))
    expected_result = [0.2 / (0.6 + 0.5 - 0.2), 0.1 / (0.4 + 0.5 - 0.1)]
    self.assertAllClose(expected_result, result, atol=1e-3)

  def test_multi_dim_input(self):
    y_pred = tf.constant([[0, 1], [0, 1]], dtype=tf.float32)
    y_true = tf.constant([[0, 0], [1, 1]])
    sample_weight = tf.constant([[0.2, 0.3], [0.4, 0.1]])

    m_obj = iou.PerClassIoU(num_classes=2)

    result = m_obj(y_true, y_pred, sample_weight=sample_weight)

    # cm = [[0.2, 0.3],
    #       [0.4, 0.1]]
    # sum_row = [0.6, 0.4], sum_col = [0.5, 0.5], true_positives = [0.2, 0.1]
    # iou = true_positives / (sum_row + sum_col - true_positives))
    expected_result = [0.2 / (0.6 + 0.5 - 0.2), 0.1 / (0.4 + 0.5 - 0.1)]
    self.assertAllClose(expected_result, result, atol=1e-3)

  def test_zero_valid_entries(self):
    m_obj = iou.PerClassIoU(num_classes=2)
    self.assertAllClose(m_obj.result(), [0, 0], atol=1e-3)

  def test_zero_and_non_zero_entries(self):
    y_pred = tf.constant([1], dtype=tf.float32)
    y_true = tf.constant([1])

    m_obj = iou.PerClassIoU(num_classes=2)
    result = m_obj(y_true, y_pred)

    # cm = [[0, 0],
    #       [0, 1]]
    # sum_row = [0, 1], sum_col = [0, 1], true_positives = [0, 1]
    # iou = true_positives / (sum_row + sum_col - true_positives))
    expected_result = [0, 1 / (1 + 1 - 1)]
    self.assertAllClose(expected_result, result, atol=1e-3)

  def test_update_state_and_result(self):
    y_pred = [0, 1, 0, 1]
    y_true = [0, 0, 1, 1]

    m_obj = iou.PerClassIoU(num_classes=2)

    m_obj.update_state(y_true, y_pred)
    result = m_obj.result()

    # cm = [[1, 1],
    #       [1, 1]]
    # sum_row = [2, 2], sum_col = [2, 2], true_positives = [1, 1]
    # iou = true_positives / (sum_row + sum_col - true_positives))
    expected_result = [1 / (2 + 2 - 1), 1 / (2 + 2 - 1)]
    self.assertAllClose(expected_result, result, atol=1e-3)

  def test_per_class_iou_v2(self):
    metrics = iou.PerClassIoUV2(num_classes=3)
    y_true = tf.constant([[
        [
            [0, 0, 1],
            [0, 1, 1],
        ],
        [
            [0, 1, 0],
            [0, 0, 1],
        ],
    ]])
    y_pred = tf.constant([[
        [
            [1, 0, 0],
            [1, 1, 1],
        ],
        [
            [1, 1, 1],
            [1, 0, 1],
        ],
    ]])
    metrics.update_state(y_true, y_pred)
    self.assertAllClose([0.0, 1.0, 0.5], metrics.result(), atol=1e-3)

  def test_per_class_iou_v2_sparse_input(self):
    metrics = iou.PerClassIoUV2(
        num_classes=3, sparse_y_true=True, sparse_y_pred=True)
    y_true = [[
        [1, 2, 1],
        [2, 2, 1],
    ]]
    y_pred = [[
        [2, 0, 1],
        [2, 0, 1],
    ]]
    metrics.update_state(y_true, y_pred)
    self.assertAllClose([0., 2. / 3., 1. / 4.], metrics.result(), atol=1e-3)

  def test_per_class_iou_v2_keep_tailing_dims(self):
    num_classes = 3
    num_channels = 2
    metrics = iou.PerClassIoUV2(
        num_classes=num_classes,
        shape=(num_classes, num_channels),
        sparse_y_true=True,
        sparse_y_pred=True,
        axis=0)
    y_pred = tf.constant([2, 1])
    y_true = tf.constant([2, 0])
    metrics.update_state(y_true, y_pred)
    self.assertAllClose([[0., 0.], [0., 0.], [1., 0.]],
                        metrics.result(),
                        atol=1e-3)


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