Skip to content

Conversation

@PES2UG23CS205
Copy link
Contributor

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

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @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 RandomRotation layer to both images and their associated segmentation masks. The example highlights the distinct outcomes of using fill_mode="constant" versus fill_mode="crop" when handling rotated regions, ensuring that transformations are consistently applied across both inputs via structured dictionary inputs. Additionally, the documentation for the RandomRotation layer has been enhanced with a practical code snippet showcasing this synchronized rotation capability.

Highlights

  • New Visual Example for RandomRotation: A new visual example (random_rotation_segmentation.py) has been added to demonstrate the behavior of keras.layers.RandomRotation when applied to segmentation tasks.
  • Comparison of Fill Modes: The new example explicitly compares the effects of fill_mode="constant" and fill_mode="crop" on both images and their corresponding segmentation masks after rotation.
  • Synchronized Transformations: Structured dictionary inputs are utilized in the example to ensure synchronized transformations between images and masks, which is crucial for segmentation tasks.
  • Updated Documentation: The RandomRotation layer's documentation has been updated with a new code example illustrating synchronized image and segmentation mask rotation using dictionary inputs.

🧠 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 Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@PES2UG23CS205
Copy link
Contributor Author

Hi @innat

I’ve opened a PR adding a keras-io example for RandomRotation focusing on
segmentation masks and comparing fill_mode="constant" vs "crop", with
visual before/after outputs.

PR: #22011

Thanks for the suggestion ,happy to iterate if you’d like any changes.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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.

Comment on lines +82 to +108
>>> 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])
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

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
  1. The style guide recommends using plain Python types over custom types like TensorShape to reduce cognitive load for users. The example should be backend-agnostic and avoid types specific to one backend. (link)

@codecov-commenter
Copy link

codecov-commenter commented Jan 15, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.69%. Comparing base (ef1790c) to head (83fd219).

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           
Flag Coverage Δ
keras 82.52% <ø> (ø)
keras-jax 61.55% <ø> (ø)
keras-numpy 56.56% <ø> (ø)
keras-openvino 37.42% <ø> (ø)
keras-tensorflow 63.70% <ø> (ø)
keras-torch 62.47% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

`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:
Copy link
Collaborator

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 @@
"""
Copy link
Collaborator

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants