def binary_focal_loss(gt, pr, gamma=2.0, alpha=0.25, **kwargs):
    """Implementation of Focal Loss from the paper in binary classification

    Formula:
        loss = - gt * alpha * ((1 - pr)^gamma) * log(pr) \\
               - (1 - gt) * alpha * (pr^gamma) * log(1 - pr)

    Args:
        gt: ground truth 4D keras tensor (B, H, W, C) or (B, C, H, W)
        pr: prediction 4D keras tensor (B, H, W, C) or (B, C, H, W)
        alpha: the same as weighting factor in balanced cross entropy, default 0.25
        gamma: focusing parameter for modulating factor (1-p), default 2.0

    """
    backend = kwargs['backend']
    pr = backend.clip(pr, backend.epsilon(), 1.0 - backend.epsilon())
    loss_1 = -gt * (alpha * backend.pow(1 - pr, gamma) * backend.log(pr))
    loss_0 = -(1 - gt) * ((1 - alpha) * backend.pow(pr, gamma) * backend.
        log(1 - pr))
    loss = backend.mean(loss_0 + loss_1)
    return loss
