Пример #1
0
 def __init__(self,
              layer_balance,
              ratio_thresh=4.,
              expansion_bias=0.5,
              cls_pw=1.0,
              obj_pw=1.0,
              iou_type="ciou",
              coord_type="xywh",
              iou_ratio=1.0,
              iou_weights=0.05,
              cls_weights=0.5,
              obj_weights=1.0):
     super(YOLOv5Loss, self).__init__()
     self.layer_balance = layer_balance
     self.iou_ratio = iou_ratio
     self.iou_weights = iou_weights
     self.cls_weights = cls_weights
     self.obj_weights = obj_weights
     self.expansion_bias = expansion_bias
     self.box_similarity = BoxSimilarity(iou_type, coord_type)
     self.target_builder = YOLOv5Builder(ratio_thresh, expansion_bias)
     self.cls_bce = nn.BCEWithLogitsLoss(pos_weight=torch.tensor(
         data=[cls_pw]))
     self.obj_bce = nn.BCEWithLogitsLoss(pos_weight=torch.tensor(
         data=[obj_pw]))
Пример #2
0
 def __init__(self,
              alpha=0.25,
              gamma=2.0,
              cls_cost=1,
              iou_cost=1,
              l1_cost=1):
     super(HungarianMatcher, self).__init__()
     self.alpha = alpha
     self.gamma = gamma
     self.cls_cost = cls_cost
     self.iou_cost = iou_cost
     self.l1_cost = l1_cost
     self.similarity = BoxSimilarity(iou_type="giou")
Пример #3
0
 def __init__(self,
              ratio_thresh=4.,
              expansion_bias=0.5,
              layer_balance=None,
              cls_pw=1.0,
              obj_pw=1.0,
              iou_type="ciou",
              coord_type="xywh",
              iou_ratio=1.0,
              iou_weights=0.05,
              cls_weights=0.5,
              obj_weights=1.0,
              fl_gamma=0.,
              class_smoothing_eps=0.0):
     '''
     :param ratio_thresh: 标签的长h宽w/anchor的长h_a宽w_a阈值, 即h/h_a, w/w_a都要在(1/2.26, 2.26)之间
     :param expansion_bias: anchor匹配时的centrol point位置偏移,能使同一个anchor匹配到更多的样本
     :param layer_balance: objectness loss(lobj)置信度损失在不同的层有不同的权重系数, [4.0, 1.0, 0.4] if np == 3 else [4.0, 1.0, 0.4, 0.1]  # P3-5 or P3-6
     :param cls_pw:  cls BCELoss positive_weight/分类BCELoss中正样本的权重
     :param obj_pw:  obj BCELoss positive_weight/ 有无物体BCELoss中正样本的权重
     :param iou_type:
     :param coord_type:
     :param iou_ratio:   which is used to calcu objectness by calcu iou
     :param iou_weights:  box loss gain
     :param cls_weights:  cls loss gain
     :param obj_weights:  obj loss gain (scale with pixels)/有无物体损失的系数
     :param fl_gamma: hyper-parameter gamma in focal loss
     :param class_smoothing_eps: hyper-parameter in class smoothing label
     '''
     super(YOLOv5LossOriginal, self).__init__()
     if layer_balance is None:
         layer_balance = [4.0, 1.0, 0.4]
     self.layer_balance = layer_balance
     self.iou_ratio = iou_ratio
     self.iou_weights = iou_weights
     self.cls_weights = cls_weights
     self.obj_weights = obj_weights
     self.expansion_bias = expansion_bias
     self.box_similarity = BoxSimilarity(iou_type, coord_type)  # calculate iou between pred_bbox and gt_bbox
     self.target_builder = YOLOv5Builder(ratio_thresh, expansion_bias)  # Build targets for compute_loss/ for anchors
     self.cls_bce = nn.BCEWithLogitsLoss(pos_weight=torch.tensor(data=[cls_pw]))  # classfication loss
     self.obj_bce = nn.BCEWithLogitsLoss(pos_weight=torch.tensor(data=[obj_pw]))  # predicted objectness loss
     ### focal loss wraper----------------------------------------------------------------
     self.fl_gamma=fl_gamma
     if fl_gamma>0:   # focal loss gamma
         self.cls_bce=FocalLoss(self.cls_bce,fl_gamma)
         self.obj_bce=FocalLoss(self.obj_bce,fl_gamma)
     # 样本标签平滑
     # Class label smoothing https://arxiv.org/pdf/1902.04103.pdf eqn 3
     self.cp, self.cn = smooth_BCE(eps=class_smoothing_eps)
Пример #4
0
 def __init__(self,
              top_k=9,
              alpha=0.25,
              gamma=2.0,
              iou_thresh=0.1,
              allow_low_quality_matches=True,
              iou_type="giou",
              iou_loss_weight=0.5,
              reg_loss_weight=1.3):
     self.top_k = top_k
     self.alpha = alpha
     self.gamma = gamma
     self.iou_loss_weight = iou_loss_weight
     self.reg_loss_weight = reg_loss_weight
     self.matcher = Matcher(iou_thresh=iou_thresh,
                            ignore_iou=iou_thresh,
                            allow_low_quality_matches=allow_low_quality_matches)
     self.box_coder = BoxCoder()
     self.iou_loss = IOULoss(iou_type=iou_type)
     self.box_similarity = BoxSimilarity(iou_type="iou")
     self.bce = torch.nn.BCEWithLogitsLoss(reduction="sum")
Пример #5
0
 def __init__(self,
              ratio_thresh=4.,
              expansion_bias=0.5,
              layer_balance=None,
              obj_pw=1.0,
              iou_type="giou",
              coord_type="xywh",
              iou_ratio=1.0,
              iou_weights=0.05,
              obj_weights=1.2):
     super(PedYOLOv5Loss, self).__init__()
     if layer_balance is None:
         layer_balance = [4.0, 1.0, 0.4]
     self.layer_balance = layer_balance
     self.iou_ratio = iou_ratio
     self.iou_weights = iou_weights
     self.obj_weights = obj_weights
     self.expansion_bias = expansion_bias
     self.box_similarity = BoxSimilarity(iou_type, coord_type)
     self.target_builder = PedYOLOv5Builder(ratio_thresh, expansion_bias)
     self.obj_bce = nn.BCEWithLogitsLoss(pos_weight=torch.tensor(
         data=[obj_pw]))
Пример #6
0
 def __init__(self,
              top_k,
              anchor_num_per_loc,
              strides,
              beta=2.0,
              iou_type='giou',
              iou_loss_weight=2.0,
              reg_loss_weight=0.25,
              reg_max=16):
     self.topk = top_k
     self.beta = beta
     self.reg_max = reg_max
     self.iou_type = iou_type
     self.iou_loss_weight = iou_loss_weight
     self.reg_loss_weight = reg_loss_weight
     self.matcher = ATSSMatcher(self.topk, anchor_num_per_loc)
     self.iou_loss = IOULoss(iou_type=iou_type)
     self.box_similarity = BoxSimilarity(iou_type='iou')
     self.strides = strides
     self.expand_strides = None
     self.project = Project(reg_max=reg_max)
     self.qfl = QFL(beta=beta)
     self.dfl = DFL()