-
Notifications
You must be signed in to change notification settings - Fork 4
/
ocl_metrics.py
631 lines (538 loc) · 27.1 KB
/
ocl_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
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
"""Metrics related to the evaluation of masks.
Based on OCLF library:
https://github.com/amazon-science/object-centric-learning-framework/tree/main/ocl/metrics
"""
import math
import torch
import torchmetrics
from torch import nn
import scipy.optimize
import torch.nn.functional as F
from typing import Optional, Tuple, Union
class Resize(nn.Module):
"""Module resizing tensors."""
MODES = {"nearest", "linear", "bilinear", "bicubic", "trilinear", "area", "nearest-exact"}
def __init__(
self,
size: Optional[Union[int, Tuple[int, int]]] = None,
resize_mode: str = "bilinear",
patch_mode: bool = False,
channels_last: bool = False,
):
super().__init__()
self.size = size
if resize_mode not in Resize.MODES:
raise ValueError(f"`mode` must be one of {Resize.MODES}")
self.resize_mode = resize_mode
self.patch_mode = patch_mode
self.channels_last = channels_last
self.expected_dims = 3 if patch_mode else 4
def forward(
self, input: torch.Tensor, size_tensor: Optional[torch.Tensor] = None
) -> torch.Tensor:
"""Resize tensor.
Args:
input: Tensor to resize. If `patch_mode=False`, assumed to be of shape (..., C, H, W).
If `patch_mode=True`, assumed to be of shape (..., C, P), where P is the number of
patches. Patches are assumed to be viewable as a perfect square image. If
`channels_last=True`, channel dimension is assumed to be the last dimension instead.
size_tensor: Tensor which size to resize to. If tensor has <=2 dimensions and the last
dimension of this tensor has length 2, the two entries are taken as height and width.
Otherwise, the size of the last two dimensions of this tensor are used as height
and width.
Returns: Tensor of shape (..., C, H, W), where height and width are either specified by
`size` or `size_tensor`.
"""
dims_to_flatten = input.ndim - self.expected_dims
if dims_to_flatten > 0:
flattened_dims = input.shape[: dims_to_flatten + 1]
input = input.flatten(0, dims_to_flatten)
elif dims_to_flatten < 0:
raise ValueError(
f"Tensor needs at least {self.expected_dims} dimensions, but only has {input.ndim}"
)
if self.patch_mode:
if self.channels_last:
input = input.transpose(-2, -1)
n_channels, n_patches = input.shape[-2:]
patch_size_float = math.sqrt(n_patches)
patch_size = int(math.sqrt(n_patches))
if patch_size_float != patch_size:
raise ValueError(
f"The number of patches needs to be a perfect square, but is {n_patches}."
)
input = input.view(-1, n_channels, patch_size, patch_size)
else:
if self.channels_last:
input = input.permute(0, 3, 1, 2)
if self.size is None:
if size_tensor is None:
raise ValueError("`size` is `None` but no `size_tensor` was passed.")
if size_tensor.ndim <= 2 and size_tensor.shape[-1] == 2:
height, width = size_tensor.unbind(-1)
height = torch.atleast_1d(height)[0].squeeze().detach().cpu()
width = torch.atleast_1d(width)[0].squeeze().detach().cpu()
size = (int(height), int(width))
else:
size = size_tensor.shape[-2:]
else:
size = self.size
input = torch.nn.functional.interpolate(
input,
size=size,
mode=self.resize_mode,
)
if dims_to_flatten > 0:
input = input.unflatten(0, flattened_dims)
return input
def resize_patches_to_image(
patches: torch.Tensor,
size: Optional[int] = None,
scale_factor: Optional[float] = None,
resize_mode: str = "bilinear",
) -> torch.Tensor:
"""Convert and resize a tensor of patches to image shape.
This method requires that the patches can be converted to a square image.
Args:
patches: Patches to be converted of shape (..., C, P), where C is the number of channels and
P the number of patches.
size: Image size to resize to.
scale_factor: Scale factor by which to resize the patches. Can be specified alternatively to
`size`.
resize_mode: Method to resize with. Valid options are "nearest", "nearest-exact", "bilinear",
"bicubic".
Returns:
Tensor of shape (..., C, S, S) where S is the image size.
"""
has_size = size is None
has_scale = scale_factor is None
if has_size == has_scale:
raise ValueError("Exactly one of `size` or `scale_factor` must be specified.")
n_channels = patches.shape[-2]
n_patches = patches.shape[-1]
patch_size_float = math.sqrt(n_patches)
patch_size = int(math.sqrt(n_patches))
if patch_size_float != patch_size:
raise ValueError("The number of patches needs to be a perfect square.")
image = torch.nn.functional.interpolate(
patches.view(-1, n_channels, patch_size, patch_size),
size=size,
scale_factor=scale_factor,
mode=resize_mode,
)
return image.view(*patches.shape[:-1], image.shape[-2], image.shape[-1])
class ARIMetric(torchmetrics.Metric):
"""Computes ARI metric."""
def __init__(
self,
foreground: bool = True,
convert_target_one_hot: bool = False,
ignore_overlaps: bool = False,
):
super().__init__()
self.foreground = foreground
self.convert_target_one_hot = convert_target_one_hot
self.ignore_overlaps = ignore_overlaps
self.add_state(
"values", default=torch.tensor(0.0, dtype=torch.float64), dist_reduce_fx="sum"
)
self.add_state("total", default=torch.tensor(0), dist_reduce_fx="sum")
def update(
self, prediction: torch.Tensor, target: torch.Tensor, ignore: Optional[torch.Tensor] = None
):
"""Update this metric.
Args:
prediction: Predicted mask of shape (B, C, H, W) or (B, F, C, H, W), where C is the
number of classes.
target: Ground truth mask of shape (B, K, H, W) or (B, F, K, H, W), where K is the
number of classes.
ignore: Ignore mask of shape (B, 1, H, W) or (B, 1, K, H, W)
"""
if prediction.ndim == 5:
# Merge frames, height and width to single dimension.
prediction = prediction.transpose(1, 2).flatten(-3, -1)
target = target.transpose(1, 2).flatten(-3, -1)
if ignore is not None:
ignore = ignore.to(torch.bool).transpose(1, 2).flatten(-3, -1)
elif prediction.ndim == 4:
# Merge height and width to single dimension.
prediction = prediction.flatten(-2, -1)
target = target.flatten(-2, -1)
if ignore is not None:
ignore = ignore.to(torch.bool).flatten(-2, -1)
else:
raise ValueError(f"Incorrect input shape: f{prediction.shape}")
if self.ignore_overlaps:
overlaps = (target > 0).sum(1, keepdim=True) > 1
if ignore is None:
ignore = overlaps
else:
ignore = ignore | overlaps
if ignore is not None:
assert ignore.ndim == 3 and ignore.shape[1] == 1
prediction = prediction.clone()
prediction[ignore.expand_as(prediction)] = 0
target = target.clone()
target[ignore.expand_as(target)] = 0
# Make channels / gt labels the last dimension.
prediction = prediction.transpose(-2, -1)
target = target.transpose(-2, -1)
if self.convert_target_one_hot:
target_oh = tensor_to_one_hot(target, dim=2)
# For empty pixels (all values zero), one-hot assigns 1 to the first class, correct for
# this (then it is technically not one-hot anymore).
target_oh[:, :, 0][target.sum(dim=2) == 0] = 0
target = target_oh
# Should be either 0 (empty, padding) or 1 (single object).
assert torch.all(target.sum(dim=-1) < 2), "Issues with target format, mask non-exclusive"
if self.foreground:
ari = fg_adjusted_rand_index(prediction, target)
else:
ari = adjusted_rand_index(prediction, target)
self.values += ari.sum()
self.total += len(ari)
def compute(self) -> torch.Tensor:
return self.values / self.total
class PatchARIMetric(ARIMetric):
"""Computes ARI metric assuming patch masks as input."""
def __init__(
self,
foreground=True,
resize_masks_mode: str = "bilinear",
**kwargs,
):
super().__init__(foreground=foreground, **kwargs)
self.resize_masks_mode = resize_masks_mode
def update(self, prediction: torch.Tensor, target: torch.Tensor):
"""Update this metric.
Args:
prediction: Predicted mask of shape (B, C, P) or (B, F, C, P), where C is the
number of classes and P the number of patches.
target: Ground truth mask of shape (B, K, H, W) or (B, F, K, H, W), where K is the
number of classes.
"""
h, w = target.shape[-2:]
assert h == w
prediction_resized = resize_patches_to_image(
prediction, size=h, resize_mode=self.resize_masks_mode
)
return super().update(prediction=prediction_resized, target=target)
class UnsupervisedMaskIoUMetric(torchmetrics.Metric):
"""Computes IoU metric for segmentation masks when correspondences to ground truth are not known.
Uses Hungarian matching to compute the assignment between predicted classes and ground truth
classes.
Args:
use_threshold: If `True`, convert predicted class probabilities to mask using a threshold.
If `False`, class probabilities are turned into mask using a softmax instead.
threshold: Value to use for thresholding masks.
matching: Approach to match predicted to ground truth classes. For "hungarian", computes
assignment that maximizes total IoU between all classes. For "best_overlap", uses the
predicted class with maximum overlap for each ground truth class. Using "best_overlap"
leads to the "average best overlap" metric.
compute_discovery_fraction: Instead of the IoU, compute the fraction of ground truth classes
that were "discovered", meaning that they have an IoU greater than some threshold.
correct_localization: Instead of the IoU, compute the fraction of images on which at least
one ground truth class was correctly localised, meaning that they have an IoU
greater than some threshold.
discovery_threshold: Minimum IoU to count a class as discovered/correctly localized.
ignore_background: If true, assume class at index 0 of ground truth masks is background class
that is removed before computing IoU.
ignore_overlaps: If true, remove points where ground truth masks has overlappign classes from
predictions and ground truth masks.
"""
def __init__(
self,
use_threshold: bool = False,
threshold: float = 0.5,
matching: str = "hungarian",
compute_discovery_fraction: bool = False,
correct_localization: bool = False,
discovery_threshold: float = 0.5,
ignore_background: bool = False,
ignore_overlaps: bool = False,
):
super().__init__()
self.use_threshold = use_threshold
self.threshold = threshold
self.discovery_threshold = discovery_threshold
self.compute_discovery_fraction = compute_discovery_fraction
self.correct_localization = correct_localization
if compute_discovery_fraction and correct_localization:
raise ValueError(
"Only one of `compute_discovery_fraction` and `correct_localization` can be enabled."
)
matchings = ("hungarian", "best_overlap")
if matching not in matchings:
raise ValueError(f"Unknown matching type {matching}. Valid values are {matchings}.")
self.matching = matching
self.ignore_background = ignore_background
self.ignore_overlaps = ignore_overlaps
self.add_state(
"values", default=torch.tensor(0.0, dtype=torch.float64), dist_reduce_fx="sum"
)
self.add_state("total", default=torch.tensor(0), dist_reduce_fx="sum")
def update(
self, prediction: torch.Tensor, target: torch.Tensor, ignore: Optional[torch.Tensor] = None
):
"""Update this metric.
Args:
prediction: Predicted mask of shape (B, C, H, W) or (B, F, C, H, W), where C is the
number of classes. Assumes class probabilities as inputs.
target: Ground truth mask of shape (B, K, H, W) or (B, F, K, H, W), where K is the
number of classes.
ignore: Ignore mask of shape (B, 1, H, W) or (B, 1, K, H, W)
"""
if prediction.ndim == 5:
# Merge frames, height and width to single dimension.
predictions = prediction.transpose(1, 2).flatten(-3, -1)
targets = target.transpose(1, 2).flatten(-3, -1)
if ignore is not None:
ignore = ignore.to(torch.bool).transpose(1, 2).flatten(-3, -1)
elif prediction.ndim == 4:
# Merge height and width to single dimension.
predictions = prediction.flatten(-2, -1)
targets = target.flatten(-2, -1)
if ignore is not None:
ignore = ignore.to(torch.bool).flatten(-2, -1)
else:
raise ValueError(f"Incorrect input shape: f{prediction.shape}")
if self.use_threshold:
predictions = predictions > self.threshold
else:
indices = torch.argmax(predictions, dim=1)
predictions = torch.nn.functional.one_hot(indices, num_classes=predictions.shape[1])
predictions = predictions.transpose(1, 2)
if self.ignore_background:
targets = targets[:, 1:]
targets = targets > 0 # Ensure masks are binary
if self.ignore_overlaps:
overlaps = targets.sum(1, keepdim=True) > 1
if ignore is None:
ignore = overlaps
else:
ignore = ignore | overlaps
if ignore is not None:
assert ignore.ndim == 3 and ignore.shape[1] == 1
predictions[ignore.expand_as(predictions)] = 0
targets[ignore.expand_as(targets)] = 0
# Should be either 0 (empty, padding) or 1 (single object).
assert torch.all(targets.sum(dim=1) < 2), "Issues with target format, mask non-exclusive"
for pred, target in zip(predictions, targets):
nonzero_classes = torch.sum(target, dim=-1) > 0
target = target[nonzero_classes] # Remove empty (e.g. padded) classes
if len(target) == 0:
continue # Skip elements without any target mask
iou_per_class = unsupervised_mask_iou(
pred, target, matching=self.matching, reduction="none"
)
if self.compute_discovery_fraction:
discovered = iou_per_class > self.discovery_threshold
self.values += discovered.sum() / len(discovered)
elif self.correct_localization:
correctly_localized = torch.any(iou_per_class > self.discovery_threshold)
self.values += correctly_localized.sum()
else:
self.values += iou_per_class.mean()
self.total += 1
def compute(self) -> torch.Tensor:
if self.total == 0:
return torch.zeros_like(self.values)
else:
return self.values / self.total
class MaskCorLocMetric(UnsupervisedMaskIoUMetric):
def __init__(self, **kwargs):
super().__init__(matching="best_overlap", correct_localization=True, **kwargs)
class AverageBestOverlapMetric(UnsupervisedMaskIoUMetric):
def __init__(self, **kwargs):
super().__init__(matching="best_overlap", **kwargs)
class BestOverlapObjectRecoveryMetric(UnsupervisedMaskIoUMetric):
def __init__(self, **kwargs):
super().__init__(matching="best_overlap", compute_discovery_fraction=True, **kwargs)
def unsupervised_mask_iou(
pred_mask: torch.Tensor,
true_mask: torch.Tensor,
matching: str = "hungarian",
reduction: str = "mean",
iou_empty: float = 0.0,
) -> torch.Tensor:
"""Compute intersection-over-union (IoU) between masks with unknown class correspondences.
This metric is also known as Jaccard index. Note that this is a non-batched implementation.
Args:
pred_mask: Predicted mask of shape (C, N), where C is the number of predicted classes and
N is the number of points. Masks are assumed to be binary.
true_mask: Ground truth mask of shape (K, N), where K is the number of ground truth
classes and N is the number of points. Masks are assumed to be binary.
matching: How to match predicted classes to ground truth classes. For "hungarian", computes
assignment that maximizes total IoU between all classes. For "best_overlap", uses the
predicted class with maximum overlap for each ground truth class (each predicted class
can be assigned to multiple ground truth classes). Empty ground truth classes are
assigned IoU of zero.
reduction: If "mean", return IoU averaged over classes. If "none", return per-class IoU.
iou_empty: IoU for the case when a class does not occur, but was also not predicted.
Returns:
Mean IoU over classes if reduction is `mean`, tensor of shape (K,) containing per-class IoU
otherwise.
"""
assert pred_mask.ndim == 2
assert true_mask.ndim == 2
n_gt_classes = len(true_mask)
pred_mask = pred_mask.unsqueeze(1).to(torch.bool)
true_mask = true_mask.unsqueeze(0).to(torch.bool)
intersection = torch.sum(pred_mask & true_mask, dim=-1).to(torch.float64)
union = torch.sum(pred_mask | true_mask, dim=-1).to(torch.float64)
pairwise_iou = intersection / union
# Remove NaN from divide-by-zero: class does not occur, and class was not predicted.
pairwise_iou[union == 0] = iou_empty
if matching == "hungarian":
pred_idxs, true_idxs = scipy.optimize.linear_sum_assignment(
pairwise_iou.cpu(), maximize=True
)
pred_idxs = torch.as_tensor(pred_idxs, dtype=torch.int64, device=pairwise_iou.device)
true_idxs = torch.as_tensor(true_idxs, dtype=torch.int64, device=pairwise_iou.device)
elif matching == "best_overlap":
non_empty_gt = torch.sum(true_mask.squeeze(0), dim=1) > 0
pred_idxs = torch.argmax(pairwise_iou, dim=0)[non_empty_gt]
true_idxs = torch.arange(pairwise_iou.shape[1]).to(non_empty_gt.device)[non_empty_gt]
else:
raise ValueError(f"Unknown matching {matching}")
matched_iou = pairwise_iou[pred_idxs, true_idxs]
iou = torch.zeros(n_gt_classes, dtype=torch.float64, device=pairwise_iou.device)
iou[true_idxs] = matched_iou
if reduction == "mean":
return iou.mean()
else:
return iou
"""Utility functions used in metrics computation."""
def tensor_to_one_hot(tensor: torch.Tensor, dim: int) -> torch.Tensor:
"""Convert tensor to one-hot encoding by using maximum across dimension as one-hot element."""
assert 0 <= dim
max_idxs = torch.argmax(tensor, dim=dim, keepdim=True)
shape = [1] * dim + [-1] + [1] * (tensor.ndim - dim - 1)
one_hot = max_idxs == torch.arange(tensor.shape[dim], device=tensor.device).view(*shape)
return one_hot.to(torch.long)
def adjusted_rand_index(pred_mask: torch.Tensor, true_mask: torch.Tensor) -> torch.Tensor:
"""Computes adjusted Rand index (ARI), a clustering similarity score.
This implementation ignores points with no cluster label in `true_mask` (i.e. those points for
which `true_mask` is a zero vector). In the context of segmentation, that means this function
can ignore points in an image corresponding to the background (i.e. not to an object).
Implementation adapted from https://github.com/deepmind/multi_object_datasets and
https://github.com/google-research/slot-attention-video/blob/main/savi/lib/metrics.py
Args:
pred_mask: Predicted cluster assignment encoded as categorical probabilities of shape
(batch_size, n_points, n_pred_clusters).
true_mask: True cluster assignment encoded as one-hot of shape (batch_size, n_points,
n_true_clusters).
Returns:
ARI scores of shape (batch_size,).
"""
n_pred_clusters = pred_mask.shape[-1]
pred_cluster_ids = torch.argmax(pred_mask, axis=-1)
# Convert true and predicted clusters to one-hot ('oh') representations. We use float64 here on
# purpose, otherwise mixed precision training automatically casts to FP16 in some of the
# operations below, which can create overflows.
true_mask_oh = true_mask.to(torch.float64) # already one-hot
pred_mask_oh = torch.nn.functional.one_hot(pred_cluster_ids, n_pred_clusters).to(torch.float64)
n_ij = torch.einsum("bnc,bnk->bck", true_mask_oh, pred_mask_oh)
a = torch.sum(n_ij, axis=-1)
b = torch.sum(n_ij, axis=-2)
n_fg_points = torch.sum(a, axis=1)
rindex = torch.sum(n_ij * (n_ij - 1), axis=(1, 2))
aindex = torch.sum(a * (a - 1), axis=1)
bindex = torch.sum(b * (b - 1), axis=1)
expected_rindex = aindex * bindex / torch.clamp(n_fg_points * (n_fg_points - 1), min=1)
max_rindex = (aindex + bindex) / 2
denominator = max_rindex - expected_rindex
ari = (rindex - expected_rindex) / denominator
# There are two cases for which the denominator can be zero:
# 1. If both true_mask and pred_mask assign all pixels to a single cluster.
# (max_rindex == expected_rindex == rindex == n_fg_points * (n_fg_points-1))
# 2. If both true_mask and pred_mask assign max 1 point to each cluster.
# (max_rindex == expected_rindex == rindex == 0)
# In both cases, we want the ARI score to be 1.0:
return torch.where(denominator > 0, ari, torch.ones_like(ari))
def fg_adjusted_rand_index(
pred_mask: torch.Tensor, true_mask: torch.Tensor, bg_dim: int = 0
) -> torch.Tensor:
"""Compute adjusted random index using only foreground groups (FG-ARI).
Args:
pred_mask: Predicted cluster assignment encoded as categorical probabilities of shape
(batch_size, n_points, n_pred_clusters).
true_mask: True cluster assignment encoded as one-hot of shape (batch_size, n_points,
n_true_clusters).
bg_dim: Index of background class in true mask.
Returns:
ARI scores of shape (batch_size,).
"""
n_true_clusters = true_mask.shape[-1]
assert 0 <= bg_dim < n_true_clusters
if bg_dim == 0:
true_mask_only_fg = true_mask[..., 1:]
elif bg_dim == n_true_clusters - 1:
true_mask_only_fg = true_mask[..., :-1]
else:
true_mask_only_fg = torch.cat(
(true_mask[..., :bg_dim], true_mask[..., bg_dim + 1 :]), dim=-1
)
return adjusted_rand_index(pred_mask, true_mask_only_fg)
def _all_equal_masked(values: torch.Tensor, mask: torch.Tensor, dim=-1) -> torch.Tensor:
"""Check if all masked values along a dimension of a tensor are the same.
All non-masked values are considered as true, i.e. if no value is masked, true is returned
for this dimension.
"""
assert mask.dtype == torch.bool
_, first_non_masked_idx = torch.max(mask, dim=dim)
comparison_value = values.gather(index=first_non_masked_idx.unsqueeze(dim), dim=dim)
return torch.logical_or(~mask, values == comparison_value).all(dim=dim)
def masks_to_bboxes(masks: torch.Tensor, empty_value: float = -1.0) -> torch.Tensor:
"""Compute bounding boxes around the provided masks.
Adapted from DETR: https://github.com/facebookresearch/detr/blob/main/util/box_ops.py
Args:
masks: Tensor of shape (N, H, W), where N is the number of masks, H and W are the spatial
dimensions.
empty_value: Value bounding boxes should contain for empty masks.
Returns:
Tensor of shape (N, 4), containing bounding boxes in (x1, y1, x2, y2) format, where (x1, y1)
is the coordinate of top-left corner and (x2, y2) is the coordinate of the bottom-right
corner (inclusive) in pixel coordinates. If mask is empty, all coordinates contain
`empty_value` instead.
"""
masks = masks.bool()
if masks.numel() == 0:
return torch.zeros((0, 4), device=masks.device)
large_value = 1e8
inv_mask = ~masks
h, w = masks.shape[-2:]
y = torch.arange(0, h, dtype=torch.float, device=masks.device)
x = torch.arange(0, w, dtype=torch.float, device=masks.device)
y, x = torch.meshgrid(y, x, indexing="ij")
x_mask = masks * x.unsqueeze(0)
x_max = x_mask.flatten(1).max(-1)[0]
x_min = x_mask.masked_fill(inv_mask, large_value).flatten(1).min(-1)[0]
y_mask = masks * y.unsqueeze(0)
y_max = y_mask.flatten(1).max(-1)[0]
y_min = y_mask.masked_fill(inv_mask, large_value).flatten(1).min(-1)[0]
bboxes = torch.stack((x_min, y_min, x_max, y_max), dim=1)
bboxes[x_min == large_value] = empty_value
return bboxes
def _remap_one_hot_mask(
mask: torch.Tensor, new_classes: torch.Tensor, n_new_classes: int, strip_empty: bool = False
):
"""Remap classes from binary mask to new classes.
In the case of an overlap of classes for a point, the new class with the highest ID is
assigned to that point. If no class is assigned to a point, the point will have no class
assigned after remapping as well.
Args:
mask: Binary mask of shape (B, P, K) where K is the number of old classes and P is the
number of points.
new_classes: Tensor of shape (B, K) containing ids of new classes for each old class.
n_new_classes: Number of classes after remapping, i.e. highest class id that can occur.
strip_empty: Whether to remove the empty pixels mask
Returns:
Tensor of shape (B, P, J), where J is the new number of classes.
"""
assert new_classes.shape[1] == mask.shape[2]
mask_dense = (mask * new_classes.unsqueeze(1)).max(dim=-1).values
mask = torch.nn.functional.one_hot(mask_dense.to(torch.long), num_classes=n_new_classes + 1)
if strip_empty:
mask = mask[..., 1:]
return mask