Audio 在Tensorflow中创建音频数据管道时出错

Audio 在Tensorflow中创建音频数据管道时出错,audio,tensorflow-datasets,Audio,Tensorflow Datasets,我正在尝试在张量流中构建一个音频数据管道。下面是代码 def map_func(filename, label): sar=32000 y, sr = librosa.load(filename, sr=sar, mono=True, res_type="kaiser_fast") len_y = len(y) effective_length = sr * PERIOD if len

我正在尝试在张量流中构建一个音频数据管道。下面是代码

def map_func(filename, label):
    sar=32000
    y, sr = librosa.load(filename,
                         sr=sar, mono=True, res_type="kaiser_fast")

    len_y = len(y)
    effective_length = sr * PERIOD
    if len_y < effective_length:
        new_y = np.zeros(effective_length, dtype=y.dtype)
        start = np.random.randint(effective_length - len_y)
        new_y[start:start + len_y] = y
        y = new_y.astype(np.float32)
    elif len_y > effective_length:
        start = np.random.randint(len_y - effective_length)
        y = y[start:start + effective_length].astype(np.float32)
        print(start)
    else:
        y = y.astype(np.float32)

    melspec = librosa.feature.melspectrogram(y, sr=sr, n_mels=128)
    melspec = librosa.power_to_db(melspec).astype(np.float32)

    image = mono_to_color(melspec)
    image = (image / 255.0).astype(np.float32)
    labels = np.zeros(len(BIRD_CODE), dtype="f")
    if (label != 'nocall'):
        labels[BIRD_CODE[label]] = 1

    return image, labels

dataset = tf.data.Dataset.from_tensor_slices((img_list, label_list))
dataset = dataset.shuffle(len(img_list))
dataset = dataset.map(map_func)
dataset = dataset.batch(batch_size)
def map_func(文件名、标签):
sar=32000
y、 sr=librosa.load(文件名,
sr=sar,mono=True,res\u type=“kaiser\u fast”)
len_y=len(y)
有效长度=sr*周期
如果长度y<有效长度:
新的y=np.0(有效长度,dtype=y.dtype)
开始=np.random.randint(有效长度-长度)
新建y[开始:开始+长度y]=y
y=新的y.aType(np.32)
elif长度>有效长度:
开始=np.random.randint(长度-有效长度)
y=y[起始:起始+有效长度].aType(np.float32)
打印(开始)
其他:
y=y.aType(np.32)
melspec=librosa.feature.melspectrogram(y,sr=sr,n_-mels=128)
melspec=librosa.power_to_db(melspec.astype)(np.float32)
图像=单色到彩色(melspec)
image=(image/255.0).astype(np.float32)
标签=np.零(len(鸟码),dtype=“f”)
如果(标签!=“nocall”):
标签[BIRD_代码[label]]=1
返回图像、标签
dataset=tf.data.dataset.from_tensor_切片((img_列表,label_列表))
dataset=dataset.shuffle(len(img_列表))
dataset=dataset.map(映射函数)
dataset=dataset.batch(批次大小)
这显然是错误的,不是正确的方法

[TypeError:在用户代码中:

<ipython-input-67-016fa59ba5d2>:3 map_func  *
    y, sr = librosa.load(filename,
/opt/conda/lib/python3.7/site-packages/librosa/core/audio.py:129 load  *
    with sf.SoundFile(path) as sf_desc:
/opt/conda/lib/python3.7/site-packages/soundfile.py:629 __init__
    self._file = self._open(file, mode_int, closefd)
/opt/conda/lib/python3.7/site-packages/soundfile.py:1182 _open
    raise TypeError("Invalid file: {0!r}".format(self.name))

TypeError: Invalid file: <tf.Tensor 'args_0:0' shape=() dtype=string>]
:3映射函数*
y、 sr=librosa.load(文件名,
/opt/conda/lib/python3.7/site packages/librosa/core/audio.py:129加载*
将sf.SoundFile(路径)作为sf_desc:
/opt/conda/lib/python3.7/site packages/soundfile.py:629\uu init__
self.\u file=self.\u打开(文件、模式、关闭)
/opt/conda/lib/python3.7/site-packages/soundfile.py:1182\u打开
raise TypeError(“无效文件:{0!r}”。格式(self.name))
TypeError:无效文件:]
正确的方法是什么