milesial / Pytorch-UNet

PyTorch implementation of the U-Net for image semantic segmentation with high quality images
GNU General Public License v3.0
9.14k stars 2.49k forks source link

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

Open rameses666 opened 4 years ago

rameses666 commented 4 years ago

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 commented 4 years ago

And the prediction was all black.

rameses666 commented 4 years ago

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

milesial commented 4 years ago

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

rameses666 commented 4 years ago

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 commented 4 years ago

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

Cassie0207 commented 4 years ago

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 commented 4 years ago

@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 commented 4 years ago

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

milesial commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

@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 commented 4 years ago

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 https://github.com/milesial/Pytorch-UNet/blob/84f8392b619940bd542dc670761a0a7a1357001d/dice_loss.py#L11-L12 ? Also, how many images are in your validation set?

Quebradawill commented 4 years ago

@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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 2 years ago

You can try changing the optimizer.

theHubert commented 2 years ago

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 commented 2 years ago

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

021022 commented 1 year ago

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 commented 11 months ago

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

Cailingping commented 4 months ago

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

I encountered the same problem. After adjusting the learning rate, dice remains unchanged and approaches 0. Have you solved this problem?

eneuberg commented 3 months ago

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

I encountered the same problem. After adjusting the learning rate, dice remains unchanged and approaches 0. Have you solved this problem?

any updates on this? i have tried everything from adjusting the learning rate, removing amp,... everything else is like in the README, i am using the carvana dataset. @milesial