Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dice coefficient no change during training,is always very close to 0 #173

Open
rameses666 opened this issue May 7, 2020 · 27 comments
Open

Comments

@rameses666
Copy link

Hi!I trained the model on the ultrasonic grayscale image, since there are only two classes, I changed the code to net = UNet(n_channels=1, n_classes=1, bilinear=True), and when I trained, the loss (batch) was around 0.1, but the validation dice coeff was always low, like 7.218320015785669e-9. Is this related to the number of channels? My dataset MASK background is black and the target is red, does it need to be changed to black and white? The dataset MASK and IMG are both 8-bit.

@rameses666
Copy link
Author

And the prediction was all black.

@rameses666
Copy link
Author

@milesial
Can you help me?Thank you very much.

@milesial
Copy link
Owner

milesial commented May 7, 2020

Yes the mask images should be B&W, not RGB. Try that.

@rameses666
Copy link
Author

Hi! I changed the mask images to B&W,but the loss is negative when I was trainning,and Dice coeff is > 1.I don't know why.Thank you very much.

@milesial
Copy link
Owner

milesial commented May 7, 2020

if the loss is negative you are loading your masks wrong, check that you correctly modified the preprocess method

@Cassie0207
Copy link

I had the same problem. When my mask images were B&W, the loss is negetive and Dice coeff >1. Can you be more specific,please?

@milesial
Copy link
Owner

milesial commented May 9, 2020

@Cassie0207 You have to check that your loaded masks after the preprocess method, if you have 2 or 1 classes, are 0s and 1s only. If they are not, you should modify this method to fit your dataset

@buptlxy
Copy link

buptlxy commented Jul 12, 2020

I had the same problem.The prediction was all black. what is B&W,is that a kind of color style in PIL?

@milesial
Copy link
Owner

B&W means black & white. With PIL you can convert to greyscale https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.convert .

@buptlxy
Copy link

buptlxy commented Jul 13, 2020

Thank you so much, I have changed the mask to grayscale and the predictions are still all black, I think it may caused by things below, but I am not sure about it, could you please help me?
1、the flollowing picture is dataset.py , I just add one line in get_item() to get B&W masks, and I remove the scale set and resize all pictures to 224,224. I think it is safe but I am not sure...
2、I set lr=0.001 and epochs=200 or more, the final loss is about 0.05552403015846556,but the result is still bad, I only have 50 training data, does it cause the bad result? I am trying to add data argumention, but I hope to make sure I didn't do something wrong here.
Could you please give me some suggestions, thank you very much!!!
image
image

@milesial
Copy link
Owner

With 50 training data and no data augmentation I don't think you can expect good generalization results.
The line you added seems fine if you have binary labels, you need to check that you have only 0s and 1s in your mask.
If you have more than 2 classes, the mask should contain class indices.

@Quebradawill
Copy link

With 50 training data and no data augmentation I don't think you can expect good generalization results.
The line you added seems fine if you have binary labels, you need to check that you have only 0s and 1s in your mask.
If you have more than 2 classes, the mask should contain class indices.

how many images does the training set need?

@Quebradawill
Copy link

Quebradawill commented Jul 27, 2020

I have the same question, Dice coefficient doesn't change and is always very close to 0.

I don't change the code, what is the problem?

捕获

@erfan377
Copy link

I have the same issue that even though the training is completed and prediction works and it outputs almost correct labels, the Dice coefficient is always very close to 0

@milesial
Copy link
Owner

@Quebradawill

how many images does the training set need?

It depends on the difficulty of your task and the data augmentation you're willing to do. For Carvana the training set was more than 4000 images.

@milesial
Copy link
Owner

To help me debug the DICE issue, could you please give me a sample of what your self.inter and self.union values look like in

self.inter = torch.dot(input.view(-1), target.view(-1))
self.union = torch.sum(input) + torch.sum(target) + eps
?
Also, how many images are in your validation set?

@Quebradawill
Copy link

@Quebradawill

how many images does the training set need?

It depends on the difficulty of your task and the data augmentation you're willing to do. For Carvana the training set was more than 4000 images.

thanks for your advice

@erfan377
Copy link

erfan377 commented Aug 1, 2020

My dice score now seems to be representing the correct number and it's fixed. I think the problem was the learning rate, and after adjusting that, the model seemed to learn much better even though I was getting mask prediction while the dice score was very close to 0.

@flashcp
Copy link

flashcp commented Aug 9, 2020

I meet same issue, but I have solved it when I changed the learn rate to 0.0001. I hope it can help you.

@guanshuicheng
Copy link

I meet same issue, but I have solved it when I changed the learn rate to 0.0001. I hope it can help you.

it works for me ,thanks

@lmz123321
Copy link

I meet same issue, but I have solved it when I changed the learn rate to 0.0001. I hope it can help you.

It works for me , too . Thanks a lot !

@guiLL666
Copy link

I meet same issue, but I have solved it when I changed the learn rate to 0.0001. I hope it can help you.
It works for me , too. Thank you. But sometimes I can train normally,sometimes the dice coefficient still no change and very close to 0.
Have you ever encountered such a problem?

@Huster-Hq
Copy link

You can try changing the optimizer.

@theHubert
Copy link

I have a similar problem. The dice score is very close to zero and the training is stopped by an error (IndexError: Target 198 is out of bounds.) This happens on line 93 when calculating the loss function.
I tried changing the learning rate with no success. I use b&w masks.

@ning-xi
Copy link

ning-xi commented Jul 18, 2022

preprocess() missing 1 required positional argument: 'is_mask'

@021022
Copy link

021022 commented Nov 22, 2022

preprocess() missing 1 required positional argument: 'is_mask'

But why did the car dataset work successfully? I replaced the dataset with my own dataset, and the format was the same as the original dataset, but an error occurred : IndexError: Target 225 is out of bounds. I searched all methods about this error but.... could you explained the 'missing' in more details please?

@phe121
Copy link

phe121 commented Nov 7, 2023

I converted the mask to black and white and changed the learning rate to 0.0001, but Dice coefficient also no change during training,is always very close to 0. @milesial can you help me?
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests