segmentation_models icon indicating copy to clipboard operation
segmentation_models copied to clipboard

Negative loss and IoU is greater than 1

Open numancelik34 opened this issue 5 years ago • 14 comments

I am getting a weird loss and IoU score.. Dice loss becomes negative just after 2-3 epochs, and IoU becomes greater than 1 at the same time. Why is this happening? I have already checked and my masks are binary images in {0, 1} values. Something wrong in loss function??

Thanks

numancelik34 avatar Sep 11 '20 16:09 numancelik34

Search "negative loss" in the issues as there are already a few posts that describe potential reasons for why this is.

JordanMakesMaps avatar Sep 13 '20 19:09 JordanMakesMaps

I have the same issue. I checked that the input images pixels range from 0 to 255 and mask values are uint8 in {0, 1}. I'm currently using the diceloss loss function.

LAUBENicolas avatar Sep 14 '20 14:09 LAUBENicolas

I normalized the image pixels as well using mean, std variables.. however no luck to run dice loss.. it is still in negative!! @qubvel - could you please help us here?!!

numancelik34 avatar Sep 14 '20 16:09 numancelik34

I am also facing the same issue of negative dice loss and iou_score of greater than 1.Could you please help.

jayaBalaR avatar Sep 14 '20 16:09 jayaBalaR

Try to normalize your input pictures to [0;1], simply divide them by 255. This should give you positive values of loss and score. Check all your data to be float64 or float32.

VladislavAD avatar Sep 28 '20 08:09 VladislavAD

Try to normalize your input pictures to [0;1], simply divide them by 255. This should give you positive values of loss and score. Check all your data to be float64 or float32.

sure thank you. I will try that

jayaBalaR avatar Sep 28 '20 09:09 jayaBalaR

Normalizing both input and target images has worked for me! Thanks!

adriamm98 avatar Apr 28 '21 22:04 adriamm98

So normalizing (by dividing by 255) is really only helpful if your images are originally in the range of 0-255, and your model expects a range of 0-1. If you're having trouble with loss functions, I'd double check that the correct pre-processing method is being used for the architecture you're attempting to implement. For example, ResNets trained on ImageNet expect images between the range of 0-255; each architecture may be different.

JordanMakesMaps avatar Apr 29 '21 14:04 JordanMakesMaps

Hello @JordanMakesMaps, I checked the preprocessing for resnets and it doesn't do anything, so if someone wants to use pretrained imagenet weights he shouldn't scale his images to the range of 0-1 because resnets expect images with range 0-255 ? Are you sure that I should not rescale them ?

luna9722 avatar Jun 16 '21 20:06 luna9722

I checked the paper "Deep Residual Learning for Image Recognition" I think the images were normalised, they were substracted by the mean. image

luna9722 avatar Jun 16 '21 20:06 luna9722

Hey @luna9722, for ResNet, values were still between 0-255, though some are between 0-1, or -1-1, etc., If you check the Keras pre-processing for the ResNet architectures I believe it only substracts the mean per-pixel value (as you've also highlighted in the paper).

JordanMakesMaps avatar Jun 17 '21 13:06 JordanMakesMaps

Thank you @JordanMakesMaps for your answer, I don't know why I was confused, you made it clear for me, thank you.

luna9722 avatar Jun 17 '21 15:06 luna9722

This issue happened for me and the reason was setting logit=False in the loss function by mistake.

mehran66 avatar Apr 04 '22 03:04 mehran66