将 AdditiveGaussianNoise 包含到定制的 ImageDataGenerator(序列)

Include AdditiveGaussianNoise to customized ImageDataGenerator(sequence)

我为图像数据构建了以下DataGenerator

class DataGenerator(Sequence):

    def __init__(self, x_set, y_set, batch_size):
        self.x, self.y = x_set, y_set
        self.batch_size = batch_size

    def __len__(self):
        return math.ceil(len(self.x) / self.batch_size)

    def __getitem__(self, idx):
        batch_x = self.x[idx*self.batch_size : (idx + 1)*self.batch_size]
        batch_x = np.array([resize(imread(file_name), (224, 224)) for file_name in batch_x])
        batch_x = batch_x * 1./255
        batch_y = self.y[idx*self.batch_size : (idx + 1)*self.batch_size]
        batch_y = np.array(batch_y)

        return batch_x, batch_y

x_set 是我的输入数据(图像;该图像的文件路径)和 y_set 输出数据(标签)。我现在想添加 ImageAugmentation,我想使用 AdditiveGaussianNoise (https://imgaug.readthedocs.io/en/latest/source/overview/arithmetic.html#additivegaussiannoise) 将高斯噪声添加到输入数据中。

import imgaug.augmenters as iaa
aug = iaa.AdditiveGaussianNoise(scale=(0, 0.2*255))

如何将其包含到我的 DataGenerator 中并将生成器扩展 ImageAugmentation

非常感谢!

那么 batch_x 是一个包含图像的 np.array 吗?也许我误解了什么,但它似乎与 documentation:

中的几乎相同

已编辑

def __getitem__(self, idx):
        batch_x = self.x[idx*self.batch_size : (idx + 1)*self.batch_size]
        batch_x = np.array([resize(imread(file_name), (224, 224)) for file_name in batch_x])

        # cast to integer for imgaug
        batch_x = batch_x.astype(np.uint)

        # apply augmentation
        aug = iaa.AdditiveGaussianNoise(scale=(0, 0.2*255))
        batch_x = aug(images=batch_x)

        batch_x = batch_x * 1./255

        batch_y = self.y[idx*self.batch_size : (idx + 1)*self.batch_size]
        batch_y = np.array(batch_y)

        return batch_x, batch_y