Keras seeding ImageDataGenerator versus Sequence

Viewed 2912

I'm currently using tensorflow.keras.preprocessing.image.ImageDataGenerator and flow_from_directory. For example:

from tensorflow.keras.preprocessing.image import ImageDataGenerator

 train_datagen = ImageDataGenerator(rotation_range=20,
                                    width_shift_range=0.1,
                                    height_shift_range=0.1,
                                    shear_range=0.2, 
                                    zoom_range=0.2, 
                                    fill_mode='nearest', 
                                    horizontal_flip=True,
                                    rescale=1/255.0, 
                                    preprocessing_function=preprocessing_function, 
                                    data_format='channels_last')

train_generator = train_datagen.flow_from_directory(
    directory=env.channel_dirs['train'],
    target_size=(train_size, train_size),
    color_mode="rgb",
    batch_size=batch_size,
    class_mode="categorical",
    shuffle=True,
    interpolation='bilinear',
    seed=42)

I found that even when setting a seed in both numpy and TensorFlow, the batch order is not static so I don't get reproducible results. I saw this post that recommends using a keras Sequence. However, it only has a small example for this.

Is it possible to make ImageDataGenerator batch order reproducible? Alternatively, does anyone have an example they could share of how I could use Sequence but retain the flow_from_directory along with using ImageDataGenerator's augmentation options? If an example is asking too much, summarizing how to go about this would also be greatly appreciated!

1 Answers

Here is a nice-script, by which you can control the inner process too. It means, you can define your own way of generating data in each iteration, along with flowing data from directory.

Related