Получить ошибку при загрузке файла tfrecord в tenorflow 2.0 - PullRequest
0 голосов
/ 16 января 2020

Я пытаюсь преобразовать WAV-файлы в осколки TFRecord

В приведенном ниже коде я использую tf.audio.decode_wav для получения аудиосигнала из wav-файла, метка - это индексный список предложений.

Затем я сохраняю весь wav-файл и метку в train.tfrecord и разделяю его

   def _write_tfrecord_file(self, shard_data):

        shard_path, indices = shard_data
        with tf.io.TFRecordWriter(shard_path, options='ZLIB') as out:
            for index in indices:
                file_path = self.data_dir + self.df['Filename'][index] + ".wav"
                label = str2index(self.df['Text'][index])

                raw_audio = tf.io.read_file(file_path)
                audio, sample_rate = tf.audio.decode_wav(
                    raw_audio,
                    desired_channels=1,  # mono
                    desired_samples=self.sample_rate * self.duration)

                example = tf.train.Example(features=tf.train.Features(feature={
                    'audio': _float_feature(audio.numpy().flatten().tolist()),
                    'label': _int64_feature(label)}))

                out.write(example.SerializeToString())

Затем я пишу функцию для загрузки

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.FixedLenFeature([], 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=5,
                               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))

    files_ds = tf.data.Dataset.list_files(pattern)

    ignore_order = tf.data.Options()
    ignore_order.experimental_deterministic = False
    files_ds = files_ds.with_options(ignore_order)

    ds = tf.data.TFRecordDataset(files_ds, compression_type='ZLIB')

    ds.batch(batch_size)

    ds = ds.map(lambda x: _parse_batch(x, sample_rate, duration))

    if split == 'train':
        ds.repeat(n_epochs)

    return ds.prefetch(buffer_size=AUTOTUNE)

Но я получаю ошибка

ValueError: in converted code:

    D:\Natural Language Processing\speech_to_text\utils\load_tfrecord.py:38 None  *
        ds = ds.map(lambda x: _parse_batch(x, sample_rate, duration))
    D:\Natural Language Processing\speech_to_text\utils\load_tfrecord.py:16 _parse_batch  *
        example = tf.io.parse_example(record_batch, feature_description)
    C:\Users\levan\Anaconda3\lib\site-packages\tensorflow_core\python\ops\parsing_ops.py:807 parse_example_v2
        dense_types, dense_defaults, dense_shapes, name)
    C:\Users\levan\Anaconda3\lib\site-packages\tensorflow_core\python\ops\parsing_ops.py:868 _parse_example_raw
        name=name)
    C:\Users\levan\Anaconda3\lib\site-packages\tensorflow_core\python\ops\gen_parsing_ops.py:626 parse_example
        name=name)
    C:\Users\levan\Anaconda3\lib\site-packages\tensorflow_core\python\framework\op_def_library.py:793 _apply_op_helper
        op_def=op_def)
    C:\Users\levan\Anaconda3\lib\site-packages\tensorflow_core\python\framework\func_graph.py:548 create_op
        compute_device)
    C:\Users\levan\Anaconda3\lib\site-packages\tensorflow_core\python\framework\ops.py:3429 _create_op_internal
        op_def=op_def)
    C:\Users\levan\Anaconda3\lib\site-packages\tensorflow_core\python\framework\ops.py:1773 __init__
        control_input_ops)
    C:\Users\levan\Anaconda3\lib\site-packages\tensorflow_core\python\framework\ops.py:1613 _create_c_op
        raise ValueError(str(e))

    ValueError: Shape must be rank 1 but is rank 0 for 'ParseExample/ParseExample' (op: 'ParseExample') with input shapes: [], [0], [], [], [], [].

Как я могу это исправить?

...