如何在.data的映射函数内创建标签的n副本

发布于 2025-02-11 15:30:49 字数 768 浏览 1 评论 0原文

我有以下函数可以使用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

get_waveform函数将返回单个音频文件的波形的n帧,所有帧都将具有相同的标签。那么,如何重复n次并通过拉链框架及其相应标签返回它?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∞梦里开花 2025-02-18 15:30:50

您可以只使用tf.map_fn功能重复标签。它采用函数和张量作为输入,并将函数应用于张量中的每个元素。因此,这个想法是首先使用tf.shape函数以获取波形张量的形状,然后将其传递给tf.map_fn函数作为第二个参数重复标签的次数。

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文