I am trying to use a custom preprocessing function to convert RGB images to grayscale during training. As such, I try to use tf.image.rbg_to_grayscale
for this. My function looks as following:
def prep_data(x):
x = tf.image.rgb_to_grayscale(x)
return x
datagen = ImageDataGenerator(preprocessing_function=prep_data,validation_split=0.15)
The train_generator
is defined using datagen.flow_from_dataframe(...)
. Training without this custom function works just fine, however once I use it I get the following error:
ValueError: setting an array element with a sequence.
Judging from this answer here, I assume I need to change my input to rgb_to_grayscale
, but I don't know what's the correct way of passing x
to the function.
Any idea on how to solve this?
Instead, you can use the color_mode
argument of flow_from_directory
and set it to 'grayscale'
to convert the images to grayscale. From Keras docs:
color_mode: One of "grayscale", "rbg", "rgba". Default: "rgb". Whether the images will be converted to have 1, 3, or 4 channels.