如何在 keras 中将多堆预处理函数添加到数据生成器中?

  • 本文关键字:数据 添加 处理函数 keras keras
  • 更新时间 :
  • 英文 :


我有一个带有预处理函数的训练数据生成器(可以是InceptionV3,ResNet50等的预处理输入(,如下所示:

train_datagen = ImageDataGenerator(
preprocessing_function = preprocess_input,
rotation_range=30,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0,
brightness_range= [0.5,1.0],
zoom_range=0.1,
channel_shift_range=10,
vertical_flip=True,
horizontal_flip=True,
fill_mode='nearest')

现在我想添加另一个预处理函数:

get_random_eraser(v_l=0, v_h=1, pixel_level=False)

总而言之,它将链接模型的预处理函数(因为它是在 keras 中预训练的(并实现另一个预处理函数。我该怎么做?(preprocessing_function无法获得列表 [f1、f2],并且我无法在新行中重复preprocessing_function声明(有道理,但无论如何都尝试过((

您需要使用自定义函数创建一个新的生成器:

def custom_function(input_image):
input_image = preprocess_input(input_image)
return get_random_eraser(v_l=0, v_h=1, pixel_level=False)(input_image)
new_train_gen = train_datagen = ImageDataGenerator(
preprocessing_function = custom_function,
rotation_range=30,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0,
brightness_range= [0.5,1.0],
zoom_range=0.1,
channel_shift_range=10,
vertical_flip=True,
horizontal_flip=True,
fill_mode='nearest')

在第一次尝试时在custom_function中添加print(input_image.shape)可能会很有趣,以确保input_image是单个图像还是批处理。您可能需要相应地调整您的内部功能。

最新更新