-
Notifications
You must be signed in to change notification settings - Fork 2
/
metrics.py
505 lines (454 loc) · 24.4 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
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
import timm
import numpy as np
import os
import time
import torch
import torch.nn as nn
from collections import defaultdict
from functools import partial
from skimage.measure import label
from joblib import Parallel, delayed
from scipy.optimize import linear_sum_assignment
from sklearn.preprocessing import StandardScaler
from torch.utils.data import DataLoader
from torchmetrics import Metric
from torchvision.transforms import GaussianBlur
from torchvision import models
from typing import Optional, List, Tuple, Dict
class PredsmIoU_1(Metric):
"""
Subclasses Metric. Computes mean Intersection over Union (mIoU) given ground-truth and predictions.
.update() can be called repeatedly to add data from multiple validation loops.
"""
def __init__(self,
num_pred_classes: int,
num_gt_classes: int):
"""
:param num_pred_classes: The number of predicted classes.
:param num_gt_classes: The number of gt classes.
"""
super().__init__(dist_sync_on_step=False, compute_on_step=False)
self.num_pred_classes = num_pred_classes
self.num_gt_classes = num_gt_classes
self.add_state("gt", [])
self.add_state("pred", [])
self.n_jobs = -1
def update(self, gt: torch.Tensor, pred: torch.Tensor) -> None:
self.gt.append(gt)
self.pred.append(pred)
def compute(self, is_global_zero: bool, many_to_one: bool = False,
precision_based: bool = False, linear_probe : bool = False) -> Tuple[float, List[np.int64],
List[np.int64], List[np.int64],
List[np.int64], float]:
"""
Compute mIoU with optional hungarian matching or many-to-one matching (extracts information from labels).
:param is_global_zero: Flag indicating whether process is rank zero. Computation of metric is only triggered
if True.
:param many_to_one: Compute a many-to-one mapping of predicted classes to ground truth instead of hungarian
matching.
:param precision_based: Use precision as matching criteria instead of IoU for assigning predicted class to
ground truth class.
:param linear_probe: Skip hungarian / many-to-one matching. Used for evaluating predictions of fine-tuned heads.
:return: mIoU over all classes, true positives per class, false negatives per class, false positives per class,
reordered predictions matching gt, percentage of clusters matched to background class. 1/self.num_pred_classes
if self.num_pred_classes == self.num_gt_classes.
"""
if is_global_zero:
pred = torch.cat(self.pred).cpu().numpy().astype(int)
gt = torch.cat(self.gt).cpu().numpy().astype(int)
assert len(np.unique(pred)) <= self.num_pred_classes
assert np.max(pred) <= self.num_pred_classes
return self.compute_miou(gt, pred, self.num_pred_classes, self.num_gt_classes, many_to_one=many_to_one,
precision_based=precision_based, linear_probe=linear_probe)
def compute_miou(self, gt: np.ndarray, pred: np.ndarray, num_pred: int, num_gt:int,
many_to_one=False, precision_based=False, linear_probe=False) -> Tuple[float, List[np.int64], List[np.int64], List[np.int64],
List[np.int64], float]:
"""
Compute mIoU with optional hungarian matching or many-to-one matching (extracts information from labels).
:param gt: numpy array with all flattened ground-truth class assignments per pixel
:param pred: numpy array with all flattened class assignment predictions per pixel
:param num_pred: number of predicted classes
:param num_gt: number of ground truth classes
:param many_to_one: Compute a many-to-one mapping of predicted classes to ground truth instead of hungarian
matching.
:param precision_based: Use precision as matching criteria instead of IoU for assigning predicted class to
ground truth class.
:param linear_probe: Skip hungarian / many-to-one matching. Used for evaluating predictions of fine-tuned heads.
:return: mIoU over all classes, true positives per class, false negatives per class, false positives per class,
reordered predictions matching gt, percentage of clusters matched to background class. 1/self.num_pred_classes
if self.num_pred_classes == self.num_gt_classes.
"""
assert pred.shape == gt.shape
print(f"seg map preds have size {gt.shape}")
tp = [0] * num_gt
fp = [0] * num_gt
fn = [0] * num_gt
jac = [0] * num_gt
if linear_probe:
reordered_preds = pred
matched_bg_clusters = {}
else:
if many_to_one:
match = self._original_match(num_pred, num_gt, pred, gt, precision_based=precision_based)
# remap predictions
reordered_preds = np.zeros(len(pred))
for target_i, matched_preds in match.items():
for pred_i in matched_preds:
reordered_preds[pred == int(pred_i)] = int(target_i)
matched_bg_clusters = len(match[0]) / num_pred
else:
match = self._hungarian_match(num_pred, num_gt, pred, gt)
# remap predictions
reordered_preds = np.zeros(len(pred))
for target_i, pred_i in zip(*match):
reordered_preds[pred == int(pred_i)] = int(target_i)
# merge all unmatched predictions to background
for unmatched_pred in np.delete(np.arange(num_pred), np.array(match[1])):
reordered_preds[pred == int(unmatched_pred)] = 0
matched_bg_clusters = 1/num_gt
# tp, fp, and fn evaluation
for i_part in range(0, num_gt):
tmp_all_gt = (gt == i_part)
tmp_pred = (reordered_preds == i_part)
tp[i_part] += np.sum(tmp_all_gt & tmp_pred)
fp[i_part] += np.sum(~tmp_all_gt & tmp_pred)
fn[i_part] += np.sum(tmp_all_gt & ~tmp_pred)
# Calculate IoU per class
for i_part in range(0, num_gt):
jac[i_part] = float(tp[i_part]) / max(float(tp[i_part] + fp[i_part] + fn[i_part]), 1e-8)
print("IoUs computed")
print(jac)
return np.mean(jac), tp, fp, fn, reordered_preds.astype(int).tolist(), matched_bg_clusters
@staticmethod
def get_score(flat_preds: np.ndarray, flat_targets: np.ndarray, c1: int, c2: int, precision_based: bool = False) \
-> float:
"""
Calculates IoU given gt class c1 and prediction class c2.
:param flat_preds: flattened predictions
:param flat_targets: flattened gt
:param c1: ground truth class to match
:param c2: predicted class to match
:param precision_based: flag to calculate precision instead of IoU.
:return: The score if gt-c1 was matched to predicted c2.
"""
tmp_all_gt = (flat_targets == c1)
tmp_pred = (flat_preds == c2)
tp = np.sum(tmp_all_gt & tmp_pred)
fp = np.sum(~tmp_all_gt & tmp_pred)
if not precision_based:
fn = np.sum(tmp_all_gt & ~tmp_pred)
jac = float(tp) / max(float(tp + fp + fn), 1e-8)
return jac
else:
prec = float(tp) / max(float(tp + fp), 1e-8)
return prec
def compute_score_matrix(self, num_pred: int, num_gt: int, pred: np.ndarray, gt: np.ndarray,
precision_based: bool = False) -> np.ndarray:
"""
Compute score matrix. Each element i, j of matrix is the score if i was matched j. Computation is parallelized
over self.n_jobs.
:param num_pred: number of predicted classes
:param num_gt: number of ground-truth classes
:param pred: flattened predictions
:param gt: flattened gt
:param precision_based: flag to calculate precision instead of IoU.
:return: num_pred x num_gt matrix with A[i, j] being the score if ground-truth class i was matched to
predicted class j.
"""
print("Parallelizing iou computation")
start = time.time()
score_mat = Parallel(n_jobs=self.n_jobs)(delayed(self.get_score)(pred, gt, c1, c2, precision_based=precision_based)
for c2 in range(num_pred) for c1 in range(num_gt))
print(f"took {time.time() - start} seconds")
score_mat = np.array(score_mat)
return score_mat.reshape((num_pred, num_gt)).T
def _hungarian_match(self, num_pred: int, num_gt: int, pred: np.ndarray, gt: np.ndarray) -> Tuple[np.ndarray,
np.ndarray]:
# do hungarian matching. If num_pred > num_gt match will be partial only.
iou_mat = self.compute_score_matrix(num_pred, num_gt, pred, gt)
match = linear_sum_assignment(1 - iou_mat)
print("Matched clusters to gt classes:")
print(match)
return match
def _original_match(self, num_pred, num_gt, pred, gt, precision_based=False) -> Dict[int, list]:
score_mat = self.compute_score_matrix(num_pred, num_gt, pred, gt, precision_based=precision_based)
preds_to_gts = {}
preds_to_gt_scores = {}
# Greedily match predicted class to ground-truth class by best score.
for pred_c in range(num_pred):
for gt_c in range(num_gt):
score = score_mat[gt_c, pred_c]
if (pred_c not in preds_to_gts) or (score > preds_to_gt_scores[pred_c]):
preds_to_gts[pred_c] = gt_c
preds_to_gt_scores[pred_c] = score
gt_to_matches = defaultdict(list)
for k,v in preds_to_gts.items():
gt_to_matches[v].append(k)
print("matched clusters to gt classes:")
return gt_to_matches
class PredsmIoU(torch.nn.Module):
"""
Subclasses Metric. Computes mean Intersection over Union (mIoU) given ground-truth and predictions.
.update() can be called repeatedly to add data from multiple validation loops.
"""
def __init__(self,
num_pred_classes: int,
num_gt_classes: int, involve_bg=False):
"""
:param num_pred_classes: The number of predicted classes.
:param num_gt_classes: The number of gt classes.
"""
super().__init__()
self.num_pred_classes = num_pred_classes
self.num_gt_classes = num_gt_classes
self.gt = []
self.pred = []
self.involve_bg = involve_bg
self.n_jobs = -1
def update(self, gt: torch.Tensor, pred: torch.Tensor) -> None:
self.gt.append(gt)
self.pred.append(pred)
def reset(self) -> None:
self.gt = []
self.pred = []
def compute(self, is_global_zero: bool, many_to_one: bool = False,
precision_based: bool = False, linear_probe : bool = False) -> Tuple[float, List[np.int64],
List[np.int64], List[np.int64],
List[np.int64], float]:
"""
Compute mIoU with optional hungarian matching or many-to-one matching (extracts information from labels).
:param is_global_zero: Flag indicating whether process is rank zero. Computation of metric is only triggered
if True.
:param many_to_one: Compute a many-to-one mapping of predicted classes to ground truth instead of hungarian
matching.
:param precision_based: Use precision as matching criteria instead of IoU for assigning predicted class to
ground truth class.
:param linear_probe: Skip hungarian / many-to-one matching. Used for evaluating predictions of fine-tuned heads.
:return: mIoU over all classes, true positives per class, false negatives per class, false positives per class,
reordered predictions matching gt, percentage of clusters matched to background class. 1/self.num_pred_classes
if self.num_pred_classes == self.num_gt_classes.
"""
if is_global_zero:
pred = torch.cat(self.pred).cpu().numpy().astype(int)
gt = torch.cat(self.gt).cpu().numpy().astype(int)
## I have commented the following to lines
# assert len(np.unique(pred)) <= self.num_pred_classes
# assert np.max(pred) <= self.num_pred_classes
## This block is added by me.
self.num_pred_classes = len(np.unique(pred))
self.num_gt_classes = len(np.unique(gt))
####################################3
return self.compute_miou(gt, pred, self.num_pred_classes, self.num_gt_classes, many_to_one=many_to_one,
precision_based=precision_based, linear_probe=linear_probe)
def compute_propagation_score(self, is_global_zero: bool) -> List[np.float64]:
"""
Compute the propagation performance of a give mask. The averagin is done over the number of objects and the objects' scores are computed per-frame across time. There is no matching here
therefore the objects of the gt and pred should be the same. Gt and Pred should be given for each sequence in the batch.
:param is_global_zero: Flag indicating whether process is rank zero. Computation of metric is only triggered
if True.
:param many_to_one: Compute a many-to-one mapping of predicted classes to ground truth instead of hungarian
matching.
:param precision_based: Use precision as matching criteria instead of IoU for assigning predicted class to
ground truth class.
:param linear_probe: Skip hungarian / many-to-one matching. Used for evaluating predictions of fine-tuned heads.
:return: the mIOU of all the objects in a sequence as a list of floats
"""
if is_global_zero:
pred = torch.stack(self.pred).cpu().numpy().astype(int)
gt = torch.stack(self.gt).cpu().numpy().astype(int)
## I have commented the following to lines
# assert len(np.unique(pred)) <= self.num_pred_classes
# assert np.max(pred) <= self.num_pred_classes
## This block is added by me.
self.num_pred_classes = len(np.unique(pred))
self.num_gt_classes = len(np.unique(gt))
####################################3
return self.compute_propagation_iou(gt, pred)
def compute_propagation_iou(self, gt: np.ndarray, pred: np.ndarray) -> List[np.float64]:
"""
Compute MIoU per object, per frame across time. There is no matching here. The objects of the gt and pred should be the same. Gt and Pred should be given for each sequence in the batch.
:param gt: numpy array with all flattened ground-truth class assignments
:param pred: numpy array with all flattened class assignment predictions
:param num_pred: number of predicted classes
:param num_gt: number of ground truth classes
:return: mIoU of objects in a sequence as a list of floats
"""
pred_unique = np.unique(pred)
gt_unique = np.unique(gt)
tp = {}
fp = {}
fn = {}
jac = {}
for i in gt_unique:
if i == 0:
continue
tp[i] = 0
fp[i] = 0
fn[i] = 0
jac[i] = 0
assert pred.shape == gt.shape
print(f"seg map preds have size {gt.shape}")
# tp, fp, and fn evaluation
for i_part in np.unique(gt):
frames_have_part = 0
if i_part == 0:
continue
for i in range(gt.shape[0]):
tmp_all_gt = (gt[i] == i_part)
tmp_pred = (pred[i] == i_part)
if np.sum(tmp_all_gt) > 0:
frames_have_part += 1
tp[i_part] += np.sum(tmp_all_gt & tmp_pred)
fp[i_part] += np.sum(~tmp_all_gt & tmp_pred)
fn[i_part] += np.sum(tmp_all_gt & ~tmp_pred)
jac[i_part] += float(tp[i_part]) / max(float(tp[i_part] + fp[i_part] + fn[i_part]), 1e-8)
jac[i_part] = jac[i_part] / frames_have_part
tp[i_part] = tp[i_part] / frames_have_part
fp[i_part] = fp[i_part] / frames_have_part
fn[i_part] = fn[i_part] / frames_have_part
return list(jac.values())
def compute_miou(self, gt: np.ndarray, pred: np.ndarray, num_pred: int, num_gt:int,
many_to_one=False, precision_based=False, linear_probe=False) -> Tuple[float, List[np.int64], List[np.int64], List[np.int64],
List[np.int64], float]:
"""
Compute mIoU with optional hungarian matching or many-to-one matching (extracts information from labels).
:param gt: numpy array with all flattened ground-truth class assignments per pixel
:param pred: numpy array with all flattened class assignment predictions per pixel
:param num_pred: number of predicted classes
:param num_gt: number of ground truth classes
:param many_to_one: Compute a many-to-one mapping of predicted classes to ground truth instead of hungarian
matching.
:param precision_based: Use precision as matching criteria instead of IoU for assigning predicted class to
ground truth class.
:param linear_probe: Skip hungarian / many-to-one matching. Used for evaluating predictions of fine-tuned heads.
:return: mIoU over all classes, true positives per class, false negatives per class, false positives per class,
reordered predictions matching gt, percentage of clusters matched to background class. 1/self.num_pred_classes
if self.num_pred_classes == self.num_gt_classes.
"""
# print(np.all(pred == gt))
# print(np.unique(pred))
pred_unique = np.unique(pred)
gt_unique = np.unique(gt)
tp = {}
fp = {}
fn = {}
jac = {}
for i in gt_unique:
tp[i] = 0
fp[i] = 0
fn[i] = 0
jac[i] = 0
assert pred.shape == gt.shape
print(f"seg map preds have size {gt.shape}")
if linear_probe:
reordered_preds = pred
matched_bg_clusters = {}
else:
if many_to_one:
match = self._original_match(num_pred, num_gt, pred, gt, precision_based=precision_based)
# remap predictions
reordered_preds = np.zeros(len(pred))
for target_i, matched_preds in match.items():
for pred_i in matched_preds:
reordered_preds[pred == pred_unique[int(pred_i)]] = gt_unique[int(target_i)]
matched_bg_clusters = len(match[0]) / num_pred
else:
match = self._hungarian_match(num_pred, num_gt, pred, gt)
# remap predictions
reordered_preds = np.zeros(len(pred))
for target_i, pred_i in zip(*match):
reordered_preds[pred == pred_unique[int(pred_i)]] = gt_unique[int(target_i)]
# merge all unmatched predictions to background
for unmatched_pred in np.delete(np.arange(num_pred), np.array(match[1])):
reordered_preds[pred == pred_unique[int(unmatched_pred)]] = 0
matched_bg_clusters = 1/num_gt
# tp, fp, and fn evaluation
valid = gt != 0
for i_part in np.unique(gt):
tmp_all_gt = (gt == i_part)
tmp_pred = (reordered_preds == i_part)
tp[i_part] += np.sum(tmp_all_gt & tmp_pred)
fp[i_part] += np.sum(~tmp_all_gt & tmp_pred)
fn[i_part] += np.sum(tmp_all_gt & ~tmp_pred)
# Calculate IoU per class
for i_part in np.unique(gt):
jac[i_part] = float(tp[i_part]) / max(float(tp[i_part] + fp[i_part] + fn[i_part]), 1e-8)
print(jac)
if not self.involve_bg:
jac.pop(0, None)
if len(jac.keys()) == 0: ### When the found cluster is solely back-ground
jac[0] = 0
print("IoUs computed")
## I am going to change the return value type of reordered_preds.
# return np.mean(jac), tp, fp, fn, reordered_preds.astype(int).tolist(), matched_bg_clusters
# print(jac)
return np.mean(np.array(list(jac.values()))), tp, fp, fn, reordered_preds.astype(int), matched_bg_clusters ## before match it was matched_bg_clusters
@staticmethod
def get_score(flat_preds: np.ndarray, flat_targets: np.ndarray, c1: int, c2: int, precision_based: bool = False) \
-> float:
"""
Calculates IoU given gt class c1 and prediction class c2.
:param flat_preds: flattened predictions
:param flat_targets: flattened gt
:param c1: ground truth class to match
:param c2: predicted class to match
:param precision_based: flag to calculate precision instead of IoU.
:return: The score if gt-c1 was matched to predicted c2.
"""
tmp_all_gt = (flat_targets == c1)
tmp_pred = (flat_preds == c2)
tp = np.sum(tmp_all_gt & tmp_pred)
fp = np.sum(~tmp_all_gt & tmp_pred)
if not precision_based:
fn = np.sum(tmp_all_gt & ~tmp_pred)
jac = float(tp) / max(float(tp + fp + fn), 1e-8)
return jac
else:
prec = float(tp) / max(float(tp + fp), 1e-8)
return prec
def compute_score_matrix(self, num_pred: int, num_gt: int, pred: np.ndarray, gt: np.ndarray,
precision_based: bool = False) -> np.ndarray:
"""
Compute score matrix. Each element i, j of matrix is the score if i was matched j. Computation is parallelized
over self.n_jobs.
:param num_pred: number of predicted classes
:param num_gt: number of ground-truth classes
:param pred: flattened predictions
:param gt: flattened gt
:param precision_based: flag to calculate precision instead of IoU.
:return: num_pred x num_gt matrix with A[i, j] being the score if ground-truth class i was matched to
predicted class j.
"""
print("Parallelizing iou computation")
start = time.time()
# score_mat = Parallel(n_jobs=self.n_jobs)(delayed(self.get_score)(pred, gt, c1, c2, precision_based=precision_based)
# for c2 in range(num_pred) for c1 in range(num_gt))
score_mat = Parallel(n_jobs=self.n_jobs)(delayed(self.get_score)(pred, gt, c1, c2, precision_based=precision_based)
for c2 in np.unique(pred) for c1 in np.unique(gt))
print(f"took {time.time() - start} seconds")
score_mat = np.array(score_mat)
return score_mat.reshape((num_pred, num_gt)).T
def _hungarian_match(self, num_pred: int, num_gt: int, pred: np.ndarray, gt: np.ndarray) -> Tuple[np.ndarray,
np.ndarray]:
# do hungarian matching. If num_pred > num_gt match will be partial only.
iou_mat = self.compute_score_matrix(num_pred, num_gt, pred, gt)
match = linear_sum_assignment(1 - iou_mat)
print("Matched clusters to gt classes:")
print(match)
return match
def _original_match(self, num_pred, num_gt, pred, gt, precision_based=False) -> Dict[int, list]:
score_mat = self.compute_score_matrix(num_pred, num_gt, pred, gt, precision_based=precision_based)
preds_to_gts = {}
preds_to_gt_scores = {}
# Greedily match predicted class to ground-truth class by best score.
for pred_c in range(num_pred):
for gt_c in range(num_gt):
score = score_mat[gt_c, pred_c]
if (pred_c not in preds_to_gts) or (score > preds_to_gt_scores[pred_c]):
preds_to_gts[pred_c] = gt_c
preds_to_gt_scores[pred_c] = score
gt_to_matches = defaultdict(list)
for k,v in preds_to_gts.items():
gt_to_matches[v].append(k)
print("matched clusters to gt classes:")
return gt_to_matches