我试图将2D矢量(语音频谱图,长度不一样)序列化为TFRecord。
这有什么好的例子吗?我尝试了一些代码here和here,但似乎不符合我的需求
如何将长度序列不同的2D(或任何N-D阵列)序列化为TFRecords?
我目前的实施(没有错误,但我认为它不正确):
# extract spectrogram from wav
rate, wav = wavfile.read(wav_path)
_, __, Sxx = spectrogram(wav, fs=rate, nperseg=254, noverlap=127)
spectrogram_feat = np.moveaxis(Sxx, 0, -1) # shift axis [freq, time] -> [time, freq]
# make example
ex = tf.train.SequenceExample()
ex.context.feature['seq_length'].int64_list.value.append(spectrogram_feat.shape[0])
ex.context.feature['n_feature'].int64_list.value.append(spectrogram_feat.shape[1])
fl_spectogram = ex.feature_lists.feature_list['spectrogram']
fl_label = ex.feature_lists.feature_list['label']
for i in range(spectrogram_feat.shape[0]):
# print(spectrogram_feat[i, :].reshape(1, -1))
for f in spectrogram_feat[i, :]:
fl_spectogram.feature.add().float_list.value.append(f)
writer.write(ex.SerializeToString())