将多个ImageDataGenerator的输出合并到一个张量

时间:2019-09-11 17:47:29

标签: python tensorflow keras

假设我要在两个preprocessing_functions中将两个ImageDataGenerator应用于图像,然后将这些输出连接到一个张量以训练模型。

即,如果我有RGB图像,我的模型将在输入张量中期望有6个通道,因为我想串联两个发生器的输出(每个发生器有3个通道)。

具体:

# first preproc function
def preproc_function_1(image):
    image = np.array(image)
    return image * 0.1

# second preproc function
def preproc_function_2(image):
    image = np.array(image)
    return image * 0.2

# datagen_1 - that applies preproc_function_1 
train_datagen_1 = ImageDataGenerator(
     preprocessing_function=preproc_function_1)

# datagen_2 - that applies preproc_function_2
train_datagen_2 = ImageDataGenerator(
     preprocessing_function=preproc_function_2)

如果我假设有这样的并行ImageDataGenerator,我们也要使用两个验证生成器

 validation_datagen_1 = ImageDataGenerator()

 validation_datagen_2 = ImageDataGenerator()

问题:

如何继续使用model.fit_generator()

我认为我必须使用<generator>.flow_from_directory()(可以从flow_from_directory读取我的数据)来在model.fit_generator()中提供generator来训练模型。

我需要创建两个<generator>.flow_from_directory()对象吗?我在这里迷路了。

非常感谢您的帮助

1 个答案:

答案 0 :(得分:2)

仅创建一个预处理功能会更容易:

def preproc(image):
     return np.concatenate([
                            preproc_function_1(image), 
                            preproc_function_2(image)
                           ], axis=-1) #or axis=1 if channels_first

选项2-使用两个发电机

由于您遇到了6通道问题,因此此自定义生成器应该可以工作。但是您将没有“预处理”,而将具有“后处理”:

class Processor(keras.utils.Sequence):
    def __init__(self, keras_generator)
        self.keras_generator = keras_generator

    def __len__(self):
        return len(self.keras_generator(

    def __getitem__(self, i):
        batch_x, batch_y = self.keras_generator[i]

        #do batch processing here

        return processed_batch_x, batch_y

    def on_epoch_end(self):
        self.keras_generator.on_epoch_end()

custom_generator = Processor(validation_datagen.flow_from_directory(...))