[英]"ValueError: Input 0 of layer "sequential" is incompatible with the layer" In prediction
I am trying to make a classifier of voices, mine and others and then apply it to a future program.我正在尝试对我的和其他人的声音进行分类,然后将其应用于未来的程序。 I used the CNN model for this, in training it gave very good results, I converted the audio to a spectrogram for CNN to understand.
我为此使用了 CNN model,在训练中它给出了非常好的结果,我将音频转换为频谱图以便 CNN 理解。 The problem is in the prediction, I do the same of converting the audio to a spectrogram but it gives me this error.
问题出在预测中,我将音频转换为频谱图也是如此,但它给了我这个错误。
ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 129, 1071, 1), found shape=(None, 1071)
While in the model I put this and gave no error在 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'))
This is my code for the prediction这是我的预测代码
### 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, "%")
any idea?任何想法?
This is definitely more of a comment than an answer but I cannot write those due to a lack in reputation, so feel free to move it to comments...这绝对是评论而不是答案,但由于缺乏声誉,我无法写这些,所以请随时将其移至评论...
So the problem is that the expected shape (and thus the architecture of your network) and your data's shape don't match.所以问题是预期的形状(以及你的网络架构)和你的数据的形状不匹配。 I guess that's because the predict() call expects you to hand over a batch (look at the first dimension of each shape) of samples to evaluate.
我猜这是因为 predict() 调用希望您交出一批样本(查看每个形状的第一个维度)以进行评估。 You may get around this by wrapping the spectrogram argument inside the predict call with a list:
vc_prediction = voice_model.predict([spectogram])[0]
.您可以通过使用列表将 spectrogram 参数包装在 predict 调用中来解决此问题:
vc_prediction = voice_model.predict([spectogram])[0]
。 If this doesn't do the trick I'd recommend to further investigate the shapes of training and evaluation data, I like to do this during runtime in debug mode.如果这不能解决我建议进一步研究训练和评估数据的形状的技巧,我喜欢在运行时以调试模式执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.