Keras seed does not augment images and masks the same

Viewed 218

I'm trying to transform my images and mask labels in sync, but the random augmentations are applied differently to both generators (proven when I save both images to a directory).

I've tried

  • seeding all generators in my library stack
  • disabling shuffle and multiprocessing
  • ensuring augmentation options are identical between images and masks

One thing that did work was setting batch size to 1, but then my training performance suffered.

seed_val=0

data_gen_args = dict(
    rescale=1./255,
    horizontal_flip=True,
    validation_split=validation_split)

image_datagen = ImageDataGenerator(**data_gen_args)
mask_datagen = ImageDataGenerator(**data_gen_args)

image_generator = image_datagen.flow_from_directory(
    'data/x/train_images/',
    target_size=(224, 224),
    color_mode='rgb',
    class_mode=None,
    batch_size=batch_size,
    subset='training',
    save_to_dir='tmp/img/',
    seed=seed_val)

mask_generator = mask_datagen.flow_from_directory(
    'data/x/train_annotations/',
    target_size=(224, 224),
    color_mode='grayscale',
    class_mode=None,
    batch_size=batch_size,
    subset='training',
    save_to_dir='tmp/mask/',
    seed=seed_val)

train_generator = zip((image_generator), (mask_generator))
2 Answers

try setting shuffle to False to both image and mask

image_generator = image_datagen.flow_from_directory(
    'data/x/train_images/',
    target_size=(224, 224),
    shuffle = False,
    color_mode='rgb',
    class_mode=None,
    batch_size=batch_size,
    subset='training',
    save_to_dir='tmp/img/',
    seed=seed_val)

mask_generator = mask_datagen.flow_from_directory(
    'data/x/train_annotations/',
    target_size=(224, 224),
    shuffle = False,
    color_mode='grayscale',
    class_mode=None,
    batch_size=batch_size,
    subset='training',
    save_to_dir='tmp/mask/',
    seed=seed_val)

I had the problem that my augmentations for the images and masks weren't in the same order, e.g.

img: pan, increase brightness, transform, rgbshift
mask: pan, transform

With that, "increase brightness" changes the value from the seed. Should be

img: pan, transform, increase brightness, rgbshift 
mask: pan, transform
Related