how to input multiple images with flow_from_dataframe in keras?

  • Last Update :
  • Techknowledgy :

Instead what you can do is create two generators and more appropriately allow your network to have two inputs:

in1 = generator.flow_from_dataframe(dataframe, target_size = (64, 64, 1), x_col = 'image_1',
   y_col = 'similarity',
   class_mode = 'sparse', subset = 'training')

in2 = generator.flow_from_dataframe(dataframe, target_size = (64, 64, 1), x_col = 'image_2',
   y_col = 'similarity',
   class_mode = 'sparse', subset = 'training')

And then build a model using functional API that accepts two image inputs:

input_image1 = Input(shape = (64, 64, 1))
input_image2 = Input(shape = (64, 64, 1))
#...all other layers to create output_layer
model = Model([input_image1, input_image2], output)
#...

With the help of @nuric I was able to input multiple images. Here is full code for creating flow:

def get_flow_from_dataframe(generator, dataframe,
      image_shape = (64, 64),
      subset = 'training',
      color_mode = 'grayscale', batch_size = 64):
   train_generator_1 = generator.flow_from_dataframe(dataframe, target_size = image_shape,
      color_mode = color_mode,
      x_col = 'image_1',
      y_col = 'prediction',
      class_mode = 'binary',
      shuffle = True,
      batch_size = batch_size,
      seed = 7,
      subset = subset, drop_duplicates = False)

train_generator_2 = generator.flow_from_dataframe(dataframe, target_size = image_shape,
   color_mode = color_mode,
   x_col = 'image_2',
   y_col = 'prediction',
   class_mode = 'binary',
   shuffle = True,
   batch_size = batch_size,
   seed = 7,
   subset = subset, drop_duplicates = False)
while True:
   x_1 = train_generator_1.next()
x_2 = train_generator_2.next()

yield [x_1[0], x_2[0]], x_1[1]

Full code of fit_generator:

train_gen = get_flow_from_dataframe(generator, dataframe, image_shape = (64, 64),
   color_mode = 'rgb',
   batch_size = batch_size)
valid_gen = get_flow_from_dataframe(generator, dataframe, image_shape = (64, 64),
   color_mode = 'rgb',
   batch_size = batch_size,
   subset = 'validation')

model.fit_generator(train_gen, epochs = 50,
   steps_per_epoch = step_size,
   validation_data = valid_gen,
   validation_steps = step_size,
   callbacks = get_call_backs('../models/model_1.h5', monitor = 'val_acc'),
)

Suggestion : 2

I have been trying to create Siamese model for finding image similarity between 2 images (it has 2 input images). At the beginning I tested it with a small dataset, it fitted in my RAM and it worked kinda well. Now, I want to increase the training sample size and in order to do that I created images.csv file. In this file, I have 3 columns: image_1, image_2, similarity,image_1 and image_2 are absolute paths to images. similarity is either 0 or 1. I tried, 1 week ago 1 Answer. Assuming you already have resized and other preprocessing your image data into a multi-dimensional numpy array and split the data into training and test. To use the flow () method. You first want to create a generator using ImageDataGenerator (). The example below DOES NOT DO image augmentation. ,after removing image_2 and having x_col=image_1 error disappeared but it has only 1 input image. What should I do?


generator.flow_from_dataframe(dataframe, target_size = (64, 64, 1), x_col = ['image_1', 'image_2'], y_col = 'similarity', class_mode = 'sparse', subset = 'training')

def get_flow_from_dataframe(generator, dataframe, image_shape = (64, 64), subset = 'training', color_mode = 'grayscale', batch_size = 64): train_generator_1 = generator.flow_from_dataframe(dataframe, target_size = image_shape, color_mode = color_mode, x_col = 'image_1', y_col = 'prediction', class_mode = 'binary', shuffle = True, batch_size = batch_size, seed = 7, subset = subset, drop_duplicates = False) train_generator_2 = generator.flow_from_dataframe(dataframe, target_size = image_shape, color_mode = color_mode, x_col = 'image_2', y_col = 'prediction', class_mode = 'binary', shuffle = True, batch_size = batch_size, seed = 7, subset = subset, drop_duplicates = False) while True: x_1 = train_generator_1.next() x_2 = train_generator_2.next() yield [x_1[0], x_2[0]], x_1[1]
generator.flow_from_dataframe(dataframe, target_size = (64, 64, 1), x_col = ['image_1', 'image_2'], y_col = 'similarity', class_mode = 'sparse', subset = 'training')
in1 = generator.flow_from_dataframe(dataframe, target_size = (64, 64, 1), x_col = 'image_1', y_col = 'similarity', class_mode = 'sparse', subset = 'training') in2 = generator.flow_from_dataframe(dataframe, target_size = (64, 64, 1), x_col = 'image_2', y_col = 'similarity', class_mode = 'sparse', subset = 'training')
input_image1 = Input(shape = (64, 64, 1)) input_image2 = Input(shape = (64, 64, 1)) #...all other layers to create output_layer model = Model([input_image1, input_image2], output) #...
def get_flow_from_dataframe(generator, dataframe, image_shape = (64, 64), subset = 'training', color_mode = 'grayscale', batch_size = 64): train_generator_1 = generator.flow_from_dataframe(dataframe, target_size = image_shape, color_mode = color_mode, x_col = 'image_1', y_col = 'prediction', class_mode = 'binary', shuffle = True, batch_size = batch_size, seed = 7, subset = subset, drop_duplicates = False) train_generator_2 = generator.flow_from_dataframe(dataframe, target_size = image_shape, color_mode = color_mode, x_col = 'image_2', y_col = 'prediction', class_mode = 'binary', shuffle = True, batch_size = batch_size, seed = 7, subset = subset, drop_duplicates = False) while True: x_1 = train_generator_1.next() x_2 = train_generator_2.next() yield [x_1[0], x_2[0]], x_1[1]

Suggestion : 3

You can't flow two images from a single anycodings_keras generator using that method, it is anycodings_keras designed to handle one, from anycodings_keras documentation:,With the help of @nuric I was able to anycodings_keras input multiple images. Here is full code anycodings_keras for creating flow:,And then build a model using functional anycodings_keras API that accepts two image inputs:,x_col: string, column in dataframe that anycodings_keras contains the filenames (or absolute anycodings_keras paths if directory is None).

image_1 and image_2 are absolute paths to anycodings_python images. similarity is either 0 or 1. I anycodings_python tried

generator.flow_from_dataframe(dataframe, target_size = (64, 64, 1), x_col = ['image_1', 'image_2'],
   y_col = 'similarity',
   class_mode = 'sparse', subset = 'training')

Instead what you can do is create two anycodings_keras generators and more appropriately allow anycodings_keras your network to have two inputs:

in1 = generator.flow_from_dataframe(dataframe, target_size = (64, 64, 1), x_col = 'image_1',
   y_col = 'similarity',
   class_mode = 'sparse', subset = 'training')

in2 = generator.flow_from_dataframe(dataframe, target_size = (64, 64, 1), x_col = 'image_2',
   y_col = 'similarity',
   class_mode = 'sparse', subset = 'training')

And then build a model using functional anycodings_keras API that accepts two image inputs:

input_image1 = Input(shape = (64, 64, 1))
input_image2 = Input(shape = (64, 64, 1))
#...all other layers to create output_layer
model = Model([input_image1, input_image2], output)
#...

With the help of @nuric I was able to anycodings_keras input multiple images. Here is full code anycodings_keras for creating flow:

def get_flow_from_dataframe(generator, dataframe,
      image_shape = (64, 64),
      subset = 'training',
      color_mode = 'grayscale', batch_size = 64):
   train_generator_1 = generator.flow_from_dataframe(dataframe, target_size = image_shape,
      color_mode = color_mode,
      x_col = 'image_1',
      y_col = 'prediction',
      class_mode = 'binary',
      shuffle = True,
      batch_size = batch_size,
      seed = 7,
      subset = subset, drop_duplicates = False)

train_generator_2 = generator.flow_from_dataframe(dataframe, target_size = image_shape,
   color_mode = color_mode,
   x_col = 'image_2',
   y_col = 'prediction',
   class_mode = 'binary',
   shuffle = True,
   batch_size = batch_size,
   seed = 7,
   subset = subset, drop_duplicates = False)
while True:
   x_1 = train_generator_1.next()
x_2 = train_generator_2.next()

yield [x_1[0], x_2[0]], x_1[1]

Full code of fit_generator:

train_gen = get_flow_from_dataframe(generator, dataframe, image_shape = (64, 64),
   color_mode = 'rgb',
   batch_size = batch_size)
valid_gen = get_flow_from_dataframe(generator, dataframe, image_shape = (64, 64),
   color_mode = 'rgb',
   batch_size = batch_size,
   subset = 'validation')

model.fit_generator(train_gen, epochs = 50,
   steps_per_epoch = step_size,
   validation_data = valid_gen,
   validation_steps = step_size,
   callbacks = get_call_backs('../models/model_1.h5', monitor = 'val_acc'),
)