|
| 1 | +## Copyright 2019 The TensorFlow Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================== |
| 15 | +"""Tests for focal loss.""" |
| 16 | + |
| 17 | +from __future__ import absolute_import |
| 18 | +from __future__ import division |
| 19 | +from __future__ import print_function |
| 20 | + |
| 21 | +import numpy as np |
| 22 | +import tensorflow as tf |
| 23 | +import tensorflow.keras.backend as K |
| 24 | +from tensorflow_addons.utils import test_utils |
| 25 | +from tensorflow_addons.losses import sigmoid_focal_crossentropy, SigmoidFocalCrossEntropy |
| 26 | + |
| 27 | + |
| 28 | +@test_utils.run_all_in_graph_and_eager_modes |
| 29 | +class SigmoidFocalCrossEntropyTest(tf.test.TestCase): |
| 30 | + def test_config(self): |
| 31 | + bce_obj = SigmoidFocalCrossEntropy( |
| 32 | + reduction=tf.keras.losses.Reduction.NONE, |
| 33 | + name='sigmoid_focal_crossentropy') |
| 34 | + self.assertEqual(bce_obj.name, 'sigmoid_focal_crossentropy') |
| 35 | + self.assertEqual(bce_obj.reduction, tf.keras.losses.Reduction.NONE) |
| 36 | + |
| 37 | + def to_logit(self, prob): |
| 38 | + logit = np.log(prob / (1. - prob)) |
| 39 | + return logit |
| 40 | + |
| 41 | + def log10(self, x): |
| 42 | + numerator = tf.math.log(x) |
| 43 | + denominator = tf.math.log(tf.constant(10, dtype=numerator.dtype)) |
| 44 | + return numerator / denominator |
| 45 | + |
| 46 | + # Test with logits |
| 47 | + def test_with_logits(self): |
| 48 | + # predictiions represented as logits |
| 49 | + prediction_tensor = tf.constant( |
| 50 | + [[self.to_logit(0.97)], [self.to_logit(0.91)], |
| 51 | + [self.to_logit(0.73)], [self.to_logit(0.27)], |
| 52 | + [self.to_logit(0.09)], [self.to_logit(0.03)]], tf.float32) |
| 53 | + # Ground truth |
| 54 | + target_tensor = tf.constant([[1], [1], [1], [0], [0], [0]], tf.float32) |
| 55 | + |
| 56 | + fl = sigmoid_focal_crossentropy( |
| 57 | + y_true=target_tensor, |
| 58 | + y_pred=prediction_tensor, |
| 59 | + from_logits=True, |
| 60 | + alpha=None, |
| 61 | + gamma=None) |
| 62 | + bce = K.binary_crossentropy( |
| 63 | + target_tensor, prediction_tensor, from_logits=True) |
| 64 | + |
| 65 | + # When alpha and gamma are None, it should be equal to BCE |
| 66 | + self.assertAllClose(fl, bce) |
| 67 | + |
| 68 | + # When gamma==2.0 |
| 69 | + fl = sigmoid_focal_crossentropy( |
| 70 | + y_true=target_tensor, |
| 71 | + y_pred=prediction_tensor, |
| 72 | + from_logits=True, |
| 73 | + alpha=None, |
| 74 | + gamma=2.0) |
| 75 | + |
| 76 | + # order_of_ratio = np.power(10, np.floor(np.log10(bce/FL))) |
| 77 | + order_of_ratio = tf.pow(10.0, tf.math.floor(self.log10(bce / fl))) |
| 78 | + pow_values = tf.constant([[1000], [100], [10], [10], [100], [1000]]) |
| 79 | + self.assertAllClose(order_of_ratio, pow_values) |
| 80 | + |
| 81 | + # Test without logits |
| 82 | + def test_without_logits(self): |
| 83 | + # predictiions represented as logits |
| 84 | + prediction_tensor = tf.constant( |
| 85 | + [[0.97], [0.91], [0.73], [0.27], [0.09], [0.03]], tf.float32) |
| 86 | + # Ground truth |
| 87 | + target_tensor = tf.constant([[1], [1], [1], [0], [0], [0]], tf.float32) |
| 88 | + |
| 89 | + fl = sigmoid_focal_crossentropy( |
| 90 | + y_true=target_tensor, |
| 91 | + y_pred=prediction_tensor, |
| 92 | + alpha=None, |
| 93 | + gamma=None) |
| 94 | + bce = K.binary_crossentropy(target_tensor, prediction_tensor) |
| 95 | + |
| 96 | + # When alpha and gamma are None, it should be equal to BCE |
| 97 | + self.assertAllClose(fl, bce) |
| 98 | + |
| 99 | + # When gamma==2.0 |
| 100 | + fl = sigmoid_focal_crossentropy( |
| 101 | + y_true=target_tensor, |
| 102 | + y_pred=prediction_tensor, |
| 103 | + alpha=None, |
| 104 | + gamma=2.0) |
| 105 | + |
| 106 | + order_of_ratio = tf.pow(10.0, tf.math.floor(self.log10(bce / fl))) |
| 107 | + pow_values = tf.constant([[1000], [100], [10], [10], [100], [1000]]) |
| 108 | + self.assertAllClose(order_of_ratio, pow_values) |
| 109 | + |
| 110 | + |
| 111 | +if __name__ == '__main__': |
| 112 | + tf.test.main() |
0 commit comments