简体   繁体   English

如何使用具有两个输入和两个输出并使用两个 ImageDataGenerator 方法 (flow_from_directory) 的功能 API 训练 Keras model

[英]How to train a Keras model using Functional API which has two inputs and two outputs and uses two ImageDataGenerator methods (flow_from_directory)

I want to create a model using the Functional Keras API that will have two inputs and two outputs.我想使用具有两个输入和两个输出的功能 Keras API 创建一个 model。 The model will be using two instances of the ImageDataGenerator.flow_from_directory() method to get images from two different directories (inputs1 and inputs2 respectively). model 将使用ImageDataGenerator.flow_from_directory()方法的两个实例从两个不同的目录(分别为输入 1 和输入 2)获取图像。

The model also is using two lambda layers to append the images procurred by the generators to a list for further inspection. model 还使用两个 lambda 层将生成器生成的图像添加到列表中以供进一步检查。

My question is how to train such a model.我的问题是如何训练这样的 model。 Here is some toy code:这是一些玩具代码:

# Define our example directories and files
train_dir1 ='...\\cats_v_dogs_sample_training1'

train_dir2 = '...\\cats_v_dogs_sample_training2'

# Add our data-augmentation parameters to ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255.,
                                   rotation_range = 40,
                                   width_shift_range = 0.2,
                                   height_shift_range = 0.2,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)

# Flow training images in batches of 1 using train_datagen generator: inputs1
train_generator1 = train_datagen.flow_from_directory(train_dir1,
                                                    batch_size = 1,
                                                    class_mode = 'binary', 
                                                    target_size = (150, 150), shuffle = False)     

# Flow training images in batches of 1 using train_datagen generator: inputs2
train_generator2 = train_datagen.flow_from_directory(train_dir2,
                                                    batch_size = 1,
                                                    class_mode = 'binary', 
                                                    target_size = (150, 150), shuffle = False)     

imgs1 = []
imgs2 = []

def f_lambda1(x):

    imgs1.append(x)

    return(x)



def f_lambda2(x):

    imgs2.append(x)

    return(x)



# This returns a tensor
inputs1 = Input(shape=(150, 150, 3))
inputs2 = Input(shape=(150, 150, 3))

l1 = Lambda(f_lambda1, name = 'lambda1')(inputs1)
l2 = Lambda(f_lambda2 , name = 'lambda2')(inputs2)

x1 = Flatten()(inputs1)

x1 = Dense(1024, activation='relu')(x1)

x1 = Dropout(0.2)(x1)  

outputs1 = Dense(1, activation='sigmoid')(x1)    


x2 = Flatten()(inputs1)

x2 = Dense(1024, activation='relu')(x2)

x2 = Dropout(0.2)(x2)  

outputs2 = Dense(1, activation='sigmoid')(x2)    

model.compile()

# Train model on dataset -- The problem is that I have two not one training_generator, so the code below will not work

model.fit_generator(generator=training_generator,
                    validation_data=validation_generator,
                    use_multiprocessing=True,
                    workers=6)

Create a joined generator.创建一个连接的生成器。

In this example, both train generators must have the same length:在此示例中,两个火车生成器必须具有相同的长度:

class JoinedGenerator(keras.utils.Sequence):
    def __init__(self, generator1, generator2)
        self.generator1 = generator1
        self.generator2 = generator2 

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

    def __getitem__(self, i):
        x1, y1 = self.generator1[i]
        x2, y2 = self.generator2[i]
        return [x1, x2], [y1, y2]

    def on_epoch_end(self):
        self.generator1.on_epoch_end()
        self.generator2.on_epoch_end()

Be careful: you will probably need shuffle=False in the two generators so your data don't get mixed (unless that is not a problem)请注意:您可能需要shuffle=False在两个生成器中,这样您的数据就不会混合(除非这不是问题)

Use it as:将其用作:

training_generator = JoinedGenerator(train_generator1, train_generator2)

And you forgot to define your model:你忘了定义你的 model:

model = Model([inputs1, inputs2], [outputs1, outputs2])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Keras ImageDataGenerator 方法 flow_from_directory - Keras ImageDataGenerator method flow_from_directory Keras:如何在使用带有 flow_from_dataframe / flow_from_directory 的 ImageDataGenerator 时禁用调整图像大小? - Keras: how to disable resizing of images when using an ImageDataGenerator with flow_from_dataframe / flow_from_directory? 如何在 keras 中使用 ImageDataGenerator 和 flow_from_directory 保存增强图像 - How to save augmented images using ImageDataGenerator and flow_from_directory in keras Keras ImageDataGenerator和flow_from_directory class_mode ='input' - Keras ImageDataGenerator and flow_from_directory class_mode='input' 使用 ImageDataGenerator 和 flow_from_directory 时,Keras 中的数据增强是否应用于验证集 - Is data augmentation in Keras applied to the validation set when using ImageDataGenerator and flow_from_directory 我可以使用Keras中的ImageDataGenerator()和flow_from_directory()生成uint8标签吗? - Can I generate uint8 label using ImageDataGenerator() and flow_from_directory() in Keras? 我可以在 MacOS 上使用 Keras ImageDataGenerator().flow_from_directory 跳过文件吗? - Can I skip files using Keras ImageDataGenerator().flow_from_directory on MacOS? 如何在Keras中获取由ImageDataGenerator的.flow_from_directory函数扫描的类的名称? - How do I fetch the names of the classes scanned by .flow_from_directory function of ImageDataGenerator in Keras? 使用Keras的flow_from_directory和FCNN - using Keras' flow_from_directory with FCNN Keras:如何使用 flow_from_directory 提供自定义标签? - Keras: How to provide custom labels using flow_from_directory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM