Tensorflow 错误 "has type list, but expected one of: int, long, float"
Tensorflow error "has type list, but expected one of: int, long, float"
本来打算用tensorflow和librosa做点什么,但是用TFRecore的时候出错了。 google后没找到答案,想在这里咨询一下。
def create_record():
writer = tf.python_io.TFRecordWriter("./music_data/train.tfrecords")
for index, class_name in enumerate(classes):
class_path = "f:/Classical music/"+class_name+"/dataset/"
for a in os.listdir(class_path):
wav_path = class_path + a
print(wav_path)
wav,sr = librosa.load(wav_path,sr=None)
mfcc = librosa.feature.mfcc(wav,sr,n_mfcc=128) # is a numpy.ndarray ,with shape (128,1293)
print(index,mfcc.shape,type(mfcc))
# mfcc_list=[]
# for i in range(mfcc.shape[0]):
# mfcc_list.append([float(x) for x in mfcc[i]])
example = tf.train.Example(
feartures = tf.train.Features(feature={
"label":tf.train.Feature(int64_list=tf.train.Int64List(value=[index])),
"mfcc":tf.train.Feature(float_list=tf.train.FloatList(value=mfcc.tolist()))
}))
writer.write(example.SerializeToString())
writer.close()
TypeError Traceback (most recent call last)
<ipython-input-137-9bb818ee02d5> in <module>()
----> 1 create_record()
<ipython-input-136-9a256cba70a6> in create_record()
15 feartures = tf.train.Features(feature={
16 "label":tf.train.Feature(int64_list=tf.train.Int64List(value=[index])),
---> 17 "mfcc":tf.train.Feature(float_list=tf.train.FloatList(value=mfcc.tolist()))
18 }))
19 writer.write(example.SerializeToString())
TypeError: [-389.381029172618, -393.08814551655723, -404.7248725876356, -407.1006984237564, -409.22695909850626 has type list, but expected one of: int, long, float
我尝试了 https://github.com/tensorflow/tensorflow/issues/9554,but 之类的解决方案,但它对我不起作用。
谢谢!
MFCC 给你一个二维数组,它会相应地转换为列表列表。但是,TFRrecords 只接受 "flat" 列表作为特征值。
您可以通过将 value=mfcc.flatten()
放入 float_list
来解决这个问题。稍后,当将 TFRecord 解析为模型的输入时,您将需要再次将其重塑为 2D。如果形状总是相同的 (128, 1293),这很容易。如果形状是可变的,您可以将 mfcc.shape
作为另一个特征放入记录中,这样您就可以 "remember" 每个示例的原始形状。例如:
example = tf.train.Example(
features = tf.train.Features(feature={
"label": tf.train.Feature(int64_list=tf.train.Int64List(value=[index])),
"mfcc": tf.train.Feature(float_list=tf.train.FloatList(value=mfcc.flatten())),
"shape": tf.train.Feature(int64_list=tf.train.Int64List(value=mfcc.shape))
}))
本来打算用tensorflow和librosa做点什么,但是用TFRecore的时候出错了。 google后没找到答案,想在这里咨询一下。
def create_record():
writer = tf.python_io.TFRecordWriter("./music_data/train.tfrecords")
for index, class_name in enumerate(classes):
class_path = "f:/Classical music/"+class_name+"/dataset/"
for a in os.listdir(class_path):
wav_path = class_path + a
print(wav_path)
wav,sr = librosa.load(wav_path,sr=None)
mfcc = librosa.feature.mfcc(wav,sr,n_mfcc=128) # is a numpy.ndarray ,with shape (128,1293)
print(index,mfcc.shape,type(mfcc))
# mfcc_list=[]
# for i in range(mfcc.shape[0]):
# mfcc_list.append([float(x) for x in mfcc[i]])
example = tf.train.Example(
feartures = tf.train.Features(feature={
"label":tf.train.Feature(int64_list=tf.train.Int64List(value=[index])),
"mfcc":tf.train.Feature(float_list=tf.train.FloatList(value=mfcc.tolist()))
}))
writer.write(example.SerializeToString())
writer.close()
TypeError Traceback (most recent call last)
<ipython-input-137-9bb818ee02d5> in <module>()
----> 1 create_record()
<ipython-input-136-9a256cba70a6> in create_record()
15 feartures = tf.train.Features(feature={
16 "label":tf.train.Feature(int64_list=tf.train.Int64List(value=[index])),
---> 17 "mfcc":tf.train.Feature(float_list=tf.train.FloatList(value=mfcc.tolist()))
18 }))
19 writer.write(example.SerializeToString())
TypeError: [-389.381029172618, -393.08814551655723, -404.7248725876356, -407.1006984237564, -409.22695909850626 has type list, but expected one of: int, long, float
我尝试了 https://github.com/tensorflow/tensorflow/issues/9554,but 之类的解决方案,但它对我不起作用。
谢谢!
MFCC 给你一个二维数组,它会相应地转换为列表列表。但是,TFRrecords 只接受 "flat" 列表作为特征值。
您可以通过将 value=mfcc.flatten()
放入 float_list
来解决这个问题。稍后,当将 TFRecord 解析为模型的输入时,您将需要再次将其重塑为 2D。如果形状总是相同的 (128, 1293),这很容易。如果形状是可变的,您可以将 mfcc.shape
作为另一个特征放入记录中,这样您就可以 "remember" 每个示例的原始形状。例如:
example = tf.train.Example(
features = tf.train.Features(feature={
"label": tf.train.Feature(int64_list=tf.train.Int64List(value=[index])),
"mfcc": tf.train.Feature(float_list=tf.train.FloatList(value=mfcc.flatten())),
"shape": tf.train.Feature(int64_list=tf.train.Int64List(value=mfcc.shape))
}))