(self,
                pred: torch.Tensor,
                gt: torch.Tensor,
                mask: torch.Tensor,
                return_origin=False)
return balance_loss
Args:
            pred: shape :math:`(N, 1, H, W)`, the prediction of network
            gt: shape :math:`(N, 1, H, W)`, the target
            mask: shape :math:`(N, H, W)`, the mask indicates positive regions
Args:
            pred: shape :math:`(N, 1, H, W)`, the prediction of network
            gt: shape :math:`(N, 1, H, W)`, the target
            mask: shape :math:`(N, H, W)`, the mask indicates positive regions
Args : pred : shape : math : ( N 1 H W ) the prediction of network gt : shape : math : ( N 1 H W ) the target mask : shape : math : ( N H W ) the mask indicates positive regions
def forward(self,
                pred: torch.Tensor,
                gt: torch.Tensor,
                mask: torch.Tensor,
                return_origin=False):
        '''
        Args:
            pred: shape :math:`(N, 1, H, W)`, the prediction of network
            gt: shape :math:`(N, 1, H, W)`, the target
            mask: shape :math:`(N, H, W)`, the mask indicates positive regions
        '''
        positive = (gt * mask).byte()
        negative = ((1 - gt) * mask).byte()
        positive_count = int(positive.float().sum())
        negative_count = min(int(negative.float().sum()), int(positive_count * self.negative_ratio))
        loss = nn.functional.binary_cross_entropy(pred, gt, reduction='none')
        positive_loss = loss * positive.float()
        negative_loss = loss * negative.float()
        # negative_loss, _ = torch.topk(negative_loss.view(-1).contiguous(), negative_count)
        negative_loss, _ = negative_loss.view(-1).topk(negative_count)

        balance_loss = (positive_loss.sum() + negative_loss.sum()) / (positive_count + negative_count + self.eps)

        if return_origin:
            return balance_loss, loss
        return balance_loss
def forward ( self , pred : torch . Tensor , gt : torch . Tensor , mask : torch . Tensor , return_origin = False ) : positive = ( gt * mask ) . byte ( ) negative = ( ( 1 - gt ) * mask ) . byte ( ) positive_count = int ( positive . float ( ) . sum ( ) ) negative_count = min ( int ( negative . float ( ) . sum ( ) ) , int ( positive_count * self . negative_ratio ) ) loss = nn . functional . binary_cross_entropy ( pred , gt , reduction = 'none' ) positive_loss = loss * positive . float ( ) negative_loss = loss * negative . float ( ) # negative_loss, _ = torch.topk(negative_loss.view(-1).contiguous(), negative_count) negative_loss , _ = negative_loss . view ( - 1 ) . topk ( negative_count ) balance_loss = ( positive_loss . sum ( ) + negative_loss . sum ( ) ) / ( positive_count + negative_count + self . eps ) if return_origin : return balance_loss , loss return balance_loss