I was going to do something with tensorflow and librosa, but when I used TFRecore, there was an error. I didn't find the answer after google, so I want to consult you here.
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
I try the solution like https://github.com/tensorflow/tensorflow/issues/9554,but it isn't work on me .
Thanks!
MFCC gives you a 2d array, which will accordingly be converted to a list of lists. However, TFRrecords only accepts "flat" lists as feature values.
You can work around this by putting value=mfcc.flatten()
into the float_list
instead. Later, when parsing the TFRecord as input into your model, you will need to reshape it to 2D again. If the shapes are always the same (128, 1293), this is easy. If the shapes are variable, you can put mfcc.shape
into the record as another feature, so that you "remember" each example's original shape. E.g.:
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))
}))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With