-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
99 lines (80 loc) · 3.16 KB
/
helpers.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
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import confusion_matrix
import numpy as np
def plt_rectangle(plt,x1,y1,x2,y2,label="", color="yellow", linewidth= 2):
'''
== Input ==
plt : matplotlib.pyplot object
label : string containing the object class name
x1 : top left corner x coordinate
y1 : top left corner y coordinate
x2 : bottom right corner x coordinate
y2 : bottom right corner y coordinate
'''
if(label != ""):
plt.text(x1,y1,label,fontsize=20,backgroundcolor="magenta")
plt.plot([x1,x1],[y1,y2], linewidth=linewidth,color=color)
plt.plot([x2,x2],[y1,y2], linewidth=linewidth,color=color)
plt.plot([x1,x2],[y1,y1], linewidth=linewidth,color=color)
plt.plot([x1,x2],[y2,y2], linewidth=linewidth,color=color)
def plot_loss_acc(history):
"""Plot training and (optionally) validation loss and accuracy"""
loss = history.history['loss']
epochs = range(1, len(loss) + 1)
plt.figure(figsize=(10, 10))
plt.subplot(2, 1, 1)
plt.plot(epochs, loss, '.--', label='Training loss')
final_loss = loss[-1]
title = 'Training loss: {:.4f}'.format(final_loss)
plt.ylabel('Loss')
if 'val_loss' in history.history:
val_loss = history.history['val_loss']
plt.plot(epochs, val_loss, 'o-', label='Validation loss')
final_val_loss = val_loss[-1]
title += ', Validation loss: {:.4f}'.format(final_val_loss)
plt.title(title)
plt.legend()
acc = history.history['accuracy']
plt.subplot(2, 1, 2)
plt.plot(epochs, acc, '.--', label='Training acc')
final_acc = acc[-1]
title = 'Training accuracy: {:.2f}%'.format(final_acc * 100)
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
if 'val_accuracy' in history.history:
val_acc = history.history['val_accuracy']
plt.plot(epochs, val_acc, 'o-', label='Validation acc')
final_val_acc = val_acc[-1]
title += ', Validation accuracy: {:.2f}%'.format(final_val_acc * 100)
plt.title(title)
plt.legend()
def plot_multiclass_heatmap(y_test, y_predict, labels):
y_pred = np.argmax(y_predict, axis=-1)
y_true=np.argmax(y_test, axis=-1)
cm = confusion_matrix(y_true, y_pred, normalize='true')
# cm_normalized = []
# for ligne in cm :
# sum = np.sum(ligne)
# new_ligne = []
# for value in ligne :
# new_ligne.append(value/sum)
# cm_normalized.append(new_ligne)
class_names = list(labels)
# Plot confusion matrix in a beautiful manner
plt.figure(figsize=(16, 14))
ax= plt.subplot()
cm = cm*100
sns.heatmap(cm, annot=True, ax = ax, fmt = '.1f'); #annot=True to annotate cells
# for t in ax.texts: t.set_text(t.get_text()+ " %")
# labels, title and ticks
ax.set_xlabel('Predicted', fontsize=20)
ax.xaxis.set_label_position('bottom')
plt.xticks(rotation=90)
ax.xaxis.set_ticklabels(class_names, fontsize = 10)
ax.xaxis.tick_bottom()
ax.set_ylabel('True', fontsize=20)
ax.yaxis.set_ticklabels(class_names, fontsize = 10)
plt.yticks(rotation=0)
plt.title('Refined Confusion Matrix', fontsize=20)
plt.show()