vision/utils/box_utils.py
def hard_negative_mining(loss, labels, neg_pos_ratio):
    """
    It used to suppress the presence of a large number of negative prediction.
    It works on image level not batch level.
    For any example/image, it keeps all the positive predictions and
     cut the number of negative predictions to make sure the ratio
     between the negative examples and positive examples is no more
     the given ratio for an image.

    Args:
        loss (N, num_priors): the loss for each example.
        labels (N, num_priors): the labels.
        neg_pos_ratio:  the ratio between the negative examples and positive examples.
    """
    pos_mask = labels > 0
    ign_mask = labels == -100   # vision/datasets/voc_dataset.py:41, assign -100 to difficult boxes
    num_pos = pos_mask.long().sum(dim=1, keepdim=True)
    # num_neg = num_pos * neg_pos_ratio
    num_neg = torch.clamp( num_pos * neg_pos_ratio, min=labels.size(0)*10 )

    loss[pos_mask] = -math.inf
    loss[ign_mask] = -math.inf  # we will not choose any anchors that assigned to the ignore label.

    _, indexes = loss.sort(dim=1, descending=True)
    _, orders = indexes.sort(dim=1)
    neg_mask = orders < num_neg
    return pos_mask | neg_mask

    It used to suppress the presence of a large number of negative prediction.
    It works on image level not batch level.
    For any example/image, it keeps all the positive predictions and
     cut the number of negative predictions to make sure the ratio
     between the negative examples and positive examples is no more
     the given ratio for an image.

    Args:
        loss (N, num_priors): the loss for each example.
        labels (N, num_priors): the labels.
        neg_pos_ratio:  the ratio between the negative examples and positive examples.
    
It used to suppress the presence of a large number of negative prediction.
It works on image level not batch level.
For any example/image, it keeps all the positive predictions and
cut the number of negative predictions to make sure the ratio
between the negative examples and positive examples is no more
the given ratio for an image.
It used to suppress the presence of a large number of negative prediction . It works on image level not batch level . For any example / image it keeps all the positive predictions and cut the number of negative predictions to make sure the ratio between the negative examples and positive examples is no more the given ratio for an image .
def hard_negative_mining(loss, labels, neg_pos_ratio):
    pos_mask = labels > 0
    ign_mask = labels == -100   
    num_pos = pos_mask.long().sum(dim=1, keepdim=True)
    num_neg = torch.clamp( num_pos * neg_pos_ratio, min=labels.size(0)*10 )
    loss[pos_mask] = -math.inf
    loss[ign_mask] = -math.inf  
    _, indexes = loss.sort(dim=1, descending=True)
    _, orders = indexes.sort(dim=1)
    neg_mask = orders < num_neg
    return pos_mask | neg_mask
def hard_negative_mining ( loss , labels , neg_pos_ratio ) : pos_mask = labels > 0 ign_mask = labels == - 100 num_pos = pos_mask . long ( ) . sum ( dim = 1 , keepdim = True ) num_neg = torch . clamp ( num_pos * neg_pos_ratio , min = labels . size ( 0 ) * 10 ) loss [ pos_mask ] = - math . inf loss [ ign_mask ] = - math . inf _ , indexes = loss . sort ( dim = 1 , descending = True ) _ , orders = indexes . sort ( dim = 1 ) neg_mask = orders < num_neg return pos_mask | neg_mask
It used to suppress the presence of a large number of negative prediction.
It used to suppress the presence of a large number of negative prediction .
"""
    It used to suppress the presence of a large number of negative prediction.
    It works on image level not batch level.
    For any example/image, it keeps all the positive predictions and
     cut the number of negative predictions to make sure the ratio
     between the negative examples and positive examples is no more
     the given ratio for an image.

    Args:
        loss (N, num_priors): the loss for each example.
        labels (N, num_priors): the labels.
        neg_pos_ratio:  the ratio between the negative examples and positive examples.
    """ # vision/datasets/voc_dataset.py:41, assign -100 to difficult boxes # num_neg = num_pos * neg_pos_ratio # we will not choose any anchors that assigned to the ignore label.
loss
the loss for each example.
the loss for each example .
labels
None
neg_pos_ratio
the ratio between the negative examples and positive examples.
the ratio between the negative examples and positive examples .