Skip to content

Conversation

Phil2852
Copy link
Contributor

When calling the tensorflow version of keras.ops.image.map_coordinates with coordinates containing an index outside of the input image dimensions and constant padding, the output image is filled with fill_value due to a missing axis annotation in the tensorflow backend code.

Script to reproduce with current master branch:

import keras
import tensorflow as tf
import matplotlib.pyplot as plt


def main():
    img = tf.random.normal((128, 128))
    # identity map for comparison
    grid = tf.stack(
        tf.meshgrid(
            tf.range(0, img.shape[0], dtype=tf.float64),
            tf.range(0, img.shape[1], dtype=tf.float64),
        ),
        axis=0,
    )
    # first row and column are -1 and therefore considered out-of-bounds
    grid_with_oob = tf.stack(
        tf.meshgrid(
            tf.range(-1, img.shape[0] - 1, dtype=tf.float64),
            tf.range(-1, img.shape[1] - 1, dtype=tf.float64),
        ),
        axis=0,
    )

    identity_mapped = keras.ops.image.map_coordinates(img, grid, 1)
    mapped_with_oob = keras.ops.image.map_coordinates(img, grid_with_oob, 1)

    fig, ax = plt.subplots(1, 3)

    ax[0].imshow(img.numpy())
    ax[1].imshow(identity_mapped.numpy())
    ax[2].imshow(mapped_with_oob.numpy())

    plt.show()


if __name__ == "__main__":
    main()

Copy link

google-cla bot commented May 19, 2025

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.

@codecov-commenter
Copy link

codecov-commenter commented May 19, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 82.60%. Comparing base (dfaca0e) to head (1bf1eb3).
Report is 13 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #21304      +/-   ##
==========================================
+ Coverage   82.57%   82.60%   +0.02%     
==========================================
  Files         564      565       +1     
  Lines       54677    54772      +95     
  Branches     8500     8508       +8     
==========================================
+ Hits        45152    45243      +91     
- Misses       7435     7439       +4     
  Partials     2090     2090              
Flag Coverage Δ
keras 82.41% <100.00%> (+0.02%) ⬆️
keras-jax 63.55% <0.00%> (-0.06%) ⬇️
keras-numpy 58.71% <0.00%> (-0.04%) ⬇️
keras-openvino 33.13% <0.00%> (+0.08%) ⬆️
keras-tensorflow 63.96% <100.00%> (-0.07%) ⬇️
keras-torch 63.62% <0.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.

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

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! Please add a unit test for the fix.

@Phil2852 Phil2852 force-pushed the fix/map_coordinates-tf-constant-padding branch from 63d34bc to 395505c Compare May 20, 2025 17:06
@Phil2852 Phil2852 force-pushed the fix/map_coordinates-tf-constant-padding branch from 395505c to 1bf1eb3 Compare May 20, 2025 17:25
@Phil2852
Copy link
Contributor Author

Sure, I added a rather generic constant padding test that would have caught the issue.

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.

LGTM, thank you for the fix.

@google-ml-butler google-ml-butler bot added kokoro:force-run ready to pull Ready to be merged into the codebase labels May 20, 2025
@fchollet fchollet merged commit 7d7a6bb into keras-team:master May 20, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kokoro:force-run ready to pull Ready to be merged into the codebase size:XS
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants