Custom preprocessing_function with tf.image.rgb_to_grayscale - ValueError: setting an array element with a sequence
Custom preprocessing_function with tf.image.rgb_to_grayscale - ValueError: setting an array element with a sequence
我正在尝试使用自定义预处理函数在训练期间将 RGB 图像转换为灰度图像。因此,我尝试为此使用 tf.image.rbg_to_grayscale
。我的函数如下所示:
def prep_data(x):
x = tf.image.rgb_to_grayscale(x)
return x
datagen = ImageDataGenerator(preprocessing_function=prep_data,validation_split=0.15)
train_generator
是使用 datagen.flow_from_dataframe(...)
定义的。没有这个自定义函数的训练工作得很好,但是一旦我使用它,我就会收到以下错误:
ValueError: setting an array element with a sequence.
从这个答案 here 来看,我假设我需要将输入更改为 rgb_to_grayscale
,但我不知道将 x
传递给函数的正确方法是什么.
知道如何解决这个问题吗?
相反,您可以使用 flow_from_directory
的 color_mode
参数并将其设置为 'grayscale'
以将图像转换为灰度。来自 Keras docs:
color_mode: One of "grayscale", "rbg", "rgba". Default: "rgb". Whether the images will be converted to have 1, 3, or 4 channels.
将 RGB 图像更改为灰度(示例):
img = image.load_img('/kaggle/input/cassava-leaf-disease-classification/train_images/'+train['image_id'][i], target_size=(28,28,1), color_mode="grayscale")
我正在尝试使用自定义预处理函数在训练期间将 RGB 图像转换为灰度图像。因此,我尝试为此使用 tf.image.rbg_to_grayscale
。我的函数如下所示:
def prep_data(x):
x = tf.image.rgb_to_grayscale(x)
return x
datagen = ImageDataGenerator(preprocessing_function=prep_data,validation_split=0.15)
train_generator
是使用 datagen.flow_from_dataframe(...)
定义的。没有这个自定义函数的训练工作得很好,但是一旦我使用它,我就会收到以下错误:
ValueError: setting an array element with a sequence.
从这个答案 here 来看,我假设我需要将输入更改为 rgb_to_grayscale
,但我不知道将 x
传递给函数的正确方法是什么.
知道如何解决这个问题吗?
相反,您可以使用 flow_from_directory
的 color_mode
参数并将其设置为 'grayscale'
以将图像转换为灰度。来自 Keras docs:
color_mode: One of "grayscale", "rbg", "rgba". Default: "rgb". Whether the images will be converted to have 1, 3, or 4 channels.
将 RGB 图像更改为灰度(示例):
img = image.load_img('/kaggle/input/cassava-leaf-disease-classification/train_images/'+train['image_id'][i], target_size=(28,28,1), color_mode="grayscale")