-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrastive_loss.py
397 lines (348 loc) · 14.6 KB
/
contrastive_loss.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
# ---------------------------------------------------------------
# Copyright (c) 2022 BIT-DA. All rights reserved.
# Licensed under the Apache License, Version 2.0
# ---------------------------------------------------------------
# Note that `downscale_label_ratio` method is adapted from: https://github.com/lhoyer/DAFormer
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from collections import deque
from ..builder import LOSSES
from .utils import get_class_weight, weight_reduce_loss
def downscale_label_ratio(gt,
scale_factor,
min_ratio,
n_classes,
ignore_index=255):
assert scale_factor >= 1
if scale_factor == 1:
return gt.clone()
bs, orig_c, orig_h, orig_w = gt.shape
assert orig_c == 1
trg_h, trg_w = orig_h // scale_factor, orig_w // scale_factor
ignore_substitute = n_classes
out = gt.clone() # o/w next line would modify original gt
out[out == ignore_index] = ignore_substitute
out = F.one_hot(
out.squeeze(1), num_classes=n_classes + 1).permute(0, 3, 1, 2)
assert list(out.shape) == [bs, n_classes + 1, orig_h, orig_w], out.shape
out = F.avg_pool2d(out.float(), kernel_size=scale_factor)
gt_ratio, out = torch.max(out, dim=1, keepdim=True)
out[out == ignore_substitute] = ignore_index
out[gt_ratio < min_ratio] = ignore_index
assert list(out.shape) == [bs, 1, trg_h, trg_w], out.shape
return out
def contrast_preparations(feat,
mask,
use_avg_pool,
scale_min_ratio,
num_classes,
ignore_index):
# down-sample mask to fit feat
if use_avg_pool:
scale_factor = mask.shape[-1] // feat.shape[-1]
mask = downscale_label_ratio(mask, scale_factor, scale_min_ratio, num_classes, ignore_index).long().detach()
else:
mask = F.interpolate(mask.float(), size=feat.shape[-2:], mode='nearest').long()
# normalize the feat
# feat = F.normalize(feat, p=2, dim=1) # already normalized in proj_head.py
# transpose the feat shape
A = feat.size(1)
feat = feat.permute(0, 2, 3, 1).contiguous().view(-1, A)
mask = mask.contiguous().view(-1)
msk = (mask != ignore_index)
# remove ignore_index pixels
mask = mask[msk]
feat = feat[msk]
return feat, mask
def proto_reg(feat,
mean=None,
contrast_temp=100.,
contrast_norm=None,
**kwargs):
assert mean is not None, 'Parameter `mean` required'
assert contrast_norm is not None, 'Parameter `contrast_norm` required'
assert not mean.requires_grad
assert feat.requires_grad
if feat.size(0) == 0:
return torch.tensor(0., requires_grad=True).cuda()
mean_feat = torch.mean(feat, 0, keepdim=True)
# feat (1, A) x Ave (A, C)
proto_sim = mean_feat.mm(mean.permute(1, 0).contiguous()) / contrast_temp
loss = torch.sum(torch.softmax(proto_sim, dim=1).log()) / contrast_norm
return loss
def proto_contrastive(feat,
mask,
mean=None,
index=-1,
contrast_temp=100.,
use_avg_pool=True,
scale_min_ratio=0.75,
num_classes=19,
weight=None,
class_weight=None,
reduction='mean',
avg_factor=None,
reg_weight=0,
ignore_index=255,
**kwargs):
if index >= 0:
assert isinstance(feat, list), f'feat list expected for index={index}'
assert isinstance(mean, (list, dict)), f'mean list expected for index={index}'
feat = feat[index]
mean = mean[index]
feat, mask = contrast_preparations(feat, mask, use_avg_pool, scale_min_ratio, num_classes, ignore_index)
assert mean is not None, 'Parameter `mean` required'
assert not mean.requires_grad
assert feat.requires_grad
assert not mask.requires_grad
if feat.size(0) == 0:
return torch.tensor(0., requires_grad=True).cuda()
# feat (N, A) x Ave (A, C)
proto_sim = feat.mm(mean.permute(1, 0).contiguous()) / contrast_temp
# The wrapper function for :func:`F.cross_entropy`
# class_weight is a manual rescaling weight given to each class.
# If given, has to be a Tensor of size C element-wise losses
loss = F.cross_entropy(
proto_sim,
mask,
weight=class_weight,
reduction='none',
ignore_index=ignore_index)
# apply weights and do the reduction
if weight is not None:
weight = weight.float()
loss = weight_reduce_loss(
loss, weight=weight, reduction=reduction, avg_factor=avg_factor)
if reg_weight > 0.:
contrast_norm = num_classes * np.log(num_classes)
loss += reg_weight * proto_reg(feat, mean, contrast_temp, contrast_norm=contrast_norm)
return loss
def dist_contrastive(feat,
mask,
mean=None,
covariance=None,
ratio=1.0,
index=-1,
contrast_temp=100.,
use_avg_pool=True,
scale_min_ratio=0.75,
num_classes=19,
weight=None,
class_weight=None,
reduction='mean',
avg_factor=None,
reg_weight=0,
ignore_index=255,
**kwargs):
if index >= 0:
assert isinstance(feat, list), f'feat list expected for index={index}'
assert isinstance(mean, (list, dict)), f'mean list expected for index={index}'
assert isinstance(covariance, (list, dict)), f'covariance list expected for index={index}'
feat = feat[index]
mean = mean[index]
covariance = covariance[index]
feat, mask = contrast_preparations(feat, mask, use_avg_pool, scale_min_ratio, num_classes, ignore_index)
assert mean is not None, 'Parameter `mean` required'
assert covariance is not None, 'Parameter `covariance` required'
assert not mean.requires_grad
assert not covariance.requires_grad
assert feat.requires_grad
assert not mask.requires_grad
if feat.size(0) == 0:
return torch.tensor(0., requires_grad=True).cuda()
# feat (N, A) x Ave (A, C)
temp1 = feat.mm(mean.permute(1, 0).contiguous())
# feat (N, A)^2 x CoVariance (A, C)
covariance = covariance * ratio / contrast_temp
temp2 = 0.5 * feat.pow(2).mm(covariance.permute(1, 0).contiguous())
logits = temp1 + temp2
logits = logits / contrast_temp
# The wrapper function for :func:`F.cross_entropy`
# class_weight is a manual rescaling weight given to each class.
# If given, has to be a Tensor of size C element-wise losses
ce_loss = F.cross_entropy(
logits,
mask,
weight=class_weight,
reduction='none',
ignore_index=ignore_index)
# calculate the second term using temp2 cache
# 1. select the corresponding CoVariance of the positive class
# q_i^T \Sigma q_i / \tau -> q_i^2 mul \Sigma
jcl_loss = 0.5 * torch.sum(feat.pow(2).mul(covariance[mask]), dim=1) / contrast_temp
loss = ce_loss + jcl_loss
# apply weights and do the reduction
if weight is not None:
weight = weight.float()
loss = weight_reduce_loss(
loss, weight=weight, reduction=reduction, avg_factor=avg_factor)
if reg_weight > 0.:
contrast_norm = num_classes * np.log(num_classes)
loss += reg_weight * proto_reg(feat, mean, contrast_temp, contrast_norm=contrast_norm)
return loss
def bank_contrastive(feat,
mask,
bank=None,
mean=None,
index=-1,
contrast_temp=100.,
use_avg_pool=True,
scale_min_ratio=0.75,
num_classes=19,
weight=None,
reduction='mean',
avg_factor=None,
reg_weight=0,
ignore_index=255,
**kwargs):
if index >= 0:
assert isinstance(feat, list), f'feat list expected for index={index}'
assert isinstance(bank, (list, dict)) \
and isinstance(bank[index], deque), f'bank list expected for index={index}'
feat = feat[index]
bank = bank[index]
if reg_weight > 0.:
assert isinstance(mean, (list, dict)), f'mean list expected for index={index}'
mean = mean[index]
feat, mask = contrast_preparations(feat, mask, use_avg_pool, scale_min_ratio, num_classes, ignore_index)
assert bank is not None, 'Parameter `bank` required'
if reg_weight > 0.:
assert mean is not None, 'Parameter `mean` required'
assert not mean.requires_grad
assert feat.requires_grad
assert not mask.requires_grad
if feat.size(0) == 0:
return torch.tensor(0., requires_grad=True).cuda()
loss = []
# calculate per class
for cls in range(num_classes):
cls_filter = (mask == cls)
cls_feat = feat[cls_filter] # NcxA
pos, neg = [], []
for idx in range(num_classes):
idx_bank = list(bank[idx])
cls_bank = torch.cat(idx_bank, dim=0)
bank_sim = cls_feat.mm(cls_bank.permute(1, 0).contiguous()) / contrast_temp
if idx == cls:
pos = bank_sim # NcxMp
else:
neg.append(bank_sim.mean(1, keepdim=True)) # NcxMn -> Ncx1
neg = torch.cat(neg, dim=1) # Ncx(C-1)
exp_pos = pos.exp() # NcxMp
sum_exp_neg = neg.exp().sum(1, keepdim=True) # Ncx1
softmax_term = exp_pos / (exp_pos + sum_exp_neg) # NcxMp
cls_loss = - softmax_term.log().mean(dim=1) # Nc
loss.append(cls_loss)
loss = torch.cat(loss, dim=0)
# apply weights and do the reduction
if weight is not None:
weight = weight.float()
loss = weight_reduce_loss(
loss, weight=weight, reduction=reduction, avg_factor=avg_factor)
if reg_weight > 0.:
contrast_norm = num_classes * np.log(num_classes)
loss += reg_weight * proto_reg(feat, mean, contrast_temp, contrast_norm=contrast_norm)
return loss
@LOSSES.register_module()
class ContrastiveLoss(nn.Module):
"""ContrastiveLoss.
Args:
use_dist (bool, optional): Whether to use distribution based contrastive loss.
Defaults to False.
use_bank (bool, optional): Whether to use memory bank based contrastive loss.
Defaults to False.
use_reg (bool, optional): Whether to use regularization term.
Defaults to False.
use_avg_pool (bool, optional): Whether to use average pooling for down sampling.
Defaults to True.
contrast_temp (double, optional): Temperature used in contrastive loss.
Defaults to 100.
reduction (str, optional): . Defaults to 'mean'.
Options are "none", "mean" and "sum".
class_weight (list[float] | str, optional): Weight of each class. If in
str format, read them from a file. Defaults to None.
loss_weight (float, optional): Weight of the loss. Defaults to 1.0.
"""
def __init__(self,
use_dist=False,
use_bank=False,
use_reg=False,
use_avg_pool=True,
scale_min_ratio=0.75,
num_classes=None,
contrast_temp=100.,
reduction='mean',
class_weight=None,
loss_weight=1.0,
reg_relative_weight=1.0):
super(ContrastiveLoss, self).__init__()
assert (use_dist is False) or (use_bank is False)
assert num_classes is not None
self.use_dist = use_dist
self.use_bank = use_bank
self.use_reg = use_reg
self.use_avg_pool = use_avg_pool
self.scale_min_ratio = scale_min_ratio
self.contrast_temp = contrast_temp
self.num_classes = num_classes
self.reduction = reduction
self.loss_weight = loss_weight
self.reg_weight = reg_relative_weight
self.class_weight = get_class_weight(class_weight)
if self.use_dist:
self.contrast_criterion = dist_contrastive
elif self.use_bank:
self.contrast_criterion = bank_contrastive
else:
self.contrast_criterion = proto_contrastive
def forward(self,
feat,
mask,
weight=None,
avg_factor=None,
reduction_override=None,
**kwargs):
"""Forward function."""
# Parameters mean, covariance are sometimes required
assert reduction_override in (None, 'none', 'mean', 'sum')
reduction = (
reduction_override if reduction_override else self.reduction)
if self.class_weight is not None:
class_weight = feat.new_tensor(self.class_weight)
else:
class_weight = None
if isinstance(feat, list):
if not isinstance(self.loss_weight, list):
self.loss_weight = [self.loss_weight for _ in range(len(feat))]
loss_contrast = [self.loss_weight[i] * self.contrast_criterion(
feat,
mask,
weight=weight,
index=i,
contrast_temp=self.contrast_temp,
use_avg_pool=self.use_avg_pool,
scale_min_ratio=self.scale_min_ratio,
num_classes=self.num_classes,
class_weight=class_weight,
reduction=reduction,
avg_factor=avg_factor,
reg_weight=self.reg_weight if self.use_reg else 0,
**kwargs) for i in range(len(feat))]
loss_contrast = sum(loss_contrast)
else:
loss_contrast = self.loss_weight * self.contrast_criterion(
feat,
mask,
weight=weight,
contrast_temp=self.contrast_temp,
use_avg_pool=self.use_avg_pool,
scale_min_ratio=self.scale_min_ratio,
num_classes=self.num_classes,
class_weight=class_weight,
reduction=reduction,
avg_factor=avg_factor,
reg_weight=self.reg_weight if self.use_reg else 0,
**kwargs)
return loss_contrast