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

feat(ops): Add keras.ops.numpy.rot90 operation (#20723) #20745

Merged
merged 5 commits into from
Jan 13, 2025

Conversation

harshaljanjani
Copy link
Contributor

@harshaljanjani harshaljanjani commented Jan 9, 2025

Adds a new operation to rotate tensors by 90 degrees in the specified plane:

  • Implements rot90 operation in keras.ops.numpy module
  • Adds support for multiple rotations (k parameter) and custom axes
  • Matches numpy.rot90 behavior and API for consistency
  • Adds comprehensive test coverage including batch images support
  • Handles input validation for tensor dimensions and axes
  • Supports symbolic tensor execution
  • The operation follows the same interface as numpy.rot90 and tf.image.rot90: rot90(array, k=1, axes=(0, 1))
  • Fixes Add keras.ops.rot90 for tf.image.rot90 #20723

Adds a new operation to rotate tensors by 90 degrees in the specified plane:
- Implements rot90 operation in keras.ops.image module
- Adds support for multiple rotations (k parameter) and custom axes
- Matches numpy.rot90 behavior and API for consistency
- Adds comprehensive test coverage including batch images support
- Handles input validation for tensor dimensions and axes
- Supports symbolic tensor execution
The operation follows the same interface as numpy.rot90 and tf.image.rot90:
rot90(array, k=1, axes=(0, 1))
@codecov-commenter
Copy link

codecov-commenter commented Jan 9, 2025

Codecov Report

Attention: Patch coverage is 75.00000% with 18 lines in your changes missing coverage. Please review.

Project coverage is 81.94%. Comparing base (97c1c00) to head (c00c233).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
keras/src/ops/numpy.py 33.33% 13 Missing and 1 partial ⚠️
keras/src/backend/torch/numpy.py 69.23% 2 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #20745      +/-   ##
==========================================
- Coverage   81.95%   81.94%   -0.01%     
==========================================
  Files         553      553              
  Lines       51458    51530      +72     
  Branches     7961     7977      +16     
==========================================
+ Hits        42174    42228      +54     
- Misses       7346     7361      +15     
- Partials     1938     1941       +3     
Flag Coverage Δ
keras 81.77% <75.00%> (-0.01%) ⬇️
keras-jax 63.98% <20.83%> (-0.07%) ⬇️
keras-numpy 58.84% <22.22%> (-0.06%) ⬇️
keras-openvino 29.81% <11.11%> (-0.03%) ⬇️
keras-tensorflow 64.67% <48.61%> (-0.03%) ⬇️
keras-torch 64.02% <25.00%> (-0.06%) ⬇️

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.

Add implementations of rot90() for multiple backend frameworks:
- JAX backend implementation
- NumPy backend implementation
- PyTorch backend implementation
Copy link
Collaborator

@fchollet fchollet left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

rot90 is a numpy op: https://numpy.org/doc/stable/reference/generated/numpy.rot90.html

So please move the code to e.g. keras/src/backend/jax/numpy.py (and so on) and export the op to keras.ops.rot90 as well as keras.ops.numpy.rot90.

Move rot90 operation to numpy.py files in backend implementations since it's a numpy op (https://numpy.org/doc/stable/reference/generated/numpy.rot90.html). Now exported as both keras.ops.rot90 and keras.ops.numpy.rot90.
Resolved the 'Invalid dtype: object' error by explicitly using  to avoid naming conflicts with the custom  function.
Copy link
Collaborator

@fchollet fchollet left a comment

Choose a reason for hiding this comment

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

Thanks for the update!

keras/src/backend/tensorflow/numpy.py Outdated Show resolved Hide resolved
keras/src/backend/torch/numpy.py Outdated Show resolved Hide resolved
Replace tf.experimental.numpy.rot90 with core TF ops for XLA compatibility.
Use convert_to_tensor for input handling.
@harshaljanjani harshaljanjani changed the title feat(ops): Add keras.ops.image.rot90 operation (#20723) feat(ops): Add keras.ops.numpy.rot90 operation (#20723) Jan 12, 2025
Copy link
Collaborator

@fchollet fchollet left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution! LGTM

@google-ml-butler google-ml-butler bot added kokoro:force-run ready to pull Ready to be merged into the codebase labels Jan 13, 2025
@fchollet fchollet merged commit cdbe4fb into keras-team:master Jan 13, 2025
9 of 10 checks passed
@google-ml-butler google-ml-butler bot removed awaiting review ready to pull Ready to be merged into the codebase kokoro:force-run labels Jan 13, 2025
@harshaljanjani harshaljanjani deleted the add-keras-ops-rot90 branch January 13, 2025 02:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Merged
Development

Successfully merging this pull request may close these issues.

Add keras.ops.rot90 for tf.image.rot90
4 participants