Created
December 13, 2019 19:35
-
-
Save aiwithshekhar/34e04b5651d40744a22b5b770b9f19a9 to your computer and use it in GitHub Desktop.
calculate dice scores
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
'''calculates dice scores when Scores class for it''' | |
def dice_score(pred, targs): | |
pred = (pred>0).float() | |
return 2. * (pred*targs).sum() / (pred+targs).sum() | |
''' initialize a empty list when Scores is called, append the list with dice scores | |
for every batch, at the end of epoch calculates mean of the dice scores''' | |
class Scores: | |
def __init__(self, phase, epoch): | |
self.base_dice_scores = [] | |
def update(self, targets, outputs): | |
probs = outputs | |
dice= dice_score(probs, targets) | |
self.base_dice_scores.append(dice) | |
def get_metrics(self): | |
dice = np.mean(self.base_dice_scores) | |
return dice | |
'''return dice score for epoch when called''' | |
def epoch_log(epoch_loss, measure): | |
'''logging the metrics at the end of an epoch''' | |
dices= measure.get_metrics() | |
dice= dices | |
print("Loss: %0.4f |dice: %0.4f" % (epoch_loss, dice)) | |
return dice |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
in dice_score() you didnt squre the elements n the denominator. The dice score becomes dimentionally wrong