File size: 3,660 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
# 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 segmentation_losses."""

from absl.testing import parameterized
import tensorflow as tf, tf_keras

from official.vision.losses import segmentation_losses


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

  @parameterized.parameters(
      (True, False, 1.),
      (True, True, 0.5),
      (False, True, 1.),
  )
  def testSegmentationLoss(self, use_groundtruth_dimension,
                           use_binary_cross_entropy, top_k_percent_pixels):
    # [batch, height, width, num_layers]: [2, 3, 4, 1]
    labels = tf.random.uniform([2, 3, 4, 1], minval=0, maxval=6, dtype=tf.int32)
    # [batch, height, width, num_classes]: [2, 3, 4, 6]
    logits = tf.random.uniform([2, 3, 4, 6],
                               minval=-1,
                               maxval=1,
                               dtype=tf.float32)
    loss = segmentation_losses.SegmentationLoss(
        label_smoothing=0.,
        class_weights=[],
        ignore_label=255,
        use_groundtruth_dimension=use_groundtruth_dimension,
        use_binary_cross_entropy=use_binary_cross_entropy,
        top_k_percent_pixels=top_k_percent_pixels)(logits, labels)
    self.assertEqual(tf.rank(loss), 0)

  def testSegmentationLossTopK(self):
    labels = tf.constant([[[[0], [0]], [[0], [2]]]])
    logits = tf.constant([[[[100., 0., 0.], [100., 0, 0.]],
                           [[100., 0., 0.], [0., 1., 0.]]]])
    loss = segmentation_losses.SegmentationLoss(
        label_smoothing=0.,
        class_weights=[],
        ignore_label=255,
        use_groundtruth_dimension=True,
        top_k_percent_pixels=0.5)(logits, labels)
    self.assertAllClose(loss, 0.775718, atol=1e-4)

  def testSegmentationLossTopKWithIgnoreLabel(self):
    labels = tf.constant([[[[0], [0]], [[0], [2]]]])
    logits = tf.constant([[[[100., 0., 0.], [100., 0, 0.]],
                           [[100., 0., 0.], [0., 1., 0.]]]])
    loss = segmentation_losses.SegmentationLoss(
        label_smoothing=0.,
        class_weights=[],
        ignore_label=0,
        use_groundtruth_dimension=True,
        top_k_percent_pixels=0.5)(logits, labels)
    self.assertAllClose(loss, 1.551429, atol=1e-4)

  def testSegmentationLossGroundTruthIsMattingMap(self):
    # [batch, height, width, num_layers]: [2, 3, 4, 1]
    labels = tf.random.uniform([2, 3, 4, 1],
                               minval=0,
                               maxval=1,
                               dtype=tf.float32)
    # [batch, height, width, num_classes]: [2, 3, 4, 2]
    logits = tf.random.uniform([2, 3, 4, 2],
                               minval=-1,
                               maxval=1,
                               dtype=tf.float32)
    loss = segmentation_losses.SegmentationLoss(
        label_smoothing=0.,
        class_weights=[],
        ignore_label=255,
        use_groundtruth_dimension=True,
        use_binary_cross_entropy=False,
        top_k_percent_pixels=1.)(logits, labels)
    self.assertEqual(tf.rank(loss), 0)

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