0

The train_generator reads batches of RGB image data from disk using Keras flow_from_directory (example code below). But in my case, I have two directory of images such that I want to read a pair of images and stack them along the depth-axis to form a 6-channel image (i.e 2x R, 2x G, 2x B channels) before it goes to fit_generator.

So, my question is how to combine two RGB images along the depth axis to prepare 6-channel input data while using Keras flow_from_directory?

I'm following an example CNN code for classification from here:

https://gist.github.com/fchollet/0830affa1f7f19fd47b06d4cf89ed44d

train_generator = train_datagen.flow_from_directory(
    train_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='binary')
edkeveked
  • 17,989
  • 10
  • 55
  • 93

1 Answers1

0

flow_from_directory returns an iterator. Using map, one can concatenate the output of two iterators. (code not tested)

train_generator1 = train_datagen.flow_from_directory(
    train_data_dir1,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='binary')

train_generator2 = train_datagen.flow_from_directory(
    train_data_dir2,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='binary')

map(lambda x1, y1, x2, y2: tf.concat([x1,x2], axis=-1), tf.concat([y1,y2], axis=-1), train_generator1, train_generator2) 
edkeveked
  • 17,989
  • 10
  • 55
  • 93