加速TFRecords在CloudML for GPU上加入Keras模型

GRS*_*GRS 6 python keras tensorflow google-cloud-ml

我想以超快的速度将TFRecords输入我的模型.但是,目前,我的GPU(GCP上的单K80)负载为0%,这在CloudML上速度非常慢.

我在GCS中有TFRecords train_directory = gs://bucket/train/*.tfrecord:(大约100个文件,大小为30mb-800mb),但由于某种原因,它很难将数据快速地输入到我的模型中以便GPU.

有趣的是,将数据加载到内存中并使用numpy数组使用fit_generator()速度提高了7倍.在那里,我可以指定多处理和多工人.

我当前的设置解析tf记录并加载无限tf.Dataset.理想情况下,解决方案可以在内存中保存/完善一些批次,以便按需使用gpu.

def _parse_func(record):
    """ Parses TF Record"""
    keys_to_features = {}
    for _ in feature_list: # 300 features ['height', 'weights', 'salary']
         keys_to_features[_] = tf.FixedLenFeature([TIME_STEPS], tf.float32)
    parsed = tf.parse_single_example(record, keys_to_features)
    t = [tf.manip.reshape(parsed[_], [-1, 1]) for _ in feature_list]
    numeric_tensor = tf.concat(values=t, axis=1)

    x = dict()
    x['numeric'] = numeric_tensor
    y = ...
    w = ...

    return x, y, w

def input_fn(file_pattern, b=BATCH_SIZE):
    """
    :param file_pattern: GCS bucket to read from
    :param b: Batch size, defaults to BATCH_SIZE in hparams.py
    :return: And infinitely iterable data set using tf records of tf.data.Dataset class
    """
    files = tf.data.Dataset.list_files(file_pattern=file_pattern)
    d = files.apply(
        tf.data.experimental.parallel_interleave(
            lambda filename: tf.data.TFRecordDataset(filename),
            cycle_length=4,
            block_length=16,
            buffer_output_elements=16,
            prefetch_input_elements=16,
            sloppy=True))
    d = d.apply(tf.contrib.data.map_and_batch(
        map_func=_parse_func, batch_size=b,
        num_parallel_batches=4))
    d = d.cache()
    d = d.repeat()
    d = d.prefetch(1)
    return d
Run Code Online (Sandbox Code Playgroud)

获取火车数据

# get files from GCS bucket and load them into dataset
train_data = input_fn(train_directory, b=BATCH_SIZE)
Run Code Online (Sandbox Code Playgroud)

适合模型

model.fit(x=train_data.make_one_shot_iterator())
Run Code Online (Sandbox Code Playgroud)

我在CloudML上运行它,所以GCS和CloudML应该非常快.


CloudML CPU使用情况:

正如我们在下面看到的,CPU为70%,内存不会增加10%.那怎么dataset.cache()办?

在此输入图像描述

CloudML日志中的GPU指标

如下所示,似乎GPU已关闭!内存也是0mb.缓存存储在哪里?

没有进程在GPU上运行!

在此输入图像描述

编辑:

事实上,GPU上没有运行进程.我试图明确说明:

tf.keras.backend.set_session(tf.Session(config=tf.ConfigProto(
    allow_soft_placement=True,
    log_device_placement=True)))

train_data = input_fn(file_pattern=train_directory, b=BATCH_SIZE)

model = create_model()

with tf.device('/gpu:0'):
    model.fit(x=train_data.make_one_shot_iterator(),
              epochs=EPOCHS,
              steps_per_epoch=STEPS_PER_EPOCH,
              validation_data=test_data.make_one_shot_iterator(),
              validation_steps=VALIDATION_STEPS)
Run Code Online (Sandbox Code Playgroud)

但一切仍然利用CPU!

GRS*_*GRS 0

就我而言,我使用的是setup.py仅使用 CPU 的 Tensorflow 版本的自定义文件。

我在踢自己,请安装tensorflow-gpu