【问题标题】:TensorFlow - Interleave multiple indipently preprocessed TFRecord filesTensorFlow - 交错多个独立预处理的 TFRecord 文件
【发布时间】:2021-03-23 15:31:15
【问题描述】:

我有多个来自 Waymo 数据集的 TFRecord 文件,每个文件都包含在文件中不连续的连续点。我正在构建一个输入管道,通过window() API 为时间序列预测预处理数据,但我需要避免窗口跨越多个文件。

为此,我认为我应该单独预处理每个文件并交错最终数据集。 这是我的尝试:

import tensorflow as tf
from waymo_open_dataset import dataset_pb2 as open_dataset #for parsing Waymo frames

filenames = [os.path.join(DATASET_DIR, f) for f in os.listdir(DATASET_DIR)]
dataset = tf.data.TFRecordDataset(filenames, compression_type='')

def interleave_fn(filename):
    ds = filename.map(lambda x: tf.py_function(_parse_data, [x], [tf.float32]*N_FEATURES,), 
                          num_parallel_calls=tf.data.experimental.AUTOTUNE) 
    ds = ds.map(_concatenate_tensors).map(_set_x_shape)
    ds = build_x_dataset(ds)
    return ds

def _parse_data(data):
    # Parse feature from Waymo dataset  
    frame = open_dataset.Frame()
    frame.ParseFromString(bytearray(data.numpy()))   
    av_v_x = frame.images[0].velocity.v_x 
    av_v_y = frame.images[0].velocity.v_y 
    return av_v_x, av_v_y

def _concatenate_tensors(*x):
    #Concatenate tensor tuple in a single tensor
    return tf.stack((x))

def _set_x_shape(x):
    #Set X dataset shape. If not UNDEFINED RANK ValueError
    x.set_shape((N_FEATURES,))
    return x
    
def build_x_dataset(ds_x, window = WINDOW):
    # Extract sequences for time series prediction training
    # Selects a sliding window of WINDOW samples, shifting by 1 sample at a time
    ds_x = ds_x.window(size = window, shift = 1, drop_remainder = True)
    
    # Each element of `ds_x` is a nested dataset containing WINDOWconsecutive examples 
    ds_x = ds_x.map(lambda d: tf.data.experimental.get_single_element(d.batch(window))) 
    return ds_x

dataset = dataset.interleave(interleave_fn, num_parallel_calls=tf.data.experimental.AUTOTUNE)

返回

AttributeError: in user code:

    /tmp/xpython_26752/494049692.py:118 interleave_fn  *
        ds = filename.map(lambda x: tf.py_function(_parse_data, [x], [tf.float32]*N_FEATURES,),

    AttributeError: 'Tensor' object has no attribute 'map'

这是有道理的,因为interleave_fn 中的print(filename) 给出了

Tensor("args_0:0", shape=(), dtype=string)

我认为interleave_fn 将应用于每个TFRecordDataset,因此filename 本身就是一个数据集而不是张量。这里有什么问题?谢谢!

【问题讨论】:

    标签: python tensorflow parsing tensorflow2.0 tensorflow-datasets


    【解决方案1】:

    通过遍历所有 TFRecord 文件并将相应的数据集附加到数据集列表来解决它。然后,按照tip 对所有预处理数据集进行交错处理。

    【讨论】:

      猜你喜欢
      • 2017-06-07
      • 2011-05-28
      • 2018-04-22
      • 2019-03-18
      • 2016-02-07
      • 2022-06-14
      • 2014-12-24
      • 1970-01-01
      相关资源
      最近更新 更多