使用 keras 在 gcloud ml-engine 上处理 TB 数据的最佳方法

ave*_*evu 2 keras tensorflow google-cloud-ml tfrecord tensorflow-datasets

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

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

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

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

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

非常感谢!

sdc*_*cbr 5

如果您愿意使用tf.keras而不是实际的 Keras,您可以TFRecordDataset使用tf.dataAPI实例化 a并将其直接传递给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)
Run Code Online (Sandbox Code Playgroud)

要包含验证数据,请TFRecordDataset使用您的验证 TFRecords创建一个并将其传递给 的validation_data参数model.fit()。注意:从 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 /sf/ask/2833049761/

    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 
Run Code Online (Sandbox Code Playgroud)

您可以使用它来计算steps_per_epoch

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

steps_per_epoch = n_train // batch_size
Run Code Online (Sandbox Code Playgroud)