qubvel / segmentation_models

Segmentation models with pretrained backbones. Keras and TensorFlow Keras.
MIT License
4.73k stars 1.03k forks source link

Question about Dice Loss. I want to give more penalty to particular class. #520

Open PeterKim1 opened 2 years ago

PeterKim1 commented 2 years ago

Hello. I have one question about dice loss.

According to these code lines(https://github.com/qubvel/segmentation_models/blob/master/segmentation_models/losses.py#L94:L105),

dice loss is calculated by "1 - f_score".

So, I understood if f_score is lower, than dice loss is higher.

And, according to this code (https://github.com/qubvel/segmentation_models/blob/master/segmentation_models/base/functional.py#L151), and these codes (https://github.com/qubvel/segmentation_models/blob/master/segmentation_models/base/functional.py#L48-L54), f_score is calculated by mean(x * class_weights).

The Question is:

I have 3 classes, one is background and the others is object.

I want to give more penalty to object classes.

if i set dice_loss = sm.losses.DiceLoss(class_weights=np.array([1, 2, 2])), can i give more penalty to 2 object classes?

For example, if score is '0.6', then dice loss is '0.4'. (Becuz dice loss is 1 - f_score.)

But if i apply "class_weights", then this score will be '1.2', then dice loss is '-0.2'.

So i think if i want to give more penalty to 2 object classes, i need set class_weights as np.array([1, -2, -2]).

Is it right??