Keras实时增强添加了SaltandPiper和高斯噪声



我在以自定义方式修改Keras的ImageDataGenerator时遇到了问题,这样我就可以执行SaltAndPepper Noise和Gaussian Blur(它们不提供(。我知道这种类型的问题以前被问过很多次,我几乎读过下面的每一个链接:

但由于我无法理解完整的源代码或缺乏python知识;我正在努力将ImageDataGenerator中的这两种额外类型的增强实现为自定义类型。我非常希望有人能为我指明正确的方向,告诉我如何修改源代码,或者其他任何方式。

使用Keras模型的生成器。fit_generator

具有成品的自定义Keras数据生成器

Keras实时增强添加噪声和对比度

数据增强图像数据生成器Keras语义分割

https://stanford.edu/~shervine/blog/keras如何生成飞行中的数据

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

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

我编辑我的答案,向你展示一个实用的例子:

def my_func(img):
return img/255
train_datagen = ImageDataGenerator(preprocessing_function =my_func) 

在这里,我只是实现了一个简短的功能,可以重新缩放你的数据,但你可以实现噪音等

最新更新