我想使用 gcloud 存储上约 2TB 的图像数据训练模型。我将图像数据保存为单独的 tfrecords 并尝试按照此示例使用张量流数据 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 处理大量数据?
多谢!
如果你愿意使用tf.keras
您可以实例化一个而不是实际的 KerasTFRecordDataset
与tf.data
API 并将其直接传递给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)
要包含验证数据,请创建TFRecordDataset
与您的验证 TFRecords 并将其传递给validation_data
的论证model.fit()
。注意:这是可能的从 TensorFlow 1.9 开始.
最后注意:您需要指定steps_per_epoch
争论。我用来了解所有 TFRecord 文件中示例总数的方法是简单地迭代文件并进行计数:
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)