import torch


def pixelwise_focal(scores_t, target_scores_t, a, b):
    """Pixeslwise focal loss as in arXiv:1904.07850.

    Args:
        scores_t: :math:`(H, W)`.
        target_scores_t: :math:`(H, W)`.
        a: alpha parameter.
        b: beta parameter.

    Returns:
        :math:`(,)`
    """
    eps = 1e-10
    loss = - (1 - target_scores_t + eps)**b * scores_t**a * torch.log(1 - scores_t + eps)
    loss = loss.sum()

    mask = (target_scores_t >= 1 - eps)
    scores_t_masked = scores_t[mask]
    loss += - ((1 - scores_t_masked + eps)**a * torch.log(scores_t_masked + eps)).sum()

    return loss