-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTCD_head.py
1137 lines (1045 loc) · 55.5 KB
/
TCD_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
'''
Codes are adapted from https://github.com/open-mmlab/mmdetection/blob/master/mmdet/models/dense_heads/fcos_head.py
'''
import torch
import torch.nn as nn
import torch.nn.functional as F
from mmcv.cnn import Scale, normal_init
from mmcv.runner import force_fp32
from mmdet.core import distance2bbox, multi_apply, multiclass_nms, reduce_mean,bbox_overlaps
from ..builder import HEADS, build_loss
from .anchor_free_head import AnchorFreeHead
import numpy as np
from mmcv.ops import DeformConv2d
INF = 1e8
def weighted_binary_cross_entropy(pred, label, weight, avg_factor=None):
if pred.dim() != label.dim():
label, weight = _expand_binary_labels(label, weight, pred.size(-1))
if avg_factor is None:
avg_factor = max(torch.sum(weight > 0).float().item(), 1.)
return F.binary_cross_entropy_with_logits(
pred, label.float(), weight.float(),
reduction='sum')[None] / avg_factor
def _expand_binary_labels(labels, label_weights, label_channels):
bin_labels = labels.new_full((labels.size(0), label_channels), 0)
inds = torch.nonzero(labels >= 1).squeeze()
if inds.numel() > 0:
bin_labels[inds, labels[inds] - 1] = 1
bin_label_weights = label_weights.view(-1, 1).expand(
label_weights.size(0), label_channels)
return bin_labels, bin_label_weights
#add feature alignment module --> use the whole CLQ head: lpf 0613
class FeatureAlignment(nn.Module):
"""Feature Adaption Module.
Feature Adaption Module is implemented based on DCN v1.
It uses anchor shape prediction rather than feature map to
predict offsets of deformable conv layer.
Args:
in_channels (int): Number of channels in the input feature map.
out_channels (int): Number of channels in the output feature map.
kernel_size (int): Deformable conv kernel size.
deformable_groups (int): Deformable conv group size.
"""
def __init__(self,
in_channels,
out_channels,
kernel_size=3,
deformable_groups=4):
super(FeatureAlignment, self).__init__()
offset_channels = kernel_size * kernel_size * 2
self.conv_offset = nn.Conv2d(
4, deformable_groups * offset_channels, 1, bias=False)
self.conv_adaption = DeformConv2d(
in_channels,
out_channels,
kernel_size=kernel_size,
padding=(kernel_size - 1) // 2,
deformable_groups=deformable_groups)
self.relu = nn.ReLU(inplace=True)
def init_weights(self):
normal_init(self.conv_offset, std=0.1)
normal_init(self.conv_adaption, std=0.01)
def forward(self, x, shape):
offset = self.conv_offset(shape)
x = self.relu(self.conv_adaption(x, offset))
return x
@HEADS.register_module()
class TCD_Head_lqe(AnchorFreeHead):
def __init__(self,
num_classes,
in_channels,
regress_ranges=((-1, 64), (64, 128), (128, 256), (256, 512),(512, INF)),
# origion
#((1, 64), (32, 128), (64, 256), (128, 512), (256, 2048)) ## lpf add 0731 ; like fovea
norm_on_bbox=False,
centerness_on_reg=False,
piou = False,
piou_thr = 0.4,
loss_cls=dict(
type='FocalLoss',
use_sigmoid=True,
gamma=2.0,
alpha=0.25,
loss_weight=1.0),
loss_bbox=dict(type='IoULoss', loss_weight=1.0),
loss_centerness=dict(
type='CrossEntropyLoss',
use_sigmoid=True,
loss_weight=1.0),
norm_cfg=dict(type='GN', num_groups=32, requires_grad=True),
**kwargs):
self.regress_ranges = regress_ranges
self.norm_on_bbox = norm_on_bbox
self.centerness_on_reg = centerness_on_reg
self.piou = piou
self.piou_thr = piou_thr
super().__init__(
num_classes,
in_channels,
loss_cls=loss_cls,
loss_bbox=loss_bbox,
norm_cfg=norm_cfg,
**kwargs)
self.loss_centerness = build_loss(loss_centerness)
def _init_layers(self):
"""Initialize layers of the head."""
super()._init_layers()
self.conv_centerness = nn.Conv2d(self.feat_channels, 1, 3, padding=1)
self.scales = nn.ModuleList([Scale(1.0) for _ in self.strides])
#lpf add 20240416
self.lqe_layer = nn.Conv2d(self.feat_channels, 1, 3, padding=1)
#lpf add 20240620
self.deformable_groups = 4
self.use_feature_alignment = True
if self.use_feature_alignment:
self.feature_alignment = FeatureAlignment(
self.feat_channels,
self.feat_channels,
kernel_size=3,
deformable_groups=self.deformable_groups)
def init_weights(self):
"""Initialize weights of the head."""
super().init_weights()
normal_init(self.conv_centerness, std=0.01)
#lpf add 20240416
normal_init(self.lqe_layer, std=0.01)
#lpf add 20240620
if self.use_feature_alignment:
self.feature_alignment.init_weights()
def forward(self, feats):
"""Forward features from the upstream network.
Args:
feats (tuple[Tensor]): Features from the upstream network, each is
a 4D-tensor.
Returns:
tuple:
cls_scores (list[Tensor]): Box scores for each scale level, \
each is a 4D-tensor, the channel number is \
num_points * num_classes.
bbox_preds (list[Tensor]): Box energies / deltas for each \
scale level, each is a 4D-tensor, the channel number is \
num_points * 4.
centernesses (list[Tensor]): centerness for each scale level, \
each is a 4D-tensor, the channel number is num_points * 1.
"""
return multi_apply(self.forward_single, feats, self.scales,
self.strides)
def forward_single(self, x, scale, stride):
"""Forward features of a single scale level.
Args:
x (Tensor): FPN feature maps of the specified stride.
scale (:obj: `mmcv.cnn.Scale`): Learnable scale module to resize
the bbox prediction.
stride (int): The corresponding stride for feature maps, only
used to normalize the bbox prediction when self.norm_on_bbox
is True.
Returns:
tuple: scores for each class, bbox predictions and centerness \
predictions of input feature maps.
"""
# cls_score, bbox_pred, cls_feat, reg_feat = super().forward_single(x)
cls_feat = x
reg_feat = x
# for cls_layer in self.cls_convs:
# cls_feat = cls_layer(cls_feat)
# cls_score = self.conv_cls(cls_feat)
for reg_layer in self.reg_convs:
reg_feat = reg_layer(reg_feat)
bbox_pred = self.conv_reg(reg_feat)
# scale the bbox_pred of different level
# float to avoid overflow when enabling FP16
bbox_pred = scale(bbox_pred).float()
if self.norm_on_bbox:
bbox_pred = F.relu(bbox_pred)
if not self.training:
bbox_pred *= stride
else:
bbox_pred = bbox_pred.exp()
# lpf add 20240620
if self.use_feature_alignment:
if self.norm_on_bbox:
if not self.training:
bbox_pred_dcn= bbox_pred/stride
else:
bbox_pred_dcn=bbox_pred
else:
bbox_pred_dcn=bbox_pred
cls_feat=self.feature_alignment(cls_feat,bbox_pred_dcn) #lpf 0323 add for feature adaption
reg_feat=self.feature_alignment(reg_feat,bbox_pred_dcn) # lpf 20240111 add
for cls_conv in self.cls_convs:
cls_feat = cls_conv(cls_feat)
cls_score = self.conv_cls(cls_feat)
#lpf add 20240416 ablation 1; lqe
quality_score = self.lqe_layer(reg_feat)
cls_score = (cls_score.sigmoid()) * ((quality_score.sigmoid()).pow(0.75)) # lpf 0.8/0.2 1/0.3 0.2 1.0/0.75
if self.centerness_on_reg:
centerness = self.conv_centerness(reg_feat)
else:
centerness = self.conv_centerness(cls_feat)
return cls_score, bbox_pred, centerness,quality_score
@force_fp32(apply_to=('cls_scores', 'bbox_preds', 'centernesses'))
def loss(self,
cls_scores,
bbox_preds,
centernesses,
quality_score,
gt_bboxes,
gt_labels,
img_metas,
gt_bboxes_ignore=None):
"""Compute loss of the head.
Args:
cls_scores (list[Tensor]): Box scores for each scale level,
each is a 4D-tensor, the channel number is
num_points * num_classes.
bbox_preds (list[Tensor]): Box energies / deltas for each scale
level, each is a 4D-tensor, the channel number is
num_points * 4.
centernesses (list[Tensor]): centerness for each scale level, each
is a 4D-tensor, the channel number is num_points * 1.
gt_bboxes (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[Tensor]): class indices corresponding to each box
img_metas (list[dict]): Meta information of each image, e.g.,
image size, scaling factor, etc.
gt_bboxes_ignore (None | list[Tensor]): specify which bounding
boxes can be ignored when computing the loss.
Returns:
dict[str, Tensor]: A dictionary of loss components.
"""
assert len(cls_scores) == len(bbox_preds) == len(centernesses) == len(quality_score) ##lpf add 20240416 quality score
featmap_sizes = [featmap.size()[-2:] for featmap in cls_scores]
all_level_points = self.get_points(featmap_sizes, bbox_preds[0].dtype,
bbox_preds[0].device)
# lpf revise : add output: gt_inds : return gt_inds
labels, bbox_targets, gt_inds= self.get_targets(all_level_points, gt_bboxes,
gt_labels)
flatten_gt_inds = torch.cat(gt_inds) # lpf add
num_imgs = cls_scores[0].size(0)
# flatten cls_scores, bbox_preds and centerness
flatten_cls_scores = [
cls_score.permute(0, 2, 3, 1).reshape(-1, self.cls_out_channels)
for cls_score in cls_scores
]
flatten_bbox_preds = [
bbox_pred.permute(0, 2, 3, 1).reshape(-1, 4)
for bbox_pred in bbox_preds
]
flatten_centerness = [
centerness.permute(0, 2, 3, 1).reshape(-1)
for centerness in centernesses
]
flatten_cls_scores = torch.cat(flatten_cls_scores)
flatten_bbox_preds = torch.cat(flatten_bbox_preds)
flatten_centerness = torch.cat(flatten_centerness)
flatten_labels = torch.cat(labels)
flatten_bbox_targets = torch.cat(bbox_targets)
#lpf add 20240416
flatten_quality_score = [
quality_score_single.permute(0, 2, 3, 1).reshape(-1)
for quality_score_single in quality_score
]
flatten_quality_score=torch.cat(flatten_quality_score)
lqe_target = flatten_labels.new_zeros(flatten_labels.shape).float()
# repeat points to align with bbox_preds
flatten_points = torch.cat(
[points.repeat(num_imgs, 1) for points in all_level_points])
# FG cat_id: [0, num_classes -1], BG cat_id: num_classes
bg_class_ind = self.num_classes
if self.piou:
pos_inds_tem = ((flatten_labels >= 0) & (flatten_labels < bg_class_ind)).nonzero().reshape(-1)
# print('pos_inds_tem',pos_inds_tem.shape,pos_inds_tem.max())
# print('pos_inds_tmp',len(pos_inds_tem))
pos_bbox_targets_tem = flatten_bbox_targets[pos_inds_tem].detach()
## lpf add 0721
pos_classcore_tem,_ = flatten_cls_scores.detach()[pos_inds_tem].max(dim=1) #have used sigmoid in forward_single()
## check class_labels
##flatten_cls_scores[pos_inds_tem].max(dim=1) == flatten_cls_scores[pos_inds_tem][:,flatten_labels]
pos_classcore_threshold=torch.tensor(0.4)
left = pos_bbox_targets_tem[:, 0]
right = pos_bbox_targets_tem[:, 2]
top = pos_bbox_targets_tem[:, 1]
bottom = pos_bbox_targets_tem[:, 3]
inter_left = left.clone()
inter_right = right.clone()
inter_top = top.clone()
inter_bottom = bottom.clone()
#---------------------------------
# half_w = (left+right)/2
# half_h = (top+bottom)/2
# for i in range(len(left)):
# if half_w[i]<left[i]:
# inter_left[i] = half_w[i]
# if half_w[i]<right[i]:
# inter_right[i] = half_w[i]
# if half_h[i]<top[i]:
# inter_top[i] = half_h[i]
# if half_h[i]<bottom[i]:
# inter_bottom[i] = half_h[i]
area_u = (left+right)*(top+bottom) # #
# area_i = (inter_left+inter_right)*(inter_top+inter_bottom)
# iou_target = area_i/(area_u+area_u-area_i)
# print('iou_target', iou_target.shape)
pos_bbox_preds_tem = flatten_bbox_preds[pos_inds_tem]
left_preds = pos_bbox_preds_tem[:, 0]
right_preds = pos_bbox_preds_tem[:, 2]
top_preds = pos_bbox_preds_tem[:, 1]
bottom_preds = pos_bbox_preds_tem[:, 3]
for i in range(len(left)):
if left_preds[i] < left[i]:
inter_left[i] = left_preds[i]
if right_preds[i] < right[i]:
inter_right[i] = right_preds[i]
if top_preds[i] < top[i]:
inter_top[i] = top_preds[i]
if bottom_preds[i] < bottom[i]:
inter_bottom[i] = bottom_preds[i]
area_p = (left_preds+right_preds)*(top_preds+bottom_preds)
area_i = (inter_left + inter_right) * (inter_top + inter_bottom)
iou_target = area_i / (area_p + area_u - area_i)
# pos_inds = pos_inds_tem[iou_target>self.piou_thr]
# ##lpf add 0722 : set a new para to represent alignment of cls and loc
#if joint_feature_selection: need to consider the threshold self.piou_thr and the type of the combination align_score=iou_alpha*cls_beita >= threshold
# align_score=iou_target*(pos_labels_tem.pow(0.1))
# pos_inds = pos_inds_tem[align_score > self.piou_thr]
## lpf add 0722 seperate considering loc and cls
#else: set loc's threshold
# pos_inds_temp: (num_pos_tem,)
# pos_inds: (num_pos,)
positives_bool=(iou_target > self.piou_thr)#& (pos_classcore_tem>pos_classcore_threshold) #further select pos_inds
pos_inds = pos_inds_tem[positives_bool]
# pos_inds_ignore = pos_inds_tem[align_score<=self.piou_thr] #lpf change iou_target --> align_score
pos_inds_ignore = pos_inds_tem[(iou_target <= self.piou_thr)] # |(pos_classcore_tem<=pos_classcore_threshold)
# print('1_num_postives',len(pos_inds),len(pos_inds_ignore),len(pos_inds)+len(pos_inds_ignore))
# print('before dynamic la',len(pos_inds))
# print('flatten_gt_inds2222', flatten_gt_inds[pos_inds]==)
#lpf add 20240415
topk = 100
#alpha=0.3
assert iou_target.shape == pos_classcore_tem.shape # the shape of iou_target == pos_inds.shape
# pos_inds.shape == positives_bool.nonzero().shape ;; but the pos_inds.max() != positives_bool.nonzero().shape --> so use the positive_bool
align_score=iou_target[positives_bool]#.pow(alpha) * pos_classcore_tem[positives_bool].pow(1-alpha) #ablation 3: if consider cls_score
# lpf add 0709 : normlization align_score
align_score=align_score/align_score.max()
# align_score=pos_classcore_tem[positives_bool]
num_gts = sum(map(len, gt_labels)) # total number of gt in the batch
# print('checkpoint1',gt_labels)
# print('checkpoint2',num_gts)
device = flatten_labels.device
label_sequence = torch.arange(num_gts,device=device)# if add device --> empty label_sequence
pos_inds_ = []
#ablation 4: dynamic label assignment
#calculate pos_inds w.r.t each gt
# pos_inds_tmp <-- flatten_labels(positives).nonzeros()
# pos_inds <--> align_score/iou_target/pos_classcore_tem
for i, l in enumerate(label_sequence):
# for each gt, select the corresponding pos_inds
# pos_inds_[i] <--> align_score_ / mask_for_score
mask_for_score = (flatten_gt_inds[pos_inds] == l)
# print('i,l',i,l)
# print('flatten_gt_inds',len(mask_for_score.nonzero()))
# print('flatten_gt_inds_iii',flatten_gt_inds[pos_inds][mask_for_score])
if mask_for_score.any(): #maybe some positives have been filter out in the forementioned steps
align_score_ = align_score[mask_for_score]
# print('check 00000', len(align_score_))
# pos_inds_i = pos_inds[mask_for_score.nonzero().flatten()]
pos_inds_i = pos_inds[mask_for_score]
# print('determine11111',flatten_labels[pos_inds_i])
# print('determine1111111111111', align_score_)
# do the dynamic label assign
if topk >= align_score_.shape[0]:
# print('1111111111111')
sorted_align_score,topk_indices=torch.topk(align_score_,k=align_score_.shape[0],dim=0)
# print('determine222222',align_score_[topk_indices]==sorted_align_score)
else:
# print('222222222222222')
# sorted_align_score, topk_indices = torch.topk(align_score, k=topk, dim=0)
sorted_align_score, topk_indices = torch.sort(align_score_, dim=0, descending=True) ### align_score <--> pos_inds_i : corresponse
sorted_align_score = sorted_align_score[:topk]
pos_inds_ignore = torch.cat((pos_inds_ignore, pos_inds_i[topk_indices[topk:]]))
pos_inds_i = pos_inds_i[topk_indices[:topk]] ##### sorted_align_score <--> this new pos_inds_i : corresponse : length is also the same
# print('aiaiaiaiaiai',flatten_gt_inds[pos_inds_i]==l,sorted_align_score.min(),sorted_align_score.max())
score_gap=0.4
score_division=150
# assert topk==sorted_align_score.shape[0]
score_diff = sorted_align_score - torch.roll(sorted_align_score,-1,dims=0)
top_m=min(topk,align_score_.shape[0])
min_num = 2 # ensure at least 10 positives will be selected for each gt; except for those whose positives are origionally less than min_num
if len(score_diff)>=min_num:
for i in range(score_diff.shape[0]-2):
# division mode
if score_diff[i+1]/score_diff[i] > score_division:
# print('44444444444')
# lpf add 20240616 : ensure at least min_num samples will be selected:
#if min_num >= i+2:
# top_m=min_num
#else:
top_m=i+2 # renew topm : for example: if score_diff[0+1]/score_diff[0] > score_division, keep the 2nd samples: sorted_align_score[:0+2]
# print('top_m',top_m)
break
else:
pass
# diff mode:
# if score_diff[i] > score_gap:
# top_m=i+1
# print('top_m', top_m)
# break
# else:
# pass
if top_m < min(topk,align_score_.shape[0]):
# print('3333333333333')
pos_inds_ignore = torch.cat((pos_inds_ignore,pos_inds_i[top_m:]))
pos_inds_i = pos_inds_i[:top_m]
pos_inds_.extend(pos_inds_i)
# print('i, num_postives',i,len(pos_inds_))
## end gt_i searching
pos_inds = torch.tensor(pos_inds_) #lpf must be long : for the following usage
# print('dynamic la', len(pos_inds))
flatten_labels[pos_inds_ignore] = bg_class_ind
else:
pos_inds = ((flatten_labels >= 0) & (flatten_labels < bg_class_ind)).nonzero().reshape(-1)
# print('2_num_postives', len(pos_inds), len(pos_inds_ignore),len(pos_inds)+len(pos_inds_ignore))
# print('sssssssssssssssssss',pos_inds.shape)
num_pos = torch.tensor(
len(pos_inds), dtype=torch.float, device=bbox_preds[0].device)
num_pos = max(reduce_mean(num_pos), 1.0)
# print(type(pos_inds))
pos_bbox_preds = flatten_bbox_preds[pos_inds]
pos_centerness = flatten_centerness[pos_inds]
# print('pos_inds',pos_inds)
if len(pos_inds) > 0:
pos_bbox_targets = flatten_bbox_targets[pos_inds]
pos_centerness_targets = self.centerness_target(pos_bbox_targets)
pos_points = flatten_points[pos_inds]
pos_decoded_bbox_preds = distance2bbox(pos_points, pos_bbox_preds)
pos_decoded_target_preds = distance2bbox(pos_points,
pos_bbox_targets)
#### lpf:check if this step is right: need need to check
# print(iou_target[pos_inds] == bbox_overlaps(
# pos_decoded_bbox_preds.detach(),
# pos_decoded_target_preds,
# is_aligned=True)
# can't use iou_target[pos_inds] !! because pos_inds corresponds to (num_points,), but iou_target.shape = len(pos_inds)
#lpf add 20240416 quality score
lqe_target[pos_inds]=bbox_overlaps(
pos_decoded_bbox_preds.detach(),
pos_decoded_target_preds,
is_aligned=True)
#lpf add : weight function
k=torch.tensor(15.0)
cls_consistency_new_pos_inds,_=flatten_cls_scores.detach()[pos_inds].max(1)
cls_loc_consistency=abs(lqe_target[pos_inds]-cls_consistency_new_pos_inds)
# loc_weights=((k*cls_loc_consistency).exp()-(k).exp())/(1-(k).exp())
####### lpf: use function or directly use cls_consistency_new_pos_inds (like GFLV2: now it contains both cls_score and lqe_score)
#!!!!!!!!!!!!!!compute and analyze the distribution of cls_loc_consistency :: --> design the suitable function and parameter
# loc_weights=1./(1.0+k*cls_loc_consistency) #.pow(0.5) #### function 1 next
# loc_weights=cls_loc_consistency #### directly use function 2
# loc_weights = cls_consistency_new_pos_inds # directly use like GFLv2 function 3
# loc_weights=cls_loc_consistency * cls_consistency_new_pos_inds #### function 4: cls_score & iou
# loc_weights=cls_loc_consistency * iou_target[pos_inds].pow(0.3) #### function 5: only iou
loc_weights=(1./(1.0+k*cls_loc_consistency))*(((lqe_target[pos_inds])*cls_consistency_new_pos_inds).pow(0.5))
loc_weights = loc_weights/loc_weights.max()
# assert len(pos_centerness_targets)==len(loc_weights) # ablation 5
pos_centerness_targets_1=pos_centerness_targets * loc_weights # lpf revise: norm loc_weights --> loc_weights./loc_weights.max()
# centerness weighted iou loss
centerness_denorm = max(
reduce_mean(pos_centerness_targets_1.sum().detach()), 1e-6)
## lqe_branch's loss calculation
# print(type(lqe_target),type(iou_target),type(lqe_target[pos_inds]),type(iou_target[pos_inds]))
loss_iou_weight=1.0
# lqe_weight = lqe_target.new_ones(pos_inds.shape)
lqe_loss_weight_switch=True ## ablation 6: if weight lqe_loss and how to weight is : using which function
if lqe_loss_weight_switch:
loss_iou = loss_iou_weight*weighted_binary_cross_entropy(flatten_quality_score[pos_inds], lqe_target[pos_inds], weight=pos_centerness_targets_1,
avg_factor=centerness_denorm)
else:
loss_iou = loss_iou_weight * weighted_binary_cross_entropy(flatten_quality_score[pos_inds],
lqe_target[pos_inds],
weight=torch.tensor(1),
avg_factor=num_pos)
loss_bbox = self.loss_bbox(
pos_decoded_bbox_preds,
pos_decoded_target_preds,
weight=pos_centerness_targets_1, ##lpf will add : weight function to replace centerness by considering cls and loc
avg_factor=centerness_denorm)
loss_centerness = self.loss_centerness(
pos_centerness, pos_centerness_targets, avg_factor=num_pos) #no_centerness 0716
else:
loss_bbox = pos_bbox_preds.sum()
loss_centerness = pos_centerness.sum()
loss_iou = pos_bbox_preds.sum()*0
#lpf add 0905
# print(flatten_labels.dtype,loc_weights.dtype)
# cls_weights=torch.ones(flatten_labels.size()).cuda().detach()
# # cls_weights[pos_inds]=loc_weights.cuda().detach()
# cls_weights[pos_inds]=iou_target[(iou_target > self.piou_thr) & (pos_labels_tem>0.5)] * loc_weights.cuda().detach()
# cls_weights[pos_inds]=cls_weights[pos_inds]/(cls_weights[pos_inds].max())
# num_pos=cls_weights[pos_inds].sum()
# loss_cls = self.loss_cls(
# flatten_cls_scores, flatten_labels,weight=cls_weights, avg_factor=num_pos)
loss_cls = self.loss_cls(
flatten_cls_scores, (flatten_labels,lqe_target), avg_factor=num_pos)
return dict(
loss_cls=loss_cls,
loss_bbox=loss_bbox,
loss_centerness=loss_centerness,
loss_iou=loss_iou)
@force_fp32(apply_to=('cls_scores', 'bbox_preds', 'centernesses'))
def get_bboxes(self,
cls_scores,
bbox_preds,
centernesses,
lqe_scores, # lpf add for lqe branch's usage
img_metas,
cfg=None,
rescale=False,
with_nms=True):
"""Transform network output for a batch into bbox predictions.
Args:
cls_scores (list[Tensor]): Box scores for each scale level
with shape (N, num_points * num_classes, H, W).
bbox_preds (list[Tensor]): Box energies / deltas for each scale
level with shape (N, num_points * 4, H, W).
centernesses (list[Tensor]): Centerness for each scale level with
shape (N, num_points * 1, H, W).
img_metas (list[dict]): Meta information of each image, e.g.,
image size, scaling factor, etc.
cfg (mmcv.Config | None): Test / postprocessing configuration,
if None, test_cfg would be used. Default: None.
rescale (bool): If True, return boxes in original image space.
Default: False.
with_nms (bool): If True, do nms before return boxes.
Default: True.
Returns:
list[tuple[Tensor, Tensor]]: Each item in result_list is 2-tuple.
The first item is an (n, 5) tensor, where the first 4 columns
are bounding box positions (tl_x, tl_y, br_x, br_y) and the
5-th column is a score between 0 and 1. The second item is a
(n,) tensor where each item is the predicted class label of the
corresponding box.
"""
assert len(cls_scores) == len(bbox_preds)
num_levels = len(cls_scores)
featmap_sizes = [featmap.size()[-2:] for featmap in cls_scores]
mlvl_points = self.get_points(featmap_sizes, bbox_preds[0].dtype,
bbox_preds[0].device)
result_list = []
for img_id in range(len(img_metas)):
cls_score_list = [
cls_scores[i][img_id].detach() for i in range(num_levels)
]
bbox_pred_list = [
bbox_preds[i][img_id].detach() for i in range(num_levels)
]
centerness_pred_list = [
centernesses[i][img_id].detach() for i in range(num_levels)
]
img_shape = img_metas[img_id]['img_shape']
scale_factor = img_metas[img_id]['scale_factor']
det_bboxes = self._get_bboxes_single(
cls_score_list, bbox_pred_list, centerness_pred_list,
mlvl_points, img_shape, scale_factor, cfg, rescale, with_nms)
result_list.append(det_bboxes)
return result_list
def _get_bboxes_single(self,
cls_scores,
bbox_preds,
centernesses,
mlvl_points,
img_shape,
scale_factor,
cfg,
rescale=False,
with_nms=True):
"""Transform outputs for a single batch item into bbox predictions.
Args:
cls_scores (list[Tensor]): Box scores for a single scale level
with shape (num_points * num_classes, H, W).
bbox_preds (list[Tensor]): Box energies / deltas for a single scale
level with shape (num_points * 4, H, W).
centernesses (list[Tensor]): Centerness for a single scale level
with shape (num_points * 4, H, W).
mlvl_points (list[Tensor]): Box reference for a single scale level
with shape (num_total_points, 4).
img_shape (tuple[int]): Shape of the input image,
(height, width, 3).
scale_factor (ndarray): Scale factor of the image arrange as
(w_scale, h_scale, w_scale, h_scale).
cfg (mmcv.Config | None): Test / postprocessing configuration,
if None, test_cfg would be used.
rescale (bool): If True, return boxes in original image space.
Default: False.
with_nms (bool): If True, do nms before return boxes.
Default: True.
Returns:
tuple(Tensor):
det_bboxes (Tensor): BBox predictions in shape (n, 5), where
the first 4 columns are bounding box positions
(tl_x, tl_y, br_x, br_y) and the 5-th column is a score
between 0 and 1.
det_labels (Tensor): A (n,) tensor where each item is the
predicted class label of the corresponding box.
"""
cfg = self.test_cfg if cfg is None else cfg
assert len(cls_scores) == len(bbox_preds) == len(mlvl_points)
mlvl_bboxes = []
mlvl_scores = []
mlvl_centerness = []
for cls_score, bbox_pred, centerness, points in zip(
cls_scores, bbox_preds, centernesses, mlvl_points):
assert cls_score.size()[-2:] == bbox_pred.size()[-2:]
scores = cls_score.permute(1, 2, 0).reshape(
-1, self.cls_out_channels)#.sigmoid()
#lpf revise in 20240416 , comment sigmoid() as in forward_single(). have used sigmoid()
centerness = centerness.permute(1, 2, 0).reshape(-1).sigmoid()
bbox_pred = bbox_pred.permute(1, 2, 0).reshape(-1, 4)
nms_pre = cfg.get('nms_pre', -1)
if nms_pre > 0 and scores.shape[0] > nms_pre:
max_scores, _ = (scores * centerness[:, None]).max(dim=1)
_, topk_inds = max_scores.topk(nms_pre)
points = points[topk_inds, :]
bbox_pred = bbox_pred[topk_inds, :]
scores = scores[topk_inds, :]
centerness = centerness[topk_inds]
bboxes = distance2bbox(points, bbox_pred, max_shape=img_shape)
mlvl_bboxes.append(bboxes)
mlvl_scores.append(scores)
mlvl_centerness.append(centerness)
mlvl_bboxes = torch.cat(mlvl_bboxes)
if rescale:
mlvl_bboxes /= mlvl_bboxes.new_tensor(scale_factor)
mlvl_scores = torch.cat(mlvl_scores)
padding = mlvl_scores.new_zeros(mlvl_scores.shape[0], 1)
# remind that we set FG labels to [0, num_class-1] since mmdet v2.0
# BG cat_id: num_class
mlvl_scores = torch.cat([mlvl_scores, padding], dim=1)
mlvl_centerness = torch.cat(mlvl_centerness)
if with_nms:
det_bboxes, det_labels = multiclass_nms(
mlvl_bboxes,
mlvl_scores,
cfg.score_thr,
cfg.nms,
cfg.max_per_img,
score_factors=mlvl_centerness)
return det_bboxes, det_labels
else:
return mlvl_bboxes, mlvl_scores, mlvl_centerness
# def get_bboxes(self,
# cls_scores,
# bbox_preds,
# centernesses,
# img_metas,
# cfg=None,
# rescale=False,
# with_nms=True):
# """Transform network output for a batch into bbox predictions.
#
# Args:
# cls_scores (list[Tensor]): Box scores for each scale level
# with shape (N, num_points * num_classes, H, W).
# bbox_preds (list[Tensor]): Box energies / deltas for each scale
# level with shape (N, num_points * 4, H, W).
# centernesses (list[Tensor]): Centerness for each scale level with
# shape (N, num_points * 1, H, W).
# img_metas (list[dict]): Meta information of each image, e.g.,
# image size, scaling factor, etc.
# cfg (mmcv.Config | None): Test / postprocessing configuration,
# if None, test_cfg would be used. Default: None.
# rescale (bool): If True, return boxes in original image space.
# Default: False.
# with_nms (bool): If True, do nms before return boxes.
# Default: True.
#
# Returns:
# list[tuple[Tensor, Tensor]]: Each item in result_list is 2-tuple.
# The first item is an (n, 5) tensor, where 5 represent
# (tl_x, tl_y, br_x, br_y, score) and the score between 0 and 1.
# The shape of the second tensor in the tuple is (n,), and
# each element represents the class label of the corresponding
# box.
# """
# assert len(cls_scores) == len(bbox_preds)
# num_levels = len(cls_scores)
#
# featmap_sizes = [featmap.size()[-2:] for featmap in cls_scores]
# mlvl_points = self.get_points(featmap_sizes, bbox_preds[0].dtype,
# bbox_preds[0].device)
#
# cls_score_list = [cls_scores[i].detach() for i in range(num_levels)]
# bbox_pred_list = [bbox_preds[i].detach() for i in range(num_levels)]
# centerness_pred_list = [
# centernesses[i].detach() for i in range(num_levels)
# ]
# if torch.onnx.is_in_onnx_export():
# assert len(
# img_metas
# ) == 1, 'Only support one input image while in exporting to ONNX'
# img_shapes = img_metas[0]['img_shape_for_onnx']
# else:
# img_shapes = [
# img_metas[i]['img_shape']
# for i in range(cls_scores[0].shape[0])
# ]
# scale_factors = [
# img_metas[i]['scale_factor'] for i in range(cls_scores[0].shape[0])
# ]
# result_list = self._get_bboxes(cls_score_list, bbox_pred_list,
# centerness_pred_list, mlvl_points,
# img_shapes, scale_factors, cfg, rescale,
# with_nms)
# return result_list
#
# def _get_bboxes(self,
# cls_scores,
# bbox_preds,
# centernesses,
# mlvl_points,
# img_shapes,
# scale_factors,
# cfg,
# rescale=False,
# with_nms=True):
# """Transform outputs for a single batch item into bbox predictions.
#
# Args:
# cls_scores (list[Tensor]): Box scores for a single scale level
# with shape (N, num_points * num_classes, H, W).
# bbox_preds (list[Tensor]): Box energies / deltas for a single scale
# level with shape (N, num_points * 4, H, W).
# centernesses (list[Tensor]): Centerness for a single scale level
# with shape (N, num_points * 4, H, W).
# mlvl_points (list[Tensor]): Box reference for a single scale level
# with shape (num_total_points, 4).
# img_shapes (list[tuple[int]]): Shape of the input image,
# list[(height, width, 3)].
# scale_factors (list[ndarray]): Scale factor of the image arrange as
# (w_scale, h_scale, w_scale, h_scale).
# cfg (mmcv.Config | None): Test / postprocessing configuration,
# if None, test_cfg would be used.
# rescale (bool): If True, return boxes in original image space.
# Default: False.
# with_nms (bool): If True, do nms before return boxes.
# Default: True.
#
# Returns:
# tuple(Tensor):
# det_bboxes (Tensor): BBox predictions in shape (n, 5), where
# the first 4 columns are bounding box positions
# (tl_x, tl_y, br_x, br_y) and the 5-th column is a score
# between 0 and 1.
# det_labels (Tensor): A (n,) tensor where each item is the
# predicted class label of the corresponding box.
# """
# cfg = self.test_cfg if cfg is None else cfg
# assert len(cls_scores) == len(bbox_preds) == len(mlvl_points)
# device = cls_scores[0].device
# batch_size = cls_scores[0].shape[0]
# # convert to tensor to keep tracing
# nms_pre_tensor = torch.tensor(
# cfg.get('nms_pre', -1), device=device, dtype=torch.long)
# mlvl_bboxes = []
# mlvl_scores = []
# mlvl_centerness = []
# for cls_score, bbox_pred, centerness, points in zip(
# cls_scores, bbox_preds, centernesses, mlvl_points):
# assert cls_score.size()[-2:] == bbox_pred.size()[-2:]
# scores = cls_score.permute(0, 2, 3, 1).reshape(
# batch_size, -1, self.cls_out_channels).sigmoid()
# centerness = centerness.permute(0, 2, 3,
# 1).reshape(batch_size,
# -1).sigmoid()
#
# bbox_pred = bbox_pred.permute(0, 2, 3,
# 1).reshape(batch_size, -1, 4)
# # Always keep topk op for dynamic input in onnx
# if nms_pre_tensor > 0 and (torch.onnx.is_in_onnx_export()
# or scores.shape[-2] > nms_pre_tensor):
# from torch import _shape_as_tensor
# # keep shape as tensor and get k
# num_anchor = _shape_as_tensor(scores)[-2].to(device)
# nms_pre = torch.where(nms_pre_tensor < num_anchor,
# nms_pre_tensor, num_anchor)
#
# max_scores, _ = (scores * centerness[..., None]).max(-1)
# _, topk_inds = max_scores.topk(nms_pre)
# points = points[topk_inds, :]
# batch_inds = torch.arange(batch_size).view(
# -1, 1).expand_as(topk_inds).long()
# bbox_pred = bbox_pred[batch_inds, topk_inds, :]
# scores = scores[batch_inds, topk_inds, :]
# centerness = centerness[batch_inds, topk_inds]
# bboxes = distance2bbox(points, bbox_pred, max_shape=img_shapes)
# mlvl_bboxes.append(bboxes)
# mlvl_scores.append(scores)
# mlvl_centerness.append(centerness)
#
# batch_mlvl_bboxes = torch.cat(mlvl_bboxes, dim=1)
# if rescale:
# batch_mlvl_bboxes /= batch_mlvl_bboxes.new_tensor(
# scale_factors).unsqueeze(1)
# batch_mlvl_scores = torch.cat(mlvl_scores, dim=1)
# batch_mlvl_centerness = torch.cat(mlvl_centerness, dim=1)
#
# # Set max number of box to be feed into nms in deployment
# deploy_nms_pre = cfg.get('deploy_nms_pre', -1)
# if deploy_nms_pre > 0 and torch.onnx.is_in_onnx_export():
# batch_mlvl_scores, _ = (
# batch_mlvl_scores *
# batch_mlvl_centerness.unsqueeze(2).expand_as(batch_mlvl_scores)
# ).max(-1)
# _, topk_inds = batch_mlvl_scores.topk(deploy_nms_pre)
# batch_inds = torch.arange(batch_mlvl_scores.shape[0]).view(
# -1, 1).expand_as(topk_inds)
# batch_mlvl_scores = batch_mlvl_scores[batch_inds, topk_inds, :]
# batch_mlvl_bboxes = batch_mlvl_bboxes[batch_inds, topk_inds, :]
# batch_mlvl_centerness = batch_mlvl_centerness[batch_inds,
# topk_inds]
#
# # remind that we set FG labels to [0, num_class-1] since mmdet v2.0
# # BG cat_id: num_class
# padding = batch_mlvl_scores.new_zeros(batch_size,
# batch_mlvl_scores.shape[1], 1)
# batch_mlvl_scores = torch.cat([batch_mlvl_scores, padding], dim=-1)
#
# if with_nms:
# det_results = []
# for (mlvl_bboxes, mlvl_scores,
# mlvl_centerness) in zip(batch_mlvl_bboxes, batch_mlvl_scores,
# batch_mlvl_centerness):
# det_bbox, det_label = multiclass_nms(
# mlvl_bboxes,
# mlvl_scores,
# cfg.score_thr,
# cfg.nms,
# cfg.max_per_img,
# score_factors=mlvl_centerness)
# det_results.append(tuple([det_bbox, det_label]))
# else:
# det_results = [
# tuple(mlvl_bs)
# for mlvl_bs in zip(batch_mlvl_bboxes, batch_mlvl_scores,
# batch_mlvl_centerness)
# ]
# return det_results
def _get_points_single(self,
featmap_size,
stride,
dtype,
device,
flatten=False):
"""Get points according to feature map sizes."""
y, x = super()._get_points_single(featmap_size, stride, dtype, device)
points = torch.stack((x.reshape(-1) * stride, y.reshape(-1) * stride),
dim=-1) + stride // 2
return points
def get_targets(self, points, gt_bboxes_list, gt_labels_list):
"""Compute regression, classification and centerness targets for points
in multiple images.
Args:
points (list[Tensor]): Points of each fpn level, each has shape
(num_points, 2).
gt_bboxes_list (list[Tensor]): Ground truth bboxes of each image,
each has shape (num_gt, 4).
gt_labels_list (list[Tensor]): Ground truth labels of each box,
each has shape (num_gt,).
Returns:
tuple:
concat_lvl_labels (list[Tensor]): Labels of each level. \
concat_lvl_bbox_targets (list[Tensor]): BBox targets of each \
level.
"""
assert len(points) == len(self.regress_ranges)
num_levels = len(points)
# expand regress ranges to align with points
expanded_regress_ranges = [
points[i].new_tensor(self.regress_ranges[i])[None].expand_as(
points[i]) for i in range(num_levels)
]
# concat all levels points and regress ranges
concat_regress_ranges = torch.cat(expanded_regress_ranges, dim=0)
concat_points = torch.cat(points, dim=0)
# the number of points per img, per lvl
num_points = [center.size(0) for center in points]
# get labels and bbox_targets of each image
# lpf revise interface pitput: add pos_gt_ind
labels_list, bbox_targets_list,gt_ind_list = multi_apply(
self._get_target_single,
gt_bboxes_list,
gt_labels_list,
points=concat_points,
regress_ranges=concat_regress_ranges,
num_points_per_lvl=num_points)
### lpf create flatten_gt_inds: avoid repeated gt_inds
# lpf add 20240429: deal with repeated flatten_gt_inds across different images in a batch_size
num_gts_list = np.array(list(map(len, gt_labels_list)))
batch_size=len(gt_ind_list)
cum_num_gts = list(np.cumsum(num_gts_list)) # calculate sum : right diresction one by one
# print('get_targets : num_gts1111', num_gts_list,'batch_size',batch_size,'cum_num_gts',cum_num_gts)
# print('num_gt_list',num_gts_list)
# print('cum_num_gts',cum_num_gts)
for j in range(1, batch_size):
# print('size1',cum_num_gts[j-1],gt_inds[j].shape)
gt_ind_list[j] += int(cum_num_gts[j - 1])
# split to per img, per level
labels_list = [labels.split(num_points, 0) for labels in labels_list]
bbox_targets_list = [
bbox_targets.split(num_points, 0)
for bbox_targets in bbox_targets_list
]
# lpf add
## num_points : [num_points_level1,num_points_level2,num_points_level3.num_points_level4.num_points_level5]
gt_ind_list=[gt_ind.split(num_points, 0) for gt_ind in gt_ind_list]
# print('get_targets,shape222',batch_size,len(gt_ind_list[0]),len(gt_ind_list[1]),gt_ind_list[0][0].shape,gt_ind_list[1][1].shape,gt_ind_list[0][2].shape,gt_ind_list[1][3].shape,gt_ind_list[0][4].shape)
# gt_inds = torch.cat(gt_ind_list) # num_positives -- pos_inds
# print('size2',flatten_gt_inds.shape)
# print('cum_num_gts',cum_num_gts)
# print('gt_inds',len(gt_inds),len(gt_inds[0]))
# concat per level image
concat_lvl_labels = []
concat_lvl_bbox_targets = []
#lpf add
concat_lvl_gt_ind = []
for i in range(num_levels):