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

Add a condition to verify training status during image processing #20650

Merged
merged 5 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
34 changes: 22 additions & 12 deletions keras/src/layers/preprocessing/image_preprocessing/mix_up.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,36 +66,38 @@ def get_random_transformation(self, data, training=True, seed=None):
}

def transform_images(self, images, transformation=None, training=True):
if training:
images = self._mix_up_input(images, transformation)
return images

def _mix_up_input(self, images, transformation):
images = self.backend.cast(images, self.compute_dtype)
mix_weight = transformation["mix_weight"]
permutation_order = transformation["permutation_order"]

mix_weight = self.backend.cast(
self.backend.numpy.reshape(mix_weight, [-1, 1, 1, 1]),
dtype=self.compute_dtype,
)

mix_up_images = self.backend.cast(
self.backend.numpy.take(images, permutation_order, axis=0),
dtype=self.compute_dtype,
)

images = mix_weight * images + (1.0 - mix_weight) * mix_up_images

return images

def transform_labels(self, labels, transformation, training=True):
if training:
labels = self._mix_up_labels(labels, transformation)
return labels

def _mix_up_labels(self, labels, transformation):
mix_weight = transformation["mix_weight"]
permutation_order = transformation["permutation_order"]

labels_for_mix_up = self.backend.numpy.take(
labels, permutation_order, axis=0
)

mix_weight = self.backend.numpy.reshape(mix_weight, [-1, 1])

labels = mix_weight * labels + (1.0 - mix_weight) * labels_for_mix_up

return labels

def transform_bounding_boxes(
Expand All @@ -104,6 +106,11 @@ def transform_bounding_boxes(
transformation,
training=True,
):
if training:
return self._mix_up_bounding_boxes(bounding_boxes, transformation)
return bounding_boxes

def _mix_up_bounding_boxes(self, bounding_boxes, transformation):
permutation_order = transformation["permutation_order"]
boxes, classes = bounding_boxes["boxes"], bounding_boxes["classes"]
boxes_for_mix_up = self.backend.numpy.take(boxes, permutation_order)
Expand All @@ -117,20 +124,23 @@ def transform_bounding_boxes(
def transform_segmentation_masks(
self, segmentation_masks, transformation, training=True
):
if training:
segmentation_masks = self._mix_up_segmentation_masks(
segmentation_masks, transformation
)
return segmentation_masks

def _mix_up_segmentation_masks(self, segmentation_masks, transformation):
mix_weight = transformation["mix_weight"]
permutation_order = transformation["permutation_order"]

mix_weight = self.backend.numpy.reshape(mix_weight, [-1, 1, 1, 1])

segmentation_masks_for_mix_up = self.backend.numpy.take(
segmentation_masks, permutation_order
)

segmentation_masks = (
mix_weight * segmentation_masks
+ (1.0 - mix_weight) * segmentation_masks_for_mix_up
)

return segmentation_masks

def compute_output_shape(self, input_shape):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ def test_layer(self):
run_training_check=not testing.tensorflow_uses_gpu(),
)

def test_mix_up_inference(self):
seed = 3481
layer = layers.MixUp(alpha=0.2)
np.random.seed(seed)
inputs = np.random.randint(0, 255, size=(224, 224, 3))
output = layer(inputs, training=False)
self.assertAllClose(inputs, output)

def test_mix_up_basic_functionality(self):
image = np.random.random((64, 64, 3))
mix_up_layer = layers.MixUp(alpha=1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,19 @@ def get_random_transformation(self, data, training=True, seed=None):
return {"factor": invert * factor * 0.5}

def transform_images(self, images, transformation=None, training=True):
if training:
images = self._apply_random_hue_to_images(images, transformation)
return images

def _apply_random_hue_to_images(self, images, transformation):
images = transform_value_range(images, self.value_range, (0, 1))
adjust_factors = transformation["factor"]
adjust_factors = self.backend.cast(adjust_factors, images.dtype)
adjust_factors = self.backend.numpy.expand_dims(adjust_factors, -1)
adjust_factors = self.backend.numpy.expand_dims(adjust_factors, -1)

images = self.backend.image.rgb_to_hsv(
images, data_format=self.data_format
)

if self.data_format == "channels_first":
h_channel = images[:, 0, :, :] + adjust_factors
h_channel = self.backend.numpy.where(
Expand All @@ -142,10 +145,8 @@ def transform_images(self, images, transformation=None, training=True):
images = self.backend.image.hsv_to_rgb(
images, data_format=self.data_format
)

images = self.backend.numpy.clip(images, 0, 1)
images = transform_value_range(images, (0, 1), self.value_range)

return images

def transform_labels(self, labels, transformation, training=True):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ def test_layer(self):
expected_output_shape=(8, 3, 4, 3),
)

def test_random_hue_inference(self):
seed = 3481
layer = layers.RandomHue(0.2, [0, 1.0])
np.random.seed(seed)
inputs = np.random.randint(0, 255, size=(224, 224, 3))
output = layer(inputs, training=False)
self.assertAllClose(inputs, output)

def test_random_hue_value_range(self):
image = keras.random.uniform(shape=(3, 3, 3), minval=0, maxval=1)

Expand Down
Loading