-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils.py
211 lines (171 loc) · 5.93 KB
/
utils.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import pandas as pd
import numpy as np
import sys
import matplotlib.pyplot as plt
from sklearn.metrics import roc_curve, auc
CLASSES_DEFINICATION_PATH = 'data\\label\\classes.json'
def pairwise(it):
it = iter(it)
while True:
try:
yield next(it), next(it)
except StopIteration:
# no more elements in the iterator
return
def hexaToRgb(codeHexa):
r_hex = codeHexa[1:3]
g_hex = codeHexa[3:5]
b_hex = codeHexa[5:7]
return int(r_hex, 16), int(g_hex, 16), int(b_hex, 16)
def isMulticlassDataset():
df = pd.read_json(CLASSES_DEFINICATION_PATH)
if(len(df)>1):
return True
else:
return False
def getClassesLabelList():
classes = []
if (isMulticlassDataset()):
classes.append('background')
df = pd.read_json(CLASSES_DEFINICATION_PATH)
df = df.sort_values(['id'], ascending=[True])
for index, row in df.iterrows():
classes.append(row['name'])
return classes
def getPaletteColors():
# 0 : background
palette = [(0,0,0)]
df = pd.read_json(CLASSES_DEFINICATION_PATH)
df = df.sort_values(['id'], ascending=[True])
for index, row in df.iterrows():
r,g,b = hexaToRgb(row['color'])
palette.append(
tuple((r, g, b))
)
palette.append((0,255,0))
print("________________________")
#print(palette[0])
return palette
def mask2img(mask):
#Pour du Integer labeling
'''
palette = {
0: (0, 0, 0),
1: (255, 0, 0),
2: (0, 255, 0),
3: (0, 0, 255),
4: (0, 255, 255),
}
'''
palette = getPaletteColors()
rows = mask.shape[0]
cols = mask.shape[1]
image = np.zeros((rows, cols, 3), dtype=np.uint8)
for j in range(rows):
for i in range(cols):
image[j, i] = palette[np.argmax(mask[j, i])]
return image
def mask2imgMultipleClasses(mask):
#pour du one hot encoding
'''
palette = {
0: (0, 0, 0),
1: (255, 0, 0),
2: (0, 255, 0),
3: (0, 0, 255),
4: (0, 255, 255),
}
'''
np.set_printoptions(threshold=sys.maxsize)
palette = getPaletteColors()
rows = mask.shape[0]
cols = mask.shape[1]
image = np.zeros((rows, cols, 3), dtype=np.uint8)
for j in range(rows):
for i in range(cols):
try:
image[j, i] = palette[mask[j, i]]
except IndexError:
pass
#print(mask[j, i])
return image
def plot_roc_curve(y_true, y_pred, title):
fpr_keras, tpr_keras, thresholds_keras = roc_curve(y_true, y_pred)
auc_keras = auc(fpr_keras, tpr_keras)
fig, ax = plt.subplots(1, 1)
ax.plot(fpr_keras, tpr_keras, label='ROC curve (area = %0.3f)' % auc_keras)
ax.plot([0, 1], [0, 1], 'k--')
# ax.set_xlim([0.0, 1.0])
# ax.set_ylim([0.0, 1.05])
ax.set_xlabel('False Positive Rate')
ax.set_ylabel('True Positive Rate')
ax.set_title(title)
ax.legend(loc="lower right")
#plt.show()
plt.savefig('./result/log/rocCurve/curve_{}.png'.format(title))
print('fpr : {}'.format(fpr_keras))
print('tpr : {}'.format(tpr_keras))
print('auc : {}'.format(auc_keras))
def plot_confusion_matrix(cm,
target_names,
title='Confusion matrix',
cmap=None,
normalize=True):
"""
given a sklearn confusion matrix (cm), make a nice plot
Arguments
---------
cm: confusion matrix from sklearn.metrics.confusion_matrix
target_names: given classification classes such as [0, 1, 2]
the class names, for example: ['high', 'medium', 'low']
title: the text to display at the top of the matrix
cmap: the gradient of the values displayed from matplotlib.pyplot.cm
see http://matplotlib.org/examples/color/colormaps_reference.html
plt.get_cmap('jet') or plt.cm.Blues
normalize: If False, plot the raw numbers
If True, plot the proportions
Usage
-----
plot_confusion_matrix(cm = cm, # confusion matrix created by
# sklearn.metrics.confusion_matrix
normalize = True, # show proportions
target_names = y_labels_vals, # list of names of the classes
title = best_estimator_name) # title of graph
"""
import matplotlib.pyplot as plt
import numpy as np
import itertools
accuracy = np.trace(cm) / float(np.sum(cm))
misclass = 1 - accuracy
if cmap is None:
cmap = plt.get_cmap('Blues')
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
plt.figure(figsize=(8, 6))
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
if target_names is not None:
tick_marks = np.arange(len(target_names))
plt.xticks(tick_marks, target_names, rotation=45)
plt.yticks(tick_marks, target_names)
thresh = cm.max() / 1.5 if normalize else cm.max() / 2
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
if normalize:
plt.text(j, i, "{:0.4f}".format(cm[i, j]),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
else:
plt.text(j, i, "{:,}".format(cm[i, j]),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.ylabel('True label')
plt.xlabel('Predicted label\naccuracy={:0.4f}; misclass={:0.4f}'.format(accuracy, misclass))
plt.tight_layout()
#plt.show()
plt.savefig('./result/log/confusionMatrix/matrix_{}.png'.format(title))
if __name__ == '__main__':
"""
# MAIN
"""
#getPaletteColors()