我是新来的人,所以如果有人可以解释这个错误,那会对我有很大帮助
代码:
train_image, test_image, train_label, test_label = train_test_split(X, Y, test_size=0.30,random_state=42)#splits data, 30% for test and 70% for train
train_image = train_image.reshape(train_image.shape[0],32,32,3)
test_image = test_image.reshape(test_image.shape[0],32,32,3)
train_label = to_categorical(train_label, num_classes=len(data['Class']))
test_label = to_categorical(test_label, num_classes=len(data['Class']))
model = Sequential()
conv_01 = Conv2D(filters = 32,kernel_size=(3,3),activation='relu',input_shape=(32,32,3))
model.add(conv_01)
conv_02 = Conv2D(filters=64,kernel_size=(3,3),activation='relu')
model.add(conv_02)
pool = MaxPool2D(pool_size = (2,2),strides = (2,2), padding = 'same')
model.add(pool)
conv_11 = Conv2D(filters=64,kernel_size=(3,3),activation='relu')
model.add(conv_11)
pool_2 = MaxPool2D(pool_size=(2,2),strides=(2,2),padding='same')
model.add(pool_2)
drop = Dropout(0.3)
model.add(drop)
conv_out = Conv2D(filters = 3,kernel_size=(1,1),activation='softmax')
model.add(conv_out)
model.compile(optimizer='Adam',loss='categorical_crossentropy',metrics=['accuracy'])
model.fit(train_image,train_label,epochs=10,verbose = 1,validation_data=(test_image,test_label))
tes_loss,test_acc = model.evaluate(test_image,test_label)
prediction = model.predict(test_image)
train_image.shape:(66256,32,32,3)
test_image.shape:(28396,32,32,3)
train_label.shape:(66256,58)
test_label.shape:(28396,58)
错误消息:
Traceback (most recent call last):
File "processing.py", line 59, in <module>
model.fit(train_image,train_label,epochs=10,verbose = 1,validation_data=(test_image,test_label))
File "/home/mihir/Desktop/myenv/lib/python3.5/site-packages/tensorflow/python/keras/engine/training.py", line 1278, in fit
validation_split=validation_split)
File "/home/mihir/Desktop/myenv/lib/python3.5/site-packages/tensorflow/python/keras/engine/training.py", line 917, in _standardize_user_data
exception_prefix='target')
File "/home/mihir/Desktop/myenv/lib/python3.5/site-packages/tensorflow/python/keras/engine/training_utils.py", line 182, in standardize_input_data
'with shape ' + str(data_shape))
ValueError: Error when checking target: expected conv2d_3 to have 4 dimensions, but got array with shape (66256, 58)
答案 0 :(得分:0)
在分类的上下文中,这似乎是您要尝试执行的任务,通常在开始时会有多个conv +池化层,然后它们后面是一个或几个密集层(即,完全连接的层,通常是缩写为“ fc”)。您需要考虑的另一件事是,您应该在第一个Dense层之前使用Flatten
层,以平整最后一个卷积层的输出。
考虑了以上几点后,这是您的代码:
model = Sequential()
model.add(Conv2D(filters = 32,kernel_size=(3,3),activation='relu',input_shape=(32,32,3)))
model.add(Conv2D(filters=64,kernel_size=(3,3),activation='relu'))
model.add(MaxPool2D(pool_size = (2,2),strides = (2,2), padding = 'same'))
model.add(Conv2D(filters=64,kernel_size=(3,3),activation='relu'))
model.add(MaxPool2D(pool_size=(2,2),strides=(2,2),padding='same'))
model.add(Conv2D(filters =128,kernel_size=(1,1),activation='relu'))
model.add(MaxPool2D(pool_size=(2,2),strides=(2,2),padding='same'))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(58, activation='softmax'))
model.summary()
这是模型摘要:
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 30, 30, 32) 896
_________________________________________________________________
conv2d_2 (Conv2D) (None, 28, 28, 64) 18496
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 14, 14, 64) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 12, 12, 64) 36928
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 6, 6, 64) 0
_________________________________________________________________
conv2d_4 (Conv2D) (None, 6, 6, 128) 8320
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 3, 3, 128) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 1152) 0
_________________________________________________________________
dense_1 (Dense) (None, 64) 73792
_________________________________________________________________
dropout_1 (Dropout) (None, 64) 0
_________________________________________________________________
dense_2 (Dense) (None, 58) 3770
=================================================================
Total params: 142,202
Trainable params: 142,202
Non-trainable params: 0
_________________________________________________________________