solarization_test.py 2.65 KB
Newer Older
zhanggzh's avatar
zhanggzh 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
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
# Copyright 2022 The KerasCV Authors
#
# 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
#
#     https://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.
import tensorflow as tf
from absl.testing import parameterized

from keras_cv.layers.preprocessing.solarization import Solarization


class SolarizationTest(tf.test.TestCase, parameterized.TestCase):
    @parameterized.named_parameters(
        ("0_255", 0, 255),
        ("64_191", 64, 191),
        ("127_128", 127, 128),
        ("191_64", 191, 64),
        ("255_0", 255, 0),
    )
    def test_output_values(self, input_value, expected_value):
        solarization = Solarization(value_range=(0, 255))

        self._test_input_output(
            layer=solarization,
            input_value=input_value,
            expected_value=expected_value,
            dtype=tf.uint8,
        )

    @parameterized.named_parameters(
        ("0_245", 0, 245),
        ("255_0", 255, 0),
    )
    def test_solarization_with_addition(self, input_value, output_value):
        solarization = Solarization(addition_factor=(10.0, 10.0), value_range=(0, 255))
        self._test_input_output(
            layer=solarization,
            input_value=input_value,
            expected_value=output_value,
            dtype=tf.float32,
        )

    @parameterized.named_parameters(
        ("0_0", 0, 0),
        ("64_64", 64, 64),
        ("127_127", 127, 127),
        ("191_64", 191, 64),
        ("255_0", 255, 0),
    )
    def test_only_values_above_threshold_are_solarized(self, input_value, output_value):
        solarization = Solarization(threshold_factor=(128, 128), value_range=(0, 255))

        self._test_input_output(
            layer=solarization,
            input_value=input_value,
            expected_value=output_value,
            dtype=tf.uint8,
        )

    def _test_input_output(self, layer, input_value, expected_value, dtype):
        input = tf.ones(shape=(2, 224, 224, 3), dtype=dtype) * input_value
        expected_output = tf.clip_by_value(
            (
                tf.ones(shape=(2, 224, 224, 3), dtype=layer.compute_dtype)
                * expected_value
            ),
            0,
            255,
        )

        output = layer(input)

        self.assertAllClose(output, expected_output)