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 support of segmentation mask in Augmix layer #1988

Merged
merged 9 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
34 changes: 34 additions & 0 deletions examples/layers/preprocessing/segmentation/aug_mix_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright 2023 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.
"""aug_mix_demo.py shows how to use the AugMix preprocessing layer.

Uses the oxford iiit pet_dataset. In this script the pets
are loaded, then are passed through the preprocessing layers.
Finally, they are shown using matplotlib.
"""
import demo_utils
import tensorflow as tf

from keras_cv.layers import preprocessing


def main():
ds = demo_utils.load_oxford_iiit_pet_dataset()
augmix = preprocessing.AugMix([0, 255])
ds = ds.map(augmix, num_parallel_calls=tf.data.AUTOTUNE)
demo_utils.visualize_dataset(ds)


if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions examples/layers/preprocessing/segmentation/demo_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
def normalize(input_image, input_mask):
input_image = tf.image.convert_image_dtype(input_image, tf.float32)
input_image = (input_image - mean) / tf.maximum(std, backend.epsilon())
input_image = input_image / 255
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this break other demos?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this seems incorrect -- we're already scaling by mean and stddev so this shouldn't be done

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I checked other demos as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why make this change though? It seems like it's not a reasonable transform given the existing input scale of the image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this, the value range of image is greater than 255. As we are setting Augmix value range to be [0, 255], so the demo does not work without making this change. Any better alternative? 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay this seems more or less reasonable. However, this does make resize_demo.py strange, as that file already manually rescales by 255.0 on line 34. Let's get rid of that rescaling there and then this is fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

input_mask -= 1
return input_image, input_mask

Expand Down
2 changes: 1 addition & 1 deletion keras_cv/layers/preprocessing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The provided table gives an overview of the different augmentation layers availa

| Layer Name | Vectorized | Segmentation Masks | BBoxes | Class Labels |
| :-- | :--: | :--: | :--: | :--: |
| AugMix | ❌ | | ✅ | ✅ |
| AugMix | ❌ | | ✅ | ✅ |
| AutoContrast | ✅ | ✅ | ✅ | ✅ |
| ChannelShuffle | ✅ | ✅ | ✅ | ✅ |
| CutMix | ❌ | ✅ | ❌ | ✅ |
Expand Down
30 changes: 30 additions & 0 deletions keras_cv/layers/preprocessing/aug_mix.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,36 @@ def augment_image(self, image, transformation=None, **kwargs):
def augment_label(self, label, transformation=None, **kwargs):
return label

def augment_segmentation_mask(
self, segmentation_masks, transformation=None, **kwargs
):
chain_mixing_weights = self._sample_from_dirichlet(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sampling these random values differently for the image augmentation and the mask augmentation will cause the image + mask to be augmented inconsistently with one another (your demo image shows, for example, that the dog mask is rotated differently than the dog itself).

We should implement and use get_random_transformation to get a single chain_mixing_weights and a single weight_sample that is used for both images and labels

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking into it.

tf.ones([self.num_chains]) * self.alpha
)
weight_sample = self._sample_from_beta(self.alpha, self.alpha)

result = tf.zeros_like(segmentation_masks)
curr_chain = tf.constant([0], dtype=tf.int32)

(
segmentation_masks,
chain_mixing_weights,
curr_chain,
result,
) = tf.while_loop(
lambda segmentation_masks, chain_mixing_weights, curr_chain, result: tf.less( # noqa: E501
curr_chain, self.num_chains
),
self._loop_on_width,
[segmentation_masks, chain_mixing_weights, curr_chain, result],
)

# Apply the mixing of segmentation_masks similar to images
result = (
weight_sample * segmentation_masks + (1 - weight_sample) * result
)
return result

