【问题标题】:Best way to process terabytes of data on gcloud ml-engine with keras使用 keras 在 gcloud ml-engine 上处理 TB 级数据的最佳方法
【发布时间】:2019-02-04 20:24:29
【问题描述】:

我想在 gcloud 存储上大约 2TB 的图像数据上训练一个模型。我将图像数据保存为单独的 tfrecord,并尝试按照此示例使用 tensorflow 数据 api

https://medium.com/@moritzkrger/speeding-up-keras-with-tfrecord-datasets-5464f9836c36

但似乎 keras 的 model.fit(...) 不支持基于 tfrecord 数据集的验证

https://github.com/keras-team/keras/pull/8388

有没有更好的方法来处理我缺少的来自 ml-engine 的 keras 的大量数据?

非常感谢!

【问题讨论】:

    标签: tensorflow keras google-cloud-ml tensorflow-datasets tfrecord


    【解决方案1】:

    如果您愿意使用 tf.keras 而不是实际的 Keras,您可以使用 tf.data API 实例化 TFRecordDataset 并将其直接传递给 model.fit()奖励:您可以直接从 Google Cloud 存储流式传输,无需先下载数据

    # Construct a TFRecordDataset
    ds_train tf.data.TFRecordDataset('gs://') # path to TFRecords on GCS
    ds_train = ds_train.shuffle(1000).batch(32)
    
    model.fit(ds_train)
    

    要包含验证数据,请使用您的验证 TFRecords 创建一个TFRecordDataset,并将其传递给model.fit()validation_data 参数。注意:这是可能的as of TensorFlow 1.9

    最后一点:您需要指定 steps_per_epoch 参数。我用来知道所有 TFRecordfiles 中示例总数的一个技巧是简单地遍历文件并计数:

    import tensorflow as tf
    
    def n_records(record_list):
        """Get the total number of records in a collection of TFRecords.
        Since a TFRecord file is intended to act as a stream of data,
        this needs to be done naively by iterating over the file and counting.
        See https://stackoverflow.com/questions/40472139
    
        Args:
            record_list (list): list of GCS paths to TFRecords files
        """
        counter = 0
        for f in record_list:
            counter +=\
                sum(1 for _ in tf.python_io.tf_record_iterator(f))
        return counter 
    

    你可以用它来计算steps_per_epoch

    n_train = n_records([gs://path-to-tfrecords/record1,
                         gs://path-to-tfrecords/record2])
    
    steps_per_epoch = n_train // batch_size
    

    【讨论】:

    • 听起来不错,有没有办法在 model.fit 中包含验证数据?
    • 另一个问题,我运行了我的模型,但我的模型的输入形状有错误。数据集有一个形状为 (?, 224,224,1) 的图像,一个热标签 (?,2) 是一个数组,因此数据集的形状似乎是 [(?, 224,224,1), (?,2)] .我在我的 keras 模型中输入什么形状? x = Input( (224,224,1) )
    • 哦,消息是:检查模型输入时出错:您传递给模型的 Numpy 数组列表不是模型预期的大小。预计会看到 1 个数组,但得到了以下 2 个数组的列表: [, ]...
    • 抱歉,这是因为我传递的是迭代器而不是数据集。
    • 成功了!我不得不将我的 tensorflow 版本升级到 1.12。
    猜你喜欢
    • 2020-10-29
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 2018-02-23
    • 2013-04-17
    • 2019-04-20
    • 1970-01-01
    相关资源
    最近更新 更多