-
Notifications
You must be signed in to change notification settings - Fork 2
/
locmat_cl.py
321 lines (251 loc) · 11.1 KB
/
locmat_cl.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
import torch
import torch.nn as nn
from openselfsup.utils import print_log
from . import builder
from .registry import MODELS
from .utils.loc_mat_utils import cal_intersection_batch
@MODELS.register_module
class LocMatCL(nn.Module):
'''DenseCL.
Part of the code is borrowed from:
"https://github.com/facebookresearch/moco/blob/master/moco/builder.py".
'''
def __init__(self,
backbone,
neck=None,
neck_matching=None,
head=None,
pretrained=None,
queue_len=65536,
feat_dim=128,
momentum=0.999,
loss_lambda=0.5,
**kwargs):
super(LocMatCL, self).__init__()
self.encoder_q = nn.Sequential(
builder.build_backbone(backbone), builder.build_neck(neck), builder.build_neck(neck_matching))
self.encoder_k = nn.Sequential(
builder.build_backbone(backbone), builder.build_neck(neck), builder.build_neck(neck_matching))
self.backbone = self.encoder_q[0]
for param in self.encoder_k.parameters():
param.requires_grad = False
self.head = builder.build_head(head)
self.init_weights(pretrained=pretrained)
self.queue_len = queue_len
self.momentum = momentum
self.loss_lambda = loss_lambda
# create the queue
self.register_buffer("queue", torch.randn(feat_dim, queue_len))
self.queue = nn.functional.normalize(self.queue, dim=0)
self.register_buffer("queue_ptr", torch.zeros(1, dtype=torch.long))
# create the second queue for dense output
self.register_buffer("queue2", torch.randn(feat_dim, queue_len))
self.queue2 = nn.functional.normalize(self.queue2, dim=0)
self.register_buffer("queue2_ptr", torch.zeros(1, dtype=torch.long))
def init_weights(self, pretrained=None):
if pretrained is not None:
print_log('load model from: {}'.format(pretrained), logger='root')
self.encoder_q[0].init_weights(pretrained=pretrained)
self.encoder_q[1].init_weights(init_linear='kaiming')
self.encoder_q[2].init_weights(init_linear='kaiming')
for param_q, param_k in zip(self.encoder_q.parameters(),
self.encoder_k.parameters()):
param_k.data.copy_(param_q.data)
@torch.no_grad()
def _momentum_update_key_encoder(self):
"""
Momentum update of the key encoder
"""
for param_q, param_k in zip(self.encoder_q.parameters(),
self.encoder_k.parameters()):
param_k.data = param_k.data * self.momentum + \
param_q.data * (1. - self.momentum)
@torch.no_grad()
def _dequeue_and_enqueue(self, keys):
# gather keys before updating queue
keys = concat_all_gather(keys)
batch_size = keys.shape[0]
ptr = int(self.queue_ptr)
assert self.queue_len % batch_size == 0 # for simplicity
# replace the keys at ptr (dequeue and enqueue)
self.queue[:, ptr:ptr + batch_size] = keys.transpose(0, 1)
ptr = (ptr + batch_size) % self.queue_len # move pointer
self.queue_ptr[0] = ptr
@torch.no_grad()
def _dequeue_and_enqueue2(self, keys):
# gather keys before updating queue
keys = concat_all_gather(keys)
batch_size = keys.shape[0]
ptr = int(self.queue2_ptr)
assert self.queue_len % batch_size == 0 # for simplicity
# replace the keys at ptr (dequeue and enqueue)
self.queue2[:, ptr:ptr + batch_size] = keys.transpose(0, 1)
ptr = (ptr + batch_size) % self.queue_len # move pointer
self.queue2_ptr[0] = ptr
@torch.no_grad()
def _batch_shuffle_ddp(self, x):
"""
Batch shuffle, for making use of BatchNorm.
*** Only support DistributedDataParallel (DDP) model. ***
"""
# gather from all gpus
batch_size_this = x.shape[0]
x_gather = concat_all_gather(x)
batch_size_all = x_gather.shape[0]
num_gpus = batch_size_all // batch_size_this
# random shuffle index
idx_shuffle = torch.randperm(batch_size_all).cuda()
# broadcast to all gpus
torch.distributed.broadcast(idx_shuffle, src=0)
# index for restoring
idx_unshuffle = torch.argsort(idx_shuffle)
# shuffled index for this gpu
gpu_idx = torch.distributed.get_rank()
idx_this = idx_shuffle.view(num_gpus, -1)[gpu_idx]
return x_gather[idx_this], idx_unshuffle
@torch.no_grad()
def _batch_unshuffle_ddp(self, x, idx_unshuffle):
"""
Undo batch shuffle.
*** Only support DistributedDataParallel (DDP) model. ***
"""
# gather from all gpus
batch_size_this = x.shape[0]
x_gather = concat_all_gather(x)
batch_size_all = x_gather.shape[0]
num_gpus = batch_size_all // batch_size_this
# restored index for this gpu
gpu_idx = torch.distributed.get_rank()
idx_this = idx_unshuffle.view(num_gpus, -1)[gpu_idx]
return x_gather[idx_this]
def match_local(self, boxes, q_grid, k_grid):
boxes = boxes.flatten(2, 3)
box_q = boxes[:, 0, ...]
box_k = boxes[:, 1, ...]
# print("box_qk:", box_q.shape) # 32, 49, 4
with torch.no_grad():
intersect_q, intersect_k = cal_intersection_batch(box_q, box_k)
b, n, m = intersect_q.shape
intersect_q = intersect_q.flatten(0, 1)
intersect_k = intersect_k.flatten(0, 1)
weight_q = torch.sum(intersect_q, dim=-1)
weight_k = torch.sum(intersect_k, dim=-1)
# print("intersect_q:", intersect_q.shape, "intersect_k:", intersect_k.shape)
matching_mat_q = non_zero_divide(intersect_q, weight_q).reshape(b, n, m)
matching_mat_k = non_zero_divide(intersect_k, weight_k).reshape(b, n, m)
# print("weight_q:", weight_q.shape)
weight_qk = torch.cat((weight_q, weight_k), dim=0)
# print("matching_mat_q:", matching_mat_q.shape, "matching_mat_k:", matching_mat_k.shape)
mat_q_q_grid = q_grid
with torch.no_grad():
mat_q_k_grid = self.encoder_k[2](k_grid, matching_mat_q)
mat_k_q_grid = self.encoder_q[2](q_grid, matching_mat_k)
mat_k_k_grid = k_grid
q_grid = torch.cat((mat_q_q_grid, mat_k_q_grid), dim=0).flatten(0, 1)
k_grid = torch.cat((mat_q_k_grid, mat_k_k_grid), dim=0).flatten(0, 1)
# print("q_grid:", q_grid.shape, "k_grid:", k_grid.shape)
q_grid = nn.functional.normalize(q_grid, dim=1)
k_grid = nn.functional.normalize(k_grid, dim=1)
return q_grid, k_grid, weight_qk
def forward_train(self, img, boxes, **kwargs):
assert img.dim() == 5, \
"Input must have 5 dims, got: {}".format(img.dim())
assert boxes.dim() == 5, \
"Boxes must have 5 dims, got: {}".format(boxes.dim())
im_q = img[:, 0, ...].contiguous()
im_k = img[:, 1, ...].contiguous()
# compute query features
q_b = self.encoder_q[0](im_q) # backbone features
q, q_grid, q2 = self.encoder_q[1](q_b) # queries: NxC; NxCxS^2
# q_b = q_b[0]
# q_b = q_b.view(q_b.size(0), q_b.size(1), -1)
q = nn.functional.normalize(q, dim=1)
q2 = nn.functional.normalize(q2, dim=1)
# q_grid = nn.functional.normalize(q_grid, dim=1)
# q_b = nn.functional.normalize(q_b, dim=1)
# compute key features
with torch.no_grad(): # no gradient to keys
self._momentum_update_key_encoder() # update the key encoder
# shuffle for making use of BN
im_k, idx_unshuffle = self._batch_shuffle_ddp(im_k)
k_b = self.encoder_k[0](im_k)
k, k_grid, k2 = self.encoder_k[1](k_b) # keys: NxC; NxCxS^2
# k_b = k_b[0]
# k_b = k_b.view(k_b.size(0), k_b.size(1), -1)
k = nn.functional.normalize(k, dim=1)
k2 = nn.functional.normalize(k2, dim=1)
# k_grid = nn.functional.normalize(k_grid, dim=1)
# k_b = nn.functional.normalize(k_b, dim=1)
# undo shuffle
k = self._batch_unshuffle_ddp(k, idx_unshuffle)
k2 = self._batch_unshuffle_ddp(k2, idx_unshuffle)
k_grid = self._batch_unshuffle_ddp(k_grid, idx_unshuffle)
# k_b = self._batch_unshuffle_ddp(k_b, idx_unshuffle)
# compute logits, global loss, single
# Einstein sum is more intuitive
# positive logits: Nx1
l_pos = torch.einsum('nc,nc->n', [q, k]).unsqueeze(-1)
# negative logits: NxK
l_neg = torch.einsum('nc,ck->nk', [q, self.queue.clone().detach()])
# local loss, dense
q_grid = q_grid.permute(0, 2, 1)
k_grid = k_grid.permute(0, 2, 1)
# q_grid = q_grid.reshape(-1, q_grid.size(2))
# k_grid = k_grid.reshape(-1, k_grid.size(2))
q_grid_mat, k_grid_mat, weights = self.match_local(boxes, q_grid, k_grid)
l_pos_dense = torch.einsum('nc,nc->n', [q_grid_mat, k_grid_mat]).unsqueeze(-1)
l_neg_dense = torch.einsum('nc,ck->nk', [q_grid_mat,
self.queue2.clone().detach()])
loss_single = self.head(l_pos, l_neg)['loss']
loss_dense = self.head(l_pos_dense, l_neg_dense, weights=weights)['loss']
losses = dict()
losses['loss_contra_single'] = loss_single * (1 - self.loss_lambda)
losses['loss_contra_dense'] = loss_dense * self.loss_lambda
self._dequeue_and_enqueue(k)
self._dequeue_and_enqueue2(k2)
return losses
def forward_test(self, img, **kwargs):
im_q = img.contiguous()
# compute query features
#_, q_grid, _ = self.encoder_q(im_q)
q_grid = self.backbone(im_q)[0]
q_grid = q_grid.view(q_grid.size(0), q_grid.size(1), -1)
q_grid = nn.functional.normalize(q_grid, dim=1)
return None, q_grid, None
def forward(self, img, mode='train', **kwargs):
if mode == 'train':
return self.forward_train(img, **kwargs)
elif mode == 'test':
return self.forward_test(img, **kwargs)
elif mode == 'extract':
return self.backbone(img)
else:
raise Exception("No such mode: {}".format(mode))
# utils
@torch.no_grad()
def concat_all_gather(tensor):
"""
Performs all_gather operation on the provided tensors.
*** Warning ***: torch.distributed.all_gather has no gradient.
"""
tensors_gather = [
torch.ones_like(tensor)
for _ in range(torch.distributed.get_world_size())
]
torch.distributed.all_gather(tensors_gather, tensor, async_op=False)
output = torch.cat(tensors_gather, dim=0)
return output
def non_zero_divide(a, b):
"""
a.shape = b, ...
b.shape = b,
Args:
a:
b:
Returns:
c = a / b, c = 0 where b = 0
"""
c = torch.zeros_like(a)
mask = (b > 0.)
c[mask, ...] = a[mask, ...] / b[mask, None]
return c