-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test_MNIST_Model.py
154 lines (120 loc) · 4.38 KB
/
Test_MNIST_Model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# -*- coding: utf-8 -*-
"""
Created on Friday 2 Feb 2020
@author: Chris.Cui
Email: [email protected]
"""
#%% load all the pkgs
from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import load_model
from keras import backend as K
from keras.utils import to_categorical
import numpy as np
#%% Load the data
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
#%% Data preprocessing
num_classes = 10
# input image dimensions
img_rows, img_cols = 28, 28
if K.image_data_format() == 'channels_first':
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
#%%
best_model_filepath = r"./models/mnist.weights.best.h5"
Model = load_model(best_model_filepath)
Model.summary()
#%%
score_train = Model.evaluate(x_train, y_train, verbose=1)
print('Train loss:', score_train[0])
print('Train accuracy:', score_train[1])
score_test = Model.evaluate(x_test, y_test, verbose=1)
print('Test loss:', score_test[0])
print('Test accuracy:', score_test[1])
#%% check results
from sklearn.metrics import classification_report, confusion_matrix
from matplotlib import pyplot as plt
import seaborn as sn
#%% make a single prediction
random_id = np.random.randint(len(x_train))
x = x_train[random_id,:,:,:]
plt.figure()
plt.imshow(np.squeeze(x)) # remove extra dimensions
plt.colorbar()
plt.grid(False)
plt.show()
x = np.expand_dims(x, axis=0)
y = Model.predict(x)
np.set_printoptions(formatter={'float': '{: 0.3f}'.format})
print('output vector {}'.format(y))
print('label: {}'.format(np.argmax(y)))
#%% make a batch prediction to get comfusion matrix
y_train_label = np.argmax(y_train, axis=1)
y_train_predic = Model.predict(x_train, verbose=1)
y_train_predic_label = np.argmax(y_train_predic, axis=1)
cm_train = confusion_matrix(y_train_label, y_train_predic_label)
print(cm_train)
plt.imshow(cm_train )
#%% testing comfusion matrix
y_test_label = np.argmax(y_test, axis=1)
y_test_predic = Model.predict(x_test, verbose=1)
y_test_predic_label = np.argmax(y_test_predic, axis=1)
cm_test = confusion_matrix(y_test_label, y_test_predic_label)
print(cm_test)
plt.imshow(cm_test )
#%% precision recall
def precision(label, confusion_matrix):
col = confusion_matrix[:, label]
return confusion_matrix[label, label] / col.sum()
def recall(label, confusion_matrix):
row = confusion_matrix[label, :]
return confusion_matrix[label, label] / row.sum()
def precision_macro_average(confusion_matrix):
rows, columns = confusion_matrix.shape
sum_of_precisions = 0
for label in range(rows):
sum_of_precisions += precision(label, confusion_matrix)
return sum_of_precisions / rows
def recall_macro_average(confusion_matrix):
rows, columns = confusion_matrix.shape
sum_of_recalls = 0
for label in range(columns):
sum_of_recalls += recall(label, confusion_matrix)
return sum_of_recalls / columns
print("Train: label precision recall")
cm = cm_train
for label in range(10):
print(f"{label:5d} {precision(label, cm):9.3f} {recall(label, cm):6.3f}")
print("precision total:", precision_macro_average(cm))
print("recall total:", recall_macro_average(cm))
print("Tests label precision recall")
cm = cm_test
for label in range(10):
print(f"{label:5d} {precision(label, cm):9.3f} {recall(label, cm):6.3f}")
print("precision total:", precision_macro_average(cm))
print("recall total:", recall_macro_average(cm))
#%% Accuracy
def accuracy(confusion_matrix):
diagonal_sum = confusion_matrix.trace()
sum_of_all_elements = confusion_matrix.sum()
return diagonal_sum / sum_of_all_elements
print("Train accuracy total:", accuracy(cm_train))
print("Train accuracy total:", accuracy(cm_test))
# %%