ในกรณีที่จำนวนข้อมูลตัวอย่าง ในแต่ละ Class แตกต่างกันมาก เรียกว่า Class Imbalance แทนที่เราจะใช้ Cross Entropy Loss ตามปกติที่เรามักจะใช้ในงาน Classification ในเคสนี้เราจะเปลี่ยนไปใช้ Loss Function พิเศษ ที่ออกแบบมาเพื่อแก้ปัญหานี้ เรียกว่า Focal Loss ดังสมการด้านล่าง

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

Focal Loss จะให้ความสำคัญ Class ที่มีจำนวนตัวอย่างน้อย หรือจะ Penalty ให้ Prediction ที่มั่นใจมาก (แปลว่าง่ายสำหรับโมเดล หรือ มีข้อมูลตัวอย่างเป็นจำนวนมาก) ให้มีน้ำหนักน้อยลง เทียบกับสัดส่วน Loss ทั้ง Batch

`p_t` is the model’s estimated probability for each class.

Focal Loss for Dense Object Detection. Credit https://arxiv.org/abs/1708.02002
Focal Loss for Dense Object Detection. Credit https://arxiv.org/abs/1708.02002

ตัวอย่าง kornia.losses.focal

Arguments:

  • alpha (float): Weighting factor :math:`\alpha \in [0, 1]`.
  • gamma (float): Focusing parameter :math:`\gamma >= 0`.
  • reduction (str, optional): Specifies the reduction to apply to the output: ‘none’ | ‘mean’ | ‘sum’. ‘none’: no reduction will be applied, ‘mean’: the sum of the output will be divided by the number of elements in the output, ‘sum’: the output will be summed. Default: ‘none’.

Shape:

  • Input: :math:`(N, C, *)` where C = number of classes.
  • Target: :math:`(N, *)` where each value is :math:`0 ≤ targets[i] ≤ C−1`.

Examples

N = 5 # num_classes 
kwargs = {"alpha": 0.5, "gamma": 2.0, "reduction": 'mean'} 
loss = kornia.losses.FocalLoss(**kwargs) 
input = torch.randn(1, N, 3, 5, requires_grad=True) 
target = torch.empty(1, 3, 5, dtype=torch.long).random_(N) 
output = loss(input, target) 
output.backward()

ตัวอย่างการประยุกต์ใช้ Focal Loss

อ่านต่อ AI วินิจฉัยโรคมะเร็งผิวหนัง 7 ชนิด ความแม่นยำ 94%

References:

แชร์ให้เพื่อน:

Surapong Kanoktipsatharporn on FacebookSurapong Kanoktipsatharporn on LinkedinSurapong Kanoktipsatharporn on Rss
Surapong Kanoktipsatharporn
Solutions Architect at Bua Labs
The ultimate test of your knowledge is your capacity to convey it to another.

Published by Surapong Kanoktipsatharporn

The ultimate test of your knowledge is your capacity to convey it to another.