【问题标题】:Keras custom data generator from numpy array来自 numpy 数组的 Keras 自定义数据生成器
【发布时间】:2020-10-02 01:49:28
【问题描述】:

我有两个分别包含 image and label 数据的 numpy 变量。有500 labeled image,每个image is 240 x 240.的形状

import numpy as np
images = np.random.randint(4, size=(500,240,240))
labels =  np.random.rand(500,240,240)

如何使用 Keras 生成器进行模型训练?提前感谢您的帮助。

【问题讨论】:

    标签: python python-3.x tensorflow keras


    【解决方案1】:

    如果您愿意对您的图片做一些小改动,您可以轻松地做到这一点。基本上你需要在images(通道维度)上再增加一个维度。

    import numpy as np
    import tensorflow as tf
    
    images = np.expand_dims(np.random.randint(4, size=(500,240,240)),-1)
    labels =  np.random.rand(500,240,240)
    
    gen = tf.keras.preprocessing.image.ImageDataGenerator()
    res = gen.flow(images, labels)
    x, y = next(res)
    
    

    您可以通过创建另一个生成器生成 Keras 生成器的数据并删除该维度来发布处理并删除该维度。

    【讨论】:

      猜你喜欢
      • 2020-12-10
      • 2019-03-16
      • 2018-10-23
      • 2019-09-28
      • 2017-06-01
      • 2019-11-10
      • 1970-01-01
      • 2020-04-07
      相关资源
      最近更新 更多