Skip to content

RandomGrayscale fails for unbatched images. #21276

Closed
@pctablet505

Description

@pctablet505

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

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions