-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmyMetrics.py
More file actions
229 lines (202 loc) · 8.3 KB
/
myMetrics.py
File metadata and controls
229 lines (202 loc) · 8.3 KB
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
import numpy as np
import tensorflow as tf
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)
def compute_and_print_IoU_per_class(confusion_matrix, num_classes, class_mask=None):
"""
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
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("---------------------------------------------------------------------------")
for k in range(0, num_classes):
if IoU_per_class[k] > 0:
print("IoU for class " + str(k) + " :" + str(IoU_per_class[k] * 100))
print("Pixel Acc for class " + str(k) + " :" + str(per_class_pixel_acc[k] * 100))
print("---------------------------------------------------------------------------")
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("---------------------------------------------------------------------------")
# logging.info(" ")
# logging.info("---------------------------------------------------------------------------")
# for k in range(0, num_classes):
# if IoU_per_class[k] > 0:
# logging.info("class: %.2f%%" % k)
# logging.info("IoU for class: %.2f%%" % (IoU_per_class[k] * 100))
# logging.info("Pixel Acc for class: %.2f%%" % (per_class_pixel_acc[k] * 100))
# logging.info("---------------------------------------------------------------------------")
#
# logging.info("---------------------------------------------------------------------------")
# logging.info(" mIoU: %.2f%%" % (mIoU * 100))
# logging.info(' mIoU_nobackgroud: %.2f%%' % (mIoU_nobackgroud * 100))
# logging.info(' mean pix acc : %.2f%%' % (mean_pix_acc * 100))
# logging.info(' mean_class_acc : %.2f%%' % ((mean_class_acc_sum / true_classes_pix) * 100))
# logging.info(' mean_pixel_acc_no_background : %.2f%%' % (mean_pixel_acc_nobackground * 100))
#
# logging.info("---------------------------------------------------------------------------")
return mIoU * 100
# def compute_and_print_IoU_per_class(confusion_matrix, num_classes, class_mask=None):
# """
# Computes and prints mean intersection over union divided per class
# :param confusion_matrix: confusion matrix needed for the computation
# """
# mIoU = 0
# # mean_class_acc_num = 0
# mIoU_nobackgroud = 0
# # mIoU_new_classes = 0
# # out = ''
# # out_pixel_acc = ''
# # index = ''
# true_classes = 0
# # 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):
# IoU = 0
# per_class_pixel_acc = 0
#
# 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
# else:
# true_classes += 1
#
# # per-class pixel accuracy
# # per_class_pixel_acc = TP / (TP + FN)
# IoU = TP / denominator
# mIoU += IoU
#
# if i > 0:
# mIoU_nobackgroud += IoU
#
# # mean class accuracy
# # if not np.isnan(per_class_pixel_acc):
# # mean_class_acc_num += TP
# # mean_class_acc_den += TP + FN
# #
# # mean_class_acc_sum += per_class_pixel_acc
# # 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
#
# # index += '%7d' % i
# # out += '%6.2f%%' % (IoU * 100)
# # out_pixel_acc += '%6.2f%%' % (per_class_pixel_acc * 100)
#
# mIoU = mIoU / true_classes
# # 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
#
# # mIoU_nobackgroud = mIoU_nobackgroud / (true_classes - 1)
#
# # logging.info(' index : ' + index)
# # logging.info(' class IoU : ' + out)
# # logging.info(' class acc : ' + out_pixel_acc)
# # logging.info(' mean pix acc : %.2f%%' % (mean_pix_acc * 100))
# # logging.info(' mIoU : %.2f%%' % (mIoU * 100))
# # logging.info(' mean_class_acc : %.2f%%' % ((mean_class_acc_sum / true_classes_pix) * 100))
# # logging.info(' mIoU_nobackground : %.2f%%' % (mIoU_nobackgroud * 100))
# # logging.info(' mean_pixel_acc_no_background : %.2f%%' % (mean_pixel_acc_nobackground * 100))
#
# return mIoU * 100
# def call_meanIoU(y_true, y_pred):
# print(y_pred.shape[0])
# iou = 0
# for k in range(0, 1):
# pr = y_pred[k, :, :, :]
# tr = y_true[k, :, :, :]
# print(pr.shape)
#
# tf.squeeze(pr, [0])
# tf.squeeze(tr, [0])
#
# pr = tf.keras.backend.argmax(pr, -1)
# tr = tf.keras.backend.argmax(tr, -1)
#
# pr = tf.keras.backend.reshape(pr, [-1])
# tr = tf.keras.backend.reshape(tr, [-1])
#
# mat = tf.math.confusion_matrix(labels=tr, predictions=pr, num_classes=21)
#
# iou = compute_and_print_IoU_per_class(confusion_matrix=mat, num_classes=21)
#
# return iou