def get_config(self):
config = {
"value_range": self.value_range,
Expand Down
41 changes: 39 additions & 2 deletions keras_cv/layers/preprocessing/aug_mix_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,20 @@ def test_return_shapes(self):
# RGB
xs = tf.ones((2, 512, 512, 3))
xs = layer(xs)
ys_segmentation_masks = tf.ones((2, 512, 512, 3))
ys_segmentation_masks = layer(ys_segmentation_masks)
self.assertEqual(xs.shape, [2, 512, 512, 3])
self.assertEqual(ys_segmentation_masks.shape, [2, 512, 512, 3])

# greyscale
xs = tf.ones((2, 512, 512, 1))
xs = layer(xs)
ys_segmentation_masks = tf.ones((2, 512, 512, 1))
ys_segmentation_masks = layer(ys_segmentation_masks)
self.assertEqual(xs.shape, [2, 512, 512, 1])
self.assertEqual(ys_segmentation_masks.shape, [2, 512, 512, 1])

def test_in_single_image(self):
def test_in_single_image_and_mask(self):
layer = preprocessing.AugMix([0, 255])

# RGB
Expand All @@ -42,7 +48,14 @@ def test_in_single_image(self):
)

xs = layer(xs)
ys_segmentation_masks = tf.cast(
tf.ones((512, 512, 3)),
dtype=tf.float32,
)

ys_segmentation_masks = layer(ys_segmentation_masks)
self.assertEqual(xs.shape, [512, 512, 3])
self.assertEqual(ys_segmentation_masks.shape, [512, 512, 3])

# greyscale
xs = tf.cast(
Expand All @@ -51,43 +64,67 @@ def test_in_single_image(self):
)

xs = layer(xs)
ys_segmentation_masks = tf.cast(
tf.ones((512, 512, 1)),
dtype=tf.float32,
)
ys_segmentation_masks = layer(ys_segmentation_masks)
self.assertEqual(xs.shape, [512, 512, 1])
self.assertEqual(ys_segmentation_masks.shape, [512, 512, 1])

def test_non_square_images(self):
def test_non_square_images_and_masks(self):
layer = preprocessing.AugMix([0, 255])

# RGB
xs = tf.ones((2, 256, 512, 3))
xs = layer(xs)
ys_segmentation_masks = tf.ones((2, 256, 512, 3))
ys_segmentation_masks = layer(ys_segmentation_masks)
self.assertEqual(xs.shape, [2, 256, 512, 3])
self.assertEqual(ys_segmentation_masks.shape, [2, 256, 512, 3])

# greyscale
xs = tf.ones((2, 256, 512, 1))
xs = layer(xs)
ys_segmentation_masks = tf.ones((2, 256, 512, 1))
ys_segmentation_masks = layer(ys_segmentation_masks)
self.assertEqual(xs.shape, [2, 256, 512, 1])
self.assertEqual(ys_segmentation_masks.shape, [2, 256, 512, 1])

def test_single_input_args(self):
layer = preprocessing.AugMix([0, 255])

# RGB
xs = tf.ones((2, 512, 512, 3))
xs = layer(xs)
ys_segmentation_masks = tf.ones((2, 512, 512, 3))
ys_segmentation_masks = layer(ys_segmentation_masks)
self.assertEqual(xs.shape, [2, 512, 512, 3])
self.assertEqual(ys_segmentation_masks.shape, [2, 512, 512, 3])

# greyscale
xs = tf.ones((2, 512, 512, 1))
xs = layer(xs)
ys_segmentation_masks = tf.ones((2, 512, 512, 1))
ys_segmentation_masks = layer(ys_segmentation_masks)
self.assertEqual(xs.shape, [2, 512, 512, 1])
self.assertEqual(ys_segmentation_masks.shape, [2, 512, 512, 1])

def test_many_augmentations(self):
layer = preprocessing.AugMix([0, 255], chain_depth=[25, 26])

# RGB
xs = tf.ones((2, 512, 512, 3))
xs = layer(xs)
ys_segmentation_masks = tf.ones((2, 512, 512, 3))
ys_segmentation_masks = layer(ys_segmentation_masks)
self.assertEqual(xs.shape, [2, 512, 512, 3])
self.assertEqual(ys_segmentation_masks.shape, [2, 512, 512, 3])

# greyscale
xs = tf.ones((2, 512, 512, 1))
xs = layer(xs)
ys_segmentation_masks = tf.ones((2, 512, 512, 1))
ys_segmentation_masks = layer(ys_segmentation_masks)
self.assertEqual(xs.shape, [2, 512, 512, 1])
self.assertEqual(ys_segmentation_masks.shape, [2, 512, 512, 1])
Loading