Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

[BUG] Seed should be passed to image_data_generator.get_random_transform #297

Open
Bienqq opened this issue May 31, 2020 · 5 comments
Open
Labels
image Related to images

Comments

@Bienqq
Copy link

Bienqq commented May 31, 2020

Seed should be passed to this function. In situation when we want to perform the same agumentation for two Image generators this leads to agumentation inconsistency

It should be like this:
params = self.image_data_generator.get_random_transform(x.shape, self.seed)

Instead of
params = self.image_data_generator.get_random_transform(x.shape)

@Bienqq Bienqq added the image Related to images label May 31, 2020
@Bienqq Bienqq changed the title Seed should be passed to image_data_generator.get_random_transform [BUG] Seed should be passed to image_data_generator.get_random_transform May 31, 2020
@Dref360
Copy link
Contributor

Dref360 commented May 31, 2020

Hello!

I see that we set the seed here: https://github.com/keras-team/keras-preprocessing/blob/master/keras_preprocessing/image/iterator.py#L58

COuld you share an example reproducing your issue?

@Bienqq
Copy link
Author

Bienqq commented May 31, 2020

I use U-Net to perform image segmentation.
I want provide the same seed for image_datagen and mask_datagen to ensure the transformation for image and mask is the same.

def generator(batch_size, train_path, image_folder, mask_folder, aug_dict, image_color_mode="grayscale",
              mask_color_mode="grayscale", image_save_prefix="image", mask_save_prefix="mask", save_to_dir=None,
              target_size=(256, 256), seed=1):

    image_datagen = ImageDataGenerator(**aug_dict)
    mask_datagen = ImageDataGenerator(**aug_dict)

    image_generator = image_datagen.flow_from_directory(
        train_path,
        classes=[image_folder],
        class_mode=None,
        color_mode=image_color_mode,
        target_size=target_size,
        batch_size=batch_size,
        save_to_dir=save_to_dir,
        save_prefix=image_save_prefix,
        seed=seed)

    mask_generator = mask_datagen.flow_from_directory(
        train_path,
        classes=[mask_folder],
        class_mode=None,
        color_mode=mask_color_mode,
        target_size=target_size,
        batch_size=batch_size,
        save_to_dir=save_to_dir,
        save_prefix=mask_save_prefix,
        seed=seed)

    train_generator = zip(image_generator, mask_generator)
    for (img, mask) in train_generator:
        img, mask = adjustData(img, mask)
        yield (img, mask)

@Dref360
Copy link
Contributor

Dref360 commented Jun 1, 2020

Hello,
I tried to reproduce your issue with the following script. I see no issue with it. Can you provide a self-contained end-to-end example?

import os

import numpy as np
from PIL import Image
from keras.preprocessing.image import ImageDataGenerator

pjoin = os.path.join

cls1_pt = '/tmp/cls1/cls1'
cls2_pt = '/tmp/cls1/cls2'
os.makedirs(cls1_pt, exist_ok=True)
os.makedirs(cls2_pt, exist_ok=True)

for i in range(100):
    img = Image.fromarray((np.ones([100, 100, 3]) * i % 255).astype(np.uint8))
    img.save(pjoin(cls1_pt, '{}.png'.format(i)))
    img.save(pjoin(cls2_pt, '{}.png'.format(i)))

flow1 = ImageDataGenerator(rescale=1 / 255.,
                           rotation_range=10, horizontal_flip=True).flow_from_directory('/tmp/cls1',
                                                                                        class_mode=None,
                                                                                        shuffle=True,
                                                                                        seed=123)
flow2 = ImageDataGenerator(rescale=1 / 255., rotation_range=10,
                           horizontal_flip=True).flow_from_directory('/tmp/cls1', class_mode=None,
                                                                     shuffle=True, seed=123)

gen = zip(flow1, flow2)
for _ in range(10):
    im1, im2 = next(gen)
    assert np.allclose(im1, im2)

@Bienqq
Copy link
Author

Bienqq commented Jun 2, 2020

Your snipped looks and works fine. But I don't know why I am receiving inconsistently transformed images when comparing saved agumented images from both generators

@Keragen
Copy link

Keragen commented Feb 9, 2021

I had the same issue using Keras for R and an older version of TF. I just found out that using brightness_range in the image_data_generator was causing the problem. using a preprocessing_function and image_modulate from magick to deal with the brightness solved the problem for me.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
image Related to images
Projects
None yet
Development

No branches or pull requests

3 participants