-
Notifications
You must be signed in to change notification settings - Fork 19.5k
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
Fixing keras.ops.image.map_coordinates Failure with uint8 Input on TensorFlow Backend" #20645
Comments
Why do you plagiarize #20608? |
Here's how to fix it: Example: Load an image (ensure it is uint8)img = tf.image.decode_image(tf.io.read_file('image_path.jpg')) Convert the image to float32 before applying map_coordinatesimg_float = tf.cast(img, dtype=tf.float32) Apply map_coordinates after conversionfrom tensorflow.image import map_coordinates Example: Apply map_coordinates on the normalized imageoutput_image = tf.image.map_coordinates(img_normalized, coordinates) The TensorFlow version you are using (some older versions might have different behavior or bugs). Load an image (ensure it is uint8)img = tf.image.decode_image(tf.io.read_file('image_path.jpg')) Convert the image to float32 before applying map_coordinatesimg_float = tf.cast(img, dtype=tf.float32) Normalize the image (if needed)img_normalized = img_float / 255.0 Example coordinates (row, col) in the form of y, xcoordinates = tf.constant([[50, 50], [100, 150]]) # Example coordinates Apply map_coordinates after conversion and scalingfrom tensorflow.image import map_coordinates |
@mehtamansi29 It looks like @NebulaNerdo and @Mr-back007 purposefully spam the issue tracker. @NebulaNerdo stole the wording from #20608 without any value added. @Mr-back007 uses some kind of LLM to generate irrelevant and subpar quality discussion content. Please close the issue and consider banning both users. |
Hi @NebulaNerdo, this issue is duplicate of #20608 and a PR #20768 was merged for the same. You can test your code with keras nightly. I am closing this. Feel free to open another issue. |
Consider the following simple example
`import keras
image = keras.ops.ones((1, 1, 3), dtype='uint8')
coordinates = keras.ops.convert_to_tensor([-1., 0., 0.])[..., None, None]
interp = keras.ops.image.map_coordinates(image, coordinates, order=1, fill_mode='constant')`
that is expected to yield [[0]]. However, with KERAS_BACKEND=tensorflow this code snippet results in
2024-12-08 16:04:24.790791: W tensorflow/core/framework/op_kernel.cc:1841] OP_REQUIRES failed at gather_nd_op.cc:65 : INVALID_ARGUMENT: indices[0,0] = [-1, 0, 0] does not index into param shape [1,1,3], node name: GatherNd 2024-12-08 16:04:24.790814: I tensorflow/core/framework/local_rendezvous.cc:405] Local rendezvous is aborting with status: INVALID_ARGUMENT: indices[0,0] = [-1, 0, 0] does not index into param shape [1,1,3], node name: GatherNd Traceback (most recent call last): File "<home>/tfmapc.py", line 11, in <module> interp = keras.ops.image.map_coordinates(image, coordinates, order=1, fill_mode='constant') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<home>/.env/lib/python3.12/site-packages/keras/src/ops/image.py", line 787, in map_coordinates return backend.image.map_coordinates( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<home>/.env/lib/python3.12/site-packages/keras/src/backend/tensorflow/image.py", line 485, in map_coordinates contribution = tf.cond(tf.reduce_all(validities), fast_path, slow_path) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<home>/.env/lib/python3.12/site-packages/tensorflow/python/util/traceback_utils.py", line 153, in error_handler raise e.with_traceback(filtered_tb) from None File "<home>/.env/lib/python3.12/site-packages/keras/src/backend/tensorflow/image.py", line 481, in slow_path tf.transpose(tf.gather_nd(input_arr, indices)), ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ tensorflow.python.framework.errors_impl.InvalidArgumentError: {{function_node __wrapped__GatherNd_device_/job:localhost/replica:0/task:0/device:CPU:0}} indices[0,0] = [-1, 0, 0] does not index into param shape [1,1,3], node name: GatherNd [Op:GatherNd] name:
The problem does not occur if I change the dtype of image from uint8 to float32 or switch either to the jax or torch backends. Also changing the fill_mode from constant to nearest avoids the issue.
Keras version: 3.7.0
The text was updated successfully, but these errors were encountered: