marouanebr
marouanebr

Reputation: 111

Augementations in Keras ImageDataGenerator

I have please two questions concerning the ImageDataGenerator:

1) Are the same augmentations used on the whole batch or each image gets its own random transformation? e.g. for rotation, does the module rotates all the images in the batch with same angle or each image get a random rotation angle ?

2) The data in ImageDataGenerator.flow is looped over (in batches) indefinitely. Is there a way to stop this infinite loop, i.e. doing the augmentation only for n number of time. Because I need to modify the batch_size in each step (not each epoch). Thanks

Upvotes: 2

Views: 2401

Answers (2)

marouanebr
marouanebr

Reputation: 111

@Neal: Thank you for the prompt answer! You were right, I probably need to better explain my task. My work is somehow similar to classifying video sequences, but my data is saved in a database. I want my code to follow this steps for one epoch:
For i in (number_of_sequences):

  1. Get N, the number of frames in the sequence i (I think that’s equivalent to batch_size, the number of N of each sequence is already saved in a list)
  2. Fetch N successive frames from my database and their labels: X_train, y_train
  3. For j in range(number_of_rotation): -
    1. Perform (the same) data Augmentation on all frames of the sequence (probably using datagen = ImageDataGenerator() and datagen.flow())
    2. Train the network on X, y

My first thought was using model.fit_generator(generator = ImageDataGenerator().flow()) but this way, I can not modify my batch_size, and honestly I did not understand your solution. Sorry for the long post, but I’m still a novice in both python and NN, but I’m really a big fan of Keras ;) Thnx!

Upvotes: 0

Neal
Neal

Reputation: 942

Answer from Francois Chollet:

1) Are the same augmentations used on the whole batch or each image gets its own random transformation? e.g. for rotation, does the module rotates all the images in the batch with same angle or each image get a random rotation angle ?

Every single sample has a different unique transformation (e.g. a random rotation within a certain range).

2) The data in ImageDataGenerator.flow is looped over (in batches) indefinitely. Is there a way to stop this infinite loop, i.e. doing the augmentation only for n number of time. Because I need to modify the batch_size in each step (not each epoch). Thanks

Unclear what is meant here. But if you are using model.fit_generator(ImageDataGenerator.flow()) then you can specify samples_per_epoch=... to only yield a specific number of samples from the generator. If you want batch-level granularity, you could do:

for x, y in model.fit_generator(ImageDataGenerator.flow()):
  model.train_on_batch(x, y)

In that case you can just break (it's a loop) after any number of batches that you want.

Upvotes: 1

Related Questions