How to create N copies of a label inside .map function of tf.data

Viewed 18

I have the following functions to read audio files along with their labels using tf.data

def get_waveform(file_path):
    audio_binary = tf.io.read_file(file_path)
    waveform, sr = tf.audio.decode_wav(contents=audio_binary,
                                       desired_channels=1)
    waveform = tf.squeeze(waveform, axis=-1)
    frames = tf.signal.frame(waveform, sr * 3, (sr * 3) // 2, pad_end=True)
    return frames


def get_label(file_path):
    parts = tf.strings.split(
        input=file_path,
        sep=os.path.sep)
    return parts[-2]

def get_waveform_and_label(file_path):
  label = get_label(file_path)
  waveform = get_waveform(file_path)
  label = ???
  return waveform, label

The get_waveform function will return N frames of a waveform for a single audio file and all frames will have the same label. So how can I repeat it N times and return it by zipping frames and their corresponding labels?

1 Answers

You can just use a tf.map_fn function to repeat the label. It takes a function and tensor as input and applies the function to every element in the tensor. So the idea is to first use the tf.shape function to get the shape of the waveform tensor and then pass that to the tf.map_fn function as the second argument to specify the number of times to repeat the label.

def get_waveform_and_label(file_path):
  label = get_label(file_path)
  waveform = get_waveform(file_path)
  label = tf.map_fn(lambda x: label, tf.shape(waveform))
  return waveform, label
Related