Last active
March 7, 2022 22:10
Star
You must be signed in to star a gist
jaccard_distance_loss for pytorch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class JaccardDistanceLoss(torch.nn.Module): | |
def __init__(self, smooth=100, dim=1, size_average=True, reduce=True): | |
""" | |
Jaccard = (|X & Y|)/ (|X|+ |Y| - |X & Y|) | |
= sum(|A*B|)/(sum(|A|)+sum(|B|)-sum(|A*B|)) | |
The jaccard distance loss is usefull for unbalanced datasets. This has been | |
shifted so it converges on 0 and is smoothed to avoid exploding or disapearing | |
gradient. | |
Ref: https://en.wikipedia.org/wiki/Jaccard_index | |
@url: https://gist.github.com/wassname/d1551adac83931133f6a84c5095ea101 | |
@author: wassname | |
""" | |
super(JaccardDistanceLoss, self).__init__() | |
self.smooth = smooth | |
self.dim = dim | |
self.size_average = size_average | |
self.reduce = reduce | |
def forward(self, y_true, y_pred): | |
intersection = (y_true * y_pred).abs().sum(self.dim) | |
sum_ = (y_true.abs() + y_pred.abs()).sum(self.dim) | |
jac = (intersection + self.smooth) / (sum_ - intersection + self.smooth) | |
losses = (1 - jac) * self.smooth | |
if self.reduce: | |
return losses.mean() if self.size_average else losses.sum() | |
else: | |
return losses | |
# Jaccobi loss test | |
y_true = torch.from_numpy(np.array([[0,0,1,0],[0,0,1,0],[0,0,1.,0.]])) | |
y_pred = torch.from_numpy(np.array([[0,0,0.9,0],[0,0,0.1,0],[1,1,0.1,1.]])) | |
jaccard_distance_loss1 = JaccardDistanceLoss(reduce=False) | |
r = jaccard_distance_loss1(y_true, y_pred) | |
print('jaccard_distance_loss',r) | |
assert r[0]<r[1] | |
assert r[1]<r[2] | |
# Jaccobi loss test | |
y_true = torch.from_numpy(np.array([[0,0,1,0],[0,0,1,0],[0,0,1.,0.]])) | |
y_pred = torch.from_numpy(np.array([[0,0,0.9,0],[0,0,0.1,0],[1,1,0.1,1.]])) | |
bce = torch.nn.BCELoss(reduce=False) | |
r1 = bce(y_true, y_pred).mean(-1) | |
print('BCELoss',r1) | |
assert r[0]<r[1] | |
assert r[1]<r[2] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment