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

Update random_cutout.py to be a subclass of VectorizedBaseImageAugmentationLayer #2123

Merged
merged 4 commits into from
Dec 8, 2023

Conversation

sup3rgiu
Copy link
Contributor

What does this PR do?

As of v0.6.4, RandomCutout is not implemented in a vectorized manner. This can be easily fixed by making it a subclass of VectorizedBaseImageAugmentationLayer and fixing the shape of some computations.

A full working vectorized implementation+benchmark is available at:
https://colab.research.google.com/drive/1luk08YFpR4VFDW_qD1RHgNFaJDV46Dlf?usp=sharing

Execution time over 10 CIFAR-10 batches of size 128:

Original implementation: 0:00:07.374743
Vectorized implementation:   0:00:00.159785

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue? Please add a link
    to it if that's the case.
  • Did you write any new necessary tests?
  • If this adds a new model, can you run a few training steps on TPU in Colab to ensure that no XLA incompatible OP are used?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@google-cla
Copy link

google-cla bot commented Oct 27, 2023

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@sampathweb
Copy link
Collaborator

Thanks for the PR!. Can you rebase with Master

Make RandomCutout a subclass of VectorizedBaseImageAugmentationLayer
@sup3rgiu
Copy link
Contributor Author

Thanks for the PR!. Can you rebase with Master

Done

Copy link
Collaborator

@divyashreepathihalli divyashreepathihalli left a comment

Choose a reason for hiding this comment

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

@sup3rgiu can you please add a demo output of this layer and a colab to check the results?

@sup3rgiu
Copy link
Contributor Author

sup3rgiu commented Dec 6, 2023

@sup3rgiu can you please add a demo output of this layer and a colab to check the results?

Sure!

Colab: https://colab.research.google.com/drive/1luk08YFpR4VFDW_qD1RHgNFaJDV46Dlf

Demo output:
image

image

The "brightness" difference of the random noise is simply because in the vectorized version I added few instructions to rescale the noise to the input image range. I think this might be useful to apply the noise correctly, regardless of the image range, but if it isn't you can simply remove these lines within the _compute_rectangle_fill method:

image_max = tf.reduce_max(inputs)
image_min = tf.reduce_min(inputs)
fill_max = tf.reduce_max(fill_value)
fill_min = tf.reduce_min(fill_value)
fill_value = (image_max - image_min) * (fill_value - fill_min) / (fill_max - fill_min) + image_min

@divyashreepathihalli
Copy link
Collaborator

Great work! Rescaling the noise to the input image range is cool!
However, there is one failing test -keras_cv/layers/preprocessing/ragged_image_test.py::RaggedImageTest::test_preserves_ragged_status_RandomCutout - ValueError: Exception encountered when calling layer 'random_cutout' (type RandomCutout).
Also, you will need to run shell/format.sh to reformat your code.

@sup3rgiu
Copy link
Contributor Author

sup3rgiu commented Dec 8, 2023

I fixed the implementation to support RaggedTensors. The test should be ok now.
I've also run shell/format.sh to reformat the code.

New colab if needed: https://colab.research.google.com/drive/1luk08YFpR4VFDW_qD1RHgNFaJDV46Dlf

@divyashreepathihalli
Copy link
Collaborator

/gcbrun

@divyashreepathihalli
Copy link
Collaborator

Thank you! triggering the tests again!

@divyashreepathihalli divyashreepathihalli merged commit 7da0c1e into keras-team:master Dec 8, 2023
6 of 9 checks passed
divyashreepathihalli pushed a commit that referenced this pull request Dec 11, 2023
* Adds Kokoro GPU Tests (#2224)

* Adds Kokoro tests

* Add Kokoro Tests

* Update random_cutout.py to be a subclass of VectorizedBaseImageAugmentationLayer (#2123)

* Update random_cutout.py

Make RandomCutout a subclass of VectorizedBaseImageAugmentationLayer

* Fix RandomCutout for ragged inputs

* Fix typo and style

* Vectorized implementation of RandomColorDegeneration and Equalization preprocessing layers (#2214)

* Vectorize RandomColorDegeneration

* Vectorize Equalization

* Vectorize Equalization

* Fix Equalization for ragged input

---------

Co-authored-by: Ramesh Sampath <1437573+sampathweb@users.noreply.github.com>
Co-authored-by: SUPERGIU <supergiu@outlook.com>
yuvraj-wale pushed a commit to yuvraj-wale/keras-cv that referenced this pull request Feb 8, 2024
…tationLayer (keras-team#2123)

* Update random_cutout.py

Make RandomCutout a subclass of VectorizedBaseImageAugmentationLayer

* Fix RandomCutout for ragged inputs

* Fix typo and style
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants