qubvel-org / segmentation_models.pytorch

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

Negative dice loss and IOU score more than 1 #415

Closed cherinae closed 3 years ago

cherinae commented 3 years ago

Hi, I followed the CamVid example and used the exact same code for the whole training process. However, the dice loss is negative, the IOU score is more than 1 and in the 100s, Fscore is also more than 1. I am unable to spot what is wrong. Please help!

loss = smp.utils.losses.DiceLoss()
metrics = [
    smp.utils.metrics.IoU(threshold=0.5),
    smp.utils.metrics.Fscore()
]

optimizer = torch.optim.Adam([ 
    dict(params=model.parameters(), lr=0.0001),
])

max_score = 0

for i in range(0, 30):

    print('\nEpoch: {}'.format(i))
    train_logs = train_epoch.run(train_loader)
    valid_logs = valid_epoch.run(valid_loader)

    # do something (save model, change lr, etc.)
    if max_score < valid_logs['iou_score']:
        max_score = valid_logs['iou_score']
        torch.save(model, f'/content/unet_{ENCODER}.pth')
        print('Model saved!')

    if i == 25:
        optimizer.param_groups[0]['lr'] = 1e-5
        print('Decrease decoder learning rate to 1e-5!')

image

doursand commented 3 years ago

I've got the same issue, i suspect that there might be a divide by batch size that is missing somewhere (at least for iou, as it converges to the batch size in my case)

doursand commented 3 years ago

ok in fact, my problem was that my mask was returning either 255 or 0, it needs to return 0 or 1. So just divide the mask/255.0 and it should work