-
Notifications
You must be signed in to change notification settings - Fork 0
/
myMetrics.py
274 lines (205 loc) · 8.21 KB
/
myMetrics.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
from Utils import *
from adj_mat_func import adj_mat_func
from tensorflow.python.keras import backend as k
def custom_adj_loss_l1(batch_size, adj_train, lambda_loss=1):
def adj_loss(y_true, y_pred):
Y_pred = k.argmax(y_pred)
Y_true = k.argmax(y_true)
adj = adj_mat_func(batch_size)
adj_pred = adj.adj_mat(Y_pred, Y_true)
adj_pred = tf.norm(tensor=adj_pred, ord=1, axis=1)
adj_true = adj.adj_mat(Y_true, Y_pred)
adj_true = tf.norm(tensor=adj_true, ord=1, axis=1)
# L1
mod = k.abs(adj_pred - adj_true)
global adj_loss_value
adj_loss_value = lambda_loss * k.mean(mod)
global categ_loss
categ_loss = k.categorical_crossentropy(y_true, y_pred)
loss = adj_loss_value + categ_loss
return loss
return adj_loss
def custom_adj_loss_l2(batch_size, adj_train, lambda_loss=1):
def adj_loss(y_true, y_pred):
Y_pred = k.argmax(y_pred)
Y_true = k.argmax(y_true)
adj = adj_mat_func(batch_size)
adj_pred = adj.adj_mat(Y_pred, Y_true)
adj_pred = tf.norm(tensor=adj_pred, ord=1, axis=1)
adj_true = adj.adj_mat(Y_true, Y_pred)
adj_true = tf.norm(tensor=adj_true, ord=1, axis=1)
# L2
quad = (adj_pred - adj_true)
quad = quad * quad
global adj_loss_value
adj_loss_value = lambda_loss * k.mean(quad)
global categ_loss
categ_loss = k.categorical_crossentropy(y_true, y_pred)
loss = adj_loss_value + categ_loss
return loss
return adj_loss
def custom_adj_loss_frobenius(batch_size, adj_train, lambda_loss=1):
def adj_loss(y_true, y_pred):
Y_pred = k.argmax(y_pred)
Y_true = k.argmax(y_true)
adj = adj_mat_func(batch_size)
adj_pred = adj.adj_mat(Y_pred, Y_true)
adj_pred = tf.norm(tensor=adj_pred, ord=1, axis=1)
adj_true = adj.adj_mat(Y_true, Y_pred)
adj_true = tf.norm(tensor=adj_true, ord=1, axis=1)
# L2
quad = (adj_pred - adj_true)
quad = quad * quad
sqrt = k.sqrt(quad)
global adj_loss_value
adj_loss_value = lambda_loss * k.mean(sqrt)
global categ_loss
categ_loss = k.categorical_crossentropy(y_true, y_pred)
loss = adj_loss_value + categ_loss
return loss
return adj_loss
def metric_adj(y_true, y_pred):
return adj_loss_value
def metric_categ_cross(y_true, y_pred):
return categ_loss
def custom_loss(layer):
def cat_loss(y_true, y_pred):
logits = layer.output
loss = k.categorical_crossentropy(y_true, logits, from_logits=True)
mask = k.sum(y_true, -1)
mask = mask > 0
loss = tf.boolean_mask(loss, mask)
loss = k.mean(loss, axis=None, keepdims=False)
return loss
# Return a function
return cat_loss
def compute_and_print_IoU_per_class(confusion_matrix, num_classes, class_mask=None, namePart=[]):
"""
Computes and prints mean intersection over union divided per class
:param confusion_matrix: confusion matrix needed for the computation
"""
mIoU = 0
mIoU_nobackgroud = 0
IoU_per_class = np.zeros([num_classes], np.float32)
true_classes = 0
per_class_pixel_acc = np.zeros([num_classes], np.float32)
mean_class_acc_num = 0
# out = ''
# out_pixel_acc = ''
# index = ''
true_classes_pix = 0
mean_class_acc_den = 0
mean_class_acc_num_nobgr = 0
mean_class_acc_den_nobgr = 0
mean_class_acc_sum_nobgr = 0
mean_class_acc_sum = 0
if class_mask == None:
class_mask = np.ones([num_classes], np.int8)
for i in range(num_classes):
if class_mask[i] == 1:
# IoU = true_positive / (true_positive + false_positive + false_negative)
TP = confusion_matrix[i, i]
FP = np.sum(confusion_matrix[:, i]) - TP
FN = np.sum(confusion_matrix[i]) - TP
# TN = np.sum(confusion_matrix) - TP - FP - FN
denominator = (TP + FP + FN)
# If the denominator is 0, we need to ignore the class.
if denominator == 0:
denominator = 1
print(namePart[i])
else:
true_classes += 1
# per-class pixel accuracy
if not TP == 0:
# if not np.isnan(TP):
tmp = (TP + FN)
per_class_pixel_acc[i] = TP / tmp
IoU = TP / denominator
IoU_per_class[i] += IoU
mIoU += IoU
if i > 0:
mIoU_nobackgroud += IoU
# mean class accuracy
if not np.isnan(per_class_pixel_acc[i]):
mean_class_acc_num += TP
mean_class_acc_den += TP + FN
mean_class_acc_sum += per_class_pixel_acc[i]
true_classes_pix += 1
if i > 0:
mean_class_acc_num_nobgr += TP
mean_class_acc_den_nobgr += TP + FN
mean_class_acc_sum_nobgr += per_class_pixel_acc[i]
mIoU = mIoU / true_classes
mIoU_nobackgroud = mIoU_nobackgroud / (true_classes - 1)
mean_pix_acc = mean_class_acc_num / mean_class_acc_den
mean_pixel_acc_nobackground = mean_class_acc_num_nobgr / mean_class_acc_den_nobgr
print("---------------------------------------------------------------------------")
print("True_classes: " + str(true_classes))
print("---------------------------------------------------------------------------")
print("-- background --")
print("IoU for class background : " + str(IoU_per_class[0] * 100))
print("Pixel Acc for class background : " + str(per_class_pixel_acc[0] * 100))
print("---------------------------------------------------------------------------")
zero_classes = []
for k in range(1, num_classes):
if IoU_per_class[k] > 0:
print("-- " + str(k) + " -- " + namePart[k] + " --")
print("IoU for class " + namePart[k] + " :" + str(IoU_per_class[k] * 100))
print("Pixel Acc for class " + str(k) + " :" + str(per_class_pixel_acc[k] * 100))
print("---------------------------------------------------------------------------")
else:
zero_classes.append(k)
print(" ")
print("--METRICS--")
print(' mean_class_acc :' + str((mean_class_acc_sum / true_classes_pix) * 100))
print(' mean pix acc :' + str(mean_pix_acc * 100))
print(' mean_pixel_acc_no_background :' + str(mean_pixel_acc_nobackground * 100))
print(" mIoU:" + str(mIoU * 100))
print(" mIoU_nobackgroud:" + str(mIoU_nobackgroud * 100))
print("---------------------------------------------------------------------------")
for j in range(len(zero_classes)):
print("class not found " + str(zero_classes[j]) + "_" + namePart[zero_classes[j]])
print("Classes_" + str(108 - len(zero_classes)) + "/108")
return mIoU * 100, IoU_per_class
# def custom_adj_loss(batch, adj_train):
# def adj_loss(y_true, y_pred):
# # adj_mat[classes[j]][classes[i]] = 1 + adj_mat[classes[j]][classes[i]]
#
# loss = k.variable(value=tf.reduce_mean(tf.abs(adj_mat)))
#
# return loss
#
# # Return a function
# return adj_loss
# def custom_metric_adj_loss(batch_size, adj_train):
# def adj_loss(y_true, y_pred):
# Y_pred = k.argmax(y_pred)
# Y_true = k.argmax(y_true)
#
# adj = adj_mat_func(batch_size)
#
# adj_pred = adj.adj_mat(Y_pred, Y_true)
# adj_pred = tf.norm(tensor=adj_pred, ord=1, axis=1)
#
# adj_true = adj.adj_mat(Y_true, Y_pred)
# adj_true = tf.norm(tensor=adj_true, ord=1, axis=1)
#
# mod = k.abs(adj_pred - adj_true)
# loss = k.mean(mod)
#
# return loss
#
# return adj_loss
# def custom_categ_losses(y_true, y_pred):
# loss = k.categorical_crossentropy(y_true, y_pred, from_logits=True)
# mask = k.sum(y_true, -1)
# mask = mask > 0
# loss = tf.boolean_mask(loss, mask)
# loss = k.mean(loss, axis=None, keepdims=False)
#
# return loss
# from tensorflow.python.keras import metrics
# def accuracy_ignoring_first_label(y_true, y_pred):
# Y_true = y_true[:, :, 1:]
# Y_pred = y_pred[:, :, 1:]
# return metrics.categorical_accuracy(Y_true, Y_pred)