forked from foamliu/Car-Recognition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyze.py
127 lines (100 loc) · 3.59 KB
/
analyze.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
# -*- coding: utf-8 -*-
import os
import matplotlib.pyplot as plt
import numpy as np
from keras.preprocessing import image
from sklearn.metrics import confusion_matrix
from tqdm import tqdm
from utils import load_model
def decode_predictions(preds, top=5):
results = []
for pred in preds:
top_indices = pred.argsort()[-top:][::-1]
result = [(class_names[i], pred[i]) for i in top_indices]
result.sort(key=lambda x: x[1], reverse=True)
results.append(result)
return results
def predict(img_dir, model):
img_files = []
for root, dirs, files in os.walk(img_dir, topdown=False):
for name in files:
img_files.append(os.path.join(root, name))
img_files = sorted(img_files)
y_pred = []
y_test = []
for img_path in tqdm(img_files):
# print(img_path)
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
preds = model.predict(x[None, :, :, :])
decoded = decode_predictions(preds, top=1)
pred_label = decoded[0][0][0]
# print(pred_label)
y_pred.append(pred_label)
tokens = img_path.split(os.pathsep)
class_id = int(tokens[-2])
# print(str(class_id))
y_test.append(class_id)
return y_pred, y_test
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
print(cm)
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
# tick_marks = np.arange(len(classes))
# plt.xticks(tick_marks, classes, rotation=45)
# plt.yticks(tick_marks, classes)
# fmt = '.2f' if normalize else 'd'
# thresh = cm.max() / 2.
# for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
# plt.text(j, i, format(cm[i, j], fmt),
# horizontalalignment="center",
# color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
def calc_acc(y_pred, y_test):
num_corrects = 0
for i in range(num_samples):
pred = y_pred[i]
test = y_test[i]
if pred == test:
num_corrects += 1
return num_corrects / num_samples
if __name__ == '__main__':
img_width, img_height = 224, 224
num_channels = 3
num_classes = 196
class_names = range(1, (num_classes + 1))
num_samples = 1629
print("\nLoad the trained ResNet model....")
model = load_model()
y_pred, y_test = predict('data/valid', model)
print("y_pred: " + str(y_pred))
print("y_test: " + str(y_test))
acc = calc_acc(y_pred, y_test)
print("%s: %.2f%%" % ('acc', acc * 100))
# Compute confusion matrix
cnf_matrix = confusion_matrix(y_test, y_pred)
np.set_printoptions(precision=2)
# Plot non-normalized confusion matrix
plt.figure()
plot_confusion_matrix(cnf_matrix, classes=class_names,
title='Confusion matrix, without normalization')
# Plot normalized confusion matrix
plt.figure()
plot_confusion_matrix(cnf_matrix, classes=class_names, normalize=True,
title='Normalized confusion matrix')
plt.show()