-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmetrics_utils.py
631 lines (550 loc) · 30.8 KB
/
metrics_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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
import os
from tensorflow import where as tfwhere, zeros_like as tfzeros_like
from tensorflow.keras.metrics import (Metric as MetricTfKeras, Accuracy as AccuracyTfKeras,
FalsePositives, TruePositives, TrueNegatives, FalseNegatives, Precision, Recall)
import tensorflow.keras.backend as K
from tensorflow.python.keras.utils import metrics_utils as metrics_utils_tf_keras
from tensorflow.python.keras.utils.generic_utils import to_list
from tensorflow.python.ops import init_ops, math_ops
import numpy as np
os.environ['SM_FRAMEWORK'] = 'tf.keras' # will tell segmentation models to use tensorflow's keras
from segmentation_models.base import Metric as MetricSM, functional
SMOOTH = 1e-5
assert SMOOTH <= 1e-5
# 0.5 is default prediction threshold for most metrics which use a threshold value
# and the threshold value is also effectively ignored for one hot metrics
global_threshold = 0.5
assert 0.0 <= global_threshold <= 1.0
# In summary, to achieve one hot metrics:
# 1. For a metric class who via definition inherits tf.keras.metrics.Metric or tf.keras.metric.MeanMetricWrapper, for
# one hot conversion in which this metric class is inherited by a sub-class one hot version:
# - in tf2, place 1H at __ call __ method or update_state method (or both), followed by corresponding super().
# - in tf1, place 1H at update_state method, followed by corresponding super().
# 2. For a metric class who via definition does NOT inherit tf.keras.metrics.Metric or tf.keras.metric.MeanMetricWrapper
# (e.g., instead, inherits segmentation_models.metrics.Metric), for one hot conversion in which this metric class is
# inherited by a sub-class one hot version (note, the class instance will be treated as a function and automatically
# wrapped with tf.keras.metrics.MeanMetricWrapper during model.compile) :
# - in tf2, place 1H at __ call __ method, followed by corresponding super(). Interestingly in tf2, the result is
# independent of whether or not the update_state method result has a return statement.
# - in tf1, place 1H at __ call __ method, followed by corresponding super().
# one hot classes are intended to act as pass-throughs. 1H (argmax) proceeds after thresholding, as done in infer.
# `MeanMetricWrapper` inheritance in custom metric: do not need to remove 'return' from `def update_state` in tf2.0
class OneHotAccuracyTfKeras(AccuracyTfKeras):
def __init__(self, name='accuracy_tfkeras_1H', dtype=None):
super().__init__(name=name, dtype=dtype)
# call redirects to parent class following one hot conversion
def __call__(self, groundtruth, prediction, **kwargs):
prediction = tfwhere(math_ops.greater(prediction, global_threshold), prediction, tfzeros_like(prediction)) # based on tf.keras binary_accuracy
prediction_onehot_indices = K.argmax(prediction, axis=-1) # based on keras.metrics.categorical_accuracy to determine max pred index (1 of channels) at each HW location
prediction_onehot = K.one_hot(prediction_onehot_indices, K.int_shape(prediction)[-1]) # assume 4D tensor is BHWC
return super().__call__(groundtruth, prediction_onehot, **kwargs)
class OneHotFalseNegatives(FalseNegatives):
def __init__(self, thresholds=None, name='FN_1H', dtype=None):
super().__init__(
thresholds=thresholds,
name=name,
dtype=dtype
)
# call redirects to parent class following one hot conversion
def __call__(self, groundtruth, prediction, **kwargs):
prediction = tfwhere(math_ops.greater(prediction, self.thresholds), prediction, tfzeros_like(prediction)) # based on tf.keras binary_accuracy
prediction_onehot_indices = K.argmax(prediction, axis=-1) # based on keras.metrics.categorical_accuracy to determine max pred index (1 of channels) at each HW location
prediction_onehot = K.one_hot(prediction_onehot_indices, K.int_shape(prediction)[-1]) # assume 4D tensor is BHWC
return super().__call__(groundtruth, prediction_onehot, **kwargs)
def update_state(self, y_true, y_pred, sample_weight=None):
super().update_state(y_true, y_pred, sample_weight)
class OneHotFalsePositives(FalsePositives):
def __init__(self, thresholds=None, name='FP_1H', dtype=None):
super().__init__(
thresholds=thresholds,
name=name,
dtype=dtype
)
# call redirects to parent class following one hot conversion
def __call__(self, groundtruth, prediction, **kwargs):
prediction = tfwhere(math_ops.greater(prediction, self.thresholds), prediction, tfzeros_like(prediction)) # based on tf.keras binary_accuracy
prediction_onehot_indices = K.argmax(prediction, axis=-1) # based on keras.metrics.categorical_accuracy to determine max pred index (1 of channels) at each HW location
prediction_onehot = K.one_hot(prediction_onehot_indices, K.int_shape(prediction)[-1]) # assume 4D tensor is BHWC
return super().__call__(groundtruth, prediction_onehot, **kwargs)
def update_state(self, y_true, y_pred, sample_weight=None):
super().update_state(y_true, y_pred, sample_weight)
class OneHotTrueNegatives(TrueNegatives):
def __init__(self, thresholds=None, name='TN_1H', dtype=None):
super().__init__(
thresholds=thresholds,
name=name,
dtype=dtype
)
# call redirects to parent class following one hot conversion
def __call__(self, groundtruth, prediction, **kwargs):
prediction = tfwhere(math_ops.greater(prediction, self.thresholds), prediction, tfzeros_like(prediction)) # based on tf.keras binary_accuracy
prediction_onehot_indices = K.argmax(prediction, axis=-1) # based on keras.metrics.categorical_accuracy to determine max pred index (1 of channels) at each HW location
prediction_onehot = K.one_hot(prediction_onehot_indices, K.int_shape(prediction)[-1]) # assume 4D tensor is BHWC
return super().__call__(groundtruth, prediction_onehot, **kwargs)
def update_state(self, y_true, y_pred, sample_weight=None):
super().update_state(y_true, y_pred, sample_weight)
class OneHotTruePositives(TruePositives):
def __init__(self, thresholds=None, name='TP_1H', dtype=None):
super().__init__(
thresholds=thresholds,
name=name,
dtype=dtype
)
# call redirects to parent class following one hot conversion
def __call__(self, groundtruth, prediction, **kwargs):
prediction = tfwhere(math_ops.greater(prediction, self.thresholds), prediction, tfzeros_like(prediction)) # based on tf.keras binary_accuracy
prediction_onehot_indices = K.argmax(prediction, axis=-1) # based on keras.metrics.categorical_accuracy to determine max pred index (1 of channels) at each HW location
prediction_onehot = K.one_hot(prediction_onehot_indices, K.int_shape(prediction)[-1]) # assume 4D tensor is BHWC
return super().__call__(groundtruth, prediction_onehot, **kwargs)
def update_state(self, y_true, y_pred, sample_weight=None):
super().update_state(y_true, y_pred, sample_weight)
class OneHotPrecision(Precision):
def __init__(self,
thresholds=None,
top_k=None,
class_id=None,
name='precision_1H',
dtype=None):
super().__init__(
thresholds=thresholds,
top_k=top_k,
class_id=class_id,
name=name,
dtype=dtype)
# call redirects to parent class following one hot conversion
def __call__(self, groundtruth, prediction, **kwargs):
prediction = tfwhere(math_ops.greater(prediction, self.thresholds), prediction, tfzeros_like(prediction)) # based on tf.keras binary_accuracy
prediction_onehot_indices = K.argmax(prediction, axis=-1) # based on keras.metrics.categorical_accuracy to determine max pred index (1 of channels) at each HW location
prediction_onehot = K.one_hot(prediction_onehot_indices, K.int_shape(prediction)[-1]) # assume 4D tensor is BHWC
return super().__call__(groundtruth, prediction_onehot, **kwargs)
def update_state(self, y_true, y_pred, sample_weight=None):
super().update_state(y_true, y_pred, sample_weight)
class OneHotRecall(Recall):
def __init__(self,
thresholds=None,
top_k=None,
class_id=None,
name='recall_1H',
dtype=None):
super().__init__(
thresholds=thresholds,
top_k=top_k,
class_id=class_id,
name=name,
dtype=dtype)
# call redirects to parent class following one hot conversion
def __call__(self, groundtruth, prediction, **kwargs):
prediction = tfwhere(math_ops.greater(prediction, self.thresholds), prediction, tfzeros_like(prediction)) # based on tf.keras binary_accuracy
prediction_onehot_indices = K.argmax(prediction, axis=-1) # based on keras.metrics.categorical_accuracy to determine max pred index (1 of channels) at each HW location# based on tf.keras binary_accuracy
prediction_onehot = K.one_hot(prediction_onehot_indices, K.int_shape(prediction)[-1]) # assume 4D tensor is BHWC
return super().__call__(groundtruth, prediction_onehot, **kwargs)
def update_state(self, y_true, y_pred, sample_weight=None):
super().update_state(y_true, y_pred, sample_weight)
# based on Keras/tf.keras precision and recall class definitions found at (depending on import source):
# keras: https://github.com/keras-team/keras/blob/7a39b6c62d43c25472b2c2476bd2a8983ae4f682/keras/metrics.py#L1154
# tf.keras: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/keras/metrics.py#L1134
class FBetaScore(MetricTfKeras):
"""Abstract base class for F1Score.
For additional information, see the
following: https://en.wikipedia.org/wiki/F1_score#Definition
If `sample_weight` is `None`, weights default to 1.
Use `sample_weight` of 0 to mask values.
If `top_k` is set, we'll calculate precision as how often on average a class
among the top-k classes with the highest predicted values of a batch entry is
correct and can be found in the label for that entry.
If `class_id` is specified, we calculate precision by considering only the
entries in the batch for which `class_id` is above the threshold and/or in the
top-k highest predictions, and computing the fraction of them for which
`class_id` is indeed a correct label."""
'''
Arguments
beta: The F-measure was derived so that F_β "measures the effectiveness of
retrieval with respect to a user who attaches β times as much importance to recall as precision".
beta=1 gives F_1 score, and is also known as the Sørensen–Dice coefficient or Dice similarity
coefficient (DSC).
thresholds: (Optional) A float value or a python list/tuple of float
threshold values in [0, 1]. A threshold is compared with prediction
values to determine the truth value of predictions (i.e., above the
threshold is `true`, below is `false`). One metric value is generated
for each threshold value. If neither thresholds nor top_k are set, the
default is to calculate precision with `thresholds=0.5`.
top_k: (Optional) Unset by default. An int value specifying the top-k
predictions to consider when calculating precision.
class_id: (Optional) Integer class ID for which we want binary metrics.
This must be in the half-open interval `[0, num_classes)`, where
`num_classes` is the last dimension of predictions.
name: (Optional) string name of the metric instance.
dtype: (Optional) data type of the metric result.
'''
def __init__(self,
beta=1,
thresholds=None,
top_k=None,
class_id=None,
name=None,
dtype=None):
name = name or str('f' + str(beta) + 'score')
super().__init__(name=name, dtype=dtype)
self.init_thresholds = thresholds
self.beta = beta
self.top_k = top_k
self.class_id = class_id
default_threshold = 0.5 if top_k is None else metrics_utils_tf_keras.NEG_INF
self.thresholds = metrics_utils_tf_keras.parse_init_thresholds(
thresholds, default_threshold=default_threshold)
self.true_positives = self.add_weight(
'true_positives',
shape=(len(self.thresholds),),
initializer=init_ops.zeros_initializer)
self.false_positives = self.add_weight(
'false_positives',
shape=(len(self.thresholds),),
initializer=init_ops.zeros_initializer)
self.false_negatives = self.add_weight(
'false_negatives',
shape=(len(self.thresholds),),
initializer=init_ops.zeros_initializer)
def update_state(self, y_true, y_pred, sample_weight=None):
# for tf v1, use 'return metrics_...'. for tf v2, use 'metrics_...' (for inherited keras/tf.keras Metric class)
metrics_utils_tf_keras.update_confusion_matrix_variables(
{
metrics_utils_tf_keras.ConfusionMatrix.TRUE_POSITIVES: self.true_positives,
metrics_utils_tf_keras.ConfusionMatrix.FALSE_POSITIVES: self.false_positives,
metrics_utils_tf_keras.ConfusionMatrix.FALSE_NEGATIVES: self.false_negatives
},
y_true,
y_pred,
thresholds=self.thresholds,
top_k=self.top_k,
class_id=self.class_id,
sample_weight=sample_weight)
def result(self):
denominator = ((1 + self.beta * self.beta) * self.true_positives + self.beta * self.beta * self.false_negatives
+ self.false_positives)
numerator = (1 + self.beta * self.beta) * self.true_positives
result = math_ops.div_no_nan(numerator, denominator)
return result[0] if len(self.thresholds) == 1 else result
def reset_states(self):
num_thresholds = len(to_list(self.thresholds))
K.batch_set_value(
[(v, np.zeros((num_thresholds,))) for v in self.variables])
def get_config(self):
config = {
'beta': self.beta,
'thresholds': self.init_thresholds,
'top_k': self.top_k,
'class_id': self.class_id
}
base_config = super().get_config()
return dict(list(base_config.items()) + list(config.items()))
class OneHotFBetaScore(FBetaScore):
def __init__(self,
beta=1,
thresholds=None,
top_k=None,
class_id=None,
name=None,
dtype=None):
name = name or str('f' + str(beta) + 'score_1H')
super().__init__(
beta=beta,
thresholds=thresholds,
top_k=top_k,
class_id=class_id,
name=name,
dtype=dtype)
# call redirects to parent class following one hot conversion
def __call__(self, groundtruth, prediction, **kwargs):
prediction = tfwhere(math_ops.greater(prediction, self.thresholds), prediction, tfzeros_like(prediction)) # based on tf.keras binary_accuracy
prediction_onehot_indices = K.argmax(prediction, axis=-1) # based on keras.metrics.categorical_accuracy to determine max pred index (1 of channels) at each HW location
prediction_onehot = K.one_hot(prediction_onehot_indices, K.int_shape(prediction)[-1]) # assume 4D tensor is BHWC
return super().__call__(groundtruth, prediction_onehot, **kwargs)
# based on Keras/tf.keras precision and recall class definitions found at (depending on import source):
# keras: https://github.com/keras-team/keras/blob/7a39b6c62d43c25472b2c2476bd2a8983ae4f682/keras/metrics.py#L1154
# tf.keras: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/keras/metrics.py#L1134
class IoUScore(MetricTfKeras):
"""Computes the mean Intersection-Over-Union metric.
Intersection-Over-Union is a common evaluation metric for semantic image
segmentation, which first computes the IOU for each semantic class and then
computes the average over classes. IOU is defined as follows:
IOU = true_positive / (true_positive + false_positive + false_negative).
The predictions are accumulated in a confusion matrix, weighted by
`sample_weight` and the metric is then calculated from it.
If `sample_weight` is `None`, weights default to 1.
Use `sample_weight` of 0 to mask values.
If `top_k` is set, we'll calculate precision as how often on average a class
among the top-k classes with the highest predicted values of a batch entry is
correct and can be found in the label for that entry.
If `class_id` is specified, we calculate precision by considering only the
entries in the batch for which `class_id` is above the threshold and/or in the
top-k highest predictions, and computing the fraction of them for which
`class_id` is indeed a correct label."""
'''
# Arguments
thresholds: (Optional) A float value or a python list/tuple of float
threshold values in [0, 1]. A threshold is compared with prediction
values to determine the truth value of predictions (i.e., above the
threshold is `true`, below is `false`). One metric value is generated
for each threshold value. If neither thresholds nor top_k are set, the
default is to calculate precision with `thresholds=0.5`.
top_k: (Optional) Unset by default. An int value specifying the top-k
predictions to consider when calculating precision.
class_id: (Optional) Integer class ID for which we want binary metrics.
This must be in the half-open interval `[0, num_classes)`, where
`num_classes` is the last dimension of predictions.
name: (Optional) string name of the metric instance.
dtype: (Optional) data type of the metric result.
'''
def __init__(self,
thresholds=None,
top_k=None,
class_id=None,
name='iou_score',
dtype=None):
super().__init__(name=name, dtype=dtype)
self.init_thresholds = thresholds
self.top_k = top_k
self.class_id = class_id
default_threshold = 0.5 if top_k is None else metrics_utils_tf_keras.NEG_INF
self.thresholds = metrics_utils_tf_keras.parse_init_thresholds(
thresholds, default_threshold=default_threshold)
self.true_positives = self.add_weight(
'true_positives',
shape=(len(self.thresholds),),
initializer=init_ops.zeros_initializer)
self.false_positives = self.add_weight(
'false_positives',
shape=(len(self.thresholds),),
initializer=init_ops.zeros_initializer)
self.false_negatives = self.add_weight(
'false_negatives',
shape=(len(self.thresholds),),
initializer=init_ops.zeros_initializer)
def update_state(self, y_true, y_pred, sample_weight=None):
# for tf v1, use 'return metrics_...'. for tf v2, use 'metrics_...' (for inherited keras/tf.keras Metric class)
metrics_utils_tf_keras.update_confusion_matrix_variables(
{
metrics_utils_tf_keras.ConfusionMatrix.TRUE_POSITIVES: self.true_positives,
metrics_utils_tf_keras.ConfusionMatrix.FALSE_POSITIVES: self.false_positives,
metrics_utils_tf_keras.ConfusionMatrix.FALSE_NEGATIVES: self.false_negatives
},
y_true,
y_pred,
thresholds=self.thresholds,
top_k=self.top_k,
class_id=self.class_id,
sample_weight=sample_weight)
def result(self):
denominator = (self.true_positives + self.false_negatives + self.false_positives)
numerator = self.true_positives
result = math_ops.div_no_nan(numerator, denominator)
return result[0] if len(self.thresholds) == 1 else result
def reset_states(self):
num_thresholds = len(to_list(self.thresholds))
K.batch_set_value(
[(v, np.zeros((num_thresholds,))) for v in self.variables])
def get_config(self):
config = {
'thresholds': self.init_thresholds,
'top_k': self.top_k,
'class_id': self.class_id
}
base_config = super().get_config()
return dict(list(base_config.items()) + list(config.items()))
class OneHotIoUScore(IoUScore):
def __init__(self,
thresholds=None,
top_k=None,
class_id=None,
name='iou_score_1H',
dtype=None):
super().__init__(
thresholds=thresholds,
top_k=top_k,
class_id=class_id,
name=name,
dtype=dtype)
# call redirects to parent class following one hot conversion
def __call__(self, groundtruth, prediction, **kwargs):
prediction = tfwhere(math_ops.greater(prediction, self.thresholds), prediction, tfzeros_like(prediction)) # based on tf.keras binary_accuracy
prediction_onehot_indices = K.argmax(prediction, axis=-1) # based on keras.metrics.categorical_accuracy to determine max pred index (1 of channels) at each HW location
prediction_onehot = K.one_hot(prediction_onehot_indices, K.int_shape(prediction)[-1]) # assume 4D tensor is BHWC
return super().__call__(groundtruth, prediction_onehot, **kwargs)
# VERSION 2 CLASSBINARYACCURACY METHOD, BASED ON KERAS PACKAGE -- ACCUMULATED OVER EPOCH (inherit KERAS.METRIC)
# based on Keras/tf.keras precision and recall class definitions found at (depending on import source):
# keras: https://github.com/keras-team/keras/blob/7a39b6c62d43c25472b2c2476bd2a8983ae4f682/keras/metrics.py#L1154
# tf.keras: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/keras/metrics.py#L1134
class ClassBinaryAccuracyTfKeras(MetricTfKeras):
r"""
.. math:: Binary Accuracy = (TN + TP)/(TN+TP+FN+FP) = Number of correct assessments/Number of all assessments,
for given class for more than one class input, output becomes mean accuracy (similar but not same as categorical)
# Arguments
thresholds: (Optional) A float value or a python list/tuple of float
threshold values in [0, 1]. A threshold is compared with prediction
values to determine the truth value of predictions (i.e., above the
threshold is `true`, below is `false`). One metric value is generated
for each threshold value. If neither thresholds nor top_k are set, the
default is to calculate precision with `thresholds=0.5`.
top_k: (Optional) Unset by default. An int value specifying the top-k
predictions to consider when calculating precision.
class_id: (Optional) Integer class ID for which we want binary metrics.
This must be in the half-open interval `[0, num_classes)`, where
`num_classes` is the last dimension of predictions.
name: (Optional) string name of the metric instance.
dtype: (Optional) data type of the metric result.
"""
def __init__(self,
thresholds=None,
top_k=None,
class_id=None,
name='class_all_binary_accuracy_tfkeras',
dtype=None):
super().__init__(name=name, dtype=dtype)
self.init_thresholds = thresholds
self.top_k = top_k
self.class_id = class_id
default_threshold = 0.5 if top_k is None else metrics_utils_tf_keras.NEG_INF
self.thresholds = metrics_utils_tf_keras.parse_init_thresholds(
thresholds, default_threshold=default_threshold)
self.true_positives = self.add_weight(
'true_positives',
shape=(len(self.thresholds),),
initializer=init_ops.zeros_initializer)
self.false_positives = self.add_weight(
'false_positives',
shape=(len(self.thresholds),),
initializer=init_ops.zeros_initializer)
self.false_negatives = self.add_weight(
'false_negatives',
shape=(len(self.thresholds),),
initializer=init_ops.zeros_initializer)
self.true_negatives = self.add_weight(
'true_negatives',
shape=(len(self.thresholds),),
initializer=init_ops.zeros_initializer)
def update_state(self, y_true, y_pred, sample_weight=None):
# for tf v1, use 'return metrics_...'. for tf v2, use 'metrics_...' (for inherited keras/tf.keras Metric class)
metrics_utils_tf_keras.update_confusion_matrix_variables(
{
metrics_utils_tf_keras.ConfusionMatrix.TRUE_POSITIVES: self.true_positives,
metrics_utils_tf_keras.ConfusionMatrix.FALSE_POSITIVES: self.false_positives,
metrics_utils_tf_keras.ConfusionMatrix.FALSE_NEGATIVES: self.false_negatives,
metrics_utils_tf_keras.ConfusionMatrix.TRUE_NEGATIVES: self.true_negatives
},
y_true,
y_pred,
thresholds=self.thresholds,
top_k=self.top_k,
class_id=self.class_id,
sample_weight=sample_weight)
def result(self):
denominator = (self.true_positives + self.false_negatives + self.false_positives + self.true_negatives)
numerator = self.true_positives + self.true_negatives
result = math_ops.div_no_nan(numerator, denominator)
return result[0] if len(self.thresholds) == 1 else result
def reset_states(self):
num_thresholds = len(to_list(self.thresholds))
K.batch_set_value(
[(v, np.zeros((num_thresholds,))) for v in self.variables])
def get_config(self):
config = {
'thresholds': self.init_thresholds,
'top_k': self.top_k,
'class_id': self.class_id
}
base_config = super().get_config()
return dict(list(base_config.items()) + list(config.items()))
class OneHotClassBinaryAccuracyTfKeras(ClassBinaryAccuracyTfKeras):
def __init__(self,
thresholds=None,
top_k=None,
class_id=None,
name='class_all_binary_accuracy_tfkeras_1H',
dtype=None):
super().__init__(
thresholds=thresholds,
top_k=top_k,
class_id=class_id,
name=name,
dtype=dtype)
# call redirects to parent class following one hot conversion
def __call__(self, groundtruth, prediction, **kwargs):
prediction = tfwhere(math_ops.greater(prediction, self.thresholds), prediction, tfzeros_like(prediction)) # based on tf.keras binary_accuracy
prediction_onehot_indices = K.argmax(prediction, axis=-1) # based on keras.metrics.categorical_accuracy to determine max pred index (1 of channels) at each HW location
prediction_onehot = K.one_hot(prediction_onehot_indices,
K.int_shape(prediction)[-1]) # assume 4D tensor is BHWC
return super().__call__(groundtruth, prediction_onehot, **kwargs)
# VERSION 1 CLASSBINARYACCURACY METHOD, BASED ON SEGMENTATION_MODELS PACKAGE -- AVERAGED OVER EPOCH
# adapted from: s_m.IOUScore() from github.com/qubvel/segmentation_models/blob/master/segmentation_models/metrics.py
class ClassBinaryAccuracySM(MetricSM):
r"""
.. math:: Binary Accuracy = (TN + TP)/(TN+TP+FN+FP) = Number of correct assessments/Number of all assessments,
for given class for more than one class input, output becomes mean accuracy (similar but not same as categorical)
Args:
class_weights: 1. or ``np.array`` of class weights (``len(weights) = num_classes``).
class_indexes: Optional integer or list of integers, classes to consider, if ``None`` all classes are used.
smooth: value to avoid division by zero
per_image: if ``True``, metric is calculated as mean over images in batch (B),
else over whole batch
threshold: value to round predictions (use ``>`` comparison), if ``None`` prediction will not be round
Returns:
A callable ``class_binary_accuracy`` instance. Can be used in ``model.compile(...)`` function.
Example:
.. code:: python
metric = ClassBinaryAccuracy()
model.compile('SGD', loss=loss, metrics=[metric])
"""
def __init__(
self,
class_weights=None,
class_indexes=None,
threshold=None,
per_image=False,
smooth=SMOOTH,
name=None
):
self.name = name or 'class_all_binary_accuracy_sm'
super().__init__(name=self.name)
self.class_weights = class_weights if class_weights is not None else 1
self.class_indexes = class_indexes
self.threshold = threshold
self.per_image = per_image
self.smooth = smooth
def __call__(self, gt, pr):
backend = self.submodules['backend']
gt, pr = functional.gather_channels(gt, pr, indexes=self.class_indexes, **self.submodules)
pr = functional.round_if_needed(pr, self.threshold, **self.submodules)
axes = functional.get_reduce_axes(self.per_image, **self.submodules)
# score calculation (assumed pr are 1-hot in practice)
tp = backend.sum(gt * pr, axis=axes)
fp = backend.sum(pr, axis=axes) - tp
fn = backend.sum(gt, axis=axes) - tp
tn = backend.sum((-gt + 1) * (-pr + 1), axis=axes)
score = (tp + tn) / (tp + tn + fp + fn + self.smooth)
# score is averaged over whole batch here (unlike Keras, where score is accumulated over batch)
score = functional.average(score, self.per_image, self.class_weights, **self.submodules)
return score
class OneHotClassBinaryAccuracySM(ClassBinaryAccuracySM):
def __init__(
self,
class_weights=None,
class_indexes=None,
threshold=None,
per_image=False,
smooth=SMOOTH,
name=None
):
self.name = name or 'class_all_binary_accuracy_sm_1H'
super().__init__(
class_weights=class_weights,
class_indexes=class_indexes,
threshold=threshold,
per_image=per_image,
smooth=smooth,
name=self.name)
# call redirects to parent class following one hot conversion
def __call__(self, groundtruth, prediction):
prediction = tfwhere(math_ops.greater(prediction, self.threshold), prediction, tfzeros_like(prediction)) # based on tf.keras binary_accuracy
prediction_onehot_indices = K.argmax(prediction, axis=-1) # based on keras.metrics.categorical_accuracy to determine max pred index (1 of channels) at each HW location
prediction_onehot = K.one_hot(prediction_onehot_indices, K.int_shape(prediction)[-1]) # assume 4D tensor is BHWC
return super().__call__(groundtruth, prediction_onehot)