ValueError: 层序列 2 的输入 0 与层不兼容:预期 min_ndim=4,发现 ndim=2。收到的完整形状:[无,40]

时间:2021-06-03 10:39:04

标签: python conv-neural-network multilabel-classification

我为音频分类项目构建了一个多标签分类的 CNN 模型。当我在音频样本上测试模型时,我收到一条错误消息,如代码底部所示。 X_train、X_test、y_train 和 y_test 的形状也提供如下:

X_train.shape
(115, 40, 1)

X_test.shape
(78, 40, 1)

y_train.shape
(115, 10)

y_test.shape
(78, 10)

input_shape = np.array((X_train.shape[1], X_train.shape[2], 1))
print(input_shape.shape)    

model.add(Conv2D(16, (3, 3), activation='relu', strides=(1, 1), 
             padding='same', input_shape=input_shape))
model.add(Conv2D(32, (3, 3), activation='relu', strides=(1, 1), padding='same'))
model.add(MaxPooling2D((2,2), strides=(2,2), padding='same'))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5)) 
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam',metrics=['acc'])

filename = audio_files[1] 
audio, sample_rate = librosa.load(filename, res_type='kaiser_fast') 
mfccs_features = librosa.feature.mfcc(y=audio, sr=sample_rate, n_mfcc=40)
mfccs_scaled_features = np.mean(mfccs_features.T,axis=0)
print(mfccs_scaled_features)
mfccs_scaled_features=mfccs_scaled_features.reshape(1,-1)
print(mfccs_scaled_features)
print(mfccs_scaled_features.shape)
predicted_label=model.predict_classes(mfccs_scaled_features)
print(predicted_label)
prediction_class = labelencoder.inverse_transform(predicted_label) 
prediction_class

---> 10 predicted_label=model.predict_classes(mfccs_scaled_features)
ValueError: Input 0 of layer sequential_2 is incompatible with the layer: : expected 
min_ndim=4, found ndim=2. Full shape received: [None, 40]

我该如何解决这个问题?

0 个答案:

没有答案