-
Notifications
You must be signed in to change notification settings - Fork 27
/
panformer_head.py
1221 lines (1074 loc) · 53.9 KB
/
panformer_head.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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import copy
import torch
import torch.nn as nn
import torch.nn.functional as F
from mmcv.cnn import Linear, bias_init_with_prob, constant_init
from mmcv.runner import force_fp32
import mmcv
from mmdet.core import multi_apply
from mmdet.models.utils.transformer import inverse_sigmoid
from mmdet.models.builder import HEADS, build_loss
from mmdet.core import (bbox_cxcywh_to_xyxy, bbox_xyxy_to_cxcywh,
build_assigner, build_sampler, multi_apply,
reduce_mean)
from mmdet.models.utils import build_transformer
from easymd.models.panformer import DETRHeadv2
@HEADS.register_module()
class PanformerHead(DETRHeadv2):
"""
Head of Panoptic SegFormer
Code is modified from the `official github repo
<https://github.com/open-mmlab/mmdetection>`_.
Args:
with_box_refine (bool): Whether to refine the reference points
in the decoder. Defaults to False.
as_two_stage (bool) : Whether to generate the proposal from
the outputs of encoder.
transformer (obj:`ConfigDict`): ConfigDict is used for building
the Encoder and Decoder.
"""
def __init__(
self,
*args,
with_box_refine=False,
as_two_stage=False,
transformer=None,
quality_threshold_things=0.25,
quality_threshold_stuff=0.25,
overlap_threshold_things=0.4,
overlap_threshold_stuff=0.2,
use_argmax=False,
datasets='coco', # MDS
thing_transformer_head=dict(
type='TransformerHead', # mask decoder for things
d_model=256,
nhead=8,
num_decoder_layers=6),
stuff_transformer_head=dict(
type='TransformerHead', # mask decoder for stuff
d_model=256,
nhead=8,
num_decoder_layers=6),
loss_mask=dict(type='DiceLoss', weight=2.0),
train_cfg=dict(
assigner=dict(type='HungarianAssigner',
cls_cost=dict(type='ClassificationCost',
weight=1.),
reg_cost=dict(type='BBoxL1Cost', weight=5.0),
iou_cost=dict(type='IoUCost',
iou_mode='giou',
weight=2.0)),
sampler=dict(type='PseudoSampler'),
),
**kwargs):
self.with_box_refine = with_box_refine
self.as_two_stage = as_two_stage
self.quality_threshold_things = quality_threshold_things
self.quality_threshold_stuff = quality_threshold_stuff
self.overlap_threshold_things = overlap_threshold_things
self.overlap_threshold_stuff = overlap_threshold_stuff
self.use_argmax = use_argmax
self.datasets = datasets
self.fp16_enabled = False
# MDS: id_and_category_maps is the category_dict
if datasets == 'coco':
from easymd.datasets.coco_panoptic import id_and_category_maps
self.cat_dict = id_and_category_maps
if self.as_two_stage:
transformer['as_two_stage'] = self.as_two_stage
self.num_dec_things = thing_transformer_head['num_decoder_layers']
self.num_dec_stuff = stuff_transformer_head['num_decoder_layers']
super(PanformerHead, self).__init__(*args,
transformer=transformer,
train_cfg=train_cfg,
**kwargs)
if train_cfg:
sampler_cfg = train_cfg['sampler_with_mask']
self.sampler_with_mask = build_sampler(sampler_cfg, context=self)
assigner_cfg = train_cfg['assigner_with_mask']
self.assigner_with_mask = build_assigner(assigner_cfg)
self.assigner_filter = build_assigner(
dict(
type='HungarianAssigner_filter',
cls_cost=dict(type='FocalLossCost', weight=2.0),
reg_cost=dict(type='BBoxL1Cost',
weight=5.0,
box_format='xywh'),
iou_cost=dict(type='IoUCost', iou_mode='giou', weight=2.0),
max_pos=
3 # Depends on GPU memory, setting it to 1, model can be trained on 1080Ti
), )
self.loss_mask = build_loss(loss_mask)
self.things_mask_head = build_transformer(thing_transformer_head)
self.stuff_mask_head = build_transformer(stuff_transformer_head)
self.count = 0
def _init_layers(self):
"""Initialize classification branch and regression branch of head."""
fc_cls = Linear(self.embed_dims, self.cls_out_channels)
fc_cls_stuff = Linear(self.embed_dims, 1)
reg_branch = []
for _ in range(self.num_reg_fcs):
reg_branch.append(Linear(self.embed_dims, self.embed_dims))
reg_branch.append(nn.ReLU())
reg_branch.append(Linear(self.embed_dims, 4))
reg_branch = nn.Sequential(*reg_branch)
def _get_clones(module, N):
return nn.ModuleList([copy.deepcopy(module) for i in range(N)])
# last reg_branch is used to generate proposal from
# encode feature map when as_two_stage is True.
num_pred = (self.transformer.decoder.num_layers + 1) if \
self.as_two_stage else self.transformer.decoder.num_layers
if self.with_box_refine:
self.cls_branches = _get_clones(fc_cls, num_pred)
self.reg_branches = _get_clones(reg_branch, num_pred)
else:
self.cls_branches = nn.ModuleList(
[fc_cls for _ in range(num_pred)])
self.reg_branches = nn.ModuleList(
[reg_branch for _ in range(num_pred)])
if not self.as_two_stage:
self.query_embedding = nn.Embedding(self.num_query,
self.embed_dims * 2)
self.stuff_query = nn.Embedding(self.num_stuff_classes,
self.embed_dims * 2)
self.reg_branches2 = _get_clones(reg_branch, self.num_dec_things) # used in mask decoder
self.cls_thing_branches = _get_clones(fc_cls, self.num_dec_things) # used in mask decoder
self.cls_stuff_branches = _get_clones(fc_cls_stuff, self.num_dec_stuff) # used in mask deocder
def init_weights(self):
"""Initialize weights of the DeformDETR head."""
self.transformer.init_weights()
if self.loss_cls.use_sigmoid:
bias_init = bias_init_with_prob(0.01)
for m in self.cls_branches:
nn.init.constant_(m.bias, bias_init)
for m in self.cls_thing_branches:
nn.init.constant_(m.bias, bias_init)
for m in self.cls_stuff_branches:
nn.init.constant_(m.bias, bias_init)
for m in self.reg_branches:
constant_init(m[-1], 0, bias=0)
for m in self.reg_branches2:
constant_init(m[-1], 0, bias=0)
nn.init.constant_(self.reg_branches[0][-1].bias.data[2:], -2.0)
if self.as_two_stage:
for m in self.reg_branches:
nn.init.constant_(m[-1].bias.data[2:], 0.0)
@force_fp32(apply_to=('mlvl_feats', ))
def forward(self, mlvl_feats, img_metas=None):
"""Forward function.
Args:
mlvl_feats (tuple[Tensor]): Features from the upstream
network, each is a 4D-tensor with shape
(N, C, H, W).
img_metas (list[dict]): List of image information.
Returns:
all_cls_scores (Tensor): Outputs from the classification head, \
shape [nb_dec, bs, num_query, cls_out_channels]. Note \
cls_out_channels should includes background.
all_bbox_preds (Tensor): Sigmoid outputs from the regression \
head with normalized coordinate format (cx, cy, w, h). \
Shape [nb_dec, bs, num_query, 4].
enc_outputs_class (Tensor): The score of each point on encode \
feature map, has shape (N, h*w, num_class). Only when \
as_two_stage is True it would be returned, otherwise \
`None` would be returned.
enc_outputs_coord (Tensor): The proposal generate from the \
encode feature map, has shape (N, h*w, 4). Only when \
as_two_stage is True it would be returned, otherwise \
`None` would be returned.
"""
batch_size = mlvl_feats[0].size(0)
input_img_h, input_img_w = img_metas[0]['batch_input_shape']
img_masks = mlvl_feats[0].new_ones(
(batch_size, input_img_h, input_img_w))
for img_id in range(batch_size):
img_h, img_w, _ = img_metas[img_id]['img_shape']
img_masks[img_id, :img_h, :img_w] = 0
hw_lvl = [feat_lvl.shape[-2:] for feat_lvl in mlvl_feats]
mlvl_masks = []
mlvl_positional_encodings = []
for feat in mlvl_feats:
mlvl_masks.append(
F.interpolate(img_masks[None],
size=feat.shape[-2:]).to(torch.bool).squeeze(0))
mlvl_positional_encodings.append(
self.positional_encoding(mlvl_masks[-1]))
query_embeds = None
if not self.as_two_stage:
query_embeds = self.query_embedding.weight
(memory, memory_pos, memory_mask, query_pos), hs, init_reference, inter_references, \
enc_outputs_class, enc_outputs_coord = self.transformer(
mlvl_feats,
mlvl_masks,
query_embeds,
mlvl_positional_encodings,
reg_branches=self.reg_branches if self.with_box_refine else None, # noqa:E501
cls_branches=self.cls_branches if self.as_two_stage else None # noqa:E501
)
memory = memory.permute(1, 0, 2)
query = hs[-1].permute(1, 0, 2)
query_pos = query_pos.permute(1, 0, 2)
memory_pos = memory_pos.permute(1, 0, 2)
len_last_feat = hw_lvl[-1][0] * hw_lvl[-1][1]
# we should feed these to mask deocder.
args_tuple = (memory[:, :-len_last_feat, :],
memory_mask[:, :-len_last_feat],
memory_pos[:, :-len_last_feat, :], query, None,
query_pos, hw_lvl)
hs = hs.permute(0, 2, 1, 3)
outputs_classes = []
outputs_coords = []
for lvl in range(hs.shape[0]):
if lvl == 0:
reference = init_reference
else:
reference = inter_references[lvl - 1]
reference = inverse_sigmoid(reference)
outputs_class = self.cls_branches[lvl](hs[lvl])
tmp = self.reg_branches[lvl](hs[lvl])
if reference.shape[-1] == 4:
tmp += reference
else:
assert reference.shape[-1] == 2
tmp[..., :2] += reference
outputs_coord = tmp.sigmoid()
outputs_classes.append(outputs_class)
outputs_coords.append(outputs_coord)
outputs_classes = torch.stack(outputs_classes)
outputs_coords = torch.stack(outputs_coords)
if self.as_two_stage:
return outputs_classes, outputs_coords, \
enc_outputs_class, \
enc_outputs_coord.sigmoid(), args_tuple, reference
else:
return outputs_classes, outputs_coords, \
None, None, args_tuple, reference
@force_fp32(apply_to=('all_cls_scores_list', 'all_bbox_preds_list',
'args_tuple', 'reference'))
def loss(
self,
all_cls_scores,
all_bbox_preds,
enc_cls_scores,
enc_bbox_preds,
args_tuple,
reference,
gt_bboxes_list,
gt_labels_list,
gt_masks_list=None,
img_metas=None,
gt_bboxes_ignore=None,
):
""""Loss function.
Args:
all_cls_scores (Tensor): Classification score of all
decoder layers, has shape
[nb_dec, bs, num_query, cls_out_channels].
all_bbox_preds (Tensor): Sigmoid regression
outputs of all decode layers. Each is a 4D-tensor with
normalized coordinate format (cx, cy, w, h) and shape
[nb_dec, bs, num_query, 4].
enc_cls_scores (Tensor): Classification scores of
points on encode feature map , has shape
(N, h*w, num_classes). Only be passed when as_two_stage is
True, otherwise is None.
enc_bbox_preds (Tensor): Regression results of each points
on the encode feature map, has shape (N, h*w, 4). Only be
passed when as_two_stage is True, otherwise is None.
args_tuple (Tuple) several args
reference (Tensor) reference from location decoder
gt_bboxes_list (list[Tensor]): Ground truth bboxes for each image
with shape (num_gts, 4) in [tl_x, tl_y, br_x, br_y] format.
gt_labels_list (list[Tensor]): Ground truth class indices for each
image with shape (num_gts, ).
img_metas (list[dict]): List of image meta information.
gt_bboxes_ignore (list[Tensor], optional): Bounding boxes
which can be ignored for each image. Default None.
Returns:
dict[str, Tensor]: A dictionary of loss components.
"""
assert gt_bboxes_ignore is None, \
f'{self.__class__.__name__} only supports ' \
f'for gt_bboxes_ignore setting to None.'
### seprate things and stuff
gt_things_lables_list = []
gt_things_bboxes_list = []
gt_things_masks_list = []
gt_stuff_labels_list = []
gt_stuff_masks_list = []
for i, each in enumerate(gt_labels_list):
# MDS: for coco, id<80 (Continuous id) is things. This is not true for other data sets
things_selected = each < self.num_things_classes
stuff_selected = things_selected == False
gt_things_lables_list.append(gt_labels_list[i][things_selected])
gt_things_bboxes_list.append(gt_bboxes_list[i][things_selected])
gt_things_masks_list.append(gt_masks_list[i][things_selected])
gt_stuff_labels_list.append(gt_labels_list[i][stuff_selected])
gt_stuff_masks_list.append(gt_masks_list[i][stuff_selected])
num_dec_layers = len(all_cls_scores)
all_gt_bboxes_list = [
gt_things_bboxes_list for _ in range(num_dec_layers - 1)
]
all_gt_labels_list = [
gt_things_lables_list for _ in range(num_dec_layers - 1)
]
# all_gt_masks_list = [gt_masks_list for _ in range(num_dec_layers-1)]
all_gt_bboxes_ignore_list = [
gt_bboxes_ignore for _ in range(num_dec_layers - 1)
]
img_metas_list = [img_metas for _ in range(num_dec_layers - 1)]
# if the location decoder codntains L layers, we compute the losses of the first L-1 layers
losses_cls, losses_bbox, losses_iou = multi_apply(
self.loss_single, all_cls_scores[:-1], all_bbox_preds[:-1],
all_gt_bboxes_list, all_gt_labels_list, img_metas_list,
all_gt_bboxes_ignore_list)
losses_cls_f, losses_bbox_f, losses_iou_f, losses_masks_things_f, losses_masks_stuff_f, loss_mask_things_list_f, loss_mask_stuff_list_f, loss_iou_list_f, loss_bbox_list_f, loss_cls_list_f, loss_cls_stuff_list_f, things_ratio, stuff_ratio = self.loss_single_panoptic(
all_cls_scores[-1], all_bbox_preds[-1], args_tuple, reference,
gt_things_bboxes_list, gt_things_lables_list, gt_things_masks_list,
(gt_stuff_labels_list, gt_stuff_masks_list), img_metas,
gt_bboxes_ignore)
loss_dict = dict()
# loss of proposal generated from encode feature map.
if enc_cls_scores is not None:
binary_labels_list = [
torch.zeros_like(gt_things_lables_list[i])
for i in range(len(img_metas))
]
enc_loss_cls, enc_losses_bbox, enc_losses_iou = \
self.loss_single(enc_cls_scores, enc_bbox_preds,
gt_things_bboxes_list, binary_labels_list,
img_metas, gt_bboxes_ignore)
loss_dict['enc_loss_cls'] = enc_loss_cls * things_ratio
loss_dict['enc_loss_bbox'] = enc_losses_bbox * things_ratio
loss_dict['enc_loss_iou'] = enc_losses_iou * things_ratio
# loss_dict['enc_loss_mask'] = enc_losses_mask
# loss from the last decoder layer
loss_dict['loss_cls'] = losses_cls_f * things_ratio
loss_dict['loss_bbox'] = losses_bbox_f * things_ratio
loss_dict['loss_iou'] = losses_iou_f * things_ratio
loss_dict['loss_mask_things'] = losses_masks_things_f * things_ratio
loss_dict['loss_mask_stuff'] = losses_masks_stuff_f * stuff_ratio
# loss from other decoder layers
num_dec_layer = 0
for i in range(len(loss_mask_things_list_f)):
loss_dict[f'd{i}.loss_mask_things_f'] = loss_mask_things_list_f[
i] * things_ratio
loss_dict[f'd{i}.loss_iou_f'] = loss_iou_list_f[i] * things_ratio
loss_dict[f'd{i}.loss_bbox_f'] = loss_bbox_list_f[i] * things_ratio
loss_dict[f'd{i}.loss_cls_f'] = loss_cls_list_f[i] * things_ratio
for i in range(len(loss_mask_stuff_list_f)):
loss_dict[f'd{i}.loss_mask_stuff_f'] = loss_mask_stuff_list_f[
i] * stuff_ratio
loss_dict[f'd{i}.loss_cls_stuff_f'] = loss_cls_stuff_list_f[
i] * stuff_ratio
for loss_cls_i, loss_bbox_i, loss_iou_i in zip(
losses_cls,
losses_bbox,
losses_iou,
):
loss_dict[f'd{num_dec_layer}.loss_cls'] = loss_cls_i * things_ratio
loss_dict[
f'd{num_dec_layer}.loss_bbox'] = loss_bbox_i * things_ratio
loss_dict[f'd{num_dec_layer}.loss_iou'] = loss_iou_i * things_ratio
num_dec_layer += 1
# print(loss_dict)
return loss_dict
def filter_query(self,
cls_scores_list,
bbox_preds_list,
gt_bboxes_list,
gt_labels_list,
img_metas,
gt_bboxes_ignore_list=None):
'''
This function aims to using the cost from the location decoder to filter out low-quality queries.
'''
assert gt_bboxes_ignore_list is None, \
'Only supports for gt_bboxes_ignore setting to None.'
num_imgs = len(cls_scores_list)
gt_bboxes_ignore_list = [
gt_bboxes_ignore_list for _ in range(num_imgs)
]
(pos_inds_mask_list, neg_inds_mask_list, labels_list,
label_weights_list, bbox_targets_list,
bbox_weights_list, pos_inds_list, neg_inds_list) = multi_apply(
self._filter_query_single, cls_scores_list, bbox_preds_list,
gt_bboxes_list, gt_labels_list, img_metas, gt_bboxes_ignore_list)
num_total_pos = sum((inds.numel() for inds in pos_inds_list))
num_total_neg = sum((inds.numel() for inds in neg_inds_list))
return pos_inds_mask_list, neg_inds_mask_list, labels_list, label_weights_list, bbox_targets_list, \
bbox_weights_list, num_total_pos, num_total_neg, pos_inds_list, neg_inds_list
def _filter_query_single(self,
cls_score,
bbox_pred,
gt_bboxes,
gt_labels,
img_meta,
gt_bboxes_ignore=None):
num_bboxes = bbox_pred.size(0)
pos_ind_mask, neg_ind_mask, assign_result = self.assigner_filter.assign(
bbox_pred, cls_score, gt_bboxes, gt_labels, img_meta,
gt_bboxes_ignore)
sampling_result = self.sampler.sample(assign_result, bbox_pred,
gt_bboxes)
pos_inds = sampling_result.pos_inds
neg_inds = sampling_result.neg_inds
# label targets
labels = gt_bboxes.new_full((num_bboxes, ),
self.num_things_classes,
dtype=torch.long)
labels[pos_inds] = gt_labels[sampling_result.pos_assigned_gt_inds]
label_weights = gt_bboxes.new_ones(num_bboxes)
# bbox targets
bbox_targets = torch.zeros_like(bbox_pred)
bbox_weights = torch.zeros_like(bbox_pred)
bbox_weights[pos_inds] = 1.0
img_h, img_w, _ = img_meta['img_shape']
# DETR regress the relative position of boxes (cxcywh) in the image.
# Thus the learning target should be normalized by the image size, also
# the box format should be converted from defaultly x1y1x2y2 to cxcywh.
factor = bbox_pred.new_tensor([img_w, img_h, img_w,
img_h]).unsqueeze(0)
pos_gt_bboxes_normalized = sampling_result.pos_gt_bboxes / factor
pos_gt_bboxes_targets = bbox_xyxy_to_cxcywh(pos_gt_bboxes_normalized)
bbox_targets[pos_inds] = pos_gt_bboxes_targets
return (pos_ind_mask, neg_ind_mask, labels, label_weights,
bbox_targets, bbox_weights, pos_inds, neg_inds)
def get_targets_with_mask(self,
cls_scores_list,
bbox_preds_list,
masks_preds_list_thing,
gt_bboxes_list,
gt_labels_list,
gt_masks_list,
img_metas,
gt_bboxes_ignore_list=None):
""""Compute regression and classification targets for a batch image.
Outputs from a single decoder layer of a single feature level are used.
Args:
cls_scores_list (list[Tensor]): Box score logits from a single
decoder layer for each image with shape [num_query,
cls_out_channels].
bbox_preds_list (list[Tensor]): Sigmoid outputs from a single
decoder layer for each image, with normalized coordinate
(cx, cy, w, h) and shape [num_query, 4].
masks_preds_list_thing (list[Tensor]):
gt_bboxes_list (list[Tensor]): Ground truth bboxes for each image
with shape (num_gts, 4) in [tl_x, tl_y, br_x, br_y] format.
gt_labels_list (list[Tensor]): Ground truth class indices for each
image with shape (num_gts, ).
img_metas (list[dict]): List of image meta information.
gt_bboxes_ignore_list (list[Tensor], optional): Bounding
boxes which can be ignored for each image. Default None.
"""
assert gt_bboxes_ignore_list is None, \
'Only supports for gt_bboxes_ignore setting to None.'
num_imgs = len(cls_scores_list)
gt_bboxes_ignore_list = [
gt_bboxes_ignore_list for _ in range(num_imgs)
]
(labels_list, label_weights_list, bbox_targets_list, bbox_weights_list,
mask_targets_list, mask_weights_list, pos_inds_list,
neg_inds_list) = multi_apply(self._get_target_single_with_mask,
cls_scores_list, bbox_preds_list,
masks_preds_list_thing, gt_bboxes_list,
gt_labels_list, gt_masks_list, img_metas,
gt_bboxes_ignore_list)
num_total_pos_thing = sum((inds.numel() for inds in pos_inds_list))
num_total_neg_thing = sum((inds.numel() for inds in neg_inds_list))
return (labels_list, label_weights_list, bbox_targets_list,
bbox_weights_list, mask_targets_list, mask_weights_list,
num_total_pos_thing, num_total_neg_thing, pos_inds_list)
def _get_target_single_with_mask(self,
cls_score,
bbox_pred,
masks_preds_things,
gt_bboxes,
gt_labels,
gt_masks,
img_meta,
gt_bboxes_ignore=None):
"""
"""
num_bboxes = bbox_pred.size(0)
# assigner and sampler
gt_masks = gt_masks.float()
assign_result = self.assigner_with_mask.assign(bbox_pred, cls_score,
masks_preds_things,
gt_bboxes, gt_labels,
gt_masks, img_meta,
gt_bboxes_ignore)
sampling_result = self.sampler_with_mask.sample(
assign_result, bbox_pred, gt_bboxes, gt_masks)
pos_inds = sampling_result.pos_inds
neg_inds = sampling_result.neg_inds
# label targets
labels = gt_bboxes.new_full((num_bboxes, ),
self.num_things_classes,
dtype=torch.long)
labels[pos_inds] = gt_labels[sampling_result.pos_assigned_gt_inds]
label_weights = gt_bboxes.new_ones(num_bboxes)
# bbox targets
bbox_targets = torch.zeros_like(bbox_pred)
bbox_weights = torch.zeros_like(bbox_pred)
bbox_weights[pos_inds] = 1.0
img_h, img_w, _ = img_meta['img_shape']
# DETR regress the relative position of boxes (cxcywh) in the image.
# Thus the learning target should be normalized by the image size, also
# the box format should be converted from defaultly x1y1x2y2 to cxcywh.
factor = bbox_pred.new_tensor([img_w, img_h, img_w,
img_h]).unsqueeze(0)
pos_gt_bboxes_normalized = sampling_result.pos_gt_bboxes / factor
pos_gt_bboxes_targets = bbox_xyxy_to_cxcywh(pos_gt_bboxes_normalized)
bbox_targets[pos_inds] = pos_gt_bboxes_targets
mask_weights = masks_preds_things.new_zeros(num_bboxes)
mask_weights[pos_inds] = 1.0
pos_gt_masks = sampling_result.pos_gt_masks
_, w, h = pos_gt_masks.shape
mask_target = masks_preds_things.new_zeros([num_bboxes, w, h])
mask_target[pos_inds] = pos_gt_masks
return (labels, label_weights, bbox_targets, bbox_weights, mask_target,
mask_weights, pos_inds, neg_inds)
def get_filter_results_and_loss(self, cls_scores, bbox_preds,
cls_scores_list, bbox_preds_list,
gt_bboxes_list, gt_labels_list, img_metas,
gt_bboxes_ignore_list):
pos_inds_mask_list, neg_inds_mask_list, labels_list, label_weights_list, bbox_targets_list, \
bbox_weights_list, num_total_pos_thing, num_total_neg_thing, pos_inds_list, neg_inds_list = self.filter_query(
cls_scores_list, bbox_preds_list,
gt_bboxes_list, gt_labels_list,
img_metas, gt_bboxes_ignore_list)
labels = torch.cat(labels_list, 0)
label_weights = torch.cat(label_weights_list, 0)
bbox_targets = torch.cat(bbox_targets_list, 0)
bbox_weights = torch.cat(bbox_weights_list, 0)
# classification loss
cls_scores = cls_scores.reshape(-1, self.cls_out_channels)
# construct weighted avg_factor to match with the official DETR repo
cls_avg_factor = num_total_pos_thing * 1.0 + \
num_total_neg_thing * self.bg_cls_weight
if self.sync_cls_avg_factor:
cls_avg_factor = reduce_mean(
cls_scores.new_tensor([cls_avg_factor]))
cls_avg_factor = max(cls_avg_factor, 1)
loss_cls = self.loss_cls(cls_scores,
labels,
label_weights,
avg_factor=cls_avg_factor)
# Compute the average number of gt boxes accross all gpus, for
# normalization purposes
num_total_pos_thing = loss_cls.new_tensor([num_total_pos_thing])
num_total_pos_thing = torch.clamp(reduce_mean(num_total_pos_thing),
min=1).item()
# construct factors used for rescale bboxes
factors = []
for img_meta, bbox_pred in zip(img_metas, bbox_preds):
img_h, img_w, _ = img_meta['img_shape']
factor = bbox_pred.new_tensor([img_w, img_h, img_w,
img_h]).unsqueeze(0).repeat(
bbox_pred.size(0), 1)
factors.append(factor)
factors = torch.cat(factors, 0)
# DETR regress the relative position of boxes (cxcywh) in the image,
# thus the learning target is normalized by the image size. So here
# we need to re-scale them for calculating IoU loss
bbox_preds = bbox_preds.reshape(-1, 4)
bboxes = bbox_cxcywh_to_xyxy(bbox_preds) * factors
bboxes_gt = bbox_cxcywh_to_xyxy(bbox_targets) * factors
# regression IoU loss, defaultly GIoU loss
loss_iou = self.loss_iou(bboxes,
bboxes_gt,
bbox_weights,
avg_factor=num_total_pos_thing)
# regression L1 loss
loss_bbox = self.loss_bbox(bbox_preds,
bbox_targets,
bbox_weights,
avg_factor=num_total_pos_thing)
return loss_cls, loss_iou, loss_bbox,\
pos_inds_mask_list, num_total_pos_thing
def loss_single_panoptic(self,
cls_scores,
bbox_preds,
args_tuple,
reference,
gt_bboxes_list,
gt_labels_list,
gt_masks_list,
gt_panoptic_list,
img_metas,
gt_bboxes_ignore_list=None):
""""Loss function for outputs from a single decoder layer of a single
feature level.
Args:
cls_scores (Tensor): Box score logits from a single decoder layer
for all images. Shape [bs, num_query, cls_out_channels].
bbox_preds (Tensor): Sigmoid outputs from a single decoder layer
for all images, with normalized coordinate (cx, cy, w, h) and
shape [bs, num_query, 4].
args_tuple:
reference:
gt_bboxes_list (list[Tensor]): Ground truth bboxes for each image
with shape (num_gts, 4) in [tl_x, tl_y, br_x, br_y] format.
gt_labels_list (list[Tensor]): Ground truth class indices for each
image with shape (num_gts, ).
img_metas (list[dict]): List of image meta information.
gt_bboxes_ignore_list (list[Tensor], optional): Bounding
boxes which can be ignored for each image. Default None.
Returns:
dict[str, Tensor]: A dictionary of loss components for outputs from
a single decoder layer.
"""
num_imgs = cls_scores.size(0)
gt_stuff_labels_list, gt_stuff_masks_list = gt_panoptic_list
cls_scores_list = [cls_scores[i] for i in range(num_imgs)]
bbox_preds_list = [bbox_preds[i] for i in range(num_imgs)]
loss_cls, loss_iou, loss_bbox, pos_inds_mask_list, num_total_pos_thing = self.get_filter_results_and_loss(
cls_scores, bbox_preds, cls_scores_list, bbox_preds_list, gt_bboxes_list, gt_labels_list, img_metas, gt_bboxes_ignore_list)
memory, memory_mask, memory_pos, query, _, query_pos, hw_lvl = args_tuple
BS, _, dim_query = query.shape[0], query.shape[1], query.shape[-1]
len_query = max([len(pos_ind) for pos_ind in pos_inds_mask_list])
thing_query = torch.zeros([BS, len_query, dim_query],
device=query.device)
stuff_query, stuff_query_pos = torch.split(self.stuff_query.weight,
self.embed_dims,
dim=1)
stuff_query_pos = stuff_query_pos.unsqueeze(0).expand(BS, -1, -1)
stuff_query = stuff_query.unsqueeze(0).expand(BS, -1, -1)
for i in range(BS):
thing_query[i, :len(pos_inds_mask_list[i])] = query[
i, pos_inds_mask_list[i]]
mask_preds_things = []
mask_preds_stuff = []
# mask_preds_inter = [[],[],[]]
mask_preds_inter_things = [[] for _ in range(self.num_dec_things)]
mask_preds_inter_stuff = [[] for _ in range(self.num_dec_stuff)]
cls_thing_preds = [[] for _ in range(self.num_dec_things)]
cls_stuff_preds = [[] for _ in range(self.num_dec_stuff)]
BS, NQ, L = bbox_preds.shape
new_bbox_preds = [
torch.zeros([BS, len_query, L]).to(bbox_preds.device)
for _ in range(self.num_dec_things)
]
mask_things, mask_inter_things, query_inter_things = self.things_mask_head(
memory, memory_mask, None, thing_query, None, None, hw_lvl=hw_lvl)
mask_stuff, mask_inter_stuff, query_inter_stuff = self.stuff_mask_head(
memory,
memory_mask,
None,
stuff_query,
None,
stuff_query_pos,
hw_lvl=hw_lvl)
mask_things = mask_things.squeeze(-1)
mask_inter_things = torch.stack(mask_inter_things, 0).squeeze(-1)
mask_stuff = mask_stuff.squeeze(-1)
mask_inter_stuff = torch.stack(mask_inter_stuff, 0).squeeze(-1)
for i in range(BS):
tmp_i = mask_things[i][:len(pos_inds_mask_list[i])].reshape(
-1, *hw_lvl[0])
mask_preds_things.append(tmp_i)
pos_ind = pos_inds_mask_list[i]
reference_i = reference[i:i + 1, pos_ind, :]
for j in range(self.num_dec_things):
tmp_i_j = mask_inter_things[j][i][:len(pos_inds_mask_list[i]
)].reshape(
-1, *hw_lvl[0])
mask_preds_inter_things[j].append(tmp_i_j)
# mask_preds_inter_things[j].append(mask_inter_things[j].reshape(-1, *hw_lvl[0]))
query_things = query_inter_things[j]
t1, t2, t3 = query_things.shape
tmp = self.reg_branches2[j](query_things.reshape(t1 * t2, t3)).reshape(t1, t2, 4)
if len(pos_ind) == 0:
tmp = tmp.sum(
) + reference_i # for reply bug of pytorch broadcast
elif reference_i.shape[-1] == 4:
tmp += reference_i
else:
assert reference_i.shape[-1] == 2
tmp[..., :2] += reference_i
outputs_coord = tmp.sigmoid()
new_bbox_preds[j][i][:len(pos_inds_mask_list[i])] = outputs_coord
cls_thing_preds[j].append(self.cls_thing_branches[j](
query_things.reshape(t1 * t2, t3)))
# stuff
tmp_i = mask_stuff[i].reshape(-1, *hw_lvl[0])
mask_preds_stuff.append(tmp_i)
for j in range(self.num_dec_stuff):
tmp_i_j = mask_inter_stuff[j][i].reshape(-1, *hw_lvl[0])
mask_preds_inter_stuff[j].append(tmp_i_j)
query_stuff = query_inter_stuff[j]
s1, s2, s3 = query_stuff.shape
cls_stuff_preds[j].append(self.cls_stuff_branches[j](
query_stuff.reshape(s1 * s2, s3)))
masks_preds_list_thing = [
mask_preds_things[i] for i in range(num_imgs)
]
mask_preds_things = torch.cat(mask_preds_things, 0)
mask_preds_inter_things = [
torch.cat(each, 0) for each in mask_preds_inter_things
]
cls_thing_preds = [torch.cat(each, 0) for each in cls_thing_preds]
cls_stuff_preds = [torch.cat(each, 0) for each in cls_stuff_preds]
mask_preds_stuff = torch.cat(mask_preds_stuff, 0)
mask_preds_inter_stuff = [
torch.cat(each, 0) for each in mask_preds_inter_stuff
]
cls_scores_list = [
cls_scores_list[i][pos_inds_mask_list[i]] for i in range(num_imgs)
]
bbox_preds_list = [
bbox_preds_list[i][pos_inds_mask_list[i]] for i in range(num_imgs)
]
gt_targets = self.get_targets_with_mask(cls_scores_list,
bbox_preds_list,
masks_preds_list_thing,
gt_bboxes_list, gt_labels_list,
gt_masks_list, img_metas,
gt_bboxes_ignore_list)
(labels_list, label_weights_list, bbox_targets_list, bbox_weights_list,
mask_targets_list, mask_weights_list, _, _,
pos_inds_list) = gt_targets
thing_labels = torch.cat(labels_list, 0)
things_weights = torch.cat(label_weights_list, 0)
bboxes_taget = torch.cat(bbox_targets_list)
bboxes_weights = torch.cat(bbox_weights_list)
factors = []
for img_meta, bbox_pred in zip(img_metas, bbox_preds_list):
img_h, img_w, _ = img_meta['img_shape']
factor = bbox_pred.new_tensor([img_w, img_h, img_w,
img_h]).unsqueeze(0).repeat(
bbox_pred.size(0), 1)
factors.append(factor)
factors = torch.cat(factors, 0)
bboxes_gt = bbox_cxcywh_to_xyxy(bboxes_taget) * factors
mask_things_gt = torch.cat(mask_targets_list, 0).to(torch.float)
mask_weight_things = torch.cat(mask_weights_list,
0).to(thing_labels.device)
mask_stuff_gt = []
mask_weight_stuff = []
stuff_labels = []
num_total_pos_stuff = 0
for i in range(BS):
num_total_pos_stuff += len(gt_stuff_labels_list[i]) ## all stuff
select_stuff_index = gt_stuff_labels_list[
i] - self.num_things_classes
mask_weight_i_stuff = torch.zeros([self.num_stuff_classes])
mask_weight_i_stuff[select_stuff_index] = 1
stuff_masks = torch.zeros(
(self.num_stuff_classes, *mask_targets_list[i].shape[-2:]),
device=mask_targets_list[i].device).to(torch.bool)
stuff_masks[select_stuff_index] = gt_stuff_masks_list[i].to(
torch.bool)
mask_stuff_gt.append(stuff_masks)
select_stuff_index = torch.cat([
select_stuff_index,
torch.tensor([self.num_stuff_classes],
device=select_stuff_index.device)
])
stuff_labels.append(1 - mask_weight_i_stuff)
mask_weight_stuff.append(mask_weight_i_stuff)
mask_weight_stuff = torch.cat(mask_weight_stuff,
0).to(thing_labels.device)
stuff_labels = torch.cat(stuff_labels, 0).to(thing_labels.device)
mask_stuff_gt = torch.cat(mask_stuff_gt, 0).to(torch.float)
num_total_pos_stuff = loss_cls.new_tensor([num_total_pos_stuff])
num_total_pos_stuff = torch.clamp(reduce_mean(num_total_pos_stuff),
min=1).item()
if mask_preds_things.shape[0] == 0:
loss_mask_things = (0 * mask_preds_things).sum()
else:
mask_preds = F.interpolate(mask_preds_things.unsqueeze(0),
scale_factor=2.0,
mode='bilinear').squeeze(0)
mask_targets_things = F.interpolate(mask_things_gt.unsqueeze(0),
size=mask_preds.shape[-2:],
mode='bilinear').squeeze(0)
loss_mask_things = self.loss_mask(mask_preds,
mask_targets_things,
mask_weight_things,
avg_factor=num_total_pos_thing)
if mask_preds_stuff.shape[0] == 0:
loss_mask_stuff = (0 * mask_preds_stuff).sum()
else:
mask_preds = F.interpolate(mask_preds_stuff.unsqueeze(0),
scale_factor=2.0,
mode='bilinear').squeeze(0)
mask_targets_stuff = F.interpolate(mask_stuff_gt.unsqueeze(0),
size=mask_preds.shape[-2:],
mode='bilinear').squeeze(0)
loss_mask_stuff = self.loss_mask(mask_preds,
mask_targets_stuff,
mask_weight_stuff,
avg_factor=num_total_pos_stuff)
loss_mask_things_list = []
loss_mask_stuff_list = []
loss_iou_list = []
loss_bbox_list = []
for j in range(len(mask_preds_inter_things)):
mask_preds_this_level = mask_preds_inter_things[j]
if mask_preds_this_level.shape[0] == 0:
loss_mask_j = (0 * mask_preds_this_level).sum()
else:
mask_preds_this_level = F.interpolate(
mask_preds_this_level.unsqueeze(0),
scale_factor=2.0,
mode='bilinear').squeeze(0)
loss_mask_j = self.loss_mask(mask_preds_this_level,
mask_targets_things,
mask_weight_things,
avg_factor=num_total_pos_thing)
loss_mask_things_list.append(loss_mask_j)
bbox_preds_this_level = new_bbox_preds[j].reshape(-1, 4)
bboxes_this_level = bbox_cxcywh_to_xyxy(
bbox_preds_this_level) * factors
# We let this loss be 0. We didn't predict bbox in our mask decoder. Predicting bbox in the mask decoder is basically useless
loss_iou_j = self.loss_iou(bboxes_this_level,
bboxes_gt,
bboxes_weights,
avg_factor=num_total_pos_thing) * 0
if bboxes_taget.shape[0] != 0:
loss_bbox_j = self.loss_bbox(
bbox_preds_this_level,
bboxes_taget,
bboxes_weights,
avg_factor=num_total_pos_thing) * 0
else:
loss_bbox_j = bbox_preds_this_level.sum() * 0
loss_iou_list.append(loss_iou_j)
loss_bbox_list.append(loss_bbox_j)
for j in range(len(mask_preds_inter_stuff)):
mask_preds_this_level = mask_preds_inter_stuff[j]
if mask_preds_this_level.shape[0] == 0:
loss_mask_j = (0 * mask_preds_this_level).sum()
else:
mask_preds_this_level = F.interpolate(
mask_preds_this_level.unsqueeze(0),
scale_factor=2.0,
mode='bilinear').squeeze(0)
loss_mask_j = self.loss_mask(mask_preds_this_level,
mask_targets_stuff,
mask_weight_stuff,
avg_factor=num_total_pos_stuff)
loss_mask_stuff_list.append(loss_mask_j)
loss_cls_thing_list = []
loss_cls_stuff_list = []
thing_labels = thing_labels.reshape(-1)
for j in range(len(mask_preds_inter_things)):
# We let this loss be 0. When using "query-filter", only partial thing queries are feed to the mask decoder. This will cause imbalance when supervising these queries.
cls_scores = cls_thing_preds[j]
if cls_scores.shape[0] == 0:
loss_cls_thing_j = cls_scores.sum() * 0
else:
loss_cls_thing_j = self.loss_cls(
cls_scores,
thing_labels,
things_weights,
avg_factor=num_total_pos_thing) * 2 * 0
loss_cls_thing_list.append(loss_cls_thing_j)
for j in range(len(mask_preds_inter_stuff)):
if cls_scores.shape[0] == 0:
loss_cls_stuff_j = cls_stuff_preds[j].sum() * 0
else:
loss_cls_stuff_j = self.loss_cls(
cls_stuff_preds[j],
stuff_labels.to(torch.long),
avg_factor=num_total_pos_stuff) * 2
loss_cls_stuff_list.append(loss_cls_stuff_j)
## dynamic adjusting the weights
things_ratio, stuff_ratio = num_total_pos_thing / (
num_total_pos_stuff + num_total_pos_thing), num_total_pos_stuff / (
num_total_pos_stuff + num_total_pos_thing)