-
Notifications
You must be signed in to change notification settings - Fork 2
/
generateConfusionMatrix.py
125 lines (98 loc) · 3.47 KB
/
generateConfusionMatrix.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
# IMPORT
import matplotlib.pyplot as plt
from tqdm import tqdm
import os
from PIL import Image
import numpy as np
from sklearn.metrics import confusion_matrix
import itertools
from keras.models import load_model
import librosa
import librosa.display
import matplotlib.pyplot as plt
"""
# Classe permettant de génerer une matrice de confusion à partir d'un dataset de test et d'un modèle entrainé
# au préalable
"""
def generateMatrix(model, datasetTestPath, imageSize, destinationMatrix):
"""
# Fonction qui va construire notre matrice de confusion
:param model: chemin du modèle à charger pour realiser la prediction
:param datasetTestPath: chemin du dataset contenant nos images de test
:param imageSize: definit la taille de l'ensemble de nos images
:param destinationMatrix: définit le chemin ou va être sauvegardé notre matrice sous format d'image
"""
#Les tableaux contenanrt les predictions
y_true = []
y_pred = []
total = 0
success = 0
index = 0
print('\nEvaluation :')
#On parcours notre dataset de test
for root, dirs, files in os.walk(datasetTestPath):
for mydir in dirs:
for sample in tqdm(os.listdir(root + '\\' + mydir), "Prediction de la classe '{}'".format(mydir)):
sample_path = root + '\\' + mydir + '\\' + sample
#Chargement et traitement de l'image
y, sr = librosa.load(sample_path)
temp = librosa.feature.melspectrogram(y=y, sr=sr)
librosa.display.specshow(librosa.power_to_db(temp, ref=np.max))
canvas = plt.get_current_fig_manager().canvas
canvas.draw()
img = Image.frombytes('RGB', canvas.get_width_height(), canvas.tostring_rgb())
img = img.resize(size=imageSize)
img = np.asarray(img) / 255.
#On reshape pour etre de la forme (nbImage,hauteurImage,largeurImage,nbCanaux)
img = img.reshape(1, img.shape[0], img.shape[1], img.shape[2])
#Prediction de notre modele
pred = np.argmax(model.predict(img))
total += 1
if pred == index:
success += 1
y_true.append(index)
y_pred.append(pred)
index += 1
#Precision de notre modele sur notre jeu de test en entier
accuracy = (success / total) * 100.
print('\nPrecision : {0:.3f}%'.format(accuracy))
cnf_matrix = confusion_matrix(y_true, y_pred)
np.set_printoptions(precision=2)
# Plot normalized confusion matrix
plt.figure()
cmap = plt.cm.Blues
classes = ['chat', 'chien']
title = 'Confusion matrix'
cnf_matrix = cnf_matrix.astype('float') / cnf_matrix.sum(axis=1)[:, np.newaxis]
#Legende de notre matrice
plt.imshow(cnf_matrix, 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'
thresh = cnf_matrix.max() / 2.
for i, j in itertools.product(range(cnf_matrix.shape[0]), range(cnf_matrix.shape[1])):
plt.text(j, i, format(cnf_matrix[i, j], fmt), horizontalalignment="center", color="white" if cnf_matrix[i, j] > thresh else "black")
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.tight_layout()
#On sauvegarde notre matrice en image
plt.savefig(destinationMatrix + '\\' + 'confusionMatrix')
def main():
"""
# Fonction main
"""
#On definit les chemins de nos divers ressources
modelPath = '.\\trainedModel\\moModel.hdf5'
datasetTestPath = '.\\datasetTest'
destinationMatrix = '.\\graph'
imageSize = (50, 50)
model = load_model(modelPath)
generateMatrix(model, datasetTestPath, imageSize, destinationMatrix)
if __name__ == "__main__":
"""
# MAIN
"""
main()