我是深度学习的初学者,并且正在尝试通过对数据集执行音频分析来练习用Python实现神经网络的实现。我一直在遵循Urban Sound Challenge教程,并完成了用于训练模型的代码,但是在尝试在测试集上运行模型时,我总是遇到错误。
这是我用于创建模型和训练的代码:
import numpy as np
from sklearn.preprocessing import LabelEncoder
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
num_labels = y.shape[1]
filter_size = 2
model = Sequential()
model.add(Dense(256, input_shape = (40,)))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(num_labels))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')
model.fit(X, y, batch_size=32, epochs=40, validation_data=(val_X, val_Y))
在拟合模型之前运行model.summary()给我:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_3 (Dense) (None, 256) 10496
_________________________________________________________________
activation_3 (Activation) (None, 256) 0
_________________________________________________________________
dropout_2 (Dropout) (None, 256) 0
_________________________________________________________________
dense_4 (Dense) (None, 10) 2570
_________________________________________________________________
activation_4 (Activation) (None, 10) 0
=================================================================
Total params: 13,066
Trainable params: 13,066
Non-trainable params: 0
_________________________________________________________________
拟合模型后,我尝试在一个文件上运行它,以便可以对声音进行分类。
file_name = ".../UrbanSoundClassifier/test/Test/5.wav"
test_X, sample_rate = librosa.load(file_name,res_type='kaiser_fast')
mfccs = np.mean(librosa.feature.mfcc(y=test_X, sr=sample_rate, n_mfcc=40).T,axis=0)
test_X = np.array(mfccs)
print(model.predict(test_X))
但是,我得到
ValueError: Error when checking : expected dense_3_input to have shape
(无,40),但得到形状为(40,1)的数组
有人会为我指出如何测试模型的正确方向吗?我不知道model.predict()
应该输入什么。
完整的代码可以在这里找到。
所以:
最简单的解决方法是重塑test_x
:
test_x = test_x.reshape((1, 40))
更复杂的是重用您具有的管道来创建train
和valid
设置测试集。请注意,在情况下,您应用于数据文件的过程完全不同test
。我将创建一个测试数据框:
test_dataframe = pd.DataFrame({'filename': ["here path to test file"]}
然后重用现有管道来创建验证集。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句