To combine the strengths and none of the weaknesses of both losses, we can simply combine them by adding them.

if the masked area and the predicted area are none, then the soft dice loss and therefore also the combined loss is undefined
I think I fucked something up in here, ask dominik
def softdiceloss(predictions, targets, smooth: float = 0.001):
    batch_size = targets.shape[0]
    intersection = (predictions * targets).view(batch_size, -1).sum(-1)

    targets_area = targets.view(batch_size, -1).sum(-1)
    predictions_area = predictions.view(batch_size, -1).sum(-1)

    dice = (2. * intersection + smooth) / (predictions_area + targets_area + smooth)
    return 1 - dice.mean()

def dice_bce_loss(predictions, targets, weights = (1,1)):
    '''
    Combination between the bce loss and the soft dice loss. 
    The goal is to get the advantages
    from the soft dice loss without its potential instabilities.
    '''
    soft_dice_loss = softdiceloss(predictions, targets)
    bce_loss = nn.BCEWithLogitsLoss()(predictions, targets)
    combination = weights[0] * soft_dice_loss + weights[1] * bce_loss
    return combination