-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmetrics.py
232 lines (209 loc) · 8 KB
/
metrics.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
from pprint import pprint
import numpy as np
import tensorflow as tf
from tensorflow.python.keras import backend as K
# TP/FP/TN/FN per class
# Precision
# Mean Precision per class
# Recall
# F1 Score
class CustomMetrics(tf.keras.metrics.Metric):
def __init__(self, nb_class, name="custom_metrics", **kwargs):
super(CustomMetrics, self).__init__(name=name, **kwargs)
self._prediction_threshold = 0.5
self._zero = tf.constant(0.0, dtype=tf.float64)
self._nb_class = nb_class
self._class_tp = self.add_weight(
name="tp", shape=(nb_class,), initializer="zeros", dtype=tf.float64
)
self._class_fp = self.add_weight(
name="fp", shape=(nb_class,), initializer="zeros", dtype=tf.float64
)
self._class_tn = self.add_weight(
name="tn", shape=(nb_class,), initializer="zeros", dtype=tf.float64
)
self._class_fn = self.add_weight(
name="fn", shape=(nb_class,), initializer="zeros", dtype=tf.float64
)
self._class_label_union_pred = self.add_weight(
name="label_union_pred",
shape=(nb_class,),
initializer="zeros",
dtype=tf.float64,
)
def update_state(self, y_true, y_pred, sample_weight=None):
y_true = tf.cast(y_true, tf.float64)
predictions = tf.cast(y_pred >= self._prediction_threshold, tf.float64)
true_positive = tf.cast(
tf.logical_and(
tf.not_equal(predictions, self._zero), tf.not_equal(y_true, self._zero)
),
tf.float64,
)
false_positive = tf.cast(
tf.logical_and(
tf.not_equal(predictions, self._zero), tf.equal(y_true, self._zero)
),
tf.float64,
)
true_negative = tf.cast(
tf.logical_and(
tf.equal(predictions, self._zero), tf.equal(y_true, self._zero)
),
tf.float64,
)
false_negative = tf.cast(
tf.logical_and(
tf.equal(predictions, self._zero), tf.not_equal(y_true, self._zero)
),
tf.float64,
)
label_union_prediction = tf.cast(
tf.logical_or(
tf.not_equal(predictions, self._zero), tf.not_equal(y_true, self._zero)
),
tf.float64,
)
self._class_tp.assign_add(tf.reduce_sum(true_positive, axis=0))
self._class_fp.assign_add(tf.reduce_sum(false_positive, axis=0))
self._class_tn.assign_add(tf.reduce_sum(true_negative, axis=0))
self._class_fn.assign_add(tf.reduce_sum(false_negative, axis=0))
self._class_label_union_pred.assign_add(
tf.reduce_sum(label_union_prediction, axis=0)
)
flat_confusion_matrix = tf.convert_to_tensor(
[
self._class_tn,
self._class_fp,
self._class_fn,
self._class_tp,
]
)
self._confusion_matrix = tf.reshape(
tf.transpose(flat_confusion_matrix), [-1, 2, 2]
)
def result(self):
# Precision
nb_label = tf.reduce_sum(self._class_tp) + tf.reduce_sum(self._class_fp)
micro_precision = tf.where(
tf.equal(nb_label, self._zero),
x=self._zero,
y=tf.divide(tf.reduce_sum(self._class_tp), nb_label),
)
nb_predict_class = self._class_tp + self._class_fp
macro_precision_class = tf.where(
tf.equal(nb_predict_class, self._zero),
x=self._zero,
y=tf.divide(self._class_tp, nb_predict_class),
)
macro_precision = tf.reduce_mean(macro_precision_class)
# Recall
nb_label = tf.reduce_sum(self._class_tp) + tf.reduce_sum(self._class_fn)
micro_recall = tf.where(
tf.equal(nb_label, self._zero),
x=self._zero,
y=tf.divide(tf.reduce_sum(self._class_tp), nb_label),
)
nb_label_class = self._class_tp + self._class_fn
macro_recall_class = tf.where(
tf.equal(nb_label_class, self._zero),
x=self._zero,
y=tf.divide(self._class_tp, nb_label_class),
)
macro_recall = tf.reduce_mean(macro_recall_class)
# Accuracy
label_union_pred_class_sum = tf.reduce_sum(self._class_label_union_pred)
micro_accuracy = tf.where(
tf.equal(label_union_pred_class_sum, self._zero),
x=self._zero,
y=tf.divide(tf.reduce_sum(self._class_tp), label_union_pred_class_sum),
)
label_union_pred_class = self._class_label_union_pred
macro_accuracy_class = tf.where(
tf.equal(label_union_pred_class, self._zero),
x=self._zero,
y=tf.divide(self._class_tp, label_union_pred_class),
)
macro_accuracy = tf.reduce_mean(macro_accuracy_class)
# F1-Score
micro_f1score = tf.where(
tf.logical_and(
tf.equal(micro_precision, self._zero),
tf.equal(micro_recall, self._zero),
),
x=self._zero,
y=2 * (micro_precision * micro_recall) / (micro_precision + micro_recall),
)
macro_f1score_class = tf.where(
tf.logical_and(
tf.equal(macro_precision_class, self._zero),
tf.equal(macro_recall_class, self._zero),
),
x=self._zero,
y=2
* (macro_precision_class * macro_recall_class)
/ (macro_precision_class + macro_recall_class),
)
macro_f1score = tf.reduce_mean(macro_f1score_class)
# F2-Score
micro_f2score = tf.where(
tf.logical_and(
tf.equal(micro_precision, self._zero),
tf.equal(micro_recall, self._zero),
),
x=self._zero,
y=(5 * micro_precision * micro_recall)
/ (4 * micro_precision + micro_recall),
)
macro_f2score_class = tf.where(
tf.logical_and(
tf.equal(macro_precision_class, self._zero),
tf.equal(macro_recall_class, self._zero),
),
x=self._zero,
y=(5 * macro_precision_class * macro_recall_class)
/ (4 * macro_precision_class + macro_recall_class),
)
macro_f2score = tf.reduce_mean(macro_f2score_class)
# pprint(
# dict(
# zip(
# [
# "Urban fabric",
# "Industrial or commercial units",
# "Arable land",
# "Permanent crops",
# "Pastures",
# "Complex cultivation patterns",
# "Land principally occupied by agriculture, with significant areas of natural vegetation",
# "Agro-forestry areas",
# "Broad-leaved forest",
# "Coniferous forest",
# "Mixed forest",
# "Natural grassland and sparsely vegetated areas",
# "Moors, heathland and sclerophyllous vegetation",
# "Transitional woodland, shrub",
# "Beaches, dunes, sands",
# "Inland wetlands",
# "Coastal wetlands",
# "Inland waters",
# "Marine waters",
# ],
# np.round(macro_f2score_class.numpy(), 4),
# )
# )
# )
return (
micro_precision,
macro_precision,
micro_recall,
macro_recall,
micro_accuracy,
macro_accuracy,
micro_f1score,
macro_f1score,
micro_f2score,
macro_f2score,
)
def reset_states(self):
K.batch_set_value([(v, np.zeros((self._nb_class,))) for v in self.variables])