CNN识别CIFAR-10 — 进行预测

1.执行预测

prediction = model.predict_classes(x_img_test_normalize)
2.预测结果

print(prediction[:10])
[3 8 8 0 6 6 1 6 3 1]
3.查看预测概率
(1)使用测试数据进行预测
我们使用model.predict输入测试数据,就可以预测概率

Predicted_Probability = model.predict(x_img_test_normalize)
(2)建立show_Predicted_Probability函数

def show_Predicted_Probability(y, prediction, x_img, Predicted_Probability, i):
    print('label:', label_dict[y[i][0]], 'predict', label_dict[prediction[i]])
    plt.figure(figsize=(2, 2))
    plt.imshow(np.reshape(x_img_test[i], (32, 32, 3)))
    plt.show()
    for j in range(10):
        print(label_dict[j] + ' probability:%1.9f' % (Predicted_Probability[i][j]))
(3)查看第1项数据的预测结果

label: cat predict cat
airplane probability:0.002762204
automobile probability:0.003279740
bird probability:0.010363355
cat probability:0.625037074
deer probability:0.012193166
dog probability:0.292445958
frog probability:0.034173731
horse probability:0.001061088
ship probability:0.017909575
truck probability:0.000774055
(3)查看第3项数据的预测结果

label: airplane predict airplane
airplane probability:0.649241984
automobile probability:0.002572708
bird probability:0.041379604
cat probability:0.002955899
deer probability:0.021011690
dog probability:0.000034731
frog probability:0.000030690
horse probability:0.000025793
ship probability:0.281421483
truck probability:0.001325390
4.显示混淆矩阵

{0: 'airplane', 1: 'automobile', 2: 'bird', 3: 'cat', 4: 'deer', 5: 'dog', 6: 'frog', 7: 'horse', 8: 'ship', 9: 'truck'}
predict    0    1    2    3    4    5    6    7    8    9
label                                                    
0        795    8   67    8   18    5   18    4   52   25
1         23  811   20    5    5    6   22    3   39   66
2         47    1  641   22  112   60   85   15   10    7
3         18   10  109  431   84  191  121   19    9    8
4         21    3   72   32  770   19   64   10    8    1
5         10    2   66  109   69  656   59   20    5    4
6          5    1   35   26   24   17  887    2    2    1
7         15    0   57   15  121   65   12  701    4   10
8         43   17   25    5   13    8   13    0  858   18
9         37   70   25   10    6    7   12   10   25  798