-
Notifications
You must be signed in to change notification settings - Fork 16
/
criterion.py
1281 lines (1109 loc) · 68.4 KB
/
criterion.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
# Copyright (c) Facebook, Inc. and its affiliates.
import torch
import torch.nn as nn
import numpy as np
import torch.nn.functional as F
from utils.box_util import generalized_box3d_iou, box3d_iou, is_clockwise
from utils.dist import all_reduce_average
from utils.misc import huber_loss
from scipy.optimize import linear_sum_assignment
from torchvision.ops import sigmoid_focal_loss
class Matcher(nn.Module):
def __init__(self, cost_class, cost_objectness, cost_giou, cost_center):
"""
Parameters:
cost_class:
Returns:
"""
super().__init__()
self.cost_class = cost_class
self.cost_objectness = cost_objectness
self.cost_giou = cost_giou
self.cost_center = cost_center
@torch.no_grad()
def forward(self, outputs, targets):
batchsize = outputs["sem_cls_prob"].shape[0]
nqueries = outputs["sem_cls_prob"].shape[1]
ngt = targets["gt_box_sem_cls_label"].shape[1]
nactual_gt = targets["nactual_gt"]
# classification cost: batch x nqueries x ngt matrix
pred_cls_prob = outputs["sem_cls_prob"]
gt_box_sem_cls_labels = (
targets["gt_box_sem_cls_label"]
.unsqueeze(1)
.expand(batchsize, nqueries, ngt)
)
class_mat = -torch.gather(pred_cls_prob, 2, gt_box_sem_cls_labels)
# objectness cost: batch x nqueries x 1
objectness_mat = -outputs["objectness_prob"].unsqueeze(-1)
# center cost: batch x nqueries x ngt
center_mat = outputs["center_dist"].detach()
# giou cost: batch x nqueries x ngt
giou_mat = -outputs["gious"].detach()
final_cost = (
self.cost_class * class_mat
+ self.cost_objectness * objectness_mat
+ self.cost_center * center_mat
+ self.cost_giou * giou_mat
)
final_cost = final_cost.detach().cpu().numpy()
assignments = []
# auxiliary variables useful for batched loss computation
batch_size, nprop = final_cost.shape[0], final_cost.shape[1]
per_prop_gt_inds = torch.zeros(
[batch_size, nprop], dtype=torch.int64, device=pred_cls_prob.device
)
proposal_matched_mask = torch.zeros(
[batch_size, nprop], dtype=torch.float32, device=pred_cls_prob.device
)
for b in range(batchsize):
assign = []
if nactual_gt[b] > 0:
assign = linear_sum_assignment(final_cost[b, :, : nactual_gt[b]])
assign = [
torch.from_numpy(x).long().to(device=pred_cls_prob.device)
for x in assign
]
per_prop_gt_inds[b, assign[0]] = assign[1]
proposal_matched_mask[b, assign[0]] = 1
assignments.append(assign)
return {
"assignments": assignments,
"per_prop_gt_inds": per_prop_gt_inds,
"proposal_matched_mask": proposal_matched_mask,
}
class SetCriterion(nn.Module):
def __init__(self, matcher, dataset_config, loss_weight_dict, train_range_max=37, only_image_class=False, only_prompt_loss=False, args=None):
super().__init__()
self.dataset_config = dataset_config
self.matcher = matcher
self.loss_weight_dict = loss_weight_dict
self.only_image_class = only_image_class
self.only_prompt_loss = only_prompt_loss
self.zeros = torch.zeros(1).to('cuda')
semcls_percls_weights = torch.ones(dataset_config.num_semcls + 1)
semcls_percls_weights[-1] = loss_weight_dict["loss_no_object_weight"]
self.register_buffer("semcls_percls_weights", semcls_percls_weights)
print('==============how many classes for contrastive losses(including bg) in loss(during training)===========')
assert train_range_max==37 or train_range_max==232 or train_range_max==10
print(train_range_max + 1)
seen_semcls_percls_weights = torch.ones(train_range_max + 1)
seen_semcls_percls_weights[-1] = loss_weight_dict["loss_no_object_contrast_weight"]
self.register_buffer("seen_semcls_percls_weights", seen_semcls_percls_weights)
self.if_skip_no_seen_scene_objectness = args.if_skip_no_seen_scene_objectness
print('==================if_skip_no_seen_scene_objectness==================')
print(self.if_skip_no_seen_scene_objectness)
del loss_weight_dict["loss_no_object_weight"]
del loss_weight_dict["loss_no_object_contrast_weight"]
if loss_weight_dict["loss_sem_cls_softmax_skip_none_gt_sample_weight"] > 0 or loss_weight_dict["loss_sem_cls_softmax_skip_none_gt_sample_en_discovery_objectness_weight"] > 0 or loss_weight_dict['loss_sem_cls_softmax_skip_none_gt_sample_keep_discovery_objectness_weight'] > 0:
self.if_skip_no_seen_scene_objectness_sample = True
else:
self.if_skip_no_seen_scene_objectness_sample = False
print('==================if_skip_no_seen_scene_objectness_sample==================')
print(self.if_skip_no_seen_scene_objectness_sample)
# self.logit_scale = nn.Parameter(torch.ones([]) * 100.0, requires_grad=False).to('cuda')
# self.logit_scale = nn.Parameter(torch.ones([]) * np.log(1 / 0.07)).to('cuda')
# self.logit_scale = self.logit_scale.exp()
# print('losssssssssssssssssssssssssssssssssssssssss')
# print(self.logit_scale)
# print('==========================')
self.confidence_type = args.confidence_type
assert self.confidence_type in ["non-confidence", "objectness", "clip+objectness", "clip-max-prob"]
print('====================confidence_type=======================')
print(self.confidence_type)
self.if_only_seen_in_loss = args.if_only_seen_in_loss
print('====================if_only_seen_in_loss==================')
print(self.if_only_seen_in_loss)
self.confidence_type_in_datalayer = args.confidence_type_in_datalayer
if self.if_only_seen_in_loss:
assert self.confidence_type_in_datalayer == 'zero-out'
# self.l1_loss = torch.nn.L1Loss()
self.loss_functions = {
"loss_sem_cls": self.loss_sem_cls,
"loss_sem_cls_softmax": self.loss_sem_cls_softmax,
"loss_sem_cls_softmax_skip_none_gt_sample": self.loss_sem_cls_softmax_skip_none_gt_sample,
"loss_sem_cls_softmax_2d_box_iou_supervised_skip_none_gt_sample": self.loss_sem_cls_softmax_2d_box_iou_supervised_skip_none_gt_sample,
"loss_sem_cls_softmax_skip_none_gt_sample_en_discovery_objectness": self.loss_sem_cls_softmax_skip_none_gt_sample_en_discovery_objectness,
"loss_sem_cls_softmax_skip_none_gt_sample_keep_discovery_objectness": self.loss_sem_cls_softmax_skip_none_gt_sample_keep_discovery_objectness,
"loss_sem_cls_softmax_discovery_novel_objectness": self.loss_sem_cls_softmax_discovery_novel_objectness,
"loss_angle": self.loss_angle,
"loss_center": self.loss_center,
"loss_size": self.loss_size,
"loss_giou": self.loss_giou,
# this isn't used during training and is logged for debugging.
# thus, this loss does not have a loss_weight associated with it.
"loss_cardinality": self.loss_cardinality,
"loss_contrastive": self.loss_contrastive,
"loss_sem_focal_cls": self.loss_sem_focal_cls,
"loss_contrast_object_text": self.loss_contrast_object_text,
"loss_region_embed": self.loss_region_embed,
"loss_predicted_region_embed_l1": self.loss_predicted_region_embed_l1,
"loss_predicted_region_embed_l1_only_last_layer": self.loss_predicted_region_embed_l1_only_last_layer,
"loss_predicted_region_embed_cos": self.loss_predicted_region_embed_cos,
"loss_image_seen_class": self.loss_image_seen_class,
"loss_batchwise_contrastive": self.loss_batchwise_contrastive,
"loss_feat_seen_sigmoid_loss": self.loss_feat_seen_sigmoid_loss,
"loss_feat_seen_softmax_loss": self.loss_feat_seen_softmax_loss,
"loss_feat_seen_softmax_weakly_loss": self.loss_feat_seen_softmax_weakly_loss,
"loss_feat_seen_softmax_weakly_loss_with_novel_cate_confi": self.loss_feat_seen_softmax_weakly_loss_with_novel_cate_confi,
"loss_feat_seen_softmax_iou_match_weakly_loss_with_novel_cate_confi": self.loss_feat_seen_softmax_iou_match_weakly_loss_with_novel_cate_confi,
"loss_feat_seen_softmax_loss_with_novel_cate_confi": self.loss_feat_seen_softmax_loss_with_novel_cate_confi,
"loss_feat_seen_sigmoid_with_full_image_loss": self.loss_feat_seen_sigmoid_with_full_image_loss,
"loss_prompt_softmax": self.loss_prompt_softmax,
"loss_prompt_sigmoid": self.loss_prompt_sigmoid
}
@torch.no_grad()
def loss_cardinality(self, outputs, targets, assignments):
# Count the number of predictions that are objects
# Cardinality is the error between predicted #objects and ground truth objects
pred_logits = outputs["sem_cls_logits"]
# Count the number of predictions that are NOT "no-object" (which is the last class)
pred_objects = (pred_logits.argmax(-1) != pred_logits.shape[-1] - 1).sum(1)
card_err = F.l1_loss(pred_objects.float(), targets["nactual_gt"])
return {"loss_cardinality": card_err}
def loss_sem_cls_softmax(self, outputs, targets, assignments):
# # Not vectorized version
# pred_logits = outputs["sem_cls_logits"]
# assign = assignments["assignments"]
# sem_cls_targets = torch.ones((pred_logits.shape[0], pred_logits.shape[1]),
# dtype=torch.int64, device=pred_logits.device)
# # initialize to background/no-object class
# sem_cls_targets *= (pred_logits.shape[-1] - 1)
# # use assignments to compute labels for matched boxes
# for b in range(pred_logits.shape[0]):
# if len(assign[b]) > 0:
# sem_cls_targets[b, assign[b][0]] = targets["gt_box_sem_cls_label"][b, assign[b][1]]
# sem_cls_targets = sem_cls_targets.view(-1)
# pred_logits = pred_logits.reshape(sem_cls_targets.shape[0], -1)
# loss = F.cross_entropy(pred_logits, sem_cls_targets, self.semcls_percls_weights, reduction="mean")
pred_logits = outputs["sem_cls_logits"]
gt_box_label = torch.gather(
targets["gt_box_sem_cls_label"], 1, assignments["per_prop_gt_inds"]
)
gt_box_label[assignments["proposal_matched_mask"].int() == 0] = (
pred_logits.shape[-1] - 1
)
loss = F.cross_entropy(
pred_logits.transpose(2, 1),
gt_box_label,
self.semcls_percls_weights,
reduction="mean",
)
if self.if_skip_no_seen_scene_objectness:
if targets["num_boxes_replica"] == 0:
loss = torch.sum(pred_logits) * 0
return {"loss_sem_cls_softmax": loss}
def loss_sem_cls_softmax_skip_none_gt_sample(self, outputs, targets, assignments):
pred_logits = outputs["sem_cls_logits"]
gt_box_label = torch.gather(
targets["gt_box_sem_cls_label"], 1, assignments["per_prop_gt_inds"]
)
gt_box_label[assignments["proposal_matched_mask"].int() == 0] = (
pred_logits.shape[-1] - 1
)
loss = F.cross_entropy(
pred_logits.transpose(2, 1),
gt_box_label,
self.semcls_percls_weights,
reduction="none",
)
final_loss = 0
cnt_has_object = 0
for batch_id in range(loss.shape[0]):
num_obj_thisbatch = torch.sum(targets['gt_box_present'][batch_id])
if num_obj_thisbatch == 0:
final_loss += (torch.sum(loss[batch_id]) * 0)
else:
final_loss += torch.sum(loss[batch_id])
cnt_has_object += 1.0
final_loss = final_loss/(cnt_has_object * loss.shape[1] + 1e-32)
return {"loss_sem_cls_softmax_skip_none_gt_sample": final_loss}
def loss_sem_cls_softmax_2d_box_iou_supervised_skip_none_gt_sample(self, outputs, targets, assignments):
pred_logits = outputs["sem_cls_logits"]
gt_box_label = torch.gather(
targets["gt_box_sem_cls_label"], 1, assignments["per_prop_gt_inds"]
)
gt_box_label[assignments["proposal_matched_mask"].int() == 0] = (
pred_logits.shape[-1] - 1
)
novel_box_judge = targets['novel_box_judge']
gt_box_label[novel_box_judge > 0] = 0
loss = F.cross_entropy(
pred_logits.transpose(2, 1),
gt_box_label,
self.semcls_percls_weights,
reduction="none",
)
final_loss = 0
cnt_has_object = 0
for batch_id in range(loss.shape[0]):
num_obj_thisbatch = torch.sum(targets['gt_box_present'][batch_id])
num_novel_box_judge = torch.sum(novel_box_judge[batch_id])
if num_obj_thisbatch == 0 and num_novel_box_judge == 0:
final_loss += (torch.sum(loss[batch_id]) * 0)
else:
final_loss += torch.sum(loss[batch_id])
cnt_has_object += 1.0
final_loss = final_loss/(cnt_has_object * loss.shape[1] + 1e-32)
return {"loss_sem_cls_softmax_2d_box_iou_supervised_skip_none_gt_sample": final_loss}
def loss_sem_cls_softmax_skip_none_gt_sample_en_discovery_objectness(self, outputs, targets, assignments):
pred_logits = outputs["sem_cls_logits"]
gt_box_label = torch.gather(
targets["gt_box_sem_cls_label"], 1, assignments["per_prop_gt_inds"]
)
gt_box_label[assignments["proposal_matched_mask"].int() == 0] = (
pred_logits.shape[-1] - 1
)
gt_novel_discovery = targets['discovery_novel'].type_as(gt_box_label)
gt_box_label[gt_novel_discovery > 0] = 0
loss = F.cross_entropy(
pred_logits.transpose(2, 1),
gt_box_label,
self.semcls_percls_weights,
reduction="none",
)
# weight_map = torch.ones_like(loss, device=loss.device)
# weight_map[gt_novel_discovery > 0] = outputs['objectness_prob'][gt_novel_discovery>0]
final_loss = 0
cnt_has_object = 0
for batch_id in range(loss.shape[0]):
num_obj_thisbatch = torch.sum(targets['gt_box_present'][batch_id])
num_discovery = torch.sum(gt_novel_discovery[batch_id])
if num_obj_thisbatch == 0 and num_discovery == 0: # if there are no gt boxes and no discovery boxes
final_loss += (torch.sum(loss[batch_id]) * 0)
else:
# final_loss += torch.sum(loss[batch_id]*weight_map[batch_id])
final_loss += torch.sum(loss[batch_id])
cnt_has_object += 1.0
final_loss = final_loss/(cnt_has_object * loss.shape[1] + 1e-32)
return {"loss_sem_cls_softmax_skip_none_gt_sample_en_discovery_objectness": final_loss}
def loss_sem_cls_softmax_skip_none_gt_sample_keep_discovery_objectness(self, outputs, targets, assignments):
pred_logits = outputs["sem_cls_logits"]
gt_box_label = torch.gather(
targets["gt_box_sem_cls_label"], 1, assignments["per_prop_gt_inds"]
)
gt_box_label[assignments["proposal_matched_mask"].int() == 0] = (
pred_logits.shape[-1] - 1
)
loss = F.cross_entropy(
pred_logits.transpose(2, 1),
gt_box_label,
self.semcls_percls_weights,
reduction="none",
)
final_loss = 0
cnt_has_object = 0
gt_novel_discovery = targets['discovery_novel'].type_as(gt_box_label)
loss_weights = torch.ones_like(loss)
loss_weights[gt_novel_discovery > 0] = 0 # do not supervise novel boxes
for batch_id in range(loss.shape[0]):
num_obj_thisbatch = torch.sum(targets['gt_box_present'][batch_id])
num_discovery = torch.sum(gt_novel_discovery[batch_id])
loss_weights_this_batch = loss_weights[batch_id]
if num_obj_thisbatch == 0:# and num_discovery == 0:
final_loss += (torch.sum(loss[batch_id]) * 0)
else:
final_loss += torch.sum(loss[batch_id] * loss_weights_this_batch)
cnt_has_object += torch.sum(loss_weights_this_batch)
final_loss = final_loss/(cnt_has_object + 1e-32)
return {"loss_sem_cls_softmax_skip_none_gt_sample_keep_discovery_objectness": final_loss}
def loss_sem_cls_softmax_discovery_novel_objectness(self, outputs, targets, assignments):
# # Not vectorized version
# pred_logits = outputs["sem_cls_logits"]
# assign = assignments["assignments"]
# sem_cls_targets = torch.ones((pred_logits.shape[0], pred_logits.shape[1]),
# dtype=torch.int64, device=pred_logits.device)
# # initialize to background/no-object class
# sem_cls_targets *= (pred_logits.shape[-1] - 1)
# # use assignments to compute labels for matched boxes
# for b in range(pred_logits.shape[0]):
# if len(assign[b]) > 0:
# sem_cls_targets[b, assign[b][0]] = targets["gt_box_sem_cls_label"][b, assign[b][1]]
# sem_cls_targets = sem_cls_targets.view(-1)
# pred_logits = pred_logits.reshape(sem_cls_targets.shape[0], -1)
# loss = F.cross_entropy(pred_logits, sem_cls_targets, self.semcls_percls_weights, reduction="mean")
pred_logits = outputs["sem_cls_logits"]
gt_box_label = torch.gather(
targets["gt_box_sem_cls_label"], 1, assignments["per_prop_gt_inds"]
)
gt_box_label[assignments["proposal_matched_mask"].int() == 0] = (
pred_logits.shape[-1] - 1
)
gt_novel_discovery = targets['discovery_novel'].type_as(gt_box_label)
gt_box_label[gt_novel_discovery > 0] = 0
loss = F.cross_entropy(
pred_logits.transpose(2, 1),
gt_box_label,
self.semcls_percls_weights,
reduction="mean",
)
return {"loss_sem_cls_softmax_discovery_novel_objectness": loss}
def loss_sem_cls(self, outputs, targets, assignments):
pred_logits = outputs["sem_cls_logits"]
gt_box_label = torch.gather(
targets["gt_box_sem_cls_label"], 1, assignments["per_prop_gt_inds"]
)
gt_box_label[assignments["proposal_matched_mask"].int() == 0] = (
pred_logits.shape[-1] - 1
)
# gt_box_label_focal = torch.ones_like(pred_logits, device='cuda')
# gt_box_label_focal[:,:,0]=1
# print('===============================')
gt_box_label_focal = F.one_hot(gt_box_label)
gt_box_label_focal = gt_box_label_focal.type_as(pred_logits)
# print(gt_box_label_focal.shape)
# for i in range(gt_box_label_focal.shape[0]):
loss = sigmoid_focal_loss(pred_logits, gt_box_label_focal, reduction='mean')
return {"loss_sem_cls": loss}
def loss_image_seen_class(self, outputs, targets, assignments):
pred_logits = outputs["seen_class_scores_per_image"]
gt_image_label = targets["gt_image_class_label"]
# print(gt_image_label)
# print(gt_image_label)
# gt_box_label = torch.gather(
# targets["gt_box_sem_cls_label"], 1, assignments["per_prop_gt_inds"]
# )
# gt_box_label[assignments["proposal_matched_mask"].int() == 0] = (
# pred_logits.shape[-1] - 1
# )
# gt_box_label_focal = torch.ones_like(pred_logits, device='cuda')
# gt_box_label_focal[:,:,0]=1
# print('===============================')
# gt_box_label_focal = F.one_hot(gt_box_label)
# gt_box_label_focal = gt_box_label_focal.type_as(pred_logits)
# print(gt_box_label_focal.shape)
# for i in range(gt_box_label_focal.shape[0]):
loss = sigmoid_focal_loss(pred_logits, gt_image_label.type_as(pred_logits), reduction='mean')
return {"loss_image_seen_class": loss}
def loss_contrast_object_text(self, outputs, targets, assignments):
gt_box_label = torch.gather(
targets["gt_box_seen_sem_cls_label"], 1, assignments["per_prop_gt_inds"]
)
text_correlation_embedding = outputs["text_correlation_embedding"]
text_correlation_embedding = text_correlation_embedding / (text_correlation_embedding.norm(dim=-1, keepdim=True) + 1e-32)
text_features_clip = targets["text_features_clip"]
temperature_param = targets["logit_scale"]
# temperature_param = torch.clip()
# correlation_map = torch.bmm(text_correlation_embedding, text_features_clip.permute(0, 2, 1)) / 0.07 #* temperature_param
correlation_map = torch.bmm(text_correlation_embedding,
text_features_clip.permute(0, 2, 1)) * temperature_param
gt_box_label[assignments["proposal_matched_mask"].int() == 0] = correlation_map.shape[-1]-1
loss = F.cross_entropy(
correlation_map.permute(0, 2, 1), # 8, 38, 128
gt_box_label, # 8, 128
self.seen_semcls_percls_weights,
reduction="mean",
)
return {"loss_contrast_object_text": loss}
def loss_contrastive(self, outputs, targets, assignments):
text_feats = outputs["pooled_updated_text_features"]
image_feats = outputs["image_features_clip"]
# logit_scale = outputs["logit_scale"]
similarity = self.logit_scale * image_feats @ text_feats.t()
labels = torch.arange(similarity.shape[0], device='cuda')
loss_i = F.cross_entropy(
similarity,
labels,
reduction="mean",
)
loss_t = F.cross_entropy(
similarity.permute(1, 0),
labels,
reduction="mean",
)
return {"loss_contrastive": (loss_i+loss_t)/2.0}
# def bias_init_with_prob(self, prior_prob: float) -> float:
# """initialize conv/fc bias value according to a given probability value."""
# bias_init = float(-np.log((1 - prior_prob) / prior_prob))
# return bias_init
#
# nn.init.constant_(layer.bias, bias_init_with_prob(0.01))
def loss_feat_seen_sigmoid_loss(self, outputs, targets, assignments):
text_correlation_embedding = outputs["text_correlation_embedding"]
# text_correlation_embedding = text_correlation_embedding / (
# text_correlation_embedding.norm(dim=-1, keepdim=True) + 1e-32)
text_features_clip = targets["text_features_clip"].to(torch.float32)
# print(text_features_clip.shape)
# temperature_param = outputs["logit_scale"]
correlation_map = torch.bmm(text_correlation_embedding,
text_features_clip.permute(0, 2, 1))
# scores = torch.nn.functional.softmax(correlation_map, dim=-1)[:, :, :]
gt_box_label = torch.gather(targets["gt_box_seen_sem_cls_label"], 1, assignments["per_prop_gt_inds"])
gt_box_label[assignments["proposal_matched_mask"].int() == 0] = (correlation_map.shape[-1])
# gt_box_label_focal = torch.ones_like(pred_logits, device='cuda')
# gt_box_label_focal[:,:,0]=1
# print('===============================')
gt_box_label_focal = F.one_hot(gt_box_label)[:,:,:-1]
gt_box_label_focal = gt_box_label_focal.type_as(correlation_map)
# for i in range(gt_box_label_focal.shape[0]):
loss = sigmoid_focal_loss(correlation_map, gt_box_label_focal, reduction='none')
# loss_weight = torch.ones_like(loss, device=loss.device)
loss_pos = torch.ones_like(loss, device=loss.device)
loss_neg = torch.ones_like(loss, device=loss.device)
loss_neg[:, :, 10:] = 0.0
loss_weight = torch.where(assignments["proposal_matched_mask"].unsqueeze(-1).repeat(1, 1, loss.shape[-1]).int() != 0, loss_pos, loss_neg) # do not apply novel signal for un-matched boxes
all_num = torch.sum(assignments["proposal_matched_mask"].int() != 0) * loss.shape[-1] + torch.sum(assignments["proposal_matched_mask"].int() == 0) * 10
final_loss = torch.sum(loss * loss_weight) / all_num
return {"loss_feat_seen_sigmoid_loss": final_loss}
def loss_feat_seen_softmax_loss(self, outputs, targets, assignments):
text_correlation_embedding = outputs["text_correlation_embedding"]
text_correlation_embedding = text_correlation_embedding / (
text_correlation_embedding.norm(dim=-1, keepdim=True) + 1e-32)
text_features_clip = targets["text_features_clip"].to(torch.float32)
# print(text_features_clip.shape)
temperature_param = targets["logit_scale"]
correlation_map = torch.bmm(text_correlation_embedding,
text_features_clip.permute(0, 2, 1)) * temperature_param
# scores = torch.nn.functional.softmax(correlation_map, dim=-1)[:, :, :]
gt_box_label = torch.gather(targets["gt_box_seen_sem_cls_label"], 1, assignments["per_prop_gt_inds"])
gt_box_label[assignments["proposal_matched_mask"].int() == 0] = (correlation_map.shape[-1]-1)
# gt_box_label_focal = torch.ones_like(pred_logits, device='cuda')
# gt_box_label_focal[:,:,0]=1
# print('===============================')
loss = F.cross_entropy(
correlation_map.transpose(2, 1),
gt_box_label,
reduction="none",
)
# loss_weight = torch.ones_like(loss, device=loss.device)
loss_pos = torch.ones_like(loss, device=loss.device)
loss_neg = torch.zeros_like(loss, device=loss.device)
loss_weight = torch.where(assignments["proposal_matched_mask"].int() != 0, loss_pos, loss_neg) # do not apply novel signal for un-matched boxes
all_num = torch.sum(assignments["proposal_matched_mask"].int() != 0) * loss.shape[-1]
final_loss = torch.sum(loss * loss_weight) / all_num
return {"loss_feat_seen_softmax_loss": final_loss}
def loss_feat_seen_softmax_weakly_loss(self, outputs, targets, assignments):
text_correlation_embedding = outputs["text_correlation_embedding"]
text_correlation_embedding = text_correlation_embedding / (
text_correlation_embedding.norm(dim=-1, keepdim=True) + 1e-32)
text_features_clip = targets["text_features_clip"].to(torch.float32)
# print(text_features_clip.shape)
temperature_param = targets["logit_scale"]
correlation_map = torch.bmm(text_correlation_embedding,
text_features_clip.permute(0, 2, 1)) * temperature_param
# scores = torch.nn.functional.softmax(correlation_map, dim=-1)[:, :, :]
seen_matched_box_label = torch.gather(targets["gt_box_seen_sem_cls_label"], 1, assignments["per_prop_gt_inds"])
# gt_box_label[assignments["proposal_matched_mask"].int() == 0] = (correlation_map.shape[-1]-1)
gt_box_label = torch.where(assignments["proposal_matched_mask"].int()>0, seen_matched_box_label, targets['weak_box_cate_label'])
if self.confidence_type=="clip-max-prob":
gt_box_confidence_map = targets['weak_confidence_weight'].detach().clone() #torch.where(assignments["proposal_matched_mask"].int()>0, seen_confidence, targets['weak_confidence_weight'])
gt_box_confidence_map[torch.logical_and(assignments["proposal_matched_mask"].int() > 0, gt_box_label !=-1)] = 1.0
elif self.confidence_type=="non-confidence":
gt_box_confidence_map = torch.ones_like(gt_box_label, device=gt_box_label.device)
elif self.confidence_type=="objectness":
gt_box_confidence_map = outputs['objectness_prob'].detach().clone()
gt_box_confidence_map[
torch.logical_and(assignments["proposal_matched_mask"].int() > 0, gt_box_label != -1)] = 1.0
elif self.confidence_type=="clip+objectness":
gt_box_confidence_map = (outputs['objectness_prob'].detach().clone()+targets['weak_confidence_weight'].detach().clone())/2.0
gt_box_confidence_map[torch.logical_and(assignments["proposal_matched_mask"].int() > 0, gt_box_label != -1)] = 1.0
gt_box_confidence = gt_box_confidence_map
gt_box_label = torch.where(gt_box_label == -1, targets['weak_box_cate_label'], gt_box_label) # when the seen is -1(means novel boxes), use weak label
# seen_confidence = torch.ones_like(targets['weak_confidence_weight'], device=targets['weak_confidence_weight'].device)
# gt_box_label_focal = torch.ones_like(pred_logits, device='cuda')
# gt_box_label_focal[:,:,0]=1
# print('===============================')
loss = F.cross_entropy(
correlation_map.transpose(2, 1),
gt_box_label,
reduction="none",
)
# loss_weight = torch.ones_like(loss, device=loss.device)
# loss_pos = torch.ones_like(loss, device=loss.device)
# loss_neg = torch.zeros_like(loss, device=loss.device)
# loss_weight = torch.where(assignments["proposal_matched_mask"].int() != 0, loss_pos, loss_neg) # do not apply novel signal for un-matched boxes
#
# all_num = torch.sum(assignments["proposal_matched_mask"].int() != 0) * loss.shape[-1]
all_num = torch.sum(gt_box_confidence > 1e-32)
final_loss = torch.sum(loss * gt_box_confidence) / all_num
return {"loss_feat_seen_softmax_weakly_loss": final_loss}
def loss_feat_seen_softmax_weakly_loss_with_novel_cate_confi(self, outputs, targets, assignments):
text_correlation_embedding = outputs["text_correlation_embedding"]
text_correlation_embedding = text_correlation_embedding / (
text_correlation_embedding.norm(dim=-1, keepdim=True) + 1e-32)
text_features_clip = targets["text_features_clip"].to(torch.float32)
# print(text_features_clip.shape)
temperature_param = targets["logit_scale"]
correlation_map = torch.bmm(text_correlation_embedding,
text_features_clip.permute(0, 2, 1)) * temperature_param
# scores = torch.nn.functional.softmax(correlation_map, dim=-1)[:, :, :]
seen_matched_box_label = torch.gather(targets["gt_box_seen_sem_cls_label"], 1, assignments["per_prop_gt_inds"])
seen_matched_confidence = torch.gather(targets["gt_box_seen_sem_cls_confi"], 1, assignments["per_prop_gt_inds"])
# gt_box_label[assignments["proposal_matched_mask"].int() == 0] = (correlation_map.shape[-1]-1)
gt_box_label = torch.where(assignments["proposal_matched_mask"].int()>0, seen_matched_box_label, targets['weak_box_cate_label'])
if self.confidence_type=="clip-max-prob":
gt_box_confidence_map = torch.where(assignments["proposal_matched_mask"].int()>0, seen_matched_confidence, targets['weak_confidence_weight'])
elif self.confidence_type=="non-confidence":
gt_box_confidence_map = torch.where(assignments["proposal_matched_mask"].int() > 0, seen_matched_confidence,
targets['weak_confidence_weight'])
gt_box_confidence_map[gt_box_confidence_map>1e-16] = 1
# gt_box_confidence_map = targets['weak_confidence_weight'].detach().clone() #torch.where(assignments["proposal_matched_mask"].int()>0, seen_confidence, targets['weak_confidence_weight'])
# gt_box_confidence_map[torch.logical_and(assignments["proposal_matched_mask"].int() > 0, gt_box_label !=-1)] = 1.0
gt_box_confidence = gt_box_confidence_map
# gt_box_label = torch.where(gt_box_label == -1, targets['weak_box_cate_label'], gt_box_label) # when the seen is -1(means novel boxes), use weak label
# seen_confidence = torch.ones_like(targets['weak_confidence_weight'], device=targets['weak_confidence_weight'].device)
# gt_box_label_focal = torch.ones_like(pred_logits, device='cuda')
# gt_box_label_focal[:,:,0]=1
# print('===============================')
loss = F.cross_entropy(
correlation_map.transpose(2, 1),
gt_box_label,
reduction="none",
)
# loss_weight = torch.ones_like(loss, device=loss.device)
# loss_pos = torch.ones_like(loss, device=loss.device)
# loss_neg = torch.zeros_like(loss, device=loss.device)
# loss_weight = torch.where(assignments["proposal_matched_mask"].int() != 0, loss_pos, loss_neg) # do not apply novel signal for un-matched boxes
#
# all_num = torch.sum(assignments["proposal_matched_mask"].int() != 0) * loss.shape[-1]
all_num = torch.sum(gt_box_confidence > 1e-32) + 1e-32
final_loss = torch.sum(loss * gt_box_confidence) / all_num
return {"loss_feat_seen_softmax_weakly_loss_with_novel_cate_confi": final_loss}
def loss_feat_seen_softmax_iou_match_weakly_loss_with_novel_cate_confi(self, outputs, targets, assignments):
text_correlation_embedding = outputs["text_correlation_embedding"]
text_correlation_embedding = text_correlation_embedding / (
text_correlation_embedding.norm(dim=-1, keepdim=True) + 1e-32)
text_features_clip = targets["text_features_clip"].to(torch.float32)
# print(text_features_clip.shape)
temperature_param = targets["logit_scale"]
correlation_map = torch.bmm(text_correlation_embedding,
text_features_clip.permute(0, 2, 1)) * temperature_param
# scores = torch.nn.functional.softmax(correlation_map, dim=-1)[:, :, :]
# seen_matched_box_label = torch.gather(targets["gt_box_seen_sem_cls_label"], 1, assignments["per_prop_gt_inds"])
# seen_matched_confidence = torch.gather(targets["gt_box_seen_sem_cls_confi"], 1, assignments["per_prop_gt_inds"])
# # gt_box_label[assignments["proposal_matched_mask"].int() == 0] = (correlation_map.shape[-1]-1)
# gt_box_label = torch.where(assignments["proposal_matched_mask"].int()>0, seen_matched_box_label, targets['weak_box_cate_label'])
# gt_box_confidence_map = torch.where(assignments["proposal_matched_mask"].int()>0, seen_matched_confidence, targets['weak_confidence_weight'])
# # gt_box_confidence_map = targets['weak_confidence_weight'].detach().clone() #torch.where(assignments["proposal_matched_mask"].int()>0, seen_confidence, targets['weak_confidence_weight'])
# # gt_box_confidence_map[torch.logical_and(assignments["proposal_matched_mask"].int() > 0, gt_box_label !=-1)] = 1.0
# gt_box_confidence = gt_box_confidence_map
gt_box_label = targets['weak_box_cate_label']
gt_box_confidence = targets['weak_confidence_weight']
# gt_box_label = torch.where(gt_box_label == -1, targets['weak_box_cate_label'], gt_box_label) # when the seen is -1(means novel boxes), use weak label
# seen_confidence = torch.ones_like(targets['weak_confidence_weight'], device=targets['weak_confidence_weight'].device)
# gt_box_label_focal = torch.ones_like(pred_logits, device='cuda')
# gt_box_label_focal[:,:,0]=1
# print('===============================')
loss = F.cross_entropy(
correlation_map.transpose(2, 1),
gt_box_label,
reduction="none",
)
# loss_weight = torch.ones_like(loss, device=loss.device)
# loss_pos = torch.ones_like(loss, device=loss.device)
# loss_neg = torch.zeros_like(loss, device=loss.device)
# loss_weight = torch.where(assignments["proposal_matched_mask"].int() != 0, loss_pos, loss_neg) # do not apply novel signal for un-matched boxes
#
# all_num = torch.sum(assignments["proposal_matched_mask"].int() != 0) * loss.shape[-1]
all_num = torch.sum(gt_box_confidence > 1e-32) + 1e-32
final_loss = torch.sum(loss * gt_box_confidence) / all_num
return {"loss_feat_seen_softmax_iou_match_weakly_loss_with_novel_cate_confi": final_loss}
def loss_feat_seen_softmax_loss_with_novel_cate_confi(self, outputs, targets, assignments):
text_correlation_embedding = outputs["text_correlation_embedding"]
text_correlation_embedding = text_correlation_embedding / (
text_correlation_embedding.norm(dim=-1, keepdim=True) + 1e-32)
text_features_clip = targets["text_features_clip"].to(torch.float32)
if self.if_only_seen_in_loss:
text_features_clip = text_features_clip[:,:10,:]
# print(text_features_clip.shape)
temperature_param = targets["logit_scale"]
correlation_map = torch.bmm(text_correlation_embedding,
text_features_clip.permute(0, 2, 1)) * temperature_param
# scores = torch.nn.functional.softmax(correlation_map, dim=-1)[:, :, :]
seen_matched_box_label = torch.gather(targets["gt_box_seen_sem_cls_label"], 1, assignments["per_prop_gt_inds"])
seen_matched_confidence = torch.gather(targets["gt_box_seen_sem_cls_confi"], 1, assignments["per_prop_gt_inds"])
if self.if_only_seen_in_loss:
seen_matched_box_label[seen_matched_confidence<1e-32] = 0 # just give up the label for confidenc=0, although here set it = 0, it will not works, because the confidence=0
seen_matched_confidence[assignments["proposal_matched_mask"].int() == 0] = 0.0
# gt_box_label[assignments["proposal_matched_mask"].int() == 0] = (correlation_map.shape[-1]-1)
#zeros_confidence_map = seen_matched_box_label #torch.zeros_like(seen_matched_box_label, device='cuda')
gt_box_label = seen_matched_box_label #torch.where(assignments["proposal_matched_mask"].int()>0, seen_matched_box_label, targets['weak_box_cate_label'])
gt_box_confidence_map = seen_matched_confidence #torch.where(assignments["proposal_matched_mask"].int()>0, seen_matched_confidence, targets['weak_confidence_weight'])
# gt_box_confidence_map = targets['weak_confidence_weight'].detach().clone() #torch.where(assignments["proposal_matched_mask"].int()>0, seen_confidence, targets['weak_confidence_weight'])
# gt_box_confidence_map[torch.logical_and(assignments["proposal_matched_mask"].int() > 0, gt_box_label !=-1)] = 1.0
gt_box_confidence = gt_box_confidence_map
# gt_box_label = torch.where(gt_box_label == -1, targets['weak_box_cate_label'], gt_box_label) # when the seen is -1(means novel boxes), use weak label
# seen_confidence = torch.ones_like(targets['weak_confidence_weight'], device=targets['weak_confidence_weight'].device)
# gt_box_label_focal = torch.ones_like(pred_logits, device='cuda')
# gt_box_label_focal[:,:,0]=1
# print('===============================')
loss = F.cross_entropy(
correlation_map.transpose(2, 1),
gt_box_label,
reduction="none",
)
# loss_weight = torch.ones_like(loss, device=loss.device)
# loss_pos = torch.ones_like(loss, device=loss.device)
# loss_neg = torch.zeros_like(loss, device=loss.device)
# loss_weight = torch.where(assignments["proposal_matched_mask"].int() != 0, loss_pos, loss_neg) # do not apply novel signal for un-matched boxes
#
# all_num = torch.sum(assignments["proposal_matched_mask"].int() != 0) * loss.shape[-1]
all_num = torch.sum(gt_box_confidence > 1e-32)+1e-16
final_loss = torch.sum(loss * gt_box_confidence) / all_num
return {"loss_feat_seen_softmax_loss_with_novel_cate_confi": final_loss}
def loss_prompt_sigmoid(self, outputs, targets, assignments):
temperature_param = outputs['prompt_temperature_param']
text_correlation_embedding = outputs['prompt_text_correlation_embedding']
# text_correlation_embedding = text_correlation_embedding / (
# text_correlation_embedding.norm(dim=-1, keepdim=True) + 1e-32)
text_features_clip = outputs['prompt_text_features_clip'] #.to(torch.float16)
# print(text_features_clip.shape)
# temperature_param = outputs["logit_scale"]
correlation_map = torch.bmm(text_correlation_embedding,
text_features_clip.permute(0, 2, 1))[:, 0, :]
gt_seen = targets['seen_classes']
# scores = torch.nn.functional.softmax(correlation_map, dim=-1)[:, :, :]
gt_box_label_focal = F.one_hot(gt_seen, num_classes=10)
gt_box_label_focal = gt_box_label_focal.type_as(correlation_map)
# print(gt_box_label_focal.shape)
# for i in range(gt_box_label_focal.shape[0]):
loss = sigmoid_focal_loss(correlation_map, gt_box_label_focal, reduction='mean') + 0 * temperature_param
return loss
def loss_prompt_softmax(self, outputs, targets, assignments):
# temperature_param = outputs['prompt_temperature_param']
gt_box_label = targets['seen_classes']
text_correlation_embedding = outputs['prompt_text_correlation_embedding']
text_correlation_embedding = text_correlation_embedding / (text_correlation_embedding.norm(dim=-1, keepdim=True) + 1e-32)
text_correlation_embedding = text_correlation_embedding
text_features_clip = outputs['prompt_text_features_clip']
temperature_param = outputs['prompt_temperature_param']
# temperature_param = torch.clip()
# correlation_map = torch.bmm(text_correlation_embedding, text_features_clip.permute(0, 2, 1)) / 0.07 #* temperature_param
correlation_map = torch.bmm(text_correlation_embedding,
text_features_clip.permute(0, 2, 1)) * temperature_param
correlation_map = correlation_map[:, 0,:]
loss = F.cross_entropy(
correlation_map, # 8, 38, 128
gt_box_label, # 8, 128
reduction="mean",
)
return loss
def loss_feat_seen_sigmoid_with_full_image_loss(self, outputs, targets, assignments):
text_correlation_embedding = outputs["text_correlation_embedding"]
full_image_embedding = targets['full_image_embedding'].unsqueeze(1)
# full_image_embedding = full_image_embedding / (
# full_image_embedding.norm(dim=-1, keepdim=True) + 1e-32)
text_features_clip = targets["text_features_clip"].to(torch.float32)[:, :10, :]
# print(text_features_clip.shape)
# temperature_param = outputs["logit_scale"]
correlation_map = torch.bmm(text_correlation_embedding,
(text_features_clip * full_image_embedding).permute(0, 2, 1))
# scores = torch.nn.functional.softmax(correlation_map, dim=-1)[:, :, :]
gt_box_label = torch.gather(
targets["gt_box_seen_sem_cls_label"], 1, assignments["per_prop_gt_inds"]
)
gt_box_label[assignments["proposal_matched_mask"].int() == 0] = (
correlation_map.shape[-1]
)
# gt_box_label_focal = torch.ones_like(pred_logits, device='cuda')
# gt_box_label_focal[:,:,0]=1
# print('===============================')
gt_box_label_focal = F.one_hot(gt_box_label)[:,:,:-1]
gt_box_label_focal = gt_box_label_focal.type_as(correlation_map)
# print(gt_box_label_focal.shape)
# for i in range(gt_box_label_focal.shape[0]):
loss = sigmoid_focal_loss(correlation_map, gt_box_label_focal, reduction='mean')
return {"loss_feat_seen_sigmoid_with_full_image_loss": loss}
def loss_batchwise_contrastive(self, outputs, targets, assignments):
text_feats = outputs["text_queried_embedding"]
image_feats = outputs["image_queried_embedding"]
text_feats_pooled = torch.mean(text_feats, dim=1)
image_feats_pooled = torch.mean(image_feats, dim=1)
text_feats_pooled = text_feats_pooled / (text_feats_pooled.norm(dim=-1, keepdim=True) + 1e-32)
image_feats_pooled = image_feats_pooled / (image_feats_pooled.norm(dim=-1, keepdim=True) + 1e-32)
# logit_scale = outputs["logit_scale"]
temperature_param = targets["logit_scale"]
similarity = temperature_param * image_feats_pooled @ text_feats_pooled.t()
labels = torch.arange(similarity.shape[0], device='cuda')
loss_i = F.cross_entropy(
similarity,
labels,
reduction="mean",
)
loss_t = F.cross_entropy(
similarity.permute(1, 0),
labels,
reduction="mean",
)
return {"loss_batchwise_contrastive": (loss_i+loss_t)/2.0}
def loss_angle(self, outputs, targets, assignments):
angle_logits = outputs["angle_logits"]
angle_residual = outputs["angle_residual_normalized"]
if targets["num_boxes_replica"] > 0:
gt_angle_label = targets["gt_angle_class_label"]
gt_angle_residual = targets["gt_angle_residual_label"]
gt_angle_residual_normalized = gt_angle_residual / (
np.pi / self.dataset_config.num_angle_bin
)
# # Non vectorized version
# assignments = assignments["assignments"]
# p_angle_logits = []
# p_angle_resid = []
# t_angle_labels = []
# t_angle_resid = []
# for b in range(angle_logits.shape[0]):
# if len(assignments[b]) > 0:
# p_angle_logits.append(angle_logits[b, assignments[b][0]])
# p_angle_resid.append(angle_residual[b, assignments[b][0], gt_angle_label[b][assignments[b][1]]])
# t_angle_labels.append(gt_angle_label[b, assignments[b][1]])
# t_angle_resid.append(gt_angle_residual_normalized[b, assignments[b][1]])
# p_angle_logits = torch.cat(p_angle_logits)
# p_angle_resid = torch.cat(p_angle_resid)
# t_angle_labels = torch.cat(t_angle_labels)
# t_angle_resid = torch.cat(t_angle_resid)
# angle_cls_loss = F.cross_entropy(p_angle_logits, t_angle_labels, reduction="sum")
# angle_reg_loss = huber_loss(p_angle_resid.flatten() - t_angle_resid.flatten()).sum()
gt_angle_label = torch.gather(
gt_angle_label, 1, assignments["per_prop_gt_inds"]
)
angle_cls_loss = F.cross_entropy(
angle_logits.transpose(2, 1), gt_angle_label, reduction="none"
)
angle_cls_loss = (
angle_cls_loss * assignments["proposal_matched_mask"]
).sum()
gt_angle_residual_normalized = torch.gather(
gt_angle_residual_normalized, 1, assignments["per_prop_gt_inds"]
)
gt_angle_label_one_hot = torch.zeros_like(
angle_residual, dtype=torch.float32
)
gt_angle_label_one_hot.scatter_(2, gt_angle_label.unsqueeze(-1), 1)
angle_residual_for_gt_class = torch.sum(
angle_residual * gt_angle_label_one_hot, -1
)
angle_reg_loss = huber_loss(
angle_residual_for_gt_class - gt_angle_residual_normalized, delta=1.0
)
angle_reg_loss = (
angle_reg_loss * assignments["proposal_matched_mask"] # only calculate the matched boxes
).sum()
angle_cls_loss /= targets["num_boxes"]
angle_reg_loss /= targets["num_boxes"]
else:
angle_cls_loss = torch.sum(angle_logits)*0 # torch.zeros(1, device=angle_logits.device).squeeze() #
angle_reg_loss = torch.sum(angle_residual)*0 # torch.zeros(1, device=angle_logits.device).squeeze() #
return {"loss_angle_cls": angle_cls_loss, "loss_angle_reg": angle_reg_loss}
def loss_region_embed(self, outputs, targets, assignments):
gt_text_correlation_embedding = torch.gather(
targets['gt_text_correlation_embedding'], 1, assignments["per_prop_gt_inds"].unsqueeze(-1).repeat(1, 1, targets['gt_text_correlation_embedding'].shape[2])
)
# gt_text_correlation_embedding = gt_text_correlation_embedding / gt_text_correlation_embedding.norm(dim=-1, keepdim=True)
text_correlation_embedding = outputs["text_correlation_embedding"]
# text_correlation_embedding = text_correlation_embedding / text_correlation_embedding.norm(dim=-1, keepdim=True)
# gt_text_correlation_embedding[assignments["proposal_matched_mask"].int() == 0] = correlation_map.shape[-1] - 1
weight_maps = torch.ones_like(assignments["proposal_matched_mask"], device='cuda')
weight_maps[assignments["proposal_matched_mask"].int() == 0] = 0.0
weight_maps = weight_maps.unsqueeze(-1)
ave_weight = text_correlation_embedding.shape[0]*text_correlation_embedding.shape[2]
# pred_region_embed = outputs["region_embedding"]
# target_region_embed = outputs["gt_region_embedding"]
l1_loss = F.l1_loss(text_correlation_embedding*weight_maps/ave_weight, gt_text_correlation_embedding*weight_maps/ave_weight, reduction='sum')
# print(l1_loss.shape)
return {"loss_region_embed": l1_loss}
def loss_predicted_region_embed_l1(self, outputs, targets, assignments):
gt_text_correlation_embedding = targets['gt_text_correlation_embedding'] #torch.gather(
#targets['gt_text_correlation_embedding'], 1, assignments["per_prop_gt_inds"].unsqueeze(-1).repeat(1, 1, targets['gt_text_correlation_embedding'].shape[2])
#)
# gt_text_correlation_embedding = gt_text_correlation_embedding / gt_text_correlation_embedding.norm(dim=-1, keepdim=True)
text_correlation_embedding = outputs["text_correlation_embedding"]
# text_correlation_embedding = text_correlation_embedding / text_correlation_embedding.norm(dim=-1, keepdim=True)
# gt_text_correlation_embedding[assignments["proposal_matched_mask"].int() == 0] = correlation_map.shape[-1] - 1
weight_maps = targets['gt_text_correlation_embedding_mask'] # torch.ones_like(assignments["proposal_matched_mask"], device='cuda')
# weight_maps[assignments["proposal_matched_mask"].int() == 0] = 0.0
# weight_maps = weight_maps.unsqueeze(-1)
ave_weight = torch.sum(weight_maps)*text_correlation_embedding.shape[2]
# pred_region_embed = outputs["region_embedding"]
# target_region_embed = outputs["gt_region_embedding"]
l1_loss = F.l1_loss(text_correlation_embedding*weight_maps, gt_text_correlation_embedding*weight_maps, reduction='sum') / ave_weight
# print(l1_loss.shape)
return {"loss_predicted_region_embed_l1": l1_loss}
def loss_predicted_region_embed_l1_only_last_layer(self, outputs, targets, assignments):
gt_text_correlation_embedding = targets['gt_text_correlation_embedding'] #torch.gather(
#targets['gt_text_correlation_embedding'], 1, assignments["per_prop_gt_inds"].unsqueeze(-1).repeat(1, 1, targets['gt_text_correlation_embedding'].shape[2])
#)
# gt_text_correlation_embedding = gt_text_correlation_embedding / gt_text_correlation_embedding.norm(dim=-1, keepdim=True)
text_correlation_embedding = outputs["text_correlation_embedding"]
# text_correlation_embedding = text_correlation_embedding / text_correlation_embedding.norm(dim=-1, keepdim=True)
# gt_text_correlation_embedding[assignments["proposal_matched_mask"].int() == 0] = correlation_map.shape[-1] - 1
weight_maps = targets['gt_text_correlation_embedding_mask'] # torch.ones_like(assignments["proposal_matched_mask"], device='cuda')
# weight_maps[assignments["proposal_matched_mask"].int() == 0] = 0.0
# weight_maps = weight_maps.unsqueeze(-1)
ave_weight = torch.sum(weight_maps)*text_correlation_embedding.shape[2]
# pred_region_embed = outputs["region_embedding"]
# target_region_embed = outputs["gt_region_embedding"]
l1_loss = F.l1_loss(text_correlation_embedding*weight_maps, gt_text_correlation_embedding*weight_maps, reduction='sum') / ave_weight
# print(l1_loss.shape)
return {"loss_predicted_region_embed_l1_only_last_layer": l1_loss}
def loss_predicted_region_embed_cos(self, outputs, targets, assignments):
gt_text_correlation_embedding = targets['gt_text_correlation_embedding'] #torch.gather(
#targets['gt_text_correlation_embedding'], 1, assignments["per_prop_gt_inds"].unsqueeze(-1).repeat(1, 1, targets['gt_text_correlation_embedding'].shape[2])
#)
# gt_text_correlation_embedding = gt_text_correlation_embedding / gt_text_correlation_embedding.norm(dim=-1, keepdim=True)
text_correlation_embedding = outputs["text_correlation_embedding"]
# text_correlation_embedding = text_correlation_embedding / text_correlation_embedding.norm(dim=-1, keepdim=True)
# gt_text_correlation_embedding[assignments["proposal_matched_mask"].int() == 0] = correlation_map.shape[-1] - 1
weight_maps = targets['gt_text_correlation_embedding_mask'] # torch.ones_like(assignments["proposal_matched_mask"], device='cuda')
# weight_maps[assignments["proposal_matched_mask"].int() == 0] = 0.0
# weight_maps = weight_maps.unsqueeze(-1)
ave_weight = torch.sum(weight_maps)
# pred_region_embed = outputs["region_embedding"]
# target_region_embed = outputs["gt_region_embedding"]
cos_loss = 1 - F.cosine_similarity(gt_text_correlation_embedding, text_correlation_embedding, dim=-1, eps=1e-16)
final_loss = torch.sum(cos_loss * weight_maps[:,:,0]) / ave_weight
# print(l1_loss.shape)
return {"loss_predicted_region_embed_cos": final_loss}
def loss_sem_focal_cls(self, outputs, targets, assignments):
pred_logits = outputs["seen_sem_cls_logits"]
gt_box_label = torch.gather(
targets["gt_box_seen_sem_cls_label"], 1, assignments["per_prop_gt_inds"]
)
gt_box_label[assignments["proposal_matched_mask"].int() == 0] = (
pred_logits.shape[-1] - 1
)
# gt_box_label_focal = torch.ones_like(pred_logits, device='cuda')