-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathutils.py
More file actions
executable file
·152 lines (120 loc) · 4.75 KB
/
utils.py
File metadata and controls
executable file
·152 lines (120 loc) · 4.75 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
import numpy as np
import torch
import torch.nn.functional as F
def get_axis(img):
# get the axis to slice the 3D volume
shape = img.shape
# get shape difference between the axes
diff_ratio = [2*abs(shape[1]-shape[2])/(shape[1]+shape[2]),
2*abs(shape[0]-shape[2])/(shape[0]+shape[2]),
2*abs(shape[0]-shape[1])/(shape[0]+shape[1])]
if diff_ratio[0] < 0.5:
valid_axis = 0
else:
min_axis = np.argmin(shape)
valid_axis = min_axis
return valid_axis
def get_padding(vol):
shape = vol.shape[1:]
if shape[0] > shape[1]:
pad1 = (shape[0] - shape[1]) // 2
pad2 = (shape[0] - shape[1]) - pad1
pad_width = [[0, 0], [0, 0], [pad1, pad2]]
else:
pad1 = (shape[1] - shape[0]) // 2
pad2 = (shape[1] - shape[0]) - pad1
pad_width = [[0, 0], [pad1, pad2], [0, 0]]
padded_size = max(shape)
return pad_width, padded_size
def remove_padding(vol, pad_width):
if pad_width is not None:
l1 = int(pad_width[1][0])
r1 = int(vol.shape[1] - pad_width[1][1])
l2 = int(pad_width[2][0])
r2 = int(vol.shape[2] - pad_width[2][1])
vol = vol[:, l1:r1, l2:r2]
return vol
def pad_and_resize(vol, size):
pad_width, padded_size = get_padding(vol)
if pad_width is not None:
vol = np.pad(vol, pad_width, mode="constant", constant_values=0)
vol = torch.from_numpy(vol).unsqueeze(0)
resized_vol = F.interpolate(
vol, size=(size, size), mode="bicubic", align_corners=False
)
return resized_vol.squeeze(0), pad_width, padded_size
def process_input(vol, size):
# vol: 3D np.ndarray
# size: int
valid_axis = get_axis(vol)
vol = np.moveaxis(vol, valid_axis, 0)
# pad to square with equal padding on both sides
vol, pad_width, padded_size = pad_and_resize(vol, size)
return vol, pad_width, padded_size, valid_axis
def process_output(vol, pad_width, padded_size, valid_axis):
# vol: torch.Tensor with batch size 1
# pad_width: tuple
# padded_size: int
# valid_axis: int
if vol.shape[-1] != padded_size or vol.shape[-2] != padded_size:
vol = F.interpolate(
vol.unsqueeze(0).float(), size=(padded_size, padded_size), mode="nearest", # align_corners=False
)
vol = vol.squeeze(0).int()
vol = vol.cpu().numpy()
vol = remove_padding(vol, pad_width)
vol = np.moveaxis(vol, 0, valid_axis)
return vol
def slice_nms(mask_preds, scores, iou_threshold=0.5, score_threshold=0.5):
# do non-max suppression for each slice
# mask_preds: (N, D, H, W), binary class probability masks
# scores: (N, D), object existence scores
# iou_threshold: IoU threshold for non-max suppression
N, D, H, W = mask_preds.shape
keep_masks = torch.zeros((N, D), dtype=torch.int64, device=mask_preds.device)
for i in range(D):
keep = nms_masks_batch_iou(mask_preds[:, i] > 0.5, scores[:,i],
iou_threshold=iou_threshold,
score_threshold=score_threshold)
if len(keep) == 0:
continue
# make the kept masks 1 and the rest 0
keep_masks[keep, i] = 1
return mask_preds * keep_masks.unsqueeze(-1).unsqueeze(-1)
def nms_masks_batch_iou(masks: torch.Tensor,
scores: torch.Tensor,
iou_threshold: float = 0.5,
score_threshold: float = 0.5):
"""
masks: (N, H, W) binary (0/1 or bool) tensor
scores: (N,) tensor of confidence scores
returns: List[int] of kept indices
"""
# ensure bool for logical ops
masks = masks.bool()
# sort in descending score order
order = scores.argsort(descending=True)
keep = []
while order.numel() > 0:
i = order[0].item()
# stop if below score threshold
if scores[i] < score_threshold:
break
# skip empty masks
if masks[i].sum() == 0:
order = order[1:]
continue
keep.append(i)
if order.numel() == 1:
break
# batch compute IoUs of mask[i] vs all remaining
cur_mask = masks[i] # (H, W)
other_masks = masks[order[1:]] # (M, H, W)
# intersection / union per mask
inter = (other_masks & cur_mask).view(other_masks.size(0), -1).sum(1).float()
union = (other_masks | cur_mask).view(other_masks.size(0), -1).sum(1).float()
ious = inter / union # (M,)
# keep only those with IoU <= threshold
remaining = torch.nonzero(ious <= iou_threshold, as_tuple=False).squeeze(1)
order = order[1:][remaining]
return keep