【问题标题】:How can i solve InvalidArgumentError: cycle_length must be > 0 when load tfrecords file我如何解决 InvalidArgumentError: cycle_length must be > 0 when load tfrecords file
【发布时间】:2020-01-16 17:58:42
【问题描述】:

我开始使用tf.TFRecord and tf.Example 构建一个高效的音频文件数据管道。但是当我尝试从保存的 tfrecords 文件中加载数据时,我收到错误 tensorflow.python.framework.errors_impl.InvalidArgumentError。我一直在为这个问题寻找很多解决方案,但没有奏效。

AUTO = tf.data.experimental.AUTOTUNE


def _parse_batch(record_batch, sample_rate, duration):
    n_sample = sample_rate * duration

    feature_description = {
        'audio': tf.io.FixedLenFeature([n_sample], tf.float32),
        'label': tf.io.VarLenFeature(tf.int64)
    }

    example = tf.io.parse_example(record_batch, feature_description)

    return example['audio'], example['label']


def get_dataset_from_tfrecords(tfrecords_dir='tfrecords', split='train', batch_size=16,
                               sample_rate=44100, duration=4, n_epochs=10):
    if split not in ('train', 'validate'):
        raise ValueError("Split must be either 'train' or 'validate'")

    pattern = os.path.join(tfrecords_dir, '{}*.tfrecord'.format(split))

    ignore_order = tf.data.Options()
    ignore_order.experimental_deterministic = False
    filenames = tf.io.gfile.glob(pattern)

    # Read TFRecord files in an interleaved order
    dataset = tf.data.TFRecordDataset(filenames, compression_type='ZLIB', num_parallel_reads=AUTO)
    dataset = dataset.with_options(ignore_order)
    # Prepare batches
    dataset = dataset.batch(batch_size)

    # Parse a batch into a dataset of [audio, label] pairs
    dataset = dataset.map(lambda x: _parse_batch(x, sample_rate, duration))

    # Repeat the training data for n_epochs. Don't repeat test/validate splits.
    if split == 'train':
        dataset = dataset.repeat(n_epochs)

    return dataset.prefetch(buffer_size=AUTO)

这是完整的错误

Traceback (most recent call last):
  File "train.py", line 25, in <module>
    main()
  File "train.py", line 16, in main
    n_epochs=n_epochs)
  File "D:\Natural Language Processing\speech_to_text\utils\load_tfrecord.py", line 33, in get_dataset_from_tfrecords
    dataset = tf.data.TFRecordDataset(filenames, compression_type='ZLIB', num_parallel_reads=AUTO)
  File "C:\Users\levan\Anaconda3\lib\site-packages\tensorflow_core\python\data\ops\readers.py", line 304, in __init__
    num_parallel_reads)
  File "C:\Users\levan\Anaconda3\lib\site-packages\tensorflow_core\python\data\ops\readers.py", line 85, in _create_dataset_reader
    prefetch_input_elements=None)
  File "C:\Users\levan\Anaconda3\lib\site-packages\tensorflow_core\python\data\ops\readers.py", line 250, in __init__
    **self._flat_structure)
  File "C:\Users\levan\Anaconda3\lib\site-packages\tensorflow_core\python\ops\gen_experimental_dataset_ops.py", line 5977, in parallel_interleave_dataset
    _six.raise_from(_core._status_to_exception(e.code, message), None)
  File "<string>", line 3, in raise_from
tensorflow.python.framework.errors_impl.InvalidArgumentError: `cycle_length` must be > 0 [Op:ParallelInterleaveDataset]

谁能帮帮我?

【问题讨论】:

  • 我也有同样的问题,请问哪里可以解决?

标签: python python-3.x tensorflow tfrecord data-pipeline


【解决方案1】:

我在 tensorflow 2.0 上遇到过类似的问题,但是升级到 2.1 解决了这个问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-11
    • 2023-03-19
    • 2012-03-03
    • 1970-01-01
    • 2019-09-14
    • 2023-03-06
    • 2021-10-02
    • 2021-07-08
    相关资源
    最近更新 更多