【问题标题】:keras Data Augmentationkeras 数据增强
【发布时间】:2020-09-17 09:49:52
【问题描述】:

我知道ImageDataGenerator 会为每个输入图像生成一个随机增强的图像。现在,我想为每个输入图像生成两个增强图像:

datagen = tf.keras.preprocessing.image.ImageDataGenerator(
        rotation_range=40,
        width_shift_range=0.2,
        height_shift_range=0.2,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True,
        fill_mode='nearest')
train_ds = datagen.flow_from_directory('/home/train/')

为了解释更多,我想在同一张图像上应用 2 个不同的增强函数,即,如果我们对 5 个图像进行采样,我们最终会在批处理中得到 2 × 5 = 10 个增强观察

请问我该如何继续?

【问题讨论】:

    标签: keras deep-learning data-augmentation


    【解决方案1】:

    我建议创建一个继承自 tf.keras.utils.Sequence 的自定义数据生成器。有很多方法可以解决这个问题,但这应该符合您正在寻找的内容:

    class double_aug_generator(tf.keras.utils.Sequence):
        def __init__(self, x, y, batch_size, aug_params1, aug_params2):
            self.x, self.y = x, y
            self.batch_size = batch_size
            self.datagen = tf.keras.preprocessing.image.ImageDataGenerator(**aug_params1)
            
            // dictionary of parameters for the second augmentation
            self.aug_params2 = aug_params2
    
        def __len__(self):
            return math.ceil(len(self.x) / self.batch_size)
        
        def load(self, file_names):
            // load and return raw images however you like
    
        def __getitem__(self, idx):
            batch_x = self.x[idx * self.batch_size:(idx + 1) *
            self.batch_size]
            batch_y = self.y[idx * self.batch_size:(idx + 1) *
            self.batch_size]
            
            // load images
            batch_x = self.load(batch_x)
            
            // apply first augmentation
            batch_x = self.datagen.flow(batch_x)
            
            // apply second
            batch_x = self.datagen.apply_transform(batch_x, self.aug_params2)
            
            return batch_x, np.array(batch_y)
    

    【讨论】:

      猜你喜欢
      • 2017-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-05
      • 2019-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多