我正在尝试使用keras库执行二进制图像分类,但是在检查目标时出现ValueError错误:预期density_1具有4维,但数组的形状为(32,2)
classifier.add(Conv2D(filters=32, kernel_size=(3,3), input_shape=(64,64,3), activation='relu'))
classifier.add(Conv2D(filters=32, kernel_size=(3,3), activation='relu'))
#Pooling_layers
classifier.add(MaxPooling2D(pool_size=(2,2)))
#conv_layers
classifier.add(Conv2D(filters=32, kernel_size=(3,3), activation='relu'))
classifier.add(Conv2D(filters=32, kernel_size=(3,3), activation='relu'))
#Max_pooling_layers
classifier.add(MaxPooling2D(pool_size=(2,2)))
classifier.add(Dense(units=2, activation='softmax'))
#Compile_classifier
classifier.compile(optimizer = 'adam', loss='categorical_crossentropy', metrics=['accuracy'])
from keras.preprocessing.image import ImageDataGenerator
train_datagen=ImageDataGenerator(rescale=1./255)
train_set = train_datagen.flow_from_directory(
'dataset/train',
target_size=(64,64),
batch_size=32,
class_mode='categorical')
test_datagen=ImageDataGenerator(rescale=1./255)
test_set = test_datagen.flow_from_directory(
'dataset/test',
target_size=(64,64),
batch_size=32,
class_mode='categorical')
classifier.fit_generator(
train_set,
steps_per_epoch=9000,
epochs=25,
validation_data=test_set,
validation_steps=3000)
在执行程序时,出现此错误消息。
ValueError: Error when checking target: expected dense_1 to have 4 dimensions, but got array with shape (32, 2)