问题
I have a model which takes two Images as inputs and generates a single image as a Target output.
All of my training image-data is in the following sub-folders:
- input1
- input2
- target
Can I use the ImageDataGenerator
class and methods like flow_from_directory
and model.fit_generator
method in keras to train the network?
How can I do this? since most examples I have come across deal with single input and a label-based target output.
In my case, I have a non-categorical target output data and multiple inputs.
Please help, as your suggestions can be really helpful.
回答1:
One possibility is to join three ImageDataGenerator
into one, using class_mode=None
(so they don't return any target), and using shuffle=False
(important). Make sure you're using the same batch_size
for each and make sure each input is in a different dir, and the targets also in a different dir, and that there are exactly the same number of images in each directory.
idg1 = ImageDataGenerator(...choose params...)
idg2 = ImageDataGenerator(...choose params...)
idg3 = ImageDataGenerator(...choose params...)
gen1 = idg1.flow_from_directory('input1_dir',
shuffle=False,
class_mode=None)
gen2 = idg2.flow_from_directory('input2_dir',
shuffle=False,
class_mode=None)
gen3 = idg3.flow_from_directory('target_dir',
shuffle=False,
class_mode=None)
Create a custom generator:
class JoinedGen(tf.keras.utils.Sequence):
def __init__(self, input_gen1, input_gen2, target_gen):
self.gen1 = input_gen1
self.gen2 = input_gen2
self.gen3 = target_gen
assert len(input_gen1) == len(input_gen2) == len(target_gen)
def __len__(self):
return len(self.gen1)
def __getitem__(self, i):
x1 = self.gen1[i]
x2 = self.gen2[i]
y = self.gen3[i]
return [x1, x2], y
def on_epoch_end(self):
self.gen1.on_epoch_end()
self.gen2.on_epoch_end()
self.gen3.on_epoch_end()
Train with this generator:
my_gen = JoinedGen(gen1, gen2, gen3)
model.fit_generator(my_gen, ...)
Or create a custom generator. All the structure for it is shown above.
来源:https://stackoverflow.com/questions/59492866/keras-imagedatagenerator-for-multiple-inputs-and-image-based-target-output