首页 > 解决方案 > Keras 模型 evaluate() 和 predict_classes() 给出了相互矛盾的结果

问题描述

我已经使用 Keras 的 VGG16 工作了一段时间,在对我自己的 4 类数据集进行微调之后,训练似乎进展顺利,训练和验证集的准确性提高了,最后,即使使用评估model.evaluate()也给出了很好的准确性关于测试数据。我试过confusion_matrix了,它也显示出很好的分类结果。为了确认,我尝试使用以下方法从我的数据集中预测图像:

im = cv2.resize(frame,(image_size, image_size), interpolation = cv2.INTER_AREA)

#convert the image pixels to a numpy array
framer = img_to_array(im)             
image = framer.reshape((1, framer.shape[0], framer.shape[1], framer.shape[2]))

# prepare the image for the VGG model
image = preprocess_input(image)  

label = FLOW1_model.predict_classes(image, verbose=0)

我只为 1 类(0 类)加载了流帧,但只有大约 30% 的图片被正确分类,而在另一类(1 类)中,分类正确率为 50%。考虑到我训练时的准确率超过 85%,而且混淆矩阵显示出非常好的结果,我觉得这很不正常。我在网上查看了所有可能的原因,但找不到问题所在......是否有一个已知问题可以解释结果中的这种差异?

编辑:

我基本上加载没有顶部的 Keras vgg,添加我自己的分类器并像这样编译模型:

# Compile the model method 2
sgd = SGD(lr=0.00001, decay = 1e-6, momentum=0.9, nesterov=True)
model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy'])

标签: pythontensorflowkerascomputer-vision

解决方案


据我所知,在多类输出的情况下,您不能使用 predict_classes() 方法。您是否已经尝试过:

FLOW1_model.predict(image, verbose=0)

推荐阅读