繁体   English   中英

"ValueError: Input 0 of layer "sequential" is in compatible with the layer" 在预测中

[英]"ValueError: Input 0 of layer "sequential" is incompatible with the layer" In prediction

我正在尝试对我的和其他人的声音进行分类,然后将其应用于未来的程序。 我为此使用了 CNN model,在训练中它给出了非常好的结果,我将音频转换为频谱图以便 CNN 理解。 问题出在预测中,我将音频转换为频谱图也是如此,但它给了我这个错误。

ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 129, 1071, 1), found shape=(None, 1071)

在 model 中,我放了这个并且没有给出错误

model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(129, 1071, 1)))
model.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))

这是我的预测代码

### VOICE CLASSIFICATION ###
voice_model = load_model(os.path.abspath('Models/voiceclassify2.model'))
classes = ['Other', 'Bernardo']

sample = os.path.abspath('Voiceclassification/Data/me/5.wav')
samplerate, data = wavfile.read(str(sample))

# convert into spectogram
frecuencies, times, spectogram = signal.spectrogram(data, samplerate)


vc_prediction = voice_model.predict(spectogram)[0]
idx = np.argmax(vc_prediction)
label = classes[idx]

print(label, " | ", vc_prediction[idx]*100, "%")

任何想法?

这绝对是评论而不是答案,但由于缺乏声誉,我无法写这些,所以请随时将其移至评论...

所以问题是预期的形状(以及你的网络架构)和你的数据的形状不匹配。 我猜这是因为 predict() 调用希望您交出一批样本(查看每个形状的第一个维度)以进行评估。 您可以通过使用列表将 spectrogram 参数包装在 predict 调用中来解决此问题: vc_prediction = voice_model.predict([spectogram])[0] 如果这不能解决我建议进一步研究训练和评估数据的形状的技巧,我喜欢在运行时以调试模式执行此操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM