如何在.data的映射函数内创建标签的n副本
我有以下函数可以使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以只使用
tf.map_fn
功能重复标签。它采用函数和张量作为输入,并将函数应用于张量中的每个元素。因此,这个想法是首先使用tf.shape
函数以获取波形张量的形状,然后将其传递给tf.map_fn
函数作为第二个参数重复标签的次数。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 thetf.shape
function to get the shape of the waveform tensor and then pass that to thetf.map_fn
function as the second argument to specify the number of times to repeat the label.