DeepLearning/Computer Vision

[Object Detection] Focal Loss

yooj_lee 2021. 9. 30. 01:05
300x250

Focal Loss


 

class imbalance in a one-stage detector

  1. inefficient training
    1. 대부분의 location이 easy negatives이고 이들은 학습에 어떠한 유용한 정보도 주지 않음.
  2. Loss overwhelmed
    1. Easy Negatives가 loss의 대부분을 차지하기 때문에 loss가 실질적으로 hard example을 제대로 반영할 수 없게 됨.

 

이전까지의 연구에서 이를 어떻게 해결해왔는가?

  • Hard Negative mining!

Ex) Boosting Classifier에서 hard example을 보다 잘 학습하기 위해 sampling을 더 많이 시키는 형태(sampling weight 부과)로 학습을 시킴.
(명시적으로 hard example을 sampling하는 형태)

  • Focal Loss naturally handles the class imbalance + efficient training enabled (w.o sampling and w.o easy negs overwhelming the loss and computed grads)

cf.) 재밌는 점은 focal loss는 the opposite of a robust loss라는 점! (기존의 robust estimation을 위해 설계된 loss 같은 경우에는 outlier를 의미없고 배척해야 하는 존재로 보아서 down-weight 해줬지만 여기에서는 오히려 inlier를 down-weight해준다는 점) → 오히려 hard example의 training에 "focus"

 

Starting from the cross entropy loss

$p_t = \begin{cases}p \ \ \ \ \ \ \ \ \ \ if\ y=1\\1-p\ \ \ o.w\end{cases}\\$
$p$는 $y=1$일 확률을 의미함

CE의 경우엔 $p_t$가 0.5보다 클 경우, 즉 어느 정도 맞히기 쉬운 경우에도 loss에 상당한 영향을 끼치고 있음. 즉, rare class의 loss가 상대적으로 무시되는 경향이 있을 것.

이를 조절하기 위해 Balanced Cross Entropy로 Weighted CE (보통 class frequency 등을 weight로 사용)가 제안됨. 하지만, 이 경우에는 class 자체의 영향력을 밸런싱하는 데에는 효과가 있지만, easy/hard example에 대해서는 여전히 구분이 어렵다는 문제가 있음.
(이를 바탕으로, class imbalance 문제와 easy/hard 문제를 저자는 다르게 접근하고 있음을 알 수 있다)

→ Easy/hard가 조금 더 포괄적인 범주.

$\therefore$ hard negatives에 집중할 수 있도록 쉬운 데이터들은 down-weight 시키자. → Focal Loss

 

Focal Loss

Cross Entropy에 modulating factor를 더해준 것 → down-weight easy examples! (easy example에 의해 loss와 그래디언트가 overwhelm되는 것을 막는다는 것에 초점을 둔다는 걸 염두에 두면, down-weight easy example로 서술하는 것을 이해할 수 있음)

$FL(p_t) = -(1-p_t)^\gamma\ log(p_t)$

$(1-p_t)^\gamma$: modulating factor

$\gamma$: tunable *focusing* parameter (ge than 0)

 

[Two Properties of the focal loss]

  1. Misclassified and small $p_t$ → Hard(small $p_t$) Negatives(misclassified)
    • $(1-p_t)^\gamma$는 1에 가까워지고, 결국 loss는 modulating factor에 의해 영향을 받지 않게 됨.
    • 반면에 $p_t$가 1에 가까워지게 되면, 즉 easy example이면 factor는 0에 가까워지고 loss는 작아지게 됨. → down-weight
  2. Focusing parameter $\gamma$
    • 쉬운 example들이 어느 정도 down-weight될 것이냐? (혹은 down-weight시킬 쉬운 example들의 기준을 어떻게 정할 거냐)
    • *"extends the range in which an example receives low loss"*
    • $\gamma$ = 0 → FL = CE
    • $\gamma$ 값이 커질 수록 modulating factor의 영향력도 커지게 됨.
    • $\gamma$ = 2 → Best Results
  3. Balancing Parameter $\alpha$
    • 실제 $\alpha$를 활용할 경우 활용하지 않는 경우 대비 약간의 정확도 향상이 있었음.
  4. $FL(p_t) = -\alpha_t(1-p_t)^\gamma\ log(p_t)$

 

Class Imbalance and Model Initialization

  • 이진 분류 모델은 각 클래스에 대한 초기 확률값을 0.5로 동일하게 설정.
  • 이처럼 동일 확률로 디폴트를 설정할 때에는, class imbalance가 존재할 경우에 앞서 loss의 dominance 문제가 생기고 이로 인해 초기 학습의 불안정성이 초래됨.
  • 따라서 이를 해결하기 위해 training 시작 시에 p에 대한 "사전확률(prior)"를 정의함.
  • e.g.) rare class에 대한 확률을 0.01 정도로 낮게 설정
  • loss function을 조정한 것이 아니라 모델 초기화를 조정한 것
  • 모델 초기화 시 CE, FL 모두 심각한 class imbalance 상황에서 학습 안정성이 향상됨을 관찰함.

 

Class Imbalance and Two-stage Detectors

  • Two-stage Detector와 같은 경우에는 이러한 class imbalance에서 상대적으로 자유로움 (w.o use of $\alpha$-balancing or FL)
  • Two-stage detectors가 클래스 불균형을 다루는 2가지 메커니즘
  1. a two-stage cascade (Region Proposals + prediction 구조)
    • Region Proposal을 수행하는 구조로, 어느 정도 location 후보군이 정해져있게 됨.
    • 또한 선택된 proposals의 경우에는 무작위로 선택된 것이 아니라 true object locations에 어느 정도 대응되는 경우가 많음.
  2. biased minibatch sampling
    • second stage를 학습할 때 positive to negative를 1:3 등의 비율로 미니배치를 구성 → sampling 시 어느 정도 imbalance를 해소하는 방향으로 미니배치 구성
    • 이는 샘플링을 통해 암묵적으로 $\alpha$-balancing을 수행하는 것과 같음

 

$\therefore$ FL 같은 경우에는 이러한 메커니즘을 loss function을 통해 one-stage detection system에 직접적으로 적용

 

 

300x250