# -*- 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()