python-3.x 加入椒盐和高斯噪声的Keras实时增强

xfb7svmp  于 2023-01-14  发布在  Python
关注(0)|答案(1)|浏览(410)

我有麻烦修改Keras的ImageDataGenerator在一个自定义的方式,使我可以执行说,盐和胡椒噪声和高斯模糊(他们不提供).我知道这种类型的问题已经问过很多次了,我已经阅读了几乎每一个可能的链接如下:
但由于我无法理解完整的源代码或缺乏python知识;我正在努力在ImageDataGenerator中将这两种额外类型的增强作为自定义的增强来实现。我非常希望有人能为我指出如何修改源代码的正确方向,或者任何其他方法。
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
SaltAndPepper噪声的示例如下,我希望将更多类型的增强添加到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
4uqofj5v

4uqofj5v1#

我想在我的代码中做类似的事情。我正在阅读文档here。参见参数preprocessing_function。您可以实现一个函数,然后将其传递给ImageDataGenerator的此参数。

相关问题