我第一次用Keras构建卷积神经网络,而我遇到了一些问题。 CNN的目的是检测490x640px灰度图像中的图案,我已将其转换为3D numpy数组。我的Pandas数据框的图像数据列中的每个图像的形状均为(490, 640, 1)
。
我的代码如下:
from sklearn.model_selection import train_test_split
import tensorflow as tf
from tensorflow.keras import layers, models
# Assigning values to split the dataset into training and test data.
X = data[0]
Y = data[1]
# Allocate 20% of the dataset to be used as test data
train_images, test_images, train_labels, test_labels = train_test_split(X, Y, test_size=0.2, random_state=10)
class_names = ['Neutral', 'Anger', 'Contempt', 'Disgust', 'Fear', 'Happy', 'Sadness', 'Surprise']
model = models.Sequential()
model.add(layers.Conv2D(490, (3, 3), activation="relu", input_shape=(490, 640, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(100, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(100, (3, 3), activation='relu'))
此时我的模型摘要如下:
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 488, 638, 490) 4900
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 244, 319, 490) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 242, 317, 100) 441100
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 121, 158, 100) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 119, 156, 100) 90100
=================================================================
Total params: 536,100
Trainable params: 536,100
Non-trainable params: 0
然后我将得到以下内容:
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(8, activation='softmax'))
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
history = model.fit(train_images, train_labels, epochs=10,
validation_data=(test_images, test_labels))
问题是,当我尝试运行此命令时,出现以下错误:
ValueError:检查输入时出错:预期conv2d_1_input具有4个维,但数组的形状为(261,1)
我必须指出train_images.shape
返回值(261,)
,而train_images[0].shape
返回值(490, 640, 1)
。
从理论上讲,这对我来说很有意义,但是我仍然迷失在如何正确地为CNN整理数据以及检测我是否做错了其他事情上。
答案 0 :(得分:0)
问题是您的某些图像尺寸不相等。您可以轻松地进行验证,例如:
shapes = list()
for i in X:
shapes.append(i.shape)
sorted_shapes = np.sort(np.array(shapes).sum(axis=1))
assert sorted_shapes[0] == sorted_shapes[1], 'Not all pictures have equal size.'
如果此行引发错误,则必须裁剪/调整它们的大小。