[英]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.