|
| 1 | +# Copyright 2022 The KerasCV Authors |
| 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 | +# https://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 | +import tensorflow as tf |
| 15 | +from absl.testing import parameterized |
| 16 | + |
| 17 | +from keras_cv.layers.preprocessing.maybe_apply import MaybeApply |
| 18 | + |
| 19 | + |
| 20 | +class ZeroOut(tf.keras.__internal__.layers.BaseImageAugmentationLayer): |
| 21 | + """Zero out all entries, for testing purposes.""" |
| 22 | + |
| 23 | + def __init__(self): |
| 24 | + super(ZeroOut, self).__init__() |
| 25 | + |
| 26 | + def augment_image(self, image, transformation=None): |
| 27 | + return 0 * image |
| 28 | + |
| 29 | + def augment_label(self, label, transformation=None): |
| 30 | + return 0 * label |
| 31 | + |
| 32 | + def augment_bounding_box(self, bounding_box, transformation=None): |
| 33 | + return 0 * bounding_box |
| 34 | + |
| 35 | + |
| 36 | +class MaybeApplyTest(tf.test.TestCase, parameterized.TestCase): |
| 37 | + rng = tf.random.Generator.from_seed(seed=1234) |
| 38 | + |
| 39 | + @parameterized.parameters([-0.5, 1.7]) |
| 40 | + def test_raises_error_on_invalid_rate_parameter(self, invalid_rate): |
| 41 | + with self.assertRaises(ValueError): |
| 42 | + MaybeApply(rate=invalid_rate, layer=ZeroOut()) |
| 43 | + |
| 44 | + def test_works_with_batched_input(self): |
| 45 | + batch_size = 32 |
| 46 | + dummy_inputs = self.rng.uniform(shape=(batch_size, 224, 224, 3)) |
| 47 | + layer = MaybeApply(rate=0.5, layer=ZeroOut(), seed=1234) |
| 48 | + |
| 49 | + outputs = layer(dummy_inputs) |
| 50 | + num_zero_inputs = self._num_zero_batches(dummy_inputs) |
| 51 | + num_zero_outputs = self._num_zero_batches(outputs) |
| 52 | + |
| 53 | + self.assertEqual(num_zero_inputs, 0) |
| 54 | + self.assertLess(num_zero_outputs, batch_size) |
| 55 | + self.assertGreater(num_zero_outputs, 0) |
| 56 | + |
| 57 | + @staticmethod |
| 58 | + def _num_zero_batches(images): |
| 59 | + num_batches = tf.shape(images)[0] |
| 60 | + num_non_zero_batches = tf.math.count_nonzero( |
| 61 | + tf.math.count_nonzero(images, axis=[1, 2, 3]), dtype=tf.int32 |
| 62 | + ) |
| 63 | + return num_batches - num_non_zero_batches |
| 64 | + |
| 65 | + def test_inputs_unchanged_with_zero_rate(self): |
| 66 | + dummy_inputs = self.rng.uniform(shape=(32, 224, 224, 3)) |
| 67 | + layer = MaybeApply(rate=0.0, layer=ZeroOut()) |
| 68 | + |
| 69 | + outputs = layer(dummy_inputs) |
| 70 | + |
| 71 | + self.assertAllClose(outputs, dummy_inputs) |
| 72 | + |
| 73 | + def test_all_inputs_changed_with_rate_equal_to_one(self): |
| 74 | + dummy_inputs = self.rng.uniform(shape=(32, 224, 224, 3)) |
| 75 | + layer = MaybeApply(rate=1.0, layer=ZeroOut()) |
| 76 | + |
| 77 | + outputs = layer(dummy_inputs) |
| 78 | + |
| 79 | + self.assertAllEqual(outputs, tf.zeros_like(dummy_inputs)) |
| 80 | + |
| 81 | + def test_works_with_single_image(self): |
| 82 | + dummy_inputs = self.rng.uniform(shape=(224, 224, 3)) |
| 83 | + layer = MaybeApply(rate=1.0, layer=ZeroOut()) |
| 84 | + |
| 85 | + outputs = layer(dummy_inputs) |
| 86 | + |
| 87 | + self.assertAllEqual(outputs, tf.zeros_like(dummy_inputs)) |
| 88 | + |
| 89 | + def test_can_modify_label(self): |
| 90 | + dummy_inputs = self.rng.uniform(shape=(32, 224, 224, 3)) |
| 91 | + dummy_labels = tf.ones(shape=(32, 2)) |
| 92 | + layer = MaybeApply(rate=1.0, layer=ZeroOut()) |
| 93 | + |
| 94 | + outputs = layer({"images": dummy_inputs, "labels": dummy_labels}) |
| 95 | + |
| 96 | + self.assertAllEqual(outputs["labels"], tf.zeros_like(dummy_labels)) |
| 97 | + |
| 98 | + def test_can_modify_bounding_box(self): |
| 99 | + dummy_inputs = self.rng.uniform(shape=(32, 224, 224, 3)) |
| 100 | + dummy_boxes = tf.ones(shape=(32, 4)) |
| 101 | + layer = MaybeApply(rate=1.0, layer=ZeroOut()) |
| 102 | + |
| 103 | + outputs = layer({"images": dummy_inputs, "bounding_boxes": dummy_boxes}) |
| 104 | + |
| 105 | + self.assertAllEqual(outputs["bounding_boxes"], tf.zeros_like(dummy_boxes)) |
| 106 | + |
| 107 | + def test_works_with_native_keras_layers(self): |
| 108 | + dummy_inputs = self.rng.uniform(shape=(32, 224, 224, 3)) |
| 109 | + zero_out = tf.keras.layers.Lambda(lambda x: {"images": 0 * x["images"]}) |
| 110 | + layer = MaybeApply(rate=1.0, layer=zero_out) |
| 111 | + |
| 112 | + outputs = layer(dummy_inputs) |
| 113 | + |
| 114 | + self.assertAllEqual(outputs, tf.zeros_like(dummy_inputs)) |
| 115 | + |
| 116 | + def test_works_with_xla(self): |
| 117 | + dummy_inputs = self.rng.uniform(shape=(32, 224, 224, 3)) |
| 118 | + # auto_vectorize=True will crash XLA |
| 119 | + layer = MaybeApply(rate=0.5, layer=ZeroOut(), auto_vectorize=False) |
| 120 | + |
| 121 | + @tf.function(jit_compile=True) |
| 122 | + def apply(x): |
| 123 | + return layer(x) |
| 124 | + |
| 125 | + apply(dummy_inputs) |
0 commit comments