yjh0410 2 years ago
parent
commit
475d511948

+ 0 - 2
config/model_config/yolov8_config.py

@@ -6,7 +6,6 @@ yolov8_cfg = {
         # ---------------- Model config ----------------
         ## Backbone
         'backbone': 'yolov8',
-        'pretrained': False,
         'bk_act': 'silu',
         'bk_norm': 'BN',
         'bk_depthwise': False,
@@ -58,7 +57,6 @@ yolov8_cfg = {
         # ---------------- Model config ----------------
         ## Backbone
         'backbone': 'yolov8',
-        'pretrained': False,
         'bk_act': 'silu',
         'bk_norm': 'BN',
         'bk_depthwise': False,

+ 4 - 8
models/detectors/yolov8/loss.py

@@ -17,14 +17,10 @@ class Criterion(object):
         self.reg_max = cfg['reg_max']
         self.use_dfl = cfg['reg_max'] > 1
         # --------------- Loss config ---------------
-        ## loss func
-        self.cls_lossf = ClassificationLoss(cfg, reduction='none')
-        self.reg_lossf = RegressionLoss(num_classes, cfg['reg_max'] - 1, self.use_dfl)
-        ## loss weight
         self.loss_cls_weight = cfg['loss_cls_weight']
         self.loss_box_weight = cfg['loss_box_weight']
         self.loss_dfl_weight = cfg['loss_dfl_weight']
-        # matcher
+        # --------------- Matcher config ---------------
         self.matcher_hpy = cfg['matcher_hpy']
         self.matcher = TaskAlignedAssigner(num_classes     = num_classes,
                                            topk_candidates = self.matcher_hpy['topk_candidates'],
@@ -139,10 +135,9 @@ class Criterion(object):
         fg_masks = torch.cat(fg_masks, 0).view(-1)                                    # [BM,]
         gt_score_targets = torch.cat(gt_score_targets, 0).view(-1, self.num_classes)  # [BM, C]
         gt_bbox_targets = torch.cat(gt_bbox_targets, 0).view(-1, 4)                   # [BM, 4]
-        bbox_weight = gt_score_targets[fg_masks].sum(-1)                              # [BM,]
-        num_fgs = max(gt_score_targets.sum(), 1)
+        num_fgs = gt_score_targets.sum()
         
-        # average loss normalizer across all the GPUs
+        # Average loss normalizer across all the GPUs
         if is_dist_avail_and_initialized():
             torch.distributed.all_reduce(num_fgs)
         num_fgs = (num_fgs / get_world_size()).clamp(1.0)
@@ -155,6 +150,7 @@ class Criterion(object):
         # ------------------ Regression loss ------------------
         box_preds_pos = box_preds.view(-1, 4)[fg_masks]
         box_targets_pos = gt_bbox_targets.view(-1, 4)[fg_masks]
+        bbox_weight = gt_score_targets[fg_masks].sum(-1)
         loss_box = self.loss_bboxes(box_preds_pos, box_targets_pos, bbox_weight)
         loss_box = loss_box.sum() / num_fgs
 

+ 5 - 7
models/detectors/yolov8/yolov8_backbone.py

@@ -13,11 +13,9 @@ class Yolov8Backbone(nn.Module):
     def __init__(self, width=1.0, depth=1.0, ratio=1.0, act_type='silu', norm_type='BN', depthwise=False):
         super(Yolov8Backbone, self).__init__()
         self.feat_dims = [round(64 * width), round(128 * width), round(256 * width), round(512 * width), round(512 * width * ratio)]
-        
-        # stride = 2
+        # P1/2
         self.layer_1 = Conv(3, self.feat_dims[0], k=3, p=1, s=2, act_type=act_type, norm_type=norm_type)
-        
-        # stride = 4
+        # P2/4
         self.layer_2 = nn.Sequential(
             Conv(self.feat_dims[0], self.feat_dims[1], k=3, p=1, s=2, act_type=act_type, norm_type=norm_type),
             Yolov8StageBlock(in_dim     = self.feat_dims[1],
@@ -28,7 +26,7 @@ class Yolov8Backbone(nn.Module):
                              norm_type  = norm_type,
                              depthwise  = depthwise)
         )
-        # stride = 8
+        # P3/8
         self.layer_3 = nn.Sequential(
             Conv(self.feat_dims[1], self.feat_dims[2], k=3, p=1, s=2, act_type=act_type, norm_type=norm_type),
             Yolov8StageBlock(in_dim     = self.feat_dims[2],
@@ -39,7 +37,7 @@ class Yolov8Backbone(nn.Module):
                              norm_type  = norm_type,
                              depthwise  = depthwise)
         )
-        # stride = 16
+        # P4/16
         self.layer_4 = nn.Sequential(
             Conv(self.feat_dims[2], self.feat_dims[3], k=3, p=1, s=2, act_type=act_type, norm_type=norm_type),
             Yolov8StageBlock(in_dim     = self.feat_dims[3],
@@ -50,7 +48,7 @@ class Yolov8Backbone(nn.Module):
                              norm_type  = norm_type,
                              depthwise  = depthwise)
         )
-        # stride = 32
+        # P5/32
         self.layer_5 = nn.Sequential(
             Conv(self.feat_dims[3], self.feat_dims[4], k=3, p=1, s=2, act_type=act_type, norm_type=norm_type),
             Yolov8StageBlock(in_dim     = self.feat_dims[4],