0

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?

today
  • 32,602
  • 8
  • 95
  • 115
AaronDT
  • 3,940
  • 8
  • 31
  • 71
  • If the answer resolved your issue, kindly *accept* it by clicking on the checkmark next to the answer to mark it as "answered" - see [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – today Nov 26 '18 at 15:56

2 Answers2

1

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.

today
  • 32,602
  • 8
  • 95
  • 115
0

change RGB image to greyscale (example):

img = image.load_img('/kaggle/input/cassava-leaf-disease-classification/train_images/'+train['image_id'][i], target_size=(28,28,1), color_mode="grayscale")
Raunak Singh
  • 33
  • 1
  • 5