我试图拟合()我的CNN模型,但我遇到了层协同工作的问题。
from keras.engine import input_layer
from keras.models import Sequential
from keras.layers import Dense , Activation , Dropout ,Flatten, BatchNormalization
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
# The model is as follows...
face_model = Sequential()
input_shape_face = (48, 48, 1)
face_model.add(Conv2D(8, kernel_size= (3, 3), input_shape = input_shape_face, padding= 'same', activation = 'LeakyReLU'))
face_model.add(MaxPooling2D(pool_size = (2, 2), padding= 'same'))
face_model.add(Conv2D(16, kernel_size= (3, 3), padding= 'same', activation = 'LeakyReLU'))
face_model.add(MaxPooling2D(pool_size = (2, 2), padding= 'same'))
face_model.add(Conv2D(32, kernel_size= (3, 3), padding= 'same', activation = 'LeakyReLU'))
face_model.add(MaxPooling2D(pool_size = (2, 2), padding= 'same'))
face_model.add(Conv2D(64, kernel_size= (3, 3), padding= 'same', activation = 'LeakyReLU'))
face_model.add(Flatten())
face_model.add(Dense(128, activation = 'LeakyReLU'))
face_model.add(Dense(6, activation = 'softmax'))
face_model.summary()
我的图层汇总:
Model: "sequential_34"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_92 (Conv2D) (None, 48, 48, 8) 80
max_pooling2d_69 (MaxPoolin (None, 24, 24, 8) 0
g2D)
conv2d_93 (Conv2D) (None, 24, 24, 16) 1168
max_pooling2d_70 (MaxPoolin (None, 12, 12, 16) 0
g2D)
conv2d_94 (Conv2D) (None, 12, 12, 32) 4640
max_pooling2d_71 (MaxPoolin (None, 6, 6, 32) 0
g2D)
conv2d_95 (Conv2D) (None, 6, 6, 64) 18496
flatten_8 (Flatten) (None, 2304) 0
dense_57 (Dense) (None, 128) 295040
dense_58 (Dense) (None, 6) 774
=================================================================
Total params: 320,198
Trainable params: 320,198
Non-trainable params: 0
_________________________________________________________________
# Compiling the model
face_model.compile(loss= 'categorical_crossentropy', optimizer= 'adam', metrics= ['accuracy'])
face_model.fit(facial_training_set, batch_size= batch_size, epochs= epochs, verbose= 1, validation_data= facial_testing_set)
收到的错误:
/usr/local/lib/python3.9/dist-packages/keras/engine/training.pyin tf__train_function(iterator)13 try:14 do_return = True ---〉15 retval_ = ag__.converted_call(ag__.ld(step_function),(ag__.ld(self),ag__.ld(iterator)),None,fscope)16除了:17 do_return = False
用户代码中的ValueError:
File "/usr/local/lib/python3.9/dist-packages/keras/engine/training.py",
第1284行,在train_function * return step_function(self,iterator)文件“/usr/local/lib/python3.9/dist-packages/keras/engine/training.py“,第1268行,在step_functionoutputs = model.distribute_strategy.run(run_step,args=(data,))文件“/usr/local/lib/python3.9/dist-packages/keras/engine/training.py“,第1249行,in run_stepoutputs = model.train_step(data)File“/usr/local/lib/python3.9/dist-packages/keras/engine/training.py“,line 1050,in train_step y_pred = self(x,training=True)File“/usr/local/lib/python3.9/dist-packages/keras/utils/traceback_utils.py”,line 70,在error_handler中,从None文件“/usr/local/lib/python3.9/dist-packages/keras/engine/input_spec.py”,第280行中引发e.with_traceback(filtered_tb),在assert_input_compatibility中,引发ValueError(
ValueError: Exception encountered when calling layer 'sequential_34' (type Sequential).
Input 0 of layer "dense_57" is incompatible with the layer: expected axis -1 of input shape to have value 2304, but received input
形状(48,384)
Call arguments received by layer 'sequential_34' (type Sequential):
• inputs=tf.Tensor(shape=(48, 48, 1), dtype=float32)
• training=True
• mask=None
2条答案
按热度按时间nuypyhwy1#
如果可以的话试试这个!我猜输入形状不兼容。
v64noz0r2#
你试过改变输入数据的形状吗?
facial_training_set = np.array(facial_training_set).reshape((-1, 48, 48, 1))
facial_testing_set = np.array(facial_testing_set).reshape((-1, 48, 48, 1))