简体   繁体   English

如何在测试集中找到错误的预测案例(使用 Keras 的 CNNs)

[英]How to find wrong prediction cases in test set (CNNs using Keras)

I'm using MNIST example with 60000 training image and 10000 testing image.我正在使用带有 60000 个训练图像和 10000 个测试图像的 MNIST 示例。 How do I find which of the 10000 testing image that has an incorrect classification/prediction?如何找到 10000 张测试图像中的哪一张分类/预测不正确?

Simply use model.predict_classes() and compare the output with true labes.只需使用model.predict_classes()并将输出与真实实验室进行比较。 ie:即:

incorrects = np.nonzero(model.predict_class(X_test).reshape((-1,)) != y_test)

to get indices of incorrect predictions获取错误预测的索引

Editing as was not clear earlier编辑之前不清楚

To identify the image files that are wrongly classified, you can use:要识别错误分类的图像文件,您可以使用:

fnames = test_generator.filenames ## fnames is all the filenames/samples used in testing
errors = np.where(y_pred != test_generator.classes)[0] ## misclassifications done on the test data where y_pred is the predicted values
for i in errors:
    print(fnames[i])

If you want to see images inclorrectly classified you can try:如果您想查看分类错误的图像,您可以尝试:

#to get an array with predictions:
predict = np.argmax(model.predict(X_val), axis=1)

#to get an array with true labels:
true_y_val = np.argmax(y_val, axis=1)

#to get an array with erros positions:~
errors = np.flatnonzero(predict != true_y_val)

#to find images wrongly classified:
for i in errors:
  plt.imshow(X_val[i])
  plt.show()
  print("Predicted label:", predict[i])
  print("True label:", true_y_val[i])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM