"ValueError: Input 0 of layer "sequential" is incompatible with the layer" In prediction
"ValueError: Input 0 of layer "sequential" is incompatible with the layer" In prediction
我正在尝试对我的和其他人的声音进行分类,然后将其应用到未来的程序中。我为此使用了 CNN 模型,在训练中它给出了非常好的结果,我将音频转换为频谱图供 CNN 理解。
问题出在预测上,我做了同样的事情,将音频转换为频谱图,但它给了我这个错误。
ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 129, 1071, 1), found shape=(None, 1071)
我在模型中输入了这个并且没有报错
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, "%")
有什么想法吗?
编辑:
经过一些摆弄后,这是解决方案:
一方面,输入的最终维度存在错误(input_shape 中的 1
)。这表示通道数(想想图像中的 RGB 通道)。为了扩展我们的频谱图,我们可以使用
spectrogram = spectrogram.reshape(spectrogram.shape + (1,))
或
spectrogram = np.expand_dims(spectrogram, -1)
.
此时频谱图的形状为(129, 1071, 1)。
另一方面,在推理过程中删除了第一个维度 (129),因为 TensorFlow 会将其解释为批处理维度。您可以通过将频谱图包装在一个(一个元素)NumPy 数组中来解决这个问题,如下所示:
spectrogram = np.array([spectrogram])
现在频谱图的形状是 (1, 129, 1071, 1) 这正是我们需要的。
原文:
这绝对是评论而不是答案,但由于缺乏声誉,我不能写这些,所以请随意将其移至评论...
所以问题是预期的形状(以及您的网络的架构)和您的数据的形状不匹配。
我想那是因为 predict() 调用希望您交出一批(查看每个形状的第一个维度)样本进行评估。
您可以通过使用列表将 spectrogram 参数包装在预测调用中来解决此问题:vc_prediction = voice_model.predict([spectogram])[0]
。
如果这不能解决问题,我建议进一步研究训练和评估数据的形状,我喜欢在运行时以调试模式执行此操作。
我正在尝试对我的和其他人的声音进行分类,然后将其应用到未来的程序中。我为此使用了 CNN 模型,在训练中它给出了非常好的结果,我将音频转换为频谱图供 CNN 理解。 问题出在预测上,我做了同样的事情,将音频转换为频谱图,但它给了我这个错误。
ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 129, 1071, 1), found shape=(None, 1071)
我在模型中输入了这个并且没有报错
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, "%")
有什么想法吗?
编辑:
经过一些摆弄后,这是解决方案:
一方面,输入的最终维度存在错误(input_shape 中的 1
)。这表示通道数(想想图像中的 RGB 通道)。为了扩展我们的频谱图,我们可以使用
spectrogram = spectrogram.reshape(spectrogram.shape + (1,))
或
spectrogram = np.expand_dims(spectrogram, -1)
.
此时频谱图的形状为(129, 1071, 1)。
另一方面,在推理过程中删除了第一个维度 (129),因为 TensorFlow 会将其解释为批处理维度。您可以通过将频谱图包装在一个(一个元素)NumPy 数组中来解决这个问题,如下所示:
spectrogram = np.array([spectrogram])
现在频谱图的形状是 (1, 129, 1071, 1) 这正是我们需要的。
原文:
这绝对是评论而不是答案,但由于缺乏声誉,我不能写这些,所以请随意将其移至评论...
所以问题是预期的形状(以及您的网络的架构)和您的数据的形状不匹配。
我想那是因为 predict() 调用希望您交出一批(查看每个形状的第一个维度)样本进行评估。
您可以通过使用列表将 spectrogram 参数包装在预测调用中来解决此问题:vc_prediction = voice_model.predict([spectogram])[0]
。
如果这不能解决问题,我建议进一步研究训练和评估数据的形状,我喜欢在运行时以调试模式执行此操作。