【问题标题】:Augmenting both X and Y images with Keras使用 Keras 增强 X 和 Y 图像
【发布时间】:2023-03-28 19:21:01
【问题描述】:

我知道如何使用 ImageDataGenerator 通过平移、翻转、旋转、剪切等来增强我的数据。问题是假设我有一个训练图像和相应的分割图像,我想增强这两者这些图像。例如,如果我将训练图像旋转 45 度,那么我还想将分割图像增加 45 度。本质上,我想对两个数据集执行相同的转换集。这可能与 ImageDataGenerator 有关,还是我必须从头开始编写所有增强功能?提前非常感谢。

【问题讨论】:

    标签: keras


    【解决方案1】:

    您可以在tf.data.Dataset.map 中使用增强并返回图像两次。我不知道有什么方法可以用ImageDataGenerator 做到这一点。

    import tensorflow as tf
    import matplotlib.pyplot as plt
    from skimage import data
    
    cats = tf.concat([data.chelsea()[None, ...] for i in range(24)], axis=0)
    
    test = tf.data.Dataset.from_tensor_slices(cats)
    
    
    def augment(image):
        image = tf.cast(x=image, dtype=tf.float32)
        image = tf.divide(x=image, y=tf.constant(255.))
        image = tf.image.random_hue(image=image, max_delta=5e-1)
        image = tf.image.random_brightness(image=image, max_delta=2e-1)
        return image, image
    
    
    test = test.batch(1).map(augment)
    
    fig = plt.figure()
    plt.subplots_adjust(wspace=.1, hspace=.2)
    images = next(iter(test.take(1)))
    for index, image in enumerate(images):
        ax = plt.subplot(1, 2, index + 1)
        ax.set_xticks([])
        ax.set_yticks([])
        ax.imshow(tf.clip_by_value(tf.squeeze(image), clip_value_min=0, clip_value_max=1))
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2018-02-24
      • 1970-01-01
      • 1970-01-01
      • 2019-11-25
      • 2021-09-30
      • 2020-03-09
      • 1970-01-01
      • 2022-08-21
      • 2021-10-05
      相关资源
      最近更新 更多