-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
284 lines (233 loc) · 8.75 KB
/
utils.py
File metadata and controls
284 lines (233 loc) · 8.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
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
import torch
import torch.nn as nn
import torch.nn.functional as F
import matplotlib.pyplot as plt
import numpy as np
from monai.losses import DiceCELoss
from skimage.measure import label, regionprops
diceceloss = DiceCELoss(sigmoid=True)
def generate_click_prompt(msk, num_points=1, pt_label=1):
"""
Generate point prompts from a mask
Args:
msk: Binary mask tensor of shape (B, 1, H, W)
num_points: Number of points to generate per mask
pt_label: Label value for the points
Returns:
Dictionary containing point coordinates and labels
"""
pt_coords_list = []
pt_labels_list = []
b, c, h, w = msk.size()
for j in range(b):
msk_s = msk[j, 0, :, :]
indices = torch.nonzero(msk_s)
for _ in range(num_points):
if indices.nelement() == 0:
# If no non-zero elements, generate a random point
random_index = torch.randint(0, h, (2,)).to(device=msk.device)
pt_label_default = torch.tensor([pt_label], device=msk.device).view(1)
else:
# Sample from non-zero elements
random_idx = torch.randint(0, indices.shape[0], (1,))
random_index = indices[random_idx].squeeze()
pt_label_default = msk_s[random_index[0], random_index[1]].view(1)
pt_coords_list.append(random_index)
pt_labels_list.append(pt_label_default)
pt_coords = torch.stack(pt_coords_list).view(b, num_points, 2)
pt_labels = torch.stack(pt_labels_list).view(b, num_points)
return {
'point_coords': pt_coords,
'point_labels': pt_labels
}
def random_box(mask, box_num=1, std=0.1, max_pixel=5):
"""
Args:
mask: Mask, should be a torch.Tensor of shape (B, 1, H, W).
box_num: Number of bounding boxes, default is 1.
std: Standard deviation of the noise, default is 0.1.
max_pixel: Maximum noise pixel value, default is 5.
Returns:
noise_boxes: Bounding boxes after noise perturbation, returned as a torch.Tensor of shape (B, box_num, 4).
"""
B, C, H, W = mask.shape
noise_boxes = []
for i in range(B):
single_mask = mask[i, 0, :, :].cpu().numpy()
label_img = label(single_mask)
regions = regionprops(label_img)
boxes = [tuple(region.bbox) for region in regions]
if len(boxes) == 0:
noise_boxes.append([(0, 0, 0, 0) for _ in range(box_num)])
continue
if len(boxes) >= box_num:
sorted_regions = sorted(regions, key=lambda x: x.area, reverse=True)[:box_num]
boxes = [tuple(region.bbox) for region in sorted_regions]
elif len(boxes) < box_num:
num_duplicates = box_num - len(boxes)
boxes += [boxes[i % len(boxes)] for i in range(num_duplicates)]
batch_noise_boxes = []
for box in boxes:
y0, x0, y1, x1 = box
width, height = abs(x1 - x0), abs(y1 - y0)
noise_std = min(width, height) * std
max_noise = max(1, min(max_pixel, int(noise_std * 5)))
noise_x = np.random.randint(-max_noise, max_noise)
noise_y = np.random.randint(-max_noise, max_noise)
x0, y0 = x0 + noise_x, y0 + noise_y
x1, y1 = x1 + noise_x, y1 + noise_y
batch_noise_boxes.append((x0, y0, x1, y1))
noise_boxes.append(batch_noise_boxes)
return torch.as_tensor(noise_boxes, dtype=torch.float)
def elbo(segm, label, kl_divergence, beta):
"""
Compute ELBO loss using DiceCE loss for reconstruction
"""
if not isinstance(kl_divergence, torch.Tensor):
kl_divergence = torch.tensor(kl_divergence).to(label.device)
reconstruction_loss = diceceloss(input=segm, target=label)
kl_loss = torch.mean(kl_divergence)
return reconstruction_loss + beta * kl_loss
def truncated_normal_(tensor, mean=0, std=1):
"""
Initialize tensor with truncated normal distribution
"""
size = tensor.shape
tmp = tensor.new_empty(size + (4,)).normal_()
valid = (tmp < 2) & (tmp > -2)
ind = valid.max(-1, keepdim=True)[1]
tensor.data.copy_(tmp.gather(-1, ind).squeeze(-1))
tensor.data.mul_(std).add_(mean)
def init_weights(m):
"""
Initialize network weights using Kaiming initialization
"""
if type(m) == nn.Conv2d or type(m) == nn.ConvTranspose2d:
nn.init.kaiming_normal_(m.weight, mode='fan_in', nonlinearity='relu')
truncated_normal_(m.bias, mean=0, std=0.001)
def init_weights_orthogonal_normal(m):
"""
Initialize network weights using orthogonal initialization
"""
if type(m) == nn.Conv2d or type(m) == nn.ConvTranspose2d:
nn.init.orthogonal_(m.weight)
truncated_normal_(m.bias, mean=0, std=0.001)
def l2_regularisation(m):
"""
Compute L2 regularization for model parameters
"""
l2_reg = None
for W in m.parameters():
if l2_reg is None:
l2_reg = W.norm(2)
else:
l2_reg = l2_reg + W.norm(2)
return l2_reg
def save_mask_prediction_example(mask, pred, iter):
"""
Save mask and prediction visualizations
"""
plt.imshow(pred[0,:,:], cmap='Greys')
plt.savefig('images/'+str(iter)+"_prediction.png")
plt.close()
plt.imshow(mask[0,:,:], cmap='Greys')
plt.savefig('images/'+str(iter)+"_mask.png")
plt.close()
def prepare_image(image, target_size):
"""
Prepare image for model input
"""
if image.dtype == torch.uint8:
image = image.float() / 255.0
if len(image.shape) == 2:
image = image.unsqueeze(0).unsqueeze(0)
elif len(image.shape) == 3:
image = image.unsqueeze(0)
if image.shape[-2:] != target_size:
image = F.interpolate(
image,
size=target_size,
mode='bilinear',
align_corners=False
)
return image
def post_process_masks(masks, threshold=0.5, min_area=100):
"""
Post-process predicted masks
"""
binary_masks = (masks > threshold).float()
for i in range(binary_masks.shape[0]):
mask = binary_masks[i, 0].cpu().numpy()
from scipy import ndimage
labeled, num_features = ndimage.label(mask)
for j in range(1, num_features + 1):
component = (labeled == j)
if component.sum() < min_area:
mask[component] = 0
binary_masks[i, 0] = torch.from_numpy(mask).to(masks.device)
return binary_masks
class AverageMeter:
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def iou(x, y, axis=-1):
smooth = 1e-8
iou_ = ((x & y).sum(axis)) / ((x | y).sum(axis) + smooth)
iou_[np.isnan(iou_)] = 1.0
return iou_
# exclude background
def distance(x, y):
try:
# x[:, None] -> (M,1,H*W,2)
# y[None, :] -> (1,N,H*W,2)
per_class_iou = iou(x[:, None], y[None, :], axis=-2)
except MemoryError:
per_class_iou = []
for x_ in x:
x_ = np.expand_dims(x_, axis=0) # (1,H*W,2)
per_class_iou.append(iou(x_, y[None, :], axis=-2))
per_class_iou = np.concatenate(per_class_iou)
return 1.0 - per_class_iou[..., 1:].mean(-1)
def calc_generalised_energy_distance(dist_0, dist_1, num_classes=2):
"""
dist_0: shape (M, H, W)
dist_1: shape (N, H, W)
"""
# (M, H, W) -> (M, H*W)
dist_0 = dist_0.reshape((len(dist_0), -1))
dist_1 = dist_1.reshape((len(dist_1), -1))
dist_0 = dist_0.cpu().numpy().astype("int")
dist_1 = dist_1.cpu().numpy().astype("int")
eye = np.eye(num_classes)
dist_0 = eye[dist_0].astype('bool')
dist_1 = eye[dist_1].astype('bool')
cross_distance = np.mean(distance(dist_0, dist_1))
distance_0 = np.mean(distance(dist_0, dist_0))
distance_1 = np.mean(distance(dist_1, dist_1))
return cross_distance, distance_0, distance_1
def generalized_energy_distance(labels, preds, thresh=0.5, num_classes=2):
"""
- labels: shape (B, M, H, W)
- preds: shape (B, N, H, W)
"""
batch_ged = []
bin_preds = (preds > thresh).float()
B = labels.shape[0]
for i in range(B):
dist_0 = labels[i] # shape (M,H,W)
dist_1 = bin_preds[i] # shape (N,H,W)
cross, d_0, d_1 = calc_generalised_energy_distance(dist_0, dist_1, num_classes=num_classes)
# GED = 2 * E[cross] - E[dist_0] - E[dist_1]
ged_i = 2.0 * cross - d_0 - d_1
batch_ged.append(ged_i)
return float(np.mean(batch_ged))