-
Notifications
You must be signed in to change notification settings - Fork 19.7k
Add RandomRotation segmentation example (fill_mode constant vs crop) #22011
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
base: master
Are you sure you want to change the base?
Add RandomRotation segmentation example (fill_mode constant vs crop) #22011
Conversation
Summary of ChangesHello @PES2UG23CS205, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a new Keras example that visually demonstrates the application of the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces a helpful example demonstrating RandomRotation for segmentation tasks, which clearly shows the difference between fill_mode="constant" and fill_mode="crop". The new example script is well-written and serves as a great visual guide. Additionally, the docstring for RandomRotation has been updated with examples. I've provided one suggestion to make the docstring example more backend-agnostic and align it better with the Keras style guide by using keras.ops and avoiding TensorShape.
| >>> import tensorflow as tf | ||
| >>> from keras import layers | ||
| >>> images = tf.random.uniform((2, 128, 128, 3)) | ||
| >>> layer = layers.RandomRotation(factor=0.2) | ||
| >>> output = layer(images) | ||
| >>> output.shape | ||
| TensorShape([2, 128, 128, 3]) | ||
| Synchronized image and segmentation mask rotation: | ||
| >>> images = tf.random.uniform((1, 128, 128, 3)) | ||
| >>> masks = tf.random.uniform( | ||
| ... (1, 128, 128, 1), maxval=2, dtype=tf.int32 | ||
| ... ) | ||
| >>> data = { | ||
| ... "images": images, | ||
| ... "segmentation_masks": masks, | ||
| ... } | ||
| >>> layer = layers.RandomRotation( | ||
| ... factor=0.25, | ||
| ... fill_mode="crop" | ||
| ... ) | ||
| >>> output = layer(data) | ||
| >>> output["images"].shape | ||
| TensorShape([1, 128, 128, 3]) | ||
| >>> output["segmentation_masks"].shape | ||
| TensorShape([1, 128, 128, 1]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The docstring examples use tensorflow and TensorShape, which are specific to the TensorFlow backend. To make the examples more backend-agnostic and align with the Keras style guide, it's better to use keras.ops and plain Python types.
The style guide (line 63) states: "Plain Python types are preferable to custom types. Use tuples, strings, ints... A custom type requires more knowledge and effort on the part of the user (e.g. TensorShape, which is also breaking established conventions of scientific Python)."
I suggest updating the examples to use keras.ops for tensor creation and tuple() to display the shape, which will make them work consistently across different backends.
>>> import keras
>>> images = keras.ops.random.uniform((2, 128, 128, 3))
>>> layer = keras.layers.RandomRotation(factor=0.2)
>>> output = layer(images)
>>> tuple(output.shape)
(2, 128, 128, 3)
Synchronized image and segmentation mask rotation:
>>> images = keras.ops.random.uniform((1, 128, 128, 3))
>>> masks = keras.ops.random.uniform(
... (1, 128, 128, 1), maxval=2, dtype="int32"
... )
>>> data = {
... "images": images,
... "segmentation_masks": masks,
... }
>>> layer = keras.layers.RandomRotation(
... factor=0.25,
... fill_mode="crop"
... )
>>> output = layer(data)
>>> tuple(output["images"].shape)
(1, 128, 128, 3)
>>> tuple(output["segmentation_masks"].shape)
(1, 128, 128, 1)References
- The style guide recommends using plain Python types over custom types like
TensorShapeto reduce cognitive load for users. The example should be backend-agnostic and avoid types specific to one backend. (link)
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #22011 +/- ##
=======================================
Coverage 82.69% 82.69%
=======================================
Files 592 592
Lines 62072 62072
Branches 9723 9723
=======================================
Hits 51332 51332
Misses 8215 8215
Partials 2525 2525
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
| `image_data_format` value found in your Keras config file at | ||
| `~/.keras/keras.json`. If you never set it, then it will be | ||
| `"channels_last"`. | ||
| Examples: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, this part is already addressed here: #21978
| @@ -0,0 +1,83 @@ | |||
| """ | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't add examples in the keras repo like this because people won't find them and they won't see the pictures. The right place for discoverability is keras.io which you can contribute to here: https://github.com/keras-team/keras-io where images get rendered for guides and examples.
The thing is, we normally do end-to-end examples, having an example just about RandomRotation would be very narrow. However, we could really use a guide showing all the image augmentation layers.
Is that something you're willing to work on?
This PR adds a visual keras-io example demonstrating how
keras.layers.RandomRotation behaves for segmentation tasks.
It compares fill_mode="constant" vs fill_mode="crop" using
structured dict inputs to ensure synchronized transforms
for images and segmentation masks.
Related to #21954
cc @innat @mattdangerw