(labels,
                         pos_cls_weight=1.0,
                         neg_cls_weight=1.0,
                         loss_norm_type=LossNormType.NormByNumPositives,
                         dtype=torch.float32)
return cls_weights, reg_weights, cared
get cls_weights and reg_weights from labels.
get cls_weights and reg_weights from labels.
get cls_weights and reg_weights from labels .
def prepare_loss_weights(labels,
                         pos_cls_weight=1.0,
                         neg_cls_weight=1.0,
                         loss_norm_type=LossNormType.NormByNumPositives,
                         dtype=torch.float32):
    """get cls_weights and reg_weights from labels.
    """
    cared = labels >= 0
    # cared: [N, num_anchors]
    positives = labels > 0
    negatives = labels == 0
    negative_cls_weights = negatives.type(dtype) * neg_cls_weight
    cls_weights = negative_cls_weights + pos_cls_weight * positives.type(dtype)
    reg_weights = positives.type(dtype)
    if loss_norm_type == LossNormType.NormByNumExamples:
        num_examples = cared.type(dtype).sum(1, keepdim=True)
        num_examples = torch.clamp(num_examples, min=1.0)
        cls_weights /= num_examples
        bbox_normalizer = positives.sum(1, keepdim=True).type(dtype)
        reg_weights /= torch.clamp(bbox_normalizer, min=1.0)
    elif loss_norm_type == LossNormType.NormByNumPositives:  # for focal loss
        pos_normalizer = positives.sum(1, keepdim=True).type(dtype)
        reg_weights /= torch.clamp(pos_normalizer, min=1.0)
        cls_weights /= torch.clamp(pos_normalizer, min=1.0)
    elif loss_norm_type == LossNormType.NormByNumPosNeg:
        pos_neg = torch.stack([positives, negatives], dim=-1).type(dtype)
        normalizer = pos_neg.sum(1, keepdim=True)  # [N, 1, 2]
        cls_normalizer = (pos_neg * normalizer).sum(-1)  # [N, M]
        cls_normalizer = torch.clamp(cls_normalizer, min=1.0)
        # cls_normalizer will be pos_or_neg_weight/num_pos_or_neg
        normalizer = torch.clamp(normalizer, min=1.0)
        reg_weights /= normalizer[:, 0:1, 0]
        cls_weights /= cls_normalizer
    elif loss_norm_type == LossNormType.DontNorm:  # support ghm loss
        pos_normalizer = positives.sum(1, keepdim=True).type(dtype)
        reg_weights /= torch.clamp(pos_normalizer, min=1.0)
    else:
        raise ValueError(
            f"unknown loss norm type. available: {list(LossNormType)}")
    return cls_weights, reg_weights, cared
def prepare_loss_weights ( labels , pos_cls_weight = 1.0 , neg_cls_weight = 1.0 , loss_norm_type = LossNormType . NormByNumPositives , dtype = torch . float32 ) : cared = labels >= 0 # cared: [N, num_anchors] positives = labels > 0 negatives = labels == 0 negative_cls_weights = negatives . type ( dtype ) * neg_cls_weight cls_weights = negative_cls_weights + pos_cls_weight * positives . type ( dtype ) reg_weights = positives . type ( dtype ) if loss_norm_type == LossNormType . NormByNumExamples : num_examples = cared . type ( dtype ) . sum ( 1 , keepdim = True ) num_examples = torch . clamp ( num_examples , min = 1.0 ) cls_weights /= num_examples bbox_normalizer = positives . sum ( 1 , keepdim = True ) . type ( dtype ) reg_weights /= torch . clamp ( bbox_normalizer , min = 1.0 ) elif loss_norm_type == LossNormType . NormByNumPositives : # for focal loss pos_normalizer = positives . sum ( 1 , keepdim = True ) . type ( dtype ) reg_weights /= torch . clamp ( pos_normalizer , min = 1.0 ) cls_weights /= torch . clamp ( pos_normalizer , min = 1.0 ) elif loss_norm_type == LossNormType . NormByNumPosNeg : pos_neg = torch . stack ( [ positives , negatives ] , dim = - 1 ) . type ( dtype ) normalizer = pos_neg . sum ( 1 , keepdim = True ) # [N, 1, 2] cls_normalizer = ( pos_neg * normalizer ) . sum ( - 1 ) # [N, M] cls_normalizer = torch . clamp ( cls_normalizer , min = 1.0 ) # cls_normalizer will be pos_or_neg_weight/num_pos_or_neg normalizer = torch . clamp ( normalizer , min = 1.0 ) reg_weights /= normalizer [ : , 0 : 1 , 0 ] cls_weights /= cls_normalizer elif loss_norm_type == LossNormType . DontNorm : # support ghm loss pos_normalizer = positives . sum ( 1 , keepdim = True ) . type ( dtype ) reg_weights /= torch . clamp ( pos_normalizer , min = 1.0 ) else : raise ValueError ( f"unknown loss norm type. available: {list(LossNormType)}" ) return cls_weights , reg_weights , cared