I want to create a model using the Functional Keras API that will have two inputs and two outputs. 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).
The model also is using two lambda layers to append the images procurred by the generators to a list for further inspection.
My question is how to train such a 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)
Use it as:
training_generator = JoinedGenerator(train_generator1, train_generator2)
And you forgot to define your model:
model = Model([inputs1, inputs2], [outputs1, outputs2])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.