2

I am having trouble with modifying Keras' ImageDataGenerator in a custom way such that I can perform say, SaltAndPepper Noise and Gaussian Blur (which they do not offer). I know this type of question has been asked many times before, and I have read almost every link possible below:

But due to my inability to understand the full source code or the lack thereof of python knowledge; I am struggling to implement these two additional types of augmentation in ImageDataGenerator as a custom one. I very much wish someone could point me in the right direction on how to modify the source code, or any other way.

Use a generator for Keras model.fit_generator

Custom Keras Data Generator with yield

Keras Realtime Augmentation adding Noise and Contrast

Data Augmentation Image Data Generator Keras Semantic Segmentation

https://stanford.edu/~shervine/blog/keras-how-to-generate-data-on-the-fly

https://github.com/keras-team/keras/issues/3338

https://towardsdatascience.com/image-augmentation-14a0aafd0498

https://towardsdatascience.com/image-augmentation-for-deep-learning-using-keras-and-histogram-equalization-9329f6ae5085

An example of SaltAndPepper noise is as follows and I wish to add more types of augmentations into ImageDataGenerator:

class SaltAndPepperNoise:
    def __init__(self, replace_probs=0.1, pepper=0, salt=255, noise_type="RGB"):
        """
        It is important to know that the replace_probs here is the
        Probability of replacing a "pixel" to salt and pepper noise.
        """

        self.replace_probs = replace_probs
        self.pepper = pepper
        self.salt = salt
        self.noise_type = noise_type


    def get_aug(self, img, bboxes):
        if self.noise_type == "SnP":
            random_matrix = np.random.rand(img.shape[0], img.shape[1])
            img[random_matrix >= (1 - self.replace_probs)] = self.salt
            img[random_matrix <= self.replace_probs] = self.pepper
        elif self.noise_type == "RGB":
            random_matrix = np.random.rand(img.shape[0], img.shape[1], img.shape[2])
            img[random_matrix >= (1 - self.replace_probs)] = self.salt
            img[random_matrix <= self.replace_probs] = self.pepper
        return img, bboxes
ilovewt
  • 911
  • 2
  • 10
  • 18

1 Answers1

1

I want to do a similar thing in my code. I am reading the documentation here. See the parameter preprocessing_function. You can implement a function and then you can pass it to this parameter to ImageDataGenerator.

I edit my answer to show you a practical example:

def my_func(img):
        return img/255

train_datagen = ImageDataGenerator(preprocessing_function =my_func) 

                             

Here I just implement a short function that rescales your data, but you can implement noises and so on.

Jonny_92
  • 42
  • 9