Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug when upranking passthrough inputs to RandAugment #2194

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,8 @@ def _ensure_inputs_are_compute_dtype(self, inputs):
inputs,
self.compute_dtype,
)
# Copy the input dict before we mutate it.
inputs = dict(inputs)
inputs[IMAGES] = preprocessing.ensure_tensor(
inputs[IMAGES],
self.compute_dtype,
Expand Down
12 changes: 9 additions & 3 deletions keras_cv/layers/preprocessing/rand_augment_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,23 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import numpy as np
import pytest
import tensorflow as tf
from absl.testing import parameterized

from keras_cv import layers
from keras_cv.backend.config import keras_3
from keras_cv.tests.test_case import TestCase


@pytest.mark.skipif(keras_3(), reason="imcompatible with Keras 3")
class RandAugmentTest(TestCase):
def test_zero_rate_pass_through(self):
rand_augment = layers.RandAugment(
value_range=(0, 255),
rate=0.0,
)
xs = np.ones((2, 512, 512, 3))
ys = rand_augment(xs)
self.assertAllClose(ys, xs)

@parameterized.named_parameters(
("0", 0),
("20", 0.2),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def _augment(self, inputs):
)
result = tf.cond(
skip_augment > self.rate,
lambda: inputs,
lambda: result,
lambda: self._random_choice(result),
)
return result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,9 @@ def _format_inputs(self, inputs):
# single image input tensor
metadata[IS_DICT] = False
inputs = {IMAGES: inputs}
else:
# Copy the input dict before we mutate it.
inputs = dict(inputs)

metadata[BATCHED] = inputs["images"].shape.rank == 4
if inputs["images"].shape.rank == 3:
Expand Down Expand Up @@ -504,6 +507,8 @@ def _ensure_inputs_are_compute_dtype(self, inputs):
inputs,
self.compute_dtype,
)
# Copy the input dict before we mutate it.
inputs = dict(inputs)
inputs[IMAGES] = preprocessing.ensure_tensor(
inputs[IMAGES],
self.compute_dtype,
Expand Down
Loading