qubvel-org / segmentation_models.pytorch

Semantic segmentation models with 500+ pretrained convolutional and transformer-based backbones.
https://smp.readthedocs.io/
MIT License
9.38k stars 1.65k forks source link

No problems with all losses, except JaccardLoss #884

Open JonasZaoui opened 3 months ago

JonasZaoui commented 3 months ago

Hi ! A quick question about a loss, I've used quite a few losses and loss mixtures without any problems, but when I use iou loss, I only get empty predictions. My ground truth is a multiclass mask, without encoding labels on channels

loss = smp.losses.JaccardLoss(mode='multiclass', from_logits=True)

However, with the other losses, I'm fine with multiclasses, and with logits too.

qubvel commented 3 months ago

Hi @JonasZaoui, thanks for the issue! Can you please provide example of tensors you are passing to the loss? Or at least it's statistics (min, max, unique) + shape

JonasZaoui commented 3 months ago

Hi @qubvel, thanks for your answer. Of course, here :

unique pred [-17.930422 -17.655249 -17.478573 ...  12.213453  12.355886  12.49832 ]
min pred -17.930422
max pred 12.49832
max pred (2, 3, 800, 800)

unique target [0 1 2]
min target 0
max target 2
max target (2, 1, 800, 800)

My hypothesis is that the model optimises itself on the very majority class, the background, and predicts 0 all the time.

When I use class = [1,2] on the other hand, I get predictions for class 1 (but not for class 2). So it's weird...

qubvel commented 3 months ago

Ok, thanks, I quickly checked the code but did not find any obvious issue. I will try to investigate it further.. Just in case you identify the issue, please let me know!

JonasZaoui commented 3 months ago

Of course! Thank you for your time.

JonasZaoui commented 3 months ago

Do you think that's if i ignore background in iou computation, it can fix the problem ? (Or increase performance for imbalanced binary segmentation)

qubvel commented 3 months ago

That might help! Or you can consider weights for classes

JonasZaoui commented 3 months ago

I have only one class (0 for background, and 1 for the class). When i ignore the class 0, my dice loss is null. When i use weighted bce it's okay. Never mind the iou :)

qubvel commented 3 months ago

If you have only a background and one class, your case can be considered as binary segmentation. There is an example notebook with dice loss for such a case. https://github.com/qubvel/segmentation_models.pytorch/blob/master/examples/binary_segmentation_intro.ipynb