Keras 验证精度很高,但预测不好

Keras 验证精度很高,但预测不好,keras,conv-neural-network,Keras,Conv Neural Network,我正在建立一个keras模型来对猫和狗进行分类。我使用了带有瓶颈特性的迁移学习和vgg模型的微调。现在我得到了非常好的验证准确率,比如97%,但是当我预测时,我得到了关于分类报告和混淆矩阵的非常糟糕的结果。有什么问题吗 这是微调的代码和我得到的结果 base_model = applications.VGG16(weights='imagenet', include_top=False, input_shape=(150,150,3)) print('Model loaded.') # bui

我正在建立一个keras模型来对猫和狗进行分类。我使用了带有瓶颈特性的迁移学习和vgg模型的微调。现在我得到了非常好的验证准确率,比如97%,但是当我预测时,我得到了关于分类报告和混淆矩阵的非常糟糕的结果。有什么问题吗

这是微调的代码和我得到的结果

base_model = applications.VGG16(weights='imagenet', include_top=False, input_shape=(150,150,3))
print('Model loaded.')

# build a classifier model to put on top of the convolutional model
top_model = Sequential()
top_model.add(Flatten(input_shape=base_model.output_shape[1:]))
top_model.add(Dense(256, activation='relu'))
top_model.add(Dropout(0.5))
top_model.add(Dense(2, activation='sigmoid'))

# note that it is necessary to start with a fully-trained
# classifier, including the top classifier,
# in order to successfully do fine-tuning
top_model.load_weights(top_model_weights_path)

# add the model on top of the convolutional base
# model.add(top_model)
model = Model(inputs=base_model.input, outputs=top_model(base_model.output))

# set the first 25 layers (up to the last conv block)
# to non-trainable (weights will not be updated)
for layer in model.layers[:15]:
    layer.trainable = False

# compile the model with a SGD/momentum optimizer
# and a very slow learning rate.
model.compile(loss='binary_crossentropy',
              optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),
              metrics=['accuracy'])

# prepare data augmentation configuration
train_datagen = ImageDataGenerator(
    rescale=1. / 255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1. / 255)

train_generator = train_datagen.flow_from_directory(
    train_data_dir,
    target_size=(img_height, img_width),
    batch_size=batch_size,
    class_mode='categorical')

validation_generator = test_datagen.flow_from_directory(
    validation_data_dir,
    target_size=(img_height, img_width),
    batch_size=batch_size,
    class_mode='categorical')

model.summary()

# fine-tune the model
model.fit_generator(
    train_generator,
    steps_per_epoch=nb_train_samples // batch_size,
    epochs=epochs,
    validation_data=validation_generator,
    validation_steps=nb_validation_samples // batch_size,
    verbose=2)
scores=model.evaluate_generator(generator=validation_generator,
steps=nb_validation_samples // batch_size)
print("Accuracy = ", scores[1])

Y_pred = model.predict_generator(validation_generator, nb_validation_samples // batch_size)

y_pred = np.argmax(Y_pred, axis=1)

print('Confusion Matrix')

print(confusion_matrix(validation_generator.classes, y_pred))

print('Classification Report')

target_names = ['Cats', 'Dogs']

print(classification_report(validation_generator.classes, y_pred, target_names=target_names))
model.save("model_tuned.h5")
精度=0.974375

混淆矩阵 [[186 214] [199201]]

分类报告

          precision    recall  f1-score   support

    Cats       0.48      0.47      0.47       400
    Dogs       0.48      0.50      0.49       400
微平均值0.48 0.48 0.48 800 宏平均值0.48 0.48 0.48 800
加权平均值0.48 0.48 0.48 800

您的模型存在两个问题。首先,如果您有多个输出神经元,则需要使用softmax激活:

top_model.add(Dense(2, activation='softmax'))
然后你必须使用分类交叉熵,二元交叉熵只适用于当你有一个乙状激活的输出神经元时

model.compile(loss='categorical_crossentropy',
              optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),
              metrics=['accuracy'])

我认为问题在于您应该在验证生成器中添加shuffle=False

validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='categorical',
shuffle=False)
问题是默认行为是洗牌图像,以便

validation_generator.classes

与发电机不匹配实际上我很困惑。我有两门课,可以用softmaxYes吗?可以,我已经做了你推荐的修改,但是我仍然得到了不好的预测。还有其他建议吗?谢谢你的帮助,sirI我也有同样的问题,基本上,你问题的标题是不正确的。你的训练准确率很高,但分类报告没有反映真实情况。
validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='categorical',
shuffle=False)