Skip to content

Commit da836af

Browse files
committed
modify fcos_rt
1 parent ab0ec20 commit da836af

File tree

6 files changed

+198
-260
lines changed

6 files changed

+198
-260
lines changed

config/fcos_config.py

Lines changed: 11 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -482,17 +482,19 @@
482482
'max_stride': 32,
483483
'out_stride': [8, 16, 32],
484484
## Neck
485-
'neck': 'pafpn',
486-
'fpn_p6_feat': False,
487-
'fpn_p7_feat': False,
488-
'fpn_p6_from_c5': False,
485+
'neck': 'fcos_rt_pafpn',
486+
'use_spp': True,
487+
'spp_act': 'silu',
488+
'spp_norm': 'BN',
489+
'depth': 3,
490+
'fpn_depthwise': False,
489491
## Head
490492
'head': 'fcos_head',
491493
'head_dim': 256,
492494
'num_cls_head': 4,
493495
'num_reg_head': 4,
494-
'head_act': 'relu',
495-
'head_norm': 'GN',
496+
'head_act': 'silu',
497+
'head_norm': 'BN',
496498
## Post-process
497499
'train_topk': 1000,
498500
'train_conf_thresh': 0.05,
@@ -502,7 +504,7 @@
502504
'test_nms_thresh': 0.45,
503505
'nms_class_agnostic': True, # We prefer to use class-agnostic NMS in the demo.
504506
# ----------------- Label Assignment -----------------
505-
'matcher': 'fcos_matcher',
507+
'matcher': 'aligned_simota',
506508
'matcher_hpy':{'center_sampling_radius': 1.5,
507509
'object_sizes_of_interest': [[-1, 64], [64, 128], [128, float('inf')]]
508510
},
@@ -511,164 +513,8 @@
511513
'focal_loss_alpha': 0.25,
512514
'focal_loss_gamma': 2.0,
513515
'loss_cls_weight': 1.0,
514-
'loss_reg_weight': 1.0,
515-
'loss_ctn_weight': 1.0,
516-
# ----------------- Training -----------------
517-
## Training scheduler
518-
'scheduler': '3x',
519-
## Optimizer
520-
'optimizer': 'sgd',
521-
'base_lr': 0.01 / 16,
522-
'backbone_lr_ratio': 1.0 / 1.0,
523-
'momentum': 0.9,
524-
'weight_decay': 1e-4,
525-
'clip_max_norm': -1.0,
526-
## LR Scheduler
527-
'lr_scheduler': 'step',
528-
'warmup': 'linear',
529-
'warmup_iters': 500,
530-
'warmup_factor': 0.00066667,
531-
## Epoch
532-
'max_epoch': 36, # 3x
533-
'lr_epoch': [24, 33], # 3x
534-
# ----------------- Input -----------------
535-
## Transforms
536-
'train_min_size': [256, 288, 320, 352, 384, 416, 448, 480, 512, 544, 576, 608], # short edge of image
537-
'train_max_size': 900,
538-
'test_min_size': 512,
539-
'test_max_size': 736,
540-
## Pixel mean & std
541-
'pixel_mean': [0.485, 0.456, 0.406],
542-
'pixel_std': [0.229, 0.224, 0.225],
543-
## Transforms
544-
'detr_style': False,
545-
'trans_config': [
546-
{'name': 'RandomHFlip'},
547-
{'name': 'RandomResize'},
548-
],
549-
'normalize_coords': False,
550-
},
551-
552-
'fcos_rt_r50_3x':{
553-
# ----------------- Model-----------------
554-
## Backbone
555-
'backbone': 'resnet50',
556-
'backbone_norm': 'FrozeBN',
557-
'res5_dilation': False,
558-
'pretrained': True,
559-
'pretrained_weight': 'imagenet1k_v1',
560-
'max_stride': 32,
561-
'out_stride': [8, 16, 32],
562-
## Neck
563-
'neck': 'pafpn',
564-
'fpn_p6_feat': False,
565-
'fpn_p7_feat': False,
566-
'fpn_p6_from_c5': False,
567-
## Head
568-
'head': 'fcos_head',
569-
'head_dim': 256,
570-
'num_cls_head': 4,
571-
'num_reg_head': 4,
572-
'head_act': 'relu',
573-
'head_norm': 'GN',
574-
## Post-process
575-
'train_topk': 1000,
576-
'train_conf_thresh': 0.05,
577-
'train_nms_thresh': 0.65,
578-
'test_topk': 100,
579-
'test_conf_thresh': 0.5,
580-
'test_nms_thresh': 0.45,
581-
'nms_class_agnostic': True, # We prefer to use class-agnostic NMS in the demo.
582-
# ----------------- Label Assignment -----------------
583-
'matcher': 'fcos_matcher',
584-
'matcher_hpy':{'center_sampling_radius': 1.5,
585-
'object_sizes_of_interest': [[-1, 64], [64, 128], [128, float('inf')]]
586-
},
587-
# ----------------- Loss weight -----------------
588-
## Loss hyper-parameters
589-
'focal_loss_alpha': 0.25,
590-
'focal_loss_gamma': 2.0,
591-
'loss_cls_weight': 1.0,
592-
'loss_reg_weight': 1.0,
593-
'loss_ctn_weight': 1.0,
594-
# ----------------- Training -----------------
595-
## Training scheduler
596-
'scheduler': '3x',
597-
## Optimizer
598-
'optimizer': 'sgd',
599-
'base_lr': 0.01 / 16,
600-
'backbone_lr_ratio': 1.0 / 1.0,
601-
'momentum': 0.9,
602-
'weight_decay': 1e-4,
603-
'clip_max_norm': -1.0,
604-
## LR Scheduler
605-
'lr_scheduler': 'step',
606-
'warmup': 'linear',
607-
'warmup_iters': 500,
608-
'warmup_factor': 0.00066667,
609-
## Epoch
610-
'max_epoch': 36, # 3x
611-
'lr_epoch': [24, 33], # 3x
612-
# ----------------- Input -----------------
613-
## Transforms
614-
'train_min_size': [256, 288, 320, 352, 384, 416, 448, 480, 512, 544, 576, 608], # short edge of image
615-
'train_max_size': 900,
616-
'test_min_size': 512,
617-
'test_max_size': 736,
618-
## Pixel mean & std
619-
'pixel_mean': [0.485, 0.456, 0.406],
620-
'pixel_std': [0.229, 0.224, 0.225],
621-
## Transforms
622-
'detr_style': False,
623-
'trans_config': [
624-
{'name': 'RandomHFlip'},
625-
{'name': 'RandomResize'},
626-
],
627-
'normalize_coords': False,
628-
},
629-
630-
'fcos_rt_r101_3x':{
631-
# ----------------- Model-----------------
632-
## Backbone
633-
'backbone': 'resnet101',
634-
'backbone_norm': 'FrozeBN',
635-
'res5_dilation': False,
636-
'pretrained': True,
637-
'pretrained_weight': 'imagenet1k_v1',
638-
'max_stride': 32,
639-
'out_stride': [8, 16, 32],
640-
## Neck
641-
'neck': 'pafpn',
642-
'fpn_p6_feat': False,
643-
'fpn_p7_feat': False,
644-
'fpn_p6_from_c5': False,
645-
## Head
646-
'head': 'fcos_head',
647-
'head_dim': 256,
648-
'num_cls_head': 4,
649-
'num_reg_head': 4,
650-
'head_act': 'relu',
651-
'head_norm': 'GN',
652-
## Post-process
653-
'train_topk': 1000,
654-
'train_conf_thresh': 0.05,
655-
'train_nms_thresh': 0.65,
656-
'test_topk': 100,
657-
'test_conf_thresh': 0.5,
658-
'test_nms_thresh': 0.45,
659-
'nms_class_agnostic': True, # We prefer to use class-agnostic NMS in the demo.
660-
# ----------------- Label Assignment -----------------
661-
'matcher': 'fcos_matcher',
662-
'matcher_hpy':{'center_sampling_radius': 1.5,
663-
'object_sizes_of_interest': [[-1, 64], [64, 128], [128, float('inf')]]
664-
},
665-
# ----------------- Loss weight -----------------
666-
## Loss hyper-parameters
667-
'focal_loss_alpha': 0.25,
668-
'focal_loss_gamma': 2.0,
669-
'loss_cls_weight': 1.0,
670-
'loss_reg_weight': 1.0,
671-
'loss_ctn_weight': 1.0,
516+
'loss_reg_weight': 2.0,
517+
'loss_ctn_weight': 0.5,
672518
# ----------------- Training -----------------
673519
## Training scheduler
674520
'scheduler': '3x',

models/detectors/fcos/criterion.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def loss_bboxes(self, pred_delta, tgt_delta, bbox_quality=None, num_boxes=1.0):
7878

7979
return loss_box.sum() / num_boxes
8080

81-
def forward(self, outputs, targets):
81+
def fcos_loss(self, outputs, targets):
8282
"""
8383
outputs['pred_cls']: (Tensor) [B, M, C]
8484
outputs['pred_reg']: (Tensor) [B, M, 4]
@@ -139,6 +139,26 @@ def forward(self, outputs, targets):
139139

140140
return loss_dict
141141

142+
def ota_loss(self, outputs, targets):
143+
return
144+
145+
def forward(self, outputs, targets):
146+
"""
147+
outputs['pred_cls']: (Tensor) [B, M, C]
148+
outputs['pred_reg']: (Tensor) [B, M, 4]
149+
outputs['pred_ctn']: (Tensor) [B, M, 1]
150+
outputs['strides']: (List) [8, 16, 32, ...] stride of the model output
151+
targets: (List) [dict{'boxes': [...],
152+
'labels': [...],
153+
'orig_size': ...}, ...]
154+
"""
155+
if self.cfg['matcher'] == "fcos_matcher":
156+
return self.fcos_loss(outputs, targets)
157+
elif self.cfg['matcher'] == "aligned_simota":
158+
return self.ota_loss(outputs, targets)
159+
else:
160+
raise NotImplementedError
161+
142162

143163
# build criterion
144164
def build_criterion(cfg, device, num_classes=80):

models/detectors/fcos/matcher.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,9 @@ def __call__(self, fpn_strides, anchors, targets):
222222

223223
# [B, M], [B, M, 4], [B, M]
224224
return torch.stack(gt_classes), torch.stack(gt_anchors_deltas), torch.stack(gt_centerness)
225+
226+
227+
class AlignedSimOtaMatcher(object):
228+
def __init__(self) -> None:
229+
pass
230+

models/neck/__init__.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from .dilated_encoder import DilatedEncoder
2-
from .fpn import BasicFPN, PaFPN, DETRXPaFPN
2+
from .fpn import BasicFPN, FcosRTPaFPN, DETRXPaFPN
33

44

55
# build neck
@@ -22,12 +22,14 @@ def build_neck(cfg, in_dim, out_dim):
2222
p7_feat = cfg['fpn_p7_feat'],
2323
from_c5 = cfg['fpn_p6_from_c5'],
2424
)
25-
elif cfg['neck'] == 'pafpn':
26-
model = PaFPN(in_dims = in_dim,
27-
out_dim = out_dim,
28-
p6_feat = cfg['fpn_p6_feat'],
29-
p7_feat = cfg['fpn_p7_feat'],
30-
)
25+
elif cfg['neck'] == 'fcos_rt_pafpn':
26+
model = FcosRTPaFPN(cfg = cfg,
27+
in_dims = in_dim,
28+
out_dim = out_dim,
29+
depth = cfg['depth'],
30+
use_spp = cfg['use_spp'],
31+
depthwise = cfg['fpn_depthwise']
32+
)
3133
elif cfg['neck'] == 'detrx_pafpn':
3234
model = DETRXPaFPN(in_dims = in_dim,
3335
out_dim = out_dim,
@@ -37,5 +39,7 @@ def build_neck(cfg, in_dim, out_dim):
3739
from_p5 = False,
3840
depthwise = cfg['fpn_depthwise']
3941
)
42+
else:
43+
raise NotImplementedError
4044

4145
return model

0 commit comments

Comments
 (0)