英文:
How to pass input to 2D Conv in Keras?
问题
我已使用librosa将声音转换为频谱图。频谱图的形状是(257, 356),我已将其重塑为(257, 356, 1)。
我创建了一个模型:
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten
model = Sequential()
model.add(Conv2D(64, kernel_size=3, activation='relu', input_shape=A.shape))
model.add(Flatten())
model.add(Dense(1, activation='softmax'))
在拟合模型时,出现了以下错误:
model.fit(A, validation_data=(A2), epochs=3)
其中A2是另一个频谱图,其维度如下:
ValueError: 检查输入时出错:预期conv2d_3_input具有4个维度,但得到的数组形状为(257, 356, 1)
模型摘要:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_24 (Conv2D) (None, 255, 354, 64) 640
_________________________________________________________________
conv2d_25 (Conv2D) (None, 253, 352, 32) 18464
_________________________________________________________________
flatten_11 (Flatten) (None, 2849792) 0
_________________________________________________________________
dense_11 (Dense) (None, 10) 28497930
=================================================================
Total params: 28,517,034
Trainable params: 28,517,034
Non-trainable params: 0
A[0]的形状是
A[0].shape = (356, 1)
英文:
I have converted voice to spectrogram using librosa. The shape of spectogram is (257, 356), which i have reshaped to (257, 356, 1).
I have created a model
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten
model = Sequential()
model.add(Conv2D(64, kernel_size=3, activation='relu', input_shape=A.shape))
model.add(Flatten())
model.add(Dense(1, activation='softmax'))
while fitting the model, following error is produced
model.fit(A,validation_data=(A2), epochs=3)
where A2 is another spectrogram with following dimensions
ValueError: Error when checking input: expected conv2d_3_input to have 4 dimensions, but got array with shape (257, 356, 1)
Model Summary
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_24 (Conv2D) (None, 255, 354, 64) 640
_________________________________________________________________
conv2d_25 (Conv2D) (None, 253, 352, 32) 18464
_________________________________________________________________
flatten_11 (Flatten) (None, 2849792) 0
_________________________________________________________________
dense_11 (Dense) (None, 10) 28497930
=================================================================
Total params: 28,517,034
Trainable params: 28,517,034
Non-trainable params: 0
And the shape of A[0] is
A[0].shape = (356, 1)
答案1
得分: 2
以下是我的工作代码:
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten
import numpy as np
A = np.zeros((1,257,356,1)) # 仅用于说明
A2 = np.zeros((1,1)) # 仅用于说明
model = Sequential()
model.add(Conv2D(64, kernel_size=(3,3), activation='relu', input_shape=A.shape[1:])) # input_shape ==> (257,356,1)
model.add(Flatten())
model.add(Dense(1, activation='softmax'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(A, A2, validation_data = (A, A2), epochs=3)
以下是3个时期的输出:
Train on 1 samples, validate on 1 samples
Epoch 1/3
1/1 [==============================] - 0s 250ms/step - loss: 0.0000e+00 - accuracy: 1.0000 - val_loss: 0.0000e+00 - val_accuracy: 1.0000
Epoch 2/3
1/1 [==============================] - 0s 141ms/step - loss: 0.0000e+00 - accuracy: 1.0000 - val_loss: 0.0000e+00 - val_accuracy: 1.0000
Epoch 3/3
1/1 [==============================] - 0s 156ms/step - loss: 0.0000e+00 - accuracy: 1.0000 - val_loss: 0.0000e+00 - val_accuracy: 1.0000
<keras.callbacks.callbacks.History at 0x1d508dbb708>
英文:
EDIT: Here's my working code:
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten
import numpy as np
A = np.zeros((1,257,356,1)) # Only for illustration
A2 = np.zeros((1,1)) # Only for illustration
model = Sequential()
model.add(Conv2D(64, kernel_size=(3,3), activation='relu', input_shape=A.shape[1:])) # input_shape ==> (257,356,1)
model.add(Flatten())
model.add(Dense(1, activation='softmax'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(A, A2, validation_data = (A, A2), epochs=3)
And here's the output for 3 epochs:
Train on 1 samples, validate on 1 samples
Epoch 1/3
1/1 [==============================] - 0s 250ms/step - loss: 0.0000e+00 - accuracy: 1.0000 - val_loss: 0.0000e+00 - val_accuracy: 1.0000
Epoch 2/3
1/1 [==============================] - 0s 141ms/step - loss: 0.0000e+00 - accuracy: 1.0000 - val_loss: 0.0000e+00 - val_accuracy: 1.0000
Epoch 3/3
1/1 [==============================] - 0s 156ms/step - loss: 0.0000e+00 - accuracy: 1.0000 - val_loss: 0.0000e+00 - val_accuracy: 1.0000
<keras.callbacks.callbacks.History at 0x1d508dbb708>
答案2
得分: 0
你的模型缺少批处理大小。
2D 卷积的输入形状为 (批处理大小, 空间维度1, 空间维度2, 通道数)
。
我不知道你的数据结构是怎样的,但看起来你没有批处理?(如果是这样的话,你需要创建大小为1的批处理维度,但这不会训练得很好,你需要大量的数据,而不是单个示例)。
所以,A.shape
必须是 (1, 257, 356, 1)
,第一个1是批处理大小,最后一个是通道数。其他两个数字是“图像”的空间维度。
而且你的 input_shape
不应包括批处理大小:input_shape=(257, 356, 1)
。
英文:
You are missing the batch size in your model.
The input shapes for 2D convolutions are (batch, spatial1, spatial2, channels)
.
I don't know the structure of your data, but it seems you don't have a batch? (If this is true you need to create the batch dimension with size 1, but this will not train well at all, you need huge amounts of data, not a single example).
So, A.shape
must be (1, 257,356, 1)
, being the first 1 the batch size, and the last the number of channels. The other two numbers are the spatial dimensions of the "image".
And your input_shape
must not include the batch size: input_shape=(257,356,1)
.
答案3
得分: 0
查看Keras官方文档页面
上的类似VGG的卷积神经网络示例。就像@Daniel提到的那样,input_shape
必须定义为(样本数, 高度, 宽度, 通道数)
的元组。
作为参考,假设你有500个样本,你的输入数据应该如下所示:
x_train = np.random.random((500, 257, 356, 1))
print(x_train.shape)
(500, 257, 356, 1)
英文:
Look out for VGG-like convnet example on keras official documentation page
.
Like @Daniel mentioned, the input_shape
must be defined as tuple of (number_of_examples, height, width, channel)
.
For reference, let's say you have 500 samples, your input data should look like this:
x_train = np.random.random((500, 257, 356, 1))
print(x_train.shape)
(500, 257, 356, 1)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论