medium research

Focal Loss

Implement Focal Loss from “Focal Loss for Dense Object Detection” (Lin et al., 2017).

Focal loss addresses class imbalance by down-weighting easy examples:

$$\text{FL}(p_t) = -\alpha_t (1 - p_t)^\gamma \log(p_t)$$

Where $p_t$ is the model’s predicted probability for the true class.

For binary classification:

  • If y=1: $p_t = p$, $\alpha_t = \alpha$
  • If y=0: $p_t = 1-p$, $\alpha_t = 1-\alpha$

Given:

  • probs: shape (batch,) — predicted probabilities (after sigmoid)
  • targets: shape (batch,) — binary targets (0 or 1)
  • alpha: float — class balancing factor
  • gamma: float — focusing parameter

Output: Scalar (float) — mean focal loss over the batch.

Hints

focal-loss lin-2017 class-imbalance object-detection loss
Detecting runtime...