Skip to content

Instantly share code, notes, and snippets.

@YimianDai
Created August 8, 2019 18:19
Show Gist options
  • Save YimianDai/46563e3edb10977a13ec27d8ae445b96 to your computer and use it in GitHub Desktop.
Save YimianDai/46563e3edb10977a13ec27d8ae445b96 to your computer and use it in GitHub Desktop.
SoftIoU Loss

自己实现的 SoftIoU Loss,用于前景背景的图像分割,相较于 Focal Loss,SoftIoU Loss 的好处是不需要设置 Loss 的参数了,只要关注 Model 就好了

from gluoncv.loss import Loss as gcvLoss

class SoftIoULoss(gcvLoss):
    def __init__(self, batch_axis=0, weight=None):
        super(SoftIoULoss, self).__init__(weight, batch_axis)

    def hybrid_forward(self, F, pred, target):
        pred = F.sigmoid(pred)
        smooth = 1
        intersection = pred * target
        loss = (intersection.sum() + smooth) / (pred.sum() + target.sum() -
                                                intersection.sum() + smooth)
        loss = 1 - loss

        return loss
@Joazs
Copy link

Joazs commented Dec 29, 2022

您好,请问将SoftIoU Loss应用在红外小目标分割有什么好处吗?

@YimianDai
Copy link
Author

正负样本太过不平衡的情况下,CrossEntropy 很可能训不出来,通常都用 SoftIoU/Dice Loss.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment