-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpet.py
600 lines (504 loc) · 25.2 KB
/
pet.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
"""
PET model and criterion classes
"""
import torch
import torch.nn.functional as F
from torch import nn
from util.misc import (NestedTensor, nested_tensor_from_tensor_list,
get_world_size, is_dist_avail_and_initialized)
from .matcher import build_matcher
from .backbones import *
from .transformer import *
from .position_encoding import build_position_encoding
class BasePETCount(nn.Module):
"""
Base PET model
"""
def __init__(self, backbone, num_classes, quadtree_layer='sparse', args=None, **kwargs):
super().__init__()
self.backbone = backbone
self.transformer = kwargs['transformer']
hidden_dim = args.hidden_dim
self.class_embed = nn.Linear(hidden_dim, num_classes + 1)
self.coord_embed = MLP(hidden_dim, hidden_dim, 2, 3)
self.pq_stride = args.sparse_stride if quadtree_layer == 'sparse' else args.dense_stride
self.feat_name = '8x' if quadtree_layer == 'sparse' else '4x'
def points_queris_embed(self, samples, stride=8, src=None, **kwargs):
"""
Generate point query embedding during training
"""
# dense position encoding at every pixel location
dense_input_embed = kwargs['dense_input_embed']
bs, c = dense_input_embed.shape[:2]
# get image shape
input = samples.tensors
image_shape = torch.tensor(input.shape[2:])
shape = (image_shape + stride//2 -1) // stride
# generate point queries
shift_x = ((torch.arange(0, shape[1]) + 0.5) * stride).long()
shift_y = ((torch.arange(0, shape[0]) + 0.5) * stride).long()
shift_y, shift_x = torch.meshgrid(shift_y, shift_x)
points_queries = torch.vstack([shift_y.flatten(), shift_x.flatten()]).permute(1,0) # 2xN --> Nx2
h, w = shift_x.shape
# get point queries embedding
query_embed = dense_input_embed[:, :, points_queries[:, 0], points_queries[:, 1]]
bs, c = query_embed.shape[:2]
query_embed = query_embed.view(bs, c, h, w)
# get point queries features, equivalent to nearest interpolation
shift_y_down, shift_x_down = points_queries[:, 0] // stride, points_queries[:, 1] // stride
query_feats = src[:, :, shift_y_down,shift_x_down]
query_feats = query_feats.view(bs, c, h, w)
return query_embed, points_queries, query_feats
def points_queris_embed_inference(self, samples, stride=8, src=None, **kwargs):
"""
Generate point query embedding during inference
"""
# dense position encoding at every pixel location
dense_input_embed = kwargs['dense_input_embed']
bs, c = dense_input_embed.shape[:2]
# get image shape
input = samples.tensors
image_shape = torch.tensor(input.shape[2:])
shape = (image_shape + stride//2 -1) // stride
# generate points queries
shift_x = ((torch.arange(0, shape[1]) + 0.5) * stride).long()
shift_y = ((torch.arange(0, shape[0]) + 0.5) * stride).long()
shift_y, shift_x = torch.meshgrid(shift_y, shift_x)
points_queries = torch.vstack([shift_y.flatten(), shift_x.flatten()]).permute(1,0) # 2xN --> Nx2
h, w = shift_x.shape
# get points queries embedding
query_embed = dense_input_embed[:, :, points_queries[:, 0], points_queries[:, 1]]
bs, c = query_embed.shape[:2]
# get points queries features, equivalent to nearest interpolation
shift_y_down, shift_x_down = points_queries[:, 0] // stride, points_queries[:, 1] // stride
query_feats = src[:, :, shift_y_down, shift_x_down]
# window-rize
query_embed = query_embed.reshape(bs, c, h, w)
points_queries = points_queries.reshape(h, w, 2).permute(2, 0, 1).unsqueeze(0)
query_feats = query_feats.reshape(bs, c, h, w)
dec_win_w, dec_win_h = kwargs['dec_win_size']
query_embed_win = window_partition(query_embed, window_size_h=dec_win_h, window_size_w=dec_win_w)
points_queries_win = window_partition(points_queries, window_size_h=dec_win_h, window_size_w=dec_win_w)
query_feats_win = window_partition(query_feats, window_size_h=dec_win_h, window_size_w=dec_win_w)
# dynamic point query generation
div = kwargs['div']
div_win = window_partition(div.unsqueeze(1), window_size_h=dec_win_h, window_size_w=dec_win_w)
valid_div = (div_win > 0.5).sum(dim=0)[:,0]
v_idx = valid_div > 0
query_embed_win = query_embed_win[:, v_idx]
query_feats_win = query_feats_win[:, v_idx]
points_queries_win = points_queries_win[:, v_idx].reshape(-1, 2)
return query_embed_win, points_queries_win, query_feats_win, v_idx
def get_point_query(self, samples, features, **kwargs):
"""
Generate point query
"""
src, _ = features[self.feat_name].decompose()
# generate points queries and position embedding
if 'train' in kwargs:
query_embed, points_queries, query_feats = self.points_queris_embed(samples, self.pq_stride, src, **kwargs)
query_embed = query_embed.flatten(2).permute(2,0,1) # NxCxHxW --> (HW)xNxC
v_idx = None
else:
query_embed, points_queries, query_feats, v_idx = self.points_queris_embed_inference(samples, self.pq_stride, src, **kwargs)
out = (query_embed, points_queries, query_feats, v_idx)
return out
def predict(self, samples, points_queries, hs, **kwargs):
"""
Crowd prediction
"""
outputs_class = self.class_embed(hs)
# normalize to 0~1
outputs_offsets = (self.coord_embed(hs).sigmoid() - 0.5) * 2.0
# normalize point-query coordinates
img_shape = samples.tensors.shape[-2:]
img_h, img_w = img_shape
points_queries = points_queries.float().cuda()
points_queries[:, 0] /= img_h
points_queries[:, 1] /= img_w
# rescale offset range during testing
if 'test' in kwargs:
outputs_offsets[...,0] /= (img_h / 256)
outputs_offsets[...,1] /= (img_w / 256)
outputs_points = outputs_offsets[-1] + points_queries
out = {'pred_logits': outputs_class[-1], 'pred_points': outputs_points, 'img_shape': img_shape, 'pred_offsets': outputs_offsets[-1]}
out['points_queries'] = points_queries
out['pq_stride'] = self.pq_stride
return out
def forward(self, samples, features, context_info, **kwargs):
encode_src, src_pos_embed, mask = context_info
# get points queries for transformer
pqs = self.get_point_query(samples, features, **kwargs)
# point querying
kwargs['pq_stride'] = self.pq_stride
hs = self.transformer(encode_src, src_pos_embed, mask, pqs, img_shape=samples.tensors.shape[-2:], **kwargs)
# prediction
points_queries = pqs[1]
outputs = self.predict(samples, points_queries, hs, **kwargs)
return outputs
class PET(nn.Module):
"""
Point quEry Transformer
"""
def __init__(self, backbone, num_classes, args=None):
super().__init__()
self.backbone = backbone
# positional embedding
self.pos_embed = build_position_encoding(args)
# feature projection
hidden_dim = args.hidden_dim
self.input_proj = nn.ModuleList([
nn.Conv2d(backbone.num_channels, hidden_dim, kernel_size=1),
nn.Conv2d(backbone.num_channels, hidden_dim, kernel_size=1),
]
)
# context encoder
self.encode_feats = '8x'
enc_win_list = [(32, 16), (32, 16), (16, 8), (16, 8)] # encoder window size
args.enc_layers = len(enc_win_list)
self.context_encoder = build_encoder(args, enc_win_list=enc_win_list)
# quadtree splitter
context_patch = (128, 64)
context_w, context_h = context_patch[0]//int(self.encode_feats[:-1]), context_patch[1]//int(self.encode_feats[:-1])
self.quadtree_splitter = nn.Sequential(
nn.AvgPool2d((context_h, context_w), stride=(context_h ,context_w)),
nn.Conv2d(hidden_dim, 1, 1),
nn.Sigmoid(),
)
# point-query quadtree
args.sparse_stride, args.dense_stride = 8, 4 # point-query stride
transformer = build_decoder(args)
self.quadtree_sparse = BasePETCount(backbone, num_classes, quadtree_layer='sparse', args=args, transformer=transformer)
self.quadtree_dense = BasePETCount(backbone, num_classes, quadtree_layer='dense', args=args, transformer=transformer)
def compute_loss(self, outputs, criterion, targets, epoch, samples):
"""
Compute loss, including:
- point query loss (Eq. (3) in the paper)
- quadtree splitter loss (Eq. (4) in the paper)
"""
output_sparse, output_dense = outputs['sparse'], outputs['dense']
weight_dict = criterion.weight_dict
warmup_ep = 5
# compute loss
if epoch >= warmup_ep:
loss_dict_sparse = criterion(output_sparse, targets, div=outputs['split_map_sparse'])
loss_dict_dense = criterion(output_dense, targets, div=outputs['split_map_dense'])
else:
loss_dict_sparse = criterion(output_sparse, targets)
loss_dict_dense = criterion(output_dense, targets)
# sparse point queries loss
loss_dict_sparse = {k+'_sp':v for k, v in loss_dict_sparse.items()}
weight_dict_sparse = {k+'_sp':v for k,v in weight_dict.items()}
loss_pq_sparse = sum(loss_dict_sparse[k] * weight_dict_sparse[k] for k in loss_dict_sparse.keys() if k in weight_dict_sparse)
# dense point queries loss
loss_dict_dense = {k+'_ds':v for k, v in loss_dict_dense.items()}
weight_dict_dense = {k+'_ds':v for k,v in weight_dict.items()}
loss_pq_dense = sum(loss_dict_dense[k] * weight_dict_dense[k] for k in loss_dict_dense.keys() if k in weight_dict_dense)
# point queries loss
losses = loss_pq_sparse + loss_pq_dense
# update loss dict and weight dict
loss_dict = dict()
loss_dict.update(loss_dict_sparse)
loss_dict.update(loss_dict_dense)
weight_dict = dict()
weight_dict.update(weight_dict_sparse)
weight_dict.update(weight_dict_dense)
# quadtree splitter loss
den = torch.tensor([target['density'] for target in targets]) # crowd density
bs = len(den)
ds_idx = den < 2 * self.quadtree_sparse.pq_stride # dense regions index
ds_div = outputs['split_map_raw'][ds_idx]
sp_div = 1 - outputs['split_map_raw']
# constrain sparse regions
loss_split_sp = 1 - sp_div.view(bs, -1).max(dim=1)[0].mean()
# constrain dense regions
if sum(ds_idx) > 0:
ds_num = ds_div.shape[0]
loss_split_ds = 1 - ds_div.view(ds_num, -1).max(dim=1)[0].mean()
else:
loss_split_ds = outputs['split_map_raw'].sum() * 0.0
# update quadtree splitter loss
loss_split = loss_split_sp + loss_split_ds
weight_split = 0.1 if epoch >= warmup_ep else 0.0
loss_dict['loss_split'] = loss_split
weight_dict['loss_split'] = weight_split
# final loss
losses += loss_split * weight_split
return {'loss_dict':loss_dict, 'weight_dict':weight_dict, 'losses':losses}
def forward(self, samples: NestedTensor, **kwargs):
"""
The forward expects a NestedTensor, which consists of:
- samples.tensor: batched images, of shape [batch_size x 3 x H x W]
- samples.mask: a binary mask of shape [batch_size x H x W], containing 1 on padded pixels
"""
# backbone
if isinstance(samples, (list, torch.Tensor)):
samples = nested_tensor_from_tensor_list(samples)
features, pos = self.backbone(samples)
# positional embedding
dense_input_embed = self.pos_embed(samples)
kwargs['dense_input_embed'] = dense_input_embed
# feature projection
features['4x'] = NestedTensor(self.input_proj[0](features['4x'].tensors), features['4x'].mask)
features['8x'] = NestedTensor(self.input_proj[1](features['8x'].tensors), features['8x'].mask)
# forward
if 'train' in kwargs:
out = self.train_forward(samples, features, pos, **kwargs)
else:
out = self.test_forward(samples, features, pos, **kwargs)
return out
def pet_forward(self, samples, features, pos, **kwargs):
# context encoding
src, mask = features[self.encode_feats].decompose()
src_pos_embed = pos[self.encode_feats]
assert mask is not None
encode_src = self.context_encoder(src, src_pos_embed, mask)
context_info = (encode_src, src_pos_embed, mask)
# apply quadtree splitter
bs, _, src_h, src_w = src.shape
sp_h, sp_w = src_h, src_w
ds_h, ds_w = int(src_h * 2), int(src_w * 2)
split_map = self.quadtree_splitter(encode_src)
split_map_dense = F.interpolate(split_map, (ds_h, ds_w)).reshape(bs, -1)
split_map_sparse = 1 - F.interpolate(split_map, (sp_h, sp_w)).reshape(bs, -1)
# quadtree layer0 forward (sparse)
if 'train' in kwargs or (split_map_sparse > 0.5).sum() > 0:
kwargs['div'] = split_map_sparse.reshape(bs, sp_h, sp_w)
kwargs['dec_win_size'] = [16, 8]
outputs_sparse = self.quadtree_sparse(samples, features, context_info, **kwargs)
else:
outputs_sparse = None
# quadtree layer1 forward (dense)
if 'train' in kwargs or (split_map_dense > 0.5).sum() > 0:
kwargs['div'] = split_map_dense.reshape(bs, ds_h, ds_w)
kwargs['dec_win_size'] = [8, 4]
outputs_dense = self.quadtree_dense(samples, features, context_info, **kwargs)
else:
outputs_dense = None
# format outputs
outputs = dict()
outputs['sparse'] = outputs_sparse
outputs['dense'] = outputs_dense
outputs['split_map_raw'] = split_map
outputs['split_map_sparse'] = split_map_sparse
outputs['split_map_dense'] = split_map_dense
return outputs
def train_forward(self, samples, features, pos, **kwargs):
outputs = self.pet_forward(samples, features, pos, **kwargs)
# compute loss
criterion, targets, epoch = kwargs['criterion'], kwargs['targets'], kwargs['epoch']
losses = self.compute_loss(outputs, criterion, targets, epoch, samples)
return losses
def test_forward(self, samples, features, pos, **kwargs):
outputs = self.pet_forward(samples, features, pos, **kwargs)
out_dense, out_sparse = outputs['dense'], outputs['sparse']
thrs = 0.5 # inference threshold
# process sparse point queries
if outputs['sparse'] is not None:
out_sparse_scores = torch.nn.functional.softmax(out_sparse['pred_logits'], -1)[..., 1]
valid_sparse = out_sparse_scores > thrs
index_sparse = valid_sparse.cpu()
else:
index_sparse = None
# process dense point queries
if outputs['dense'] is not None:
out_dense_scores = torch.nn.functional.softmax(out_dense['pred_logits'], -1)[..., 1]
valid_dense = out_dense_scores > thrs
index_dense = valid_dense.cpu()
else:
index_dense = None
# format output
div_out = dict()
output_names = out_sparse.keys() if out_sparse is not None else out_dense.keys()
for name in list(output_names):
if 'pred' in name:
if index_dense is None:
div_out[name] = out_sparse[name][index_sparse].unsqueeze(0)
elif index_sparse is None:
div_out[name] = out_dense[name][index_dense].unsqueeze(0)
else:
div_out[name] = torch.cat([out_sparse[name][index_sparse].unsqueeze(0), out_dense[name][index_dense].unsqueeze(0)], dim=1)
else:
div_out[name] = out_sparse[name] if out_sparse is not None else out_dense[name]
div_out['split_map_raw'] = outputs['split_map_raw']
return div_out
class SetCriterion(nn.Module):
""" Compute the loss for PET:
1) compute hungarian assignment between ground truth points and the outputs of the model
2) supervise each pair of matched ground-truth / prediction and split map
"""
def __init__(self, num_classes, matcher, weight_dict, eos_coef, losses):
"""
Parameters:
num_classes: one-class in crowd counting
matcher: module able to compute a matching between targets and point queries
weight_dict: dict containing as key the names of the losses and as values their relative weight.
eos_coef: relative classification weight applied to the no-object category
losses: list of all the losses to be applied. See get_loss for list of available losses.
"""
super().__init__()
self.num_classes = num_classes
self.matcher = matcher
self.weight_dict = weight_dict
self.eos_coef = eos_coef
self.losses = losses
empty_weight = torch.ones(self.num_classes + 1)
empty_weight[0] = self.eos_coef # coefficient for non-object background points
self.register_buffer('empty_weight', empty_weight)
self.div_thrs_dict = {8: 0.0, 4:0.5}
def loss_labels(self, outputs, targets, indices, num_points, log=True, **kwargs):
"""
Classification loss:
- targets dicts must contain the key "labels" containing a tensor of dim [nb_target_points]
"""
assert 'pred_logits' in outputs
src_logits = outputs['pred_logits']
idx = self._get_src_permutation_idx(indices)
target_classes_o = torch.cat([t["labels"][J] for t, (_, J) in zip(targets, indices)])
target_classes = torch.zeros(src_logits.shape[:2], dtype=torch.int64, device=src_logits.device)
target_classes[idx] = target_classes_o
# compute classification loss
if 'div' in kwargs:
# get sparse / dense image index
den = torch.tensor([target['density'] for target in targets])
den_sort = torch.sort(den)[1]
ds_idx = den_sort[:len(den_sort)//2]
sp_idx = den_sort[len(den_sort)//2:]
eps = 1e-5
# raw cross-entropy loss
weights = target_classes.clone().float()
weights[weights==0] = self.empty_weight[0]
weights[weights==1] = self.empty_weight[1]
raw_ce_loss = F.cross_entropy(src_logits.transpose(1, 2), target_classes, ignore_index=-1, reduction='none')
# binarize split map
split_map = kwargs['div']
div_thrs = self.div_thrs_dict[outputs['pq_stride']]
div_mask = split_map > div_thrs
# dual supervision for sparse/dense images
loss_ce_sp = (raw_ce_loss * weights * div_mask)[sp_idx].sum() / ((weights * div_mask)[sp_idx].sum() + eps)
loss_ce_ds = (raw_ce_loss * weights * div_mask)[ds_idx].sum() / ((weights * div_mask)[ds_idx].sum() + eps)
loss_ce = loss_ce_sp + loss_ce_ds
# loss on non-div regions
non_div_mask = split_map <= div_thrs
loss_ce_nondiv = (raw_ce_loss * weights * non_div_mask).sum() / ((weights * non_div_mask).sum() + eps)
loss_ce = loss_ce + loss_ce_nondiv
else:
loss_ce = F.cross_entropy(src_logits.transpose(1, 2), target_classes, self.empty_weight, ignore_index=-1)
losses = {'loss_ce': loss_ce}
return losses
def loss_points(self, outputs, targets, indices, num_points, **kwargs):
"""
SmoothL1 regression loss:
- targets dicts must contain the key "points" containing a tensor of dim [nb_target_points, 2]
"""
assert 'pred_points' in outputs
# get indices
idx = self._get_src_permutation_idx(indices)
src_points = outputs['pred_points'][idx]
target_points = torch.cat([t['points'][i] for t, (_, i) in zip(targets, indices)], dim=0)
# compute regression loss
losses = {}
img_shape = outputs['img_shape']
img_h, img_w = img_shape
target_points[:, 0] /= img_h
target_points[:, 1] /= img_w
loss_points_raw = F.smooth_l1_loss(src_points, target_points, reduction='none')
if 'div' in kwargs:
# get sparse / dense index
den = torch.tensor([target['density'] for target in targets])
den_sort = torch.sort(den)[1]
img_ds_idx = den_sort[:len(den_sort)//2]
img_sp_idx = den_sort[len(den_sort)//2:]
pt_ds_idx = torch.cat([torch.where(idx[0] == bs_id)[0] for bs_id in img_ds_idx])
pt_sp_idx = torch.cat([torch.where(idx[0] == bs_id)[0] for bs_id in img_sp_idx])
# dual supervision for sparse/dense images
eps = 1e-5
split_map = kwargs['div']
div_thrs = self.div_thrs_dict[outputs['pq_stride']]
div_mask = split_map > div_thrs
loss_points_div = loss_points_raw * div_mask[idx].unsqueeze(-1)
loss_points_div_sp = loss_points_div[pt_sp_idx].sum() / (len(pt_sp_idx) + eps)
loss_points_div_ds = loss_points_div[pt_ds_idx].sum() / (len(pt_ds_idx) + eps)
# loss on non-div regions
non_div_mask = split_map <= div_thrs
loss_points_nondiv = (loss_points_raw * non_div_mask[idx].unsqueeze(-1)).sum() / (non_div_mask[idx].sum() + eps)
# final point loss
losses['loss_points'] = loss_points_div_sp + loss_points_div_ds + loss_points_nondiv
else:
losses['loss_points'] = loss_points_raw.sum() / num_points
return losses
def _get_src_permutation_idx(self, indices):
# permute predictions following indices
batch_idx = torch.cat([torch.full_like(src, i) for i, (src, _) in enumerate(indices)])
src_idx = torch.cat([src for (src, _) in indices])
return batch_idx, src_idx
def _get_tgt_permutation_idx(self, indices):
# permute targets following indices
batch_idx = torch.cat([torch.full_like(tgt, i) for i, (_, tgt) in enumerate(indices)])
tgt_idx = torch.cat([tgt for (_, tgt) in indices])
return batch_idx, tgt_idx
def get_loss(self, loss, outputs, targets, indices, num_points, **kwargs):
loss_map = {
'labels': self.loss_labels,
'points': self.loss_points,
}
assert loss in loss_map, f'{loss} loss is not defined'
return loss_map[loss](outputs, targets, indices, num_points, **kwargs)
def forward(self, outputs, targets, **kwargs):
""" Loss computation
Parameters:
outputs: dict of tensors, see the output specification of the model for the format
targets: list of dicts, such that len(targets) == batch_size.
The expected keys in each dict depends on the losses applied, see each loss' doc
"""
# retrieve the matching between the outputs of the last layer and the targets
indices = self.matcher(outputs, targets)
# compute the average number of target points accross all nodes, for normalization purposes
num_points = sum(len(t["labels"]) for t in targets)
num_points = torch.as_tensor([num_points], dtype=torch.float, device=next(iter(outputs.values())).device)
if is_dist_avail_and_initialized():
torch.distributed.all_reduce(num_points)
num_points = torch.clamp(num_points / get_world_size(), min=1).item()
# compute all the requested losses
losses = {}
for loss in self.losses:
losses.update(self.get_loss(loss, outputs, targets, indices, num_points, **kwargs))
return losses
class MLP(nn.Module):
"""
Multi-layer perceptron (also called FFN)
"""
def __init__(self, input_dim, hidden_dim, output_dim, num_layers, is_reduce=False, use_relu=True):
super().__init__()
self.num_layers = num_layers
if is_reduce:
h = [hidden_dim//2**i for i in range(num_layers - 1)]
else:
h = [hidden_dim] * (num_layers - 1)
self.layers = nn.ModuleList(nn.Linear(n, k) for n, k in zip([input_dim] + h, h + [output_dim]))
self.use_relu = use_relu
def forward(self, x):
for i, layer in enumerate(self.layers):
if self.use_relu:
x = F.relu(layer(x)) if i < self.num_layers - 1 else layer(x)
else:
x = layer(x)
return x
def build_pet(args):
device = torch.device(args.device)
# build model
num_classes = 1
backbone = build_backbone_vgg(args)
model = PET(
backbone,
num_classes=num_classes,
args=args,
)
# build loss criterion
matcher = build_matcher(args)
weight_dict = {'loss_ce': args.ce_loss_coef, 'loss_points': args.point_loss_coef}
losses = ['labels', 'points']
criterion = SetCriterion(num_classes, matcher=matcher, weight_dict=weight_dict,
eos_coef=args.eos_coef, losses=losses)
criterion.to(device)
return model, criterion