-
Notifications
You must be signed in to change notification settings - Fork 25
/
module_list.py
276 lines (215 loc) · 11.5 KB
/
module_list.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
import torch
import torch.nn.functional as F
import numpy as np
import os
import copy
from torch.optim.lr_scheduler import _LRScheduler
import torchvision.transforms.functional as transforms_f
# --------------------------------------------------------------------------------
# Define EMA: Mean Teacher Framework
# --------------------------------------------------------------------------------
class EMA(object):
def __init__(self, model, alpha):
self.step = 0
self.model = copy.deepcopy(model)
self.alpha = alpha
def update(self, model):
decay = min(1 - 1 / (self.step + 1), self.alpha)
for ema_param, param in zip(self.model.parameters(), model.parameters()):
ema_param.data = decay * ema_param.data + (1 - decay) * param.data
self.step += 1
# --------------------------------------------------------------------------------
# Define Polynomial Decay
# --------------------------------------------------------------------------------
class PolyLR(_LRScheduler):
def __init__(self, optimizer, max_iters, power=0.9, last_epoch=-1, min_lr=1e-6):
self.power = power
self.max_iters = max_iters
self.min_lr = min_lr
super(PolyLR, self).__init__(optimizer, last_epoch)
def get_lr(self):
return [max(base_lr * (1 - self.last_epoch / self.max_iters) ** self.power, self.min_lr)
for base_lr in self.base_lrs]
# --------------------------------------------------------------------------------
# Define training losses
# --------------------------------------------------------------------------------
def compute_supervised_loss(predict, target, reduction=True):
if reduction:
loss = F.cross_entropy(predict, target, ignore_index=-1)
else:
loss = F.cross_entropy(predict, target, ignore_index=-1, reduction='none')
return loss
def compute_unsupervised_loss(predict, target, logits, strong_threshold):
batch_size = predict.shape[0]
valid_mask = (target >= 0).float() # only count valid pixels
weighting = logits.view(batch_size, -1).ge(strong_threshold).sum(-1) / valid_mask.view(batch_size, -1).sum(-1)
loss = F.cross_entropy(predict, target, reduction='none', ignore_index=-1)
weighted_loss = torch.mean(torch.masked_select(weighting[:, None, None] * loss, loss > 0))
return weighted_loss
# --------------------------------------------------------------------------------
# Define ReCo loss
# --------------------------------------------------------------------------------
def compute_reco_loss(rep, label, mask, prob, strong_threshold=1.0, temp=0.5, num_queries=256, num_negatives=256):
batch_size, num_feat, im_w_, im_h = rep.shape
num_segments = label.shape[1]
device = rep.device
# compute valid binary mask for each pixel
valid_pixel = label * mask
# permute representation for indexing: batch x im_h x im_w x feature_channel
rep = rep.permute(0, 2, 3, 1)
# compute prototype (class mean representation) for each class across all valid pixels
seg_feat_all_list = []
seg_feat_hard_list = []
seg_num_list = []
seg_proto_list = []
for i in range(num_segments):
valid_pixel_seg = valid_pixel[:, i] # select binary mask for i-th class
if valid_pixel_seg.sum() == 0: # not all classes would be available in a mini-batch
continue
prob_seg = prob[:, i, :, :]
rep_mask_hard = (prob_seg < strong_threshold) * valid_pixel_seg.bool() # select hard queries
seg_proto_list.append(torch.mean(rep[valid_pixel_seg.bool()], dim=0, keepdim=True))
seg_feat_all_list.append(rep[valid_pixel_seg.bool()])
seg_feat_hard_list.append(rep[rep_mask_hard])
seg_num_list.append(int(valid_pixel_seg.sum().item()))
# compute regional contrastive loss
if len(seg_num_list) <= 1: # in some rare cases, a small mini-batch might only contain 1 or no semantic class
return torch.tensor(0.0)
else:
reco_loss = torch.tensor(0.0)
seg_proto = torch.cat(seg_proto_list)
valid_seg = len(seg_num_list)
seg_len = torch.arange(valid_seg)
for i in range(valid_seg):
# sample hard queries
if len(seg_feat_hard_list[i]) > 0:
seg_hard_idx = torch.randint(len(seg_feat_hard_list[i]), size=(num_queries,))
anchor_feat_hard = seg_feat_hard_list[i][seg_hard_idx]
anchor_feat = anchor_feat_hard
else: # in some rare cases, all queries in the current query class are easy
continue
# apply negative key sampling (with no gradients)
with torch.no_grad():
# generate index mask for the current query class; e.g. [0, 1, 2] -> [1, 2, 0] -> [2, 0, 1]
seg_mask = torch.cat(([seg_len[i:], seg_len[:i]]))
# compute similarity for each negative segment prototype (semantic class relation graph)
proto_sim = torch.cosine_similarity(seg_proto[seg_mask[0]].unsqueeze(0), seg_proto[seg_mask[1:]], dim=1)
proto_prob = torch.softmax(proto_sim / temp, dim=0)
# sampling negative keys based on the generated distribution [num_queries x num_negatives]
negative_dist = torch.distributions.categorical.Categorical(probs=proto_prob)
samp_class = negative_dist.sample(sample_shape=[num_queries, num_negatives])
samp_num = torch.stack([(samp_class == c).sum(1) for c in range(len(proto_prob))], dim=1)
# sample negative indices from each negative class
negative_num_list = seg_num_list[i+1:] + seg_num_list[:i]
negative_index = negative_index_sampler(samp_num, negative_num_list)
# index negative keys (from other classes)
negative_feat_all = torch.cat(seg_feat_all_list[i+1:] + seg_feat_all_list[:i])
negative_feat = negative_feat_all[negative_index].reshape(num_queries, num_negatives, num_feat)
# combine positive and negative keys: keys = [positive key | negative keys] with 1 + num_negative dim
positive_feat = seg_proto[i].unsqueeze(0).unsqueeze(0).repeat(num_queries, 1, 1)
all_feat = torch.cat((positive_feat, negative_feat), dim=1)
seg_logits = torch.cosine_similarity(anchor_feat.unsqueeze(1), all_feat, dim=2)
reco_loss = reco_loss + F.cross_entropy(seg_logits / temp, torch.zeros(num_queries).long().to(device))
return reco_loss / valid_seg
def negative_index_sampler(samp_num, seg_num_list):
negative_index = []
for i in range(samp_num.shape[0]):
for j in range(samp_num.shape[1]):
negative_index += np.random.randint(low=sum(seg_num_list[:j]),
high=sum(seg_num_list[:j+1]),
size=int(samp_num[i, j])).tolist()
return negative_index
# --------------------------------------------------------------------------------
# Define evaluation metrics
# --------------------------------------------------------------------------------
class ConfMatrix(object):
def __init__(self, num_classes):
self.num_classes = num_classes
self.mat = None
def update(self, pred, target):
n = self.num_classes
if self.mat is None:
self.mat = torch.zeros((n, n), dtype=torch.int64, device=pred.device)
with torch.no_grad():
k = (target >= 0) & (target < n)
inds = n * target[k].to(torch.int64) + pred[k]
self.mat += torch.bincount(inds, minlength=n ** 2).reshape(n, n)
def get_metrics(self):
h = self.mat.float()
acc = torch.diag(h).sum() / h.sum()
iu = torch.diag(h) / (h.sum(1) + h.sum(0) - torch.diag(h))
return torch.mean(iu).item(), acc.item()
# --------------------------------------------------------------------------------
# Define useful functions
# --------------------------------------------------------------------------------
def label_binariser(inputs):
outputs = torch.zeros_like(inputs).to(inputs.device)
index = torch.max(inputs, dim=1)[1]
outputs = outputs.scatter_(1, index.unsqueeze(1), 1.0)
return outputs
def label_onehot(inputs, num_segments):
batch_size, im_h, im_w = inputs.shape
# remap invalid pixels (-1) into 0, otherwise we cannot create one-hot vector with negative labels.
# we will still mask out those invalid values in valid mask
inputs = torch.relu(inputs)
outputs = torch.zeros([batch_size, num_segments, im_h, im_w]).to(inputs.device)
return outputs.scatter_(1, inputs.unsqueeze(1), 1.0)
def denormalise(x, imagenet=True):
if imagenet:
x = transforms_f.normalize(x, mean=[0., 0., 0.], std=[1 / 0.229, 1 / 0.224, 1 / 0.225])
x = transforms_f.normalize(x, mean=[-0.485, -0.456, -0.406], std=[1., 1., 1.])
return x
else:
return (x + 1) / 2
def create_folder(save_dir):
if not os.path.exists(save_dir):
os.makedirs(save_dir)
def tensor_to_pil(im, label, logits):
im = denormalise(im)
im = transforms_f.to_pil_image(im.cpu())
label = label.float() / 255.
label = transforms_f.to_pil_image(label.unsqueeze(0).cpu())
logits = transforms_f.to_pil_image(logits.unsqueeze(0).cpu())
return im, label, logits
# --------------------------------------------------------------------------------
# Define semi-supervised methods (based on data augmentation)
# --------------------------------------------------------------------------------
def generate_cutout_mask(img_size, ratio=2):
cutout_area = img_size[0] * img_size[1] / ratio
w = np.random.randint(img_size[1] / ratio + 1, img_size[1])
h = np.round(cutout_area / w)
x_start = np.random.randint(0, img_size[1] - w + 1)
y_start = np.random.randint(0, img_size[0] - h + 1)
x_end = int(x_start + w)
y_end = int(y_start + h)
mask = torch.ones(img_size)
mask[y_start:y_end, x_start:x_end] = 0
return mask.float()
def generate_class_mask(pseudo_labels):
labels = torch.unique(pseudo_labels) # all unique labels
labels_select = labels[torch.randperm(len(labels))][:len(labels) // 2] # randomly select half of labels
mask = (pseudo_labels.unsqueeze(-1) == labels_select).any(-1)
return mask.float()
def generate_unsup_data(data, target, logits, mode='cutout'):
batch_size, _, im_h, im_w = data.shape
device = data.device
new_data = []
new_target = []
new_logits = []
for i in range(batch_size):
if mode == 'cutout':
mix_mask = generate_cutout_mask([im_h, im_w], ratio=2).to(device)
target[i][(1 - mix_mask).bool()] = -1
new_data.append((data[i] * mix_mask).unsqueeze(0))
new_target.append(target[i].unsqueeze(0))
new_logits.append((logits[i] * mix_mask).unsqueeze(0))
continue
if mode == 'cutmix':
mix_mask = generate_cutout_mask([im_h, im_w]).to(device)
if mode == 'classmix':
mix_mask = generate_class_mask(target[i]).to(device)
new_data.append((data[i] * mix_mask + data[(i + 1) % batch_size] * (1 - mix_mask)).unsqueeze(0))
new_target.append((target[i] * mix_mask + target[(i + 1) % batch_size] * (1 - mix_mask)).unsqueeze(0))
new_logits.append((logits[i] * mix_mask + logits[(i + 1) % batch_size] * (1 - mix_mask)).unsqueeze(0))
new_data, new_target, new_logits = torch.cat(new_data), torch.cat(new_target), torch.cat(new_logits)
return new_data, new_target.long(), new_logits