Skip to content

RandomGrayscale fails for unbatched images. #21276

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

Closed
pctablet505 opened this issue May 12, 2025 · 0 comments · Fixed by #21277
Closed

RandomGrayscale fails for unbatched images. #21276

pctablet505 opened this issue May 12, 2025 · 0 comments · Fixed by #21277
Assignees
Labels

Comments

@pctablet505
Copy link
Collaborator

pctablet505 commented May 12, 2025

When we try to pass unbatched single rgb image to RandomGrayscale it fails.

Code to reproduce

import keras
from keras import layers
import matplotlib.pyplot as plt
import numpy as np
img=np.random.uniform(size=(224,224,3))
# plt.imshow(img)
# plt.show()
out=layers.RandomGrayscale(1)(np.array(img))
# plt.imshow(out)
# plt.show()

I get the following error

/usr/local/bin/python3 /Users/hellorahul/PyCharmMiscProject/script.py 
Traceback (most recent call last):
  File "/Users/hellorahul/PyCharmMiscProject/script.py", line 8, in <module>
    out=layers.RandomGrayscale(1)(np.array(img))
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/keras/src/layers/preprocessing/tf_data_layer.py", line 49, in __call__
    return super().__call__(inputs, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/keras/src/utils/traceback_utils.py", line 122, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/keras/src/backend/tensorflow/numpy.py", line 2616, in squeeze
    raise ValueError(
ValueError: Exception encountered when calling RandomGrayscale.call().

Cannot squeeze axis=0, because the dimension is not 1.

Arguments received by RandomGrayscale.call():
  • data=tf.Tensor(shape=(224, 224, 3), dtype=float32)
  • training=True

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